< prev index next >

src/java.base/share/classes/java/lang/Class.java

Print this page




2232      *          ancestor of the class loader for the current class and
2233      *          invocation of {@link SecurityManager#checkPackageAccess
2234      *          s.checkPackageAccess()} denies access to the package
2235      *          of this class
2236      *
2237      *          </ul>
2238      *
2239      * @since 1.1
2240      * @jls 8.2 Class Members
2241      * @jls 8.3 Field Declarations
2242      */
2243     @CallerSensitive
2244     public Field[] getDeclaredFields() throws SecurityException {
2245         SecurityManager sm = System.getSecurityManager();
2246         if (sm != null) {
2247             checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
2248         }
2249         return copyFields(privateGetDeclaredFields(false));
2250     }
2251 




























2252 
2253     /**
2254      * Returns an array containing {@code Method} objects reflecting all the
2255      * declared methods of the class or interface represented by this {@code
2256      * Class} object, including public, protected, default (package)
2257      * access, and private methods, but excluding inherited methods.
2258      *
2259      * <p> If this {@code Class} object represents a type that has multiple
2260      * declared methods with the same name and parameter types, but different
2261      * return types, then the returned array has a {@code Method} object for
2262      * each such method.
2263      *
2264      * <p> If this {@code Class} object represents a type that has a class
2265      * initialization method {@code <clinit>}, then the returned array does
2266      * <em>not</em> have a corresponding {@code Method} object.
2267      *
2268      * <p> If this {@code Class} object represents a class or interface with no
2269      * declared methods, then the returned array has length 0.
2270      *
2271      * <p> If this {@code Class} object represents an array type, a primitive


3390         ReflectionFactory fact = getReflectionFactory();
3391         for (int i = 0; i < arg.length; i++) {
3392             out[i] = fact.copyMethod(arg[i]);
3393         }
3394         return out;
3395     }
3396 
3397     private static <U> Constructor<U>[] copyConstructors(Constructor<U>[] arg) {
3398         Constructor<U>[] out = arg.clone();
3399         ReflectionFactory fact = getReflectionFactory();
3400         for (int i = 0; i < out.length; i++) {
3401             out[i] = fact.copyConstructor(out[i]);
3402         }
3403         return out;
3404     }
3405 
3406     private native Field[]       getDeclaredFields0(boolean publicOnly);
3407     private native Method[]      getDeclaredMethods0(boolean publicOnly);
3408     private native Constructor<T>[] getDeclaredConstructors0(boolean publicOnly);
3409     private native Class<?>[]   getDeclaredClasses0();


3410 
3411     /**
3412      * Helper method to get the method name from arguments.
3413      */
3414     private String methodToString(String name, Class<?>[] argTypes) {
3415         return getName() + '.' + name +
3416                 ((argTypes == null || argTypes.length == 0) ?
3417                 "()" :
3418                 Arrays.stream(argTypes)
3419                         .map(c -> c == null ? "null" : c.getName())
3420                         .collect(Collectors.joining(",", "(", ")")));
3421     }
3422 
3423     /** use serialVersionUID from JDK 1.1 for interoperability */
3424     private static final long serialVersionUID = 3206093459760846163L;
3425 
3426 
3427     /**
3428      * Class Class is special cased within the Serialization Stream Protocol.
3429      *


3486 
3487     // Retrieves the desired assertion status of this class from the VM
3488     private static native boolean desiredAssertionStatus0(Class<?> clazz);
3489 
3490     /**
3491      * Returns true if and only if this class was declared as an enum in the
3492      * source code.
3493      *
3494      * @return true if and only if this class was declared as an enum in the
3495      *     source code
3496      * @since 1.5
3497      */
3498     public boolean isEnum() {
3499         // An enum must both directly extend java.lang.Enum and have
3500         // the ENUM bit set; classes for specialized enum constants
3501         // don't do the former.
3502         return (this.getModifiers() & ENUM) != 0 &&
3503         this.getSuperclass() == java.lang.Enum.class;
3504     }
3505 












