< prev index next >

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

Print this page




2295      *          s.checkPackageAccess()} denies access to the package
2296      *          of this class
2297      *
2298      *          </ul>
2299      *
2300      * @jls 8.10 Record Types
2301      * @since 14
2302      */
2303     @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.RECORDS,
2304                                  essentialAPI=false)
2305     @SuppressWarnings("preview")
2306     @CallerSensitive
2307     public RecordComponent[] getRecordComponents() {
2308         SecurityManager sm = System.getSecurityManager();
2309         if (sm != null) {
2310             checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
2311         }
2312         if (isPrimitive() || isArray()) {
2313             return new RecordComponent[0];
2314         }
2315         Object[] recordComponents = getRecordComponents0();
2316         if (recordComponents == null || recordComponents.length == 0) {
2317             return new RecordComponent[0];
2318         }
2319         RecordComponent[] result = new RecordComponent[recordComponents.length];
2320         for (int i = 0; i < recordComponents.length; i++) {
2321             result[i] = (RecordComponent)recordComponents[i];
2322         }
2323         return result;
2324     }
2325 
2326     /**
2327      * Returns an array containing {@code Method} objects reflecting all the
2328      * declared methods of the class or interface represented by this {@code
2329      * Class} object, including public, protected, default (package)
2330      * access, and private methods, but excluding inherited methods.
2331      *
2332      * <p> If this {@code Class} object represents a type that has multiple
2333      * declared methods with the same name and parameter types, but different
2334      * return types, then the returned array has a {@code Method} object for
2335      * each such method.
2336      *
2337      * <p> If this {@code Class} object represents a type that has a class
2338      * initialization method {@code <clinit>}, then the returned array does
2339      * <em>not</em> have a corresponding {@code Method} object.
2340      *
2341      * <p> If this {@code Class} object represents a class or interface with no
2342      * declared methods, then the returned array has length 0.
2343      *


3463         ReflectionFactory fact = getReflectionFactory();
3464         for (int i = 0; i < arg.length; i++) {
3465             out[i] = fact.copyMethod(arg[i]);
3466         }
3467         return out;
3468     }
3469 
3470     private static <U> Constructor<U>[] copyConstructors(Constructor<U>[] arg) {
3471         Constructor<U>[] out = arg.clone();
3472         ReflectionFactory fact = getReflectionFactory();
3473         for (int i = 0; i < out.length; i++) {
3474             out[i] = fact.copyConstructor(out[i]);
3475         }
3476         return out;
3477     }
3478 
3479     private native Field[]       getDeclaredFields0(boolean publicOnly);
3480     private native Method[]      getDeclaredMethods0(boolean publicOnly);
3481     private native Constructor<T>[] getDeclaredConstructors0(boolean publicOnly);
3482     private native Class<?>[]   getDeclaredClasses0();
3483     private native Object[]     getRecordComponents0();

3484     private native boolean      isRecord0();
3485 
3486     /**
3487      * Helper method to get the method name from arguments.
3488      */
3489     private String methodToString(String name, Class<?>[] argTypes) {
3490         return getName() + '.' + name +
3491                 ((argTypes == null || argTypes.length == 0) ?
3492                 "()" :
3493                 Arrays.stream(argTypes)
3494                         .map(c -> c == null ? "null" : c.getName())
3495                         .collect(Collectors.joining(",", "(", ")")));
3496     }
3497 
3498     /** use serialVersionUID from JDK 1.1 for interoperability */
3499     @java.io.Serial
3500     private static final long serialVersionUID = 3206093459760846163L;
3501 
3502 
3503     /**




2295      *          s.checkPackageAccess()} denies access to the package
2296      *          of this class
2297      *
2298      *          </ul>
2299      *
2300      * @jls 8.10 Record Types
2301      * @since 14
2302      */
2303     @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.RECORDS,
2304                                  essentialAPI=false)
2305     @SuppressWarnings("preview")
2306     @CallerSensitive
2307     public RecordComponent[] getRecordComponents() {
2308         SecurityManager sm = System.getSecurityManager();
2309         if (sm != null) {
2310             checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
2311         }
2312         if (isPrimitive() || isArray()) {
2313             return new RecordComponent[0];
2314         }
2315         RecordComponent[] recordComponents = getRecordComponents0();
2316         if (recordComponents == null) {
2317             return new RecordComponent[0];
2318         }
2319         return recordComponents;




2320     }
2321 
2322     /**
2323      * Returns an array containing {@code Method} objects reflecting all the
2324      * declared methods of the class or interface represented by this {@code
2325      * Class} object, including public, protected, default (package)
2326      * access, and private methods, but excluding inherited methods.
2327      *
2328      * <p> If this {@code Class} object represents a type that has multiple
2329      * declared methods with the same name and parameter types, but different
2330      * return types, then the returned array has a {@code Method} object for
2331      * each such method.
2332      *
2333      * <p> If this {@code Class} object represents a type that has a class
2334      * initialization method {@code <clinit>}, then the returned array does
2335      * <em>not</em> have a corresponding {@code Method} object.
2336      *
2337      * <p> If this {@code Class} object represents a class or interface with no
2338      * declared methods, then the returned array has length 0.
2339      *


3459         ReflectionFactory fact = getReflectionFactory();
3460         for (int i = 0; i < arg.length; i++) {
3461             out[i] = fact.copyMethod(arg[i]);
3462         }
3463         return out;
3464     }
3465 
3466     private static <U> Constructor<U>[] copyConstructors(Constructor<U>[] arg) {
3467         Constructor<U>[] out = arg.clone();
3468         ReflectionFactory fact = getReflectionFactory();
3469         for (int i = 0; i < out.length; i++) {
3470             out[i] = fact.copyConstructor(out[i]);
3471         }
3472         return out;
3473     }
3474 
3475     private native Field[]       getDeclaredFields0(boolean publicOnly);
3476     private native Method[]      getDeclaredMethods0(boolean publicOnly);
3477     private native Constructor<T>[] getDeclaredConstructors0(boolean publicOnly);
3478     private native Class<?>[]   getDeclaredClasses0();
3479     @SuppressWarnings("preview")
3480     private native RecordComponent[] getRecordComponents0();
3481     private native boolean      isRecord0();
3482 
3483     /**
3484      * Helper method to get the method name from arguments.
3485      */
3486     private String methodToString(String name, Class<?>[] argTypes) {
3487         return getName() + '.' + name +
3488                 ((argTypes == null || argTypes.length == 0) ?
3489                 "()" :
3490                 Arrays.stream(argTypes)
3491                         .map(c -> c == null ? "null" : c.getName())
3492                         .collect(Collectors.joining(",", "(", ")")));
3493     }
3494 
3495     /** use serialVersionUID from JDK 1.1 for interoperability */
3496     @java.io.Serial
3497     private static final long serialVersionUID = 3206093459760846163L;
3498 
3499 
3500     /**


< prev index next >