< prev index next >

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

Print this page

        

@@ -286,11 +286,15 @@
      */
     @CallerSensitive
     public static Class<?> forName(String className)
                 throws ClassNotFoundException {
         Class<?> caller = Reflection.getCallerClass();
-        return forName0(className, true, ClassLoader.getClassLoader(caller), caller);
+        Class<?> c = forName0(className, true, ClassLoader.getClassLoader(caller), caller);
+        if (c.isValueClass()) {
+            throw new ClassNotFoundException(className + " is a derived value class");
+        }
+        return c;
     }
 
 
     /**
      * Returns the {@code Class} object associated with the class or

@@ -369,15 +373,19 @@
                     sm.checkPermission(
                         SecurityConstants.GET_CLASSLOADER_PERMISSION);
                 }
             }
         }
-        return forName0(name, initialize, loader, caller);
+        Class<?> c = forName0(name, initialize, loader, caller);
+        if (c.isValueClass()) {
+            throw new ClassNotFoundException(name + " is a derived value class");
+        }
+        return c;
     }
 
     /** Called after security check for system loader access checks have been made. */
-    private static native Class<?> forName0(String name, boolean initialize,
+    static native Class<?> forName0(String name, boolean initialize,
                                             ClassLoader loader,
                                             Class<?> caller)
         throws ClassNotFoundException;
 
 

@@ -441,14 +449,36 @@
             }
         }
 
         PrivilegedAction<ClassLoader> pa = module::getClassLoader;
         ClassLoader cl = AccessController.doPrivileged(pa);
+        Class<?> c;
         if (cl != null) {
-            return cl.loadClass(module, name);
+            c = cl.loadClass(module, name);
         } else {
-            return BootLoader.loadClass(module, name);
+            c = BootLoader.loadClass(module, name);
+        }
+
+        return c != null && !c.isValueClass() ? c : null;
+    }
+
+    boolean isValueClass() {
+        Class<?> c = this;
+        while (c.isArray()) {
+            c = c.getComponentType();
+        }
+
+        // This could call MinimalValueTypes_1_0::isValueType or check ACC_VALUE.
+        // For now, check if it is a subtype of __Value to avoid
+        // initializing MinimalValueTypes_1_0 class during early startup.
+        return __Value.class.isAssignableFrom(c) && c != __Value.class;
+    }
+
+    private void ensureNotValueClass() {
+        if (this.isValueClass()) {
+            throw new UnsupportedOperationException("cannot reflect on derived value type "
+                + this.getName());
         }
     }
 
     /**
      * Creates a new instance of the class represented by this {@code Class}

@@ -506,10 +536,12 @@
     @CallerSensitive
     @Deprecated(since="9")
     public T newInstance()
         throws InstantiationException, IllegalAccessException
     {
+        ensureNotValueClass();
+
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
             checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), false);
         }
 

@@ -1235,10 +1267,12 @@
             for(int i = 0; i < parameterClasses.length; i++)
                 parameterClasses[i] = toClass(parameterTypes[i]);
 
             // Perform access check
             final Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass();
+            enclosingCandidate.ensureNotValueClass();
+
             SecurityManager sm = System.getSecurityManager();
             if (sm != null) {
                 enclosingCandidate.checkMemberAccess(sm, Member.DECLARED,
                                                      Reflection.getCallerClass(), true);
             }

@@ -1391,10 +1425,12 @@
             for(int i = 0; i < parameterClasses.length; i++)
                 parameterClasses[i] = toClass(parameterTypes[i]);
 
             // Perform access check
             final Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass();
+            enclosingCandidate.ensureNotValueClass();
+
             SecurityManager sm = System.getSecurityManager();
             if (sm != null) {
                 enclosingCandidate.checkMemberAccess(sm, Member.DECLARED,
                                                      Reflection.getCallerClass(), true);
             }

@@ -1687,10 +1723,12 @@
      *
      * @since 1.1
      */
     @CallerSensitive
     public Class<?>[] getClasses() {
+        ensureNotValueClass();
+
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
             checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), false);
         }
 

