RFR: 8310849: Pattern matching for instanceof and arrayType cleanup in j.l.invoke and j.l.reflect [v2]

Glavo duke at openjdk.org
Mon Jun 26 13:09:15 UTC 2023


On Mon, 26 Jun 2023 12:13:26 GMT, Chen Liang <liach at openjdk.org> wrote:

>> This patch touches java.lang.reflect and java.lang.invoke packages. It replaces instanceof + cast with pattern matching and updates  Array.newInstance().getClass() patterns with arrayType() for obtaining array types of a class.
>
> Chen Liang has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains two additional commits since the last revision:
> 
>  - Merge branch 'master' into cleanup/invoke-instanceof
>  - 8310849: Pattern matching for instanceof and arrayType cleanup in j.l.invoke and j.l.reflect

Changes requested by Glavo at github.com (no known OpenJDK username).

src/java.base/share/classes/java/lang/invoke/MethodHandleNatives.java line 359:

> 357:         if (staticArguments == null)
> 358:             return "BSA0=null";
> 359:         return "BSA1="+staticArguments;

Suggestion:

        return switch (staticArguments) {
            case Object[] array -> "BSA="+java.util.Arrays.asList(array);
            case int[] array -> "BSA@"+java.util.Arrays.toString(array);
            case null -> "BSA0=null";
            default -> "BSA1="+staticArguments;
        };

src/java.base/share/classes/sun/invoke/util/ValueConversions.java line 239:

> 237:             return ZERO_INT;
> 238:         }
> 239:         if (x instanceof Number n) {

Number res = switch (x) {
    case Number n -> n;
    case Boolean b -> b ? ONE_INT : ZERO_INT;
    case Character c -> (int) c;
    // this will fail with the required ClassCastException:
    default -> (Number) x;
};

src/java.base/share/classes/sun/invoke/util/ValueConversions.java line 262:

> 260:      */
> 261:     public static int widenSubword(Object x) {
> 262:         if (x instanceof Integer i)

return switch (x) {
    case Integer i -> i;
    case Boolean b -> fromBoolean(b);
    case Character c -> c;
    case Short s -> s;
    case Byte b -> b;
    // Fail with a ClassCastException.
    case null, default -> (int) x;
};

src/java.base/share/classes/sun/invoke/util/Wrapper.java line 583:

> 581: 
> 582:     private static Number numberValue(Object x) {
> 583:         if (x instanceof Number n)     return n;

return switch (x) {
    case Number n -> n;
    case Character c -> (int) c;
    case Boolean b -> b ? 1 : 0;
    // Remaining allowed case of void: Must be a null reference.
    case null, default -> (Number) x;
};

src/java.base/share/classes/sun/reflect/annotation/AnnotatedTypeFactory.java line 252:

> 250:         @Override
> 251:         public boolean equals(Object o) {
> 252:             if (o instanceof AnnotatedType that &&

return o instanceof AnnotatedType that &&
     !(o instanceof AnnotatedArrayType) &&
     !(o instanceof AnnotatedTypeVariable) &&
     !(o instanceof AnnotatedParameterizedType) &&
     !(o instanceof AnnotatedWildcardType) &&
     equalsTypeAndAnnotations(that);

src/java.base/share/classes/sun/reflect/generics/reflectiveObjects/GenericArrayTypeImpl.java line 73:

> 71:     @Override
> 72:     public boolean equals(Object o) {
> 73:         if (o instanceof GenericArrayType that) {

return o instanceof GenericArrayType that 
    && Objects.equals(genericComponentType, that.getGenericComponentType());

src/java.base/share/classes/sun/reflect/generics/reflectiveObjects/WildcardTypeImpl.java line 171:

> 169:     @Override
> 170:     public boolean equals(Object o) {
> 171:         if (o instanceof WildcardType that) {

return o instanceof WildcardType that
        && Arrays.equals(this.getLowerBounds(), that.getLowerBounds()) 
        && Arrays.equals(this.getUpperBounds(), that.getUpperBounds());

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

PR Review: https://git.openjdk.org/jdk/pull/14642#pullrequestreview-1498452572
PR Review Comment: https://git.openjdk.org/jdk/pull/14642#discussion_r1242146325
PR Review Comment: https://git.openjdk.org/jdk/pull/14642#discussion_r1242152785
PR Review Comment: https://git.openjdk.org/jdk/pull/14642#discussion_r1242155031
PR Review Comment: https://git.openjdk.org/jdk/pull/14642#discussion_r1242163198
PR Review Comment: https://git.openjdk.org/jdk/pull/14642#discussion_r1242161073
PR Review Comment: https://git.openjdk.org/jdk/pull/14642#discussion_r1242157078
PR Review Comment: https://git.openjdk.org/jdk/pull/14642#discussion_r1242158469


More information about the core-libs-dev mailing list