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

Print this page




 332                 throw new IllegalAccessException(
 333                     "Can not call newInstance() on the Class for java.lang.Class"
 334                 );
 335             }
 336             try {
 337                 Class<?>[] empty = {};
 338                 final Constructor<T> c = getConstructor0(empty, Member.DECLARED);
 339                 // Disable accessibility checks on the constructor
 340                 // since we have to do the security check here anyway
 341                 // (the stack depth is wrong for the Constructor's
 342                 // security check to work)
 343                 java.security.AccessController.doPrivileged(
 344                     new java.security.PrivilegedAction<Void>() {
 345                         public Void run() {
 346                                 c.setAccessible(true);
 347                                 return null;
 348                             }
 349                         });
 350                 cachedConstructor = c;
 351             } catch (NoSuchMethodException e) {
 352                 throw new InstantiationException(getName());

 353             }
 354         }
 355         Constructor<T> tmpConstructor = cachedConstructor;
 356         // Security check (same as in java.lang.reflect.Constructor)
 357         int modifiers = tmpConstructor.getModifiers();
 358         if (!Reflection.quickCheckMemberAccess(this, modifiers)) {
 359             Class<?> caller = Reflection.getCallerClass(3);
 360             if (newInstanceCallerCache != caller) {
 361                 Reflection.ensureMemberAccess(caller, this, null, modifiers);
 362                 newInstanceCallerCache = caller;
 363             }
 364         }
 365         // Run constructor
 366         try {
 367             return tmpConstructor.newInstance((Object[])null);
 368         } catch (InvocationTargetException e) {
 369             Unsafe.getUnsafe().throwException(e.getTargetException());
 370             // Not reached
 371             return null;
 372         }


 956 
 957         private EnclosingMethodInfo(Object[] enclosingInfo) {
 958             if (enclosingInfo.length != 3)
 959                 throw new InternalError("Malformed enclosing method information");
 960             try {
 961                 // The array is expected to have three elements:
 962 
 963                 // the immediately enclosing class
 964                 enclosingClass = (Class<?>) enclosingInfo[0];
 965                 assert(enclosingClass != null);
 966 
 967                 // the immediately enclosing method or constructor's
 968                 // name (can be null).
 969                 name            = (String)   enclosingInfo[1];
 970 
 971                 // the immediately enclosing method or constructor's
 972                 // descriptor (null iff name is).
 973                 descriptor      = (String)   enclosingInfo[2];
 974                 assert((name != null && descriptor != null) || name == descriptor);
 975             } catch (ClassCastException cce) {
 976                 throw new InternalError("Invalid type in enclosing method information");

 977             }
 978         }
 979 
 980         boolean isPartial() {
 981             return enclosingClass == null || name == null || descriptor == null;
 982         }
 983 
 984         boolean isConstructor() { return !isPartial() && "<init>".equals(name); }
 985 
 986         boolean isMethod() { return !isPartial() && !isConstructor() && !"<clinit>".equals(name); }
 987 
 988         Class<?> getEnclosingClass() { return enclosingClass; }
 989 
 990         String getName() { return name; }
 991 
 992         String getDescriptor() { return descriptor; }
 993 
 994     }
 995 
 996     private static Class<?> toClass(Type o) {


1222      * @since 1.5
1223      */
1224     public boolean isMemberClass() {
1225         return getSimpleBinaryName() != null && !isLocalOrAnonymousClass();
1226     }
1227 
1228     /**
1229      * Returns the "simple binary name" of the underlying class, i.e.,
1230      * the binary name without the leading enclosing class name.
1231      * Returns {@code null} if the underlying class is a top level
1232      * class.
1233      */
1234     private String getSimpleBinaryName() {
1235         Class<?> enclosingClass = getEnclosingClass();
1236         if (enclosingClass == null) // top level class
1237             return null;
1238         // Otherwise, strip the enclosing class' name
1239         try {
1240             return getName().substring(enclosingClass.getName().length());
1241         } catch (IndexOutOfBoundsException ex) {
1242             throw new InternalError("Malformed class name");

1243         }
1244     }
1245 
1246     /**
1247      * Returns {@code true} if this is a local class or an anonymous
1248      * class.  Returns {@code false} otherwise.
1249      */
1250     private boolean isLocalOrAnonymousClass() {
1251         // JVM Spec 4.8.6: A class must have an EnclosingMethod
1252         // attribute if and only if it is a local class or an
1253         // anonymous class.
1254         return getEnclosingMethodInfo() != null;
1255     }
1256 
1257     /**
1258      * Returns an array containing {@code Class} objects representing all
1259      * the public classes and interfaces that are members of the class
1260      * represented by this {@code Class} object.  This includes public
1261      * class and interface members inherited from superclasses and public class
1262      * and interface members declared by the class.  This method returns an


2937      * Class object does not represent an enum type;
2938      * identical to getEnumConstants except that the result is
2939      * uncloned, cached, and shared by all callers.
2940      */
2941     T[] getEnumConstantsShared() {
2942         if (enumConstants == null) {
2943             if (!isEnum()) return null;
2944             try {
2945                 final Method values = getMethod("values");
2946                 java.security.AccessController.doPrivileged(
2947                     new java.security.PrivilegedAction<Void>() {
2948                         public Void run() {
2949                                 values.setAccessible(true);
2950                                 return null;
2951                             }
2952                         });
2953                 enumConstants = (T[])values.invoke(null);
2954             }
2955             // These can happen when users concoct enum-like classes
2956             // that don't comply with the enum spec.
2957             catch (InvocationTargetException ex) { return null; }
2958             catch (NoSuchMethodException ex) { return null; }
2959             catch (IllegalAccessException ex) { return null; }
2960         }
2961         return enumConstants;
2962     }
2963     private volatile transient T[] enumConstants = null;
2964 
2965     /**
2966      * Returns a map from simple name to enum constant.  This package-private
2967      * method is used internally by Enum to implement
2968      *     public static <T extends Enum<T>> T valueOf(Class<T>, String)
2969      * efficiently.  Note that the map is returned by this method is
2970      * created lazily on first use.  Typically it won't ever get created.
2971      */
2972     Map<String, T> enumConstantDirectory() {
2973         if (enumConstantDirectory == null) {
2974             T[] universe = getEnumConstantsShared();
2975             if (universe == null)
2976                 throw new IllegalArgumentException(
2977                     getName() + " is not an enum type");
2978             Map<String, T> m = new HashMap<>(2 * universe.length);
2979             for (T constant : universe)




 332                 throw new IllegalAccessException(
 333                     "Can not call newInstance() on the Class for java.lang.Class"
 334                 );
 335             }
 336             try {
 337                 Class<?>[] empty = {};
 338                 final Constructor<T> c = getConstructor0(empty, Member.DECLARED);
 339                 // Disable accessibility checks on the constructor
 340                 // since we have to do the security check here anyway
 341                 // (the stack depth is wrong for the Constructor's
 342                 // security check to work)
 343                 java.security.AccessController.doPrivileged(
 344                     new java.security.PrivilegedAction<Void>() {
 345                         public Void run() {
 346                                 c.setAccessible(true);
 347                                 return null;
 348                             }
 349                         });
 350                 cachedConstructor = c;
 351             } catch (NoSuchMethodException e) {
 352                 throw (InstantiationException)
 353                     new InstantiationException(getName()).initCause(e);
 354             }
 355         }
 356         Constructor<T> tmpConstructor = cachedConstructor;
 357         // Security check (same as in java.lang.reflect.Constructor)
 358         int modifiers = tmpConstructor.getModifiers();
 359         if (!Reflection.quickCheckMemberAccess(this, modifiers)) {
 360             Class<?> caller = Reflection.getCallerClass(3);
 361             if (newInstanceCallerCache != caller) {
 362                 Reflection.ensureMemberAccess(caller, this, null, modifiers);
 363                 newInstanceCallerCache = caller;
 364             }
 365         }
 366         // Run constructor
 367         try {
 368             return tmpConstructor.newInstance((Object[])null);
 369         } catch (InvocationTargetException e) {
 370             Unsafe.getUnsafe().throwException(e.getTargetException());
 371             // Not reached
 372             return null;
 373         }


 957 
 958         private EnclosingMethodInfo(Object[] enclosingInfo) {
 959             if (enclosingInfo.length != 3)
 960                 throw new InternalError("Malformed enclosing method information");
 961             try {
 962                 // The array is expected to have three elements:
 963 
 964                 // the immediately enclosing class
 965                 enclosingClass = (Class<?>) enclosingInfo[0];
 966                 assert(enclosingClass != null);
 967 
 968                 // the immediately enclosing method or constructor's
 969                 // name (can be null).
 970                 name            = (String)   enclosingInfo[1];
 971 
 972                 // the immediately enclosing method or constructor's
 973                 // descriptor (null iff name is).
 974                 descriptor      = (String)   enclosingInfo[2];
 975                 assert((name != null && descriptor != null) || name == descriptor);
 976             } catch (ClassCastException cce) {
 977                 throw (InternalError)
 978                     new InternalError("Invalid type in enclosing method information").initCause(cce);
 979             }
 980         }
 981 
 982         boolean isPartial() {
 983             return enclosingClass == null || name == null || descriptor == null;
 984         }
 985 
 986         boolean isConstructor() { return !isPartial() && "<init>".equals(name); }
 987 
 988         boolean isMethod() { return !isPartial() && !isConstructor() && !"<clinit>".equals(name); }
 989 
 990         Class<?> getEnclosingClass() { return enclosingClass; }
 991 
 992         String getName() { return name; }
 993 
 994         String getDescriptor() { return descriptor; }
 995 
 996     }
 997 
 998     private static Class<?> toClass(Type o) {


1224      * @since 1.5
1225      */
1226     public boolean isMemberClass() {
1227         return getSimpleBinaryName() != null && !isLocalOrAnonymousClass();
1228     }
1229 
1230     /**
1231      * Returns the "simple binary name" of the underlying class, i.e.,
1232      * the binary name without the leading enclosing class name.
1233      * Returns {@code null} if the underlying class is a top level
1234      * class.
1235      */
1236     private String getSimpleBinaryName() {
1237         Class<?> enclosingClass = getEnclosingClass();
1238         if (enclosingClass == null) // top level class
1239             return null;
1240         // Otherwise, strip the enclosing class' name
1241         try {
1242             return getName().substring(enclosingClass.getName().length());
1243         } catch (IndexOutOfBoundsException ex) {
1244             throw (InternalError)
1245                 new InternalError("Malformed class name").initCause(ex);
1246         }
1247     }
1248 
1249     /**
1250      * Returns {@code true} if this is a local class or an anonymous
1251      * class.  Returns {@code false} otherwise.
1252      */
1253     private boolean isLocalOrAnonymousClass() {
1254         // JVM Spec 4.8.6: A class must have an EnclosingMethod
1255         // attribute if and only if it is a local class or an
1256         // anonymous class.
1257         return getEnclosingMethodInfo() != null;
1258     }
1259 
1260     /**
1261      * Returns an array containing {@code Class} objects representing all
1262      * the public classes and interfaces that are members of the class
1263      * represented by this {@code Class} object.  This includes public
1264      * class and interface members inherited from superclasses and public class
1265      * and interface members declared by the class.  This method returns an


2940      * Class object does not represent an enum type;
2941      * identical to getEnumConstants except that the result is
2942      * uncloned, cached, and shared by all callers.
2943      */
2944     T[] getEnumConstantsShared() {
2945         if (enumConstants == null) {
2946             if (!isEnum()) return null;
2947             try {
2948                 final Method values = getMethod("values");
2949                 java.security.AccessController.doPrivileged(
2950                     new java.security.PrivilegedAction<Void>() {
2951                         public Void run() {
2952                                 values.setAccessible(true);
2953                                 return null;
2954                             }
2955                         });
2956                 enumConstants = (T[])values.invoke(null);
2957             }
2958             // These can happen when users concoct enum-like classes
2959             // that don't comply with the enum spec.
2960             catch (InvocationTargetException | NoSuchMethodException |
2961                    IllegalAccessException ex) { return null; }

2962         }
2963         return enumConstants;
2964     }
2965     private volatile transient T[] enumConstants = null;
2966 
2967     /**
2968      * Returns a map from simple name to enum constant.  This package-private
2969      * method is used internally by Enum to implement
2970      *     public static <T extends Enum<T>> T valueOf(Class<T>, String)
2971      * efficiently.  Note that the map is returned by this method is
2972      * created lazily on first use.  Typically it won't ever get created.
2973      */
2974     Map<String, T> enumConstantDirectory() {
2975         if (enumConstantDirectory == null) {
2976             T[] universe = getEnumConstantsShared();
2977             if (universe == null)
2978                 throw new IllegalArgumentException(
2979                     getName() + " is not an enum type");
2980             Map<String, T> m = new HashMap<>(2 * universe.length);
2981             for (T constant : universe)