@@ -1756,10 +1794,12 @@
      * @jls 8.2 Class Members
      * @jls 8.3 Field Declarations
      */
     @CallerSensitive
     public Field[] getFields() throws SecurityException {
+        ensureNotValueClass();
+
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
             checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
         }
         return copyFields(privateGetPublicFields(null));

@@ -1846,10 +1886,12 @@
      * @jls 8.4 Method Declarations
      * @since 1.1
      */
     @CallerSensitive
     public Method[] getMethods() throws SecurityException {
+        ensureNotValueClass();
+
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
             checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
         }
         return copyMethods(privateGetPublicMethods());

@@ -1885,10 +1927,12 @@
      *
      * @since 1.1
      */
     @CallerSensitive
     public Constructor<?>[] getConstructors() throws SecurityException {
+        ensureNotValueClass();
+
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
             checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
         }
         return copyConstructors(privateGetDeclaredConstructors(true));

@@ -1938,10 +1982,12 @@
      * @jls 8.3 Field Declarations
      */
     @CallerSensitive
     public Field getField(String name)
         throws NoSuchFieldException, SecurityException {
+        ensureNotValueClass();
+
         Objects.requireNonNull(name);
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
             checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
         }

@@ -2047,10 +2093,12 @@
      * @since 1.1
      */
     @CallerSensitive
     public Method getMethod(String name, Class<?>... parameterTypes)
         throws NoSuchMethodException, SecurityException {
+        ensureNotValueClass();
+
         Objects.requireNonNull(name);
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
             checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
         }

@@ -2111,10 +2159,12 @@
      */
     @CallerSensitive
     public Constructor<T> getConstructor(Class<?>... parameterTypes)
         throws NoSuchMethodException, SecurityException
     {
+        ensureNotValueClass();
+
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
             checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
         }
         return getReflectionFactory().copyConstructor(

@@ -2157,10 +2207,12 @@
      *
      * @since 1.1
      */
     @CallerSensitive
     public Class<?>[] getDeclaredClasses() throws SecurityException {
+        ensureNotValueClass();
+
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
             checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), false);
         }
         return getDeclaredClasses0();

@@ -2209,10 +2261,12 @@
      * @jls 8.2 Class Members
      * @jls 8.3 Field Declarations
      */
     @CallerSensitive
     public Field[] getDeclaredFields() throws SecurityException {
+        ensureNotValueClass();
+
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
             checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
         }
         return copyFields(privateGetDeclaredFields(false));

@@ -2271,10 +2325,12 @@
      * @jls 8.4 Method Declarations
      * @since 1.1
      */
     @CallerSensitive
     public Method[] getDeclaredMethods() throws SecurityException {
+        ensureNotValueClass();
+
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
             checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
         }
         return copyMethods(privateGetDeclaredMethods(false));

@@ -2319,10 +2375,12 @@
      *
      * @since 1.1
      */
     @CallerSensitive
     public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
+        ensureNotValueClass();
+
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
             checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
         }
         return copyConstructors(privateGetDeclaredConstructors(false));

@@ -2370,10 +2428,12 @@
      * @jls 8.3 Field Declarations
      */
     @CallerSensitive
     public Field getDeclaredField(String name)
         throws NoSuchFieldException, SecurityException {
+        ensureNotValueClass();
+
         Objects.requireNonNull(name);
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
             checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
         }

@@ -2434,10 +2494,12 @@
      * @since 1.1
      */
     @CallerSensitive
     public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
         throws NoSuchMethodException, SecurityException {
+        ensureNotValueClass();
+
         Objects.requireNonNull(name);
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
             checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
         }

@@ -2489,10 +2551,12 @@
      */
     @CallerSensitive
     public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
         throws NoSuchMethodException, SecurityException
     {
+        ensureNotValueClass();
+
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
             checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
         }
 
< prev index next >