3506     // Fetches the factory for reflective objects
3507     private static ReflectionFactory getReflectionFactory() {
3508         if (reflectionFactory == null) {
3509             reflectionFactory =
3510                 java.security.AccessController.doPrivileged
3511                     (new ReflectionFactory.GetReflectionFactoryAction());
3512         }
3513         return reflectionFactory;
3514     }
3515     private static ReflectionFactory reflectionFactory;
3516 
3517     /**
3518      * Returns the elements of this enum class or null if this
3519      * Class object does not represent an enum type.
3520      *
3521      * @return an array containing the values comprising the enum class
3522      *     represented by this Class object in the order they're
3523      *     declared, or null if this Class object does not
3524      *     represent an enum type
3525      * @since 1.5


4075      * @return a {@code Class} describing the array type
4076      * @since 12
4077      */
4078     @Override
4079     public Class<?> arrayType() {
4080         return Array.newInstance(this, 0).getClass();
4081     }
4082 
4083     /**
4084      * Returns a nominal descriptor for this instance, if one can be
4085      * constructed, or an empty {@link Optional} if one cannot be.
4086      *
4087      * @return An {@link Optional} containing the resulting nominal descriptor,
4088      * or an empty {@link Optional} if one cannot be constructed.
4089      * @since 12
4090      */
4091     @Override
4092     public Optional<ClassDesc> describeConstable() {
4093         return Optional.of(ClassDesc.ofDescriptor(descriptorString()));
4094     }
























4095 }


