RFR: 8298385: Some font classes rely on blind casting to implement Object.equals()

ExE Boss duke at openjdk.org
Thu Jan 26 02:16:28 UTC 2023


On Sun, 25 Sep 2022 04:01:26 GMT, SWinxy <duke at openjdk.org> wrote:

> Some also had two separate methods to check equality, one calling the other. Objects.equals() is used in some places as a replacement for a local copy of the function. In the end, the equals methods become quicker to understand their checks, more condensed, and becomes more in line with other equality methods in the JDK.

Maybe keep the old variable names:

src/java.desktop/share/classes/java/awt/font/FontRenderContext.java line 314:

> 312:                         && o1.getFractionalMetricsHint() == getFractionalMetricsHint()
> 313:                         && o1.getTransform().equals(getTransform());
> 314:             }

Suggestion:

        if (o instanceof FontRenderContext rhs) {
            // if neither instance is a subclass, reference values directly.
            if (!rhs.defaulting && !defaulting) {
                return rhs.aaHintValue == aaHintValue
                        && rhs.fmHintValue == fmHintValue
                        && Objects.equals(tx, rhs.tx);
            } else {
                return rhs.getAntiAliasingHint() == getAntiAliasingHint()
                        && rhs.getFractionalMetricsHint() == getFractionalMetricsHint()
                        && rhs.getTransform().equals(getTransform());
            }

src/java.desktop/share/classes/java/awt/font/ImageGraphicAttribute.java line 190:

> 188:                 && getAlignment() == o1.getAlignment()
> 189:                 && fImage.equals(o1.fImage);
> 190: 

Suggestion:

        return this == o || o instanceof ImageGraphicAttribute rhs
                && fOriginX == rhs.fOriginX
                && fOriginY == rhs.fOriginY
                && getAlignment() == rhs.getAlignment()
                && fImage.equals(rhs.fImage);

src/java.desktop/share/classes/java/awt/font/ShapeGraphicAttribute.java line 217:

> 215:                 && fStroke == o1.fStroke
> 216:                 && getAlignment() == o1.getAlignment()
> 217:                 && fShape.equals(o1.fShape);

Suggestion:

        return this == o || o instanceof ShapeGraphicAttribute rhs
                && fStroke == rhs.fStroke
                && getAlignment() == rhs.getAlignment()
                && fShape.equals(rhs.fShape);

src/java.desktop/share/classes/java/awt/font/TransformAttribute.java line 162:

> 160:     public boolean equals(Object o) {
> 161:         return this == o || o instanceof TransformAttribute o1
> 162:                 && transform.equals(o1.transform);

Suggestion:

        return this == o || o instanceof TransformAttribute rhs
                && transform.equals(rhs.transform);

src/java.desktop/share/classes/sun/font/AttributeValues.java line 471:

> 469:                 && Objects.equals(font, o1.font)
> 470:                 && imUnderline == o1.imUnderline
> 471:                 && Objects.equals(imHighlight, o1.imHighlight);

Suggestion:

        return this == o || o instanceof AttributeValues rhs
                && defined == rhs.defined
                && nondefault == rhs.nondefault
                && underline == rhs.underline
                && strikethrough == rhs.strikethrough
                && superscript == rhs.superscript
                && width == rhs.width
                && kerning == rhs.kerning
                && tracking == rhs.tracking
                && ligatures == rhs.ligatures
                && runDirection == rhs.runDirection
                && bidiEmbedding == rhs.bidiEmbedding
                && swapColors == rhs.swapColors
                && Objects.equals(transform, rhs.transform)
                && Objects.equals(foreground, rhs.foreground)
                && Objects.equals(background, rhs.background)
                && Objects.equals(numericShaping, rhs.numericShaping)
                && Objects.equals(justification, rhs.justification)
                && Objects.equals(charReplacement, rhs.charReplacement)
                && size == rhs.size
                && weight == rhs.weight
                && posture == rhs.posture
                && Objects.equals(family, rhs.family)
                && Objects.equals(font, rhs.font)
                && imUnderline == rhs.imUnderline
                && Objects.equals(imHighlight, rhs.imHighlight);

src/java.desktop/share/classes/sun/font/CoreMetrics.java line 87:

> 85:                 && underlineThickness == o1.underlineThickness
> 86:                 && ssOffset == o1.ssOffset
> 87:                 && italicAngle == o1.italicAngle;

Suggestion:

        return this == o || o instanceof CoreMetrics rhs
                && ascent == rhs.ascent
                && descent == rhs.descent
                && leading == rhs.leading
                && baselineIndex == rhs.baselineIndex
                && baselineOffsets[0] == rhs.baselineOffsets[0]
                && baselineOffsets[1] == rhs.baselineOffsets[1]
                && baselineOffsets[2] == rhs.baselineOffsets[2]
                && strikethroughOffset == rhs.strikethroughOffset
                && strikethroughThickness == rhs.strikethroughThickness
                && underlineOffset == rhs.underlineOffset
                && underlineThickness == rhs.underlineThickness
                && ssOffset == rhs.ssOffset
                && italicAngle == rhs.italicAngle;

src/java.desktop/share/classes/sun/font/Decoration.java line 175:

> 173:                     && Objects.equals(fgPaint, o1.fgPaint)
> 174:                     && Objects.equals(bgPaint, o1.bgPaint)
> 175:                     && Objects.equals(imUnderline, o1.imUnderline);

Suggestion:

            return this == o || o instanceof DecorationImpl other
                    && swapColors == other.swapColors
                    && strikethrough == other.strikethrough
                    && Objects.equals(stdUnderline, other.stdUnderline)
                    && Objects.equals(fgPaint, other.fgPaint)
                    && Objects.equals(bgPaint, other.bgPaint)
                    && Objects.equals(imUnderline, other.imUnderline);

src/java.desktop/share/classes/sun/font/FontLineMetrics.java line 103:

> 101: 
> 102:     public boolean equals(Object o) {
> 103:         return this == o || o instanceof FontLineMetrics o1 && Objects.equals(cm, o1.cm);

Suggestion:

        return this == o || o instanceof FontLineMetrics rhs
            && Objects.equals(cm, rhs.cm);

src/java.desktop/share/classes/sun/font/GlyphLayout.java line 141:

> 139:                     && script == o1.script
> 140:                     && lang == o1.lang
> 141:                     && font.equals(o1.font);

Suggestion:

            return this == o || o instanceof LayoutEngineKey rhs
                    && script == rhs.script
                    && lang == rhs.lang
                    && font.equals(rhs.font);

src/java.desktop/share/classes/sun/font/GlyphLayout.java line 278:

> 276:                         && hash == o1.hash
> 277:                         && font.equals(o1.font)
> 278:                         && frc.equals(o1.frc);

Suggestion:

                return this == o || o instanceof SDKey rhs
                        && hash == rhs.hash
                        && font.equals(rhs.font)
                        && frc.equals(rhs.frc);

src/java.desktop/share/classes/sun/font/PhysicalFont.java line 45:

> 43: 
> 44:     public boolean equals(Object o) {
> 45:         return o instanceof PhysicalFont other

The old implementation also checked that the runtime class of `other` is the same as that of `this`:
Suggestion:

        return o instanceof PhysicalFont other
                && other.getClass() == this.getClass()

src/java.desktop/share/classes/sun/font/StandardGlyphVector.java line 694:

> 692:             }
> 693: 
> 694:             return Objects.equals(gti, o1.gti);

Suggestion:

        if (o instanceof StandardGlyphVector other) {
            if (glyphs.length != other.glyphs.length) {
                return false;
            }

            for (int i = 0; i < glyphs.length; ++i) {
                if (glyphs[i] != other.glyphs[i]) {
                    return false;
                }
            }

            if (!font.equals(other.font)) {
                return false;
            }

            if (!frc.equals(other.frc)) {
                return false;
            }

            if ((other.positions == null) != (positions == null)) {
                if (positions == null) {
                    initPositions();
                } else {
                    other.initPositions();
                }
            }

            if (positions != null) {
                for (int i = 0; i < positions.length; ++i) {
                    if (positions[i] != other.positions[i]) {
                        return false;
                    }
                }
            }

            return Objects.equals(gti, other.gti);

-------------

PR: https://git.openjdk.org/jdk/pull/10416



More information about the client-libs-dev mailing list