< prev index next >

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

Print this page

        

@@ -2247,10 +2247,38 @@
             checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
         }
         return copyFields(privateGetDeclaredFields(false));
     }
 
+    /**
+     * TBD
+     * @return TBD
+     * @throws SecurityException TBD
+     * @throws NoSuchMethodException TBD
+     * @since 1.14
+     */
+    @CallerSensitive
+    public Method[] getRecordAccessors() throws SecurityException, NoSuchMethodException {
+        SecurityManager sm = System.getSecurityManager();
+        if (sm != null) {
+            checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
+        }
+        if (isPrimitive() || isArray()) {
+            return new Method[0];
+        }
+        String[] componentNames = getRecordComponentNames0();
+        if (componentNames == null || componentNames.length == 0) {
+            return new Method[0];
+        }
+        Method[] result = new Method[componentNames.length];
+        int i = 0;
+        for (String componentName : componentNames) {
+            result[i] = getMethod(componentName);
+            i++;
+        }
+        return result;
+    }
 
     /**
      * Returns an array containing {@code Method} objects reflecting all the
      * declared methods of the class or interface represented by this {@code
      * Class} object, including public, protected, default (package)

@@ -3405,10 +3433,12 @@
 
     private native Field[]       getDeclaredFields0(boolean publicOnly);
     private native Method[]      getDeclaredMethods0(boolean publicOnly);
     private native Constructor<T>[] getDeclaredConstructors0(boolean publicOnly);
     private native Class<?>[]   getDeclaredClasses0();
+    private native String[]     getRecordComponentNames0();
+    private native boolean      isRecord0();
 
     /**
      * Helper method to get the method name from arguments.
      */
     private String methodToString(String name, Class<?>[] argTypes) {

@@ -3501,10 +3531,22 @@
         // don't do the former.
         return (this.getModifiers() & ENUM) != 0 &&
         this.getSuperclass() == java.lang.Enum.class;
     }
 
+    /**
+     * Returns true if and only if this class was declared as a record in the
+     * source code.
+     *
+     * @return true if and only if this class was declared as a record in the
+     *     source code
+     * @since 1.12
+     */
+    public boolean isRecord() {
+        return isRecord0();
+    }
+
     // Fetches the factory for reflective objects
     private static ReflectionFactory getReflectionFactory() {
         if (reflectionFactory == null) {
             reflectionFactory =
                 java.security.AccessController.doPrivileged

@@ -4090,6 +4132,30 @@
      */
     @Override
     public Optional<ClassDesc> describeConstable() {
         return Optional.of(ClassDesc.ofDescriptor(descriptorString()));
     }
+
+    /**
+     * Returns an array containing {@code Class} objects representing all the permitted subtypes of this class
+     * if it is sealed. Returns an empty array if this class is not sealed.
+     * @return an array of all the permitted subtypes of this class
+     * @since 12
+     */
+    public Class<?>[] getPermittedSubtypes() {
+        Class<?>[] result = getPermittedSubtypes0();
+        return (result == null) ?
+            new Class<?>[0] :
+            result;
+    }
+
+    /**
+     * Returns true if this class or interface is sealed.
+     * @return returns true if the class or interface is sealed
+     * @since 12
+     */
+    public boolean isSealed() {
+        return Modifier.isFinal(getModifiers()) && getPermittedSubtypes().length != 0;
+    }
+
+    private native Class<?>[] getPermittedSubtypes0();
 }
< prev index next >