On Fri, 31 Jul 2026 15:52:01 GMT, Chen Liang <[email protected]> wrote:

>> Introduce an instance method, `isClassOrInterface`, to `java.lang.Class` and 
>> `java.lang.invoke.TypeDescriptor$OfField`.
>> 
>> `java.lang.constant.ClassDesc` comes with an `isClassOrInterface` method. It 
>> turns out that such a check is frequently needed in the use of `Class` 
>> objects, usually as `isArray() || isPrimitive()` or `!isArray() && 
>> !isPrimitive()`, with 14 occurrences in various reflective code.
>> 
>> In addition, `Class` can offer a more efficient implementation than two 
>> checks: it can perform a single bit pattern check to derive this result, 
>> making such an API more feasible.
>> 
>> ---------
>> - [x] I confirm that I make this contribution in accordance with the 
>> [OpenJDK Interim AI Policy](https://openjdk.org/legal/ai).
>
> 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 eight additional commits since 
> the last revision:
> 
>  - Merge branch 'master' of https://github.com/openjdk/jdk into 
> feature/class-is-class-or-interface-1
>  - More spec clarifications
>  - Wording review from Joe
>  - Update usage in Valhalla
>  - Merge branch 'master' of https://github.com/openjdk/jdk into 
> feature/class-is-class-or-interface-1
>  - Simplify code
>  - Merge branch 'master' of https://github.com/openjdk/jdk into 
> feature/class-is-class-or-interface-1
>  - Class.isClassOrInterface

src/java.base/share/classes/java/lang/Class.java line 3831:

> 3829:                 isArray() ||
> 3830:                 isPrimitive() ||
> 3831:                 this == Void.TYPE) {

**Fun fact:** `Void.TYPE.isPrimitive()` already returns `true`, so this last 
comparison would always return `false`.

src/java.base/share/classes/sun/reflect/annotation/TypeAnnotationParser.java 
line 199:

> 197:             Class<?> decl) {
> 198:         if (decl == Object.class ||
> 199:                 !decl.isClassOrInterface())

This `if` statement can now fit into a single line:
Suggestion:

        if (decl == Object.class || !decl.isClassOrInterface())

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

PR Review Comment: https://git.openjdk.org/jdk/pull/32109#discussion_r3703411080
PR Review Comment: https://git.openjdk.org/jdk/pull/32109#discussion_r3703417352

Reply via email to