8349860: Make Class.isArray(), Class.isInterface() and Class.isPrimitive() non-native

Reviewed-by: dlong, rriggs, vlivanov, yzheng, liach
This commit is contained in:
Coleen Phillimore 2025-02-25 12:35:54 +00:00
parent a9c9f7f0cb
commit c413549eb7
20 changed files with 74 additions and 159 deletions

View file

@ -236,13 +236,15 @@ public final class Class<T> implements java.io.Serializable,
* This constructor is not used and prevents the default constructor being
* generated.
*/
private Class(ClassLoader loader, Class<?> arrayComponentType, int mods, ProtectionDomain pd) {
private Class(ClassLoader loader, Class<?> arrayComponentType, char mods, ProtectionDomain pd, boolean isPrim) {
// Initialize final field for classLoader. The initialization value of non-null
// prevents future JIT optimizations from assuming this final field is null.
// The following assignments are done directly by the VM without calling this constructor.
classLoader = loader;
componentType = arrayComponentType;
modifiers = mods;
protectionDomain = pd;
primitive = isPrim;
}
/**
@ -790,8 +792,9 @@ public final class Class<T> implements java.io.Serializable,
* @return {@code true} if this {@code Class} object represents an interface;
* {@code false} otherwise.
*/
@IntrinsicCandidate
public native boolean isInterface();
public boolean isInterface() {
return Modifier.isInterface(modifiers);
}
/**
@ -801,8 +804,9 @@ public final class Class<T> implements java.io.Serializable,
* {@code false} otherwise.
* @since 1.1
*/
@IntrinsicCandidate
public native boolean isArray();
public boolean isArray() {
return componentType != null;
}
/**
@ -843,8 +847,9 @@ public final class Class<T> implements java.io.Serializable,
* @since 1.1
* @jls 15.8.2 Class Literals
*/
@IntrinsicCandidate
public native boolean isPrimitive();
public boolean isPrimitive() {
return primitive;
}
/**
* Returns true if this {@code Class} object represents an annotation
@ -1002,7 +1007,8 @@ public final class Class<T> implements java.io.Serializable,
private transient Object classData; // Set by VM
private transient Object[] signers; // Read by VM, mutable
private final transient int modifiers; // Set by the VM
private final transient char modifiers; // Set by the VM
private final transient boolean primitive; // Set by the VM if the Class is a primitive type.
// package-private
Object getClassData() {
@ -1284,15 +1290,12 @@ public final class Class<T> implements java.io.Serializable,
* @since 1.1
*/
public Class<?> getComponentType() {
// Only return for array types. Storage may be reused for Class for instance types.
if (isArray()) {
return componentType;
} else {
return null;
}
return componentType;
}
private final Class<?> componentType;
// The componentType field's null value is the sole indication that the class
// is an array - see isArray().
private transient final Class<?> componentType;
/*
* Returns the {@code Class} representing the element type of an array class.

View file

@ -56,7 +56,7 @@ public class Reflection {
fieldFilterMap = Map.of(
Reflection.class, ALL_MEMBERS,
AccessibleObject.class, ALL_MEMBERS,
Class.class, Set.of("classLoader", "classData", "modifiers", "protectionDomain"),
Class.class, Set.of("classLoader", "classData", "modifiers", "protectionDomain", "primitive"),
ClassLoader.class, ALL_MEMBERS,
Constructor.class, ALL_MEMBERS,
Field.class, ALL_MEMBERS,