2232      *          ancestor of the class loader for the current class and
2233      *          invocation of {@link SecurityManager#checkPackageAccess
2234      *          s.checkPackageAccess()} denies access to the package
2235      *          of this class
2236      *
2237      *          </ul>
2238      *
2239      * @since 1.1
2240      * @jls 8.2 Class Members
2241      * @jls 8.3 Field Declarations
2242      */
2243     @CallerSensitive
2244     public Field[] getDeclaredFields() throws SecurityException {
2245         SecurityManager sm = System.getSecurityManager();
2246         if (sm != null) {
2247             checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
2248         }
2249         return copyFields(privateGetDeclaredFields(false));
2250     }
2251 
2252     /**
2253      * TBD
2254      * @return TBD
2255      * @throws SecurityException TBD
2256      * @throws NoSuchMethodException TBD
2257      * @since 1.14
2258      */
2259     @CallerSensitive
2260     public Method[] getRecordAccessors() throws SecurityException, NoSuchMethodException {
2261         SecurityManager sm = System.getSecurityManager();
2262         if (sm != null) {
2263             checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
2264         }
2265         if (isPrimitive() || isArray()) {
2266             return new Method[0];
2267         }
2268         String[] componentNames = getRecordComponentNames0();
2269         if (componentNames == null || componentNames.length == 0) {
2270             return new Method[0];
2271         }
2272         Method[] result = new Method[componentNames.length];
2273         int i = 0;
2274         for (String componentName : componentNames) {
2275             result[i] = getMethod(componentName);
2276             i++;
2277         }
2278         return result;
2279     }
2280 
2281     /**
2282      * Returns an array containing {@code Method} objects reflecting all the
2283      * declared methods of the class or interface represented by this {@code
2284      * Class} object, including public, protected, default (package)
2285      * access, and private methods, but excluding inherited methods.
2286      *
2287      * <p> If this {@code Class} object represents a type that has multiple
2288      * declared methods with the same name and parameter types, but different
2289      * return types, then the returned array has a {@code Method} object for
2290      * each such method.
2291      *
2292      * <p> If this {@code Class} object represents a type that has a class
2293      * initialization method {@code <clinit>}, then the returned array does
2294      * <em>not</em> have a corresponding {@code Method} object.
2295      *
2296      * <p> If this {@code Class} object represents a class or interface with no
2297      * declared methods, then the returned array has length 0.
2298      *
2299      * <p> If this {@code Class} object represents an array type, a primitive


3418         ReflectionFactory fact = getReflectionFactory();
3419         for (int i = 0; i < arg.length; i++) {
3420             out[i] = fact.copyMethod(arg[i]);
3421         }
3422         return out;
3423     }
3424 
3425     private static <U> Constructor<U>[] copyConstructors(Constructor<U>[] arg) {
3426         Constructor<U>[] out = arg.clone();
3427         ReflectionFactory fact = getReflectionFactory();
3428         for (int i = 0; i < out.length; i++) {
3429             out[i] = fact.copyConstructor(out[i]);
3430         }
3431         return out;
3432     }
3433 
3434     private native Field[]       getDeclaredFields0(boolean publicOnly);
3435     private native Method[]      getDeclaredMethods0(boolean publicOnly);
3436     private native Constructor<T>[] getDeclaredConstructors0(boolean publicOnly);
3437     private native Class<?>[]   getDeclaredClasses0();
3438     private native String[]     getRecordComponentNames0();
3439     private native boolean      isRecord0();
3440 
3441     /**
3442      * Helper method to get the method name from arguments.
3443      */
3444     private String methodToString(String name, Class<?>[] argTypes) {
3445         return getName() + '.' + name +
3446                 ((argTypes == null || argTypes.length == 0) ?
3447                 "()" :
3448                 Arrays.stream(argTypes)
3449                         .map(c -> c == null ? "null" : c.getName())
3450                         .collect(Collectors.joining(",", "(", ")")));
3451     }
3452 
3453     /** use serialVersionUID from JDK 1.1 for interoperability */
3454     private static final long serialVersionUID = 3206093459760846163L;
3455 
3456 
3457     /**
3458      * Class Class is special cased within the Serialization Stream Protocol.
3459      *


3516 
3517     // Retrieves the desired assertion status of this class from the VM
3518     private static native boolean desiredAssertionStatus0(Class<?> clazz);
3519 
3520     /**
3521      * Returns true if and only if this class was declared as an enum in the
3522      * source code.
3523      *
3524      * @return true if and only if this class was declared as an enum in the
3525      *     source code
3526      * @since 1.5
3527      */
3528     public boolean isEnum() {
3529         // An enum must both directly extend java.lang.Enum and have
3530         // the ENUM bit set; classes for specialized enum constants
3531         // don't do the former.
3532         return (this.getModifiers() & ENUM) != 0 &&
3533         this.getSuperclass() == java.lang.Enum.class;
3534     }
3535 
3536     /**
3537      * Returns true if and only if this class was declared as a record in the
3538      * source code.
3539      *
3540      * @return true if and only if this class was declared as a record in the
3541      *     source code
3542      * @since 1.12
3543      */
3544     public boolean isRecord() {
3545         return isRecord0();
3546     }
3547 
3548     // Fetches the factory for reflective objects
3549     private static ReflectionFactory getReflectionFactory() {
3550         if (reflectionFactory == null) {
3551             reflectionFactory =
3552                 java.security.AccessController.doPrivileged
3553                     (new ReflectionFactory.GetReflectionFactoryAction());
3554         }
3555         return reflectionFactory;
3556     }
3557     private static ReflectionFactory reflectionFactory;
3558 
3559     /**
3560      * Returns the elements of this enum class or null if this
3561      * Class object does not represent an enum type.
3562      *
3563      * @return an array containing the values comprising the enum class
3564      *     represented by this Class object in the order they're
3565      *     declared, or null if this Class object does not
3566      *     represent an enum type
3567      * @since 1.5


4117      * @return a {@code Class} describing the array type
4118      * @since 12
4119      */
4120     @Override
4121     public Class<?> arrayType() {
4122         return Array.newInstance(this, 0).getClass();
4123     }
4124 
4125     /**
4126      * Returns a nominal descriptor for this instance, if one can be
4127      * constructed, or an empty {@link Optional} if one cannot be.
4128      *
4129      * @return An {@link Optional} containing the resulting nominal descriptor,
4130      * or an empty {@link Optional} if one cannot be constructed.
4131      * @since 12
4132      */
4133     @Override
4134     public Optional<ClassDesc> describeConstable() {
4135         return Optional.of(ClassDesc.ofDescriptor(descriptorString()));
4136     }
4137 
4138     /**
4139      * Returns an array containing {@code Class} objects representing all the permitted subtypes of this class
4140      * if it is sealed. Returns an empty array if this class is not sealed.
4141      * @return an array of all the permitted subtypes of this class
4142      * @since 12
4143      */
4144     public Class<?>[] getPermittedSubtypes() {
4145         Class<?>[] result = getPermittedSubtypes0();
4146         return (result == null) ?
4147             new Class<?>[0] :
4148             result;
4149     }
4150 
4151     /**
4152      * Returns true if this class or interface is sealed.
4153      * @return returns true if the class or interface is sealed
4154      * @since 12
4155      */
4156     public boolean isSealed() {
4157         return Modifier.isFinal(getModifiers()) && getPermittedSubtypes().length != 0;
4158     }
4159 
4160     private native Class<?>[] getPermittedSubtypes0();
4161 }
< prev index next >