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

Print this page




2953                                 values.setAccessible(true);
2954                                 return null;
2955                             }
2956                         });
2957                 @SuppressWarnings("unchecked")
2958                 T[] temporaryConstants = (T[])values.invoke(null);
2959                 enumConstants = temporaryConstants;
2960             }
2961             // These can happen when users concoct enum-like classes
2962             // that don't comply with the enum spec.
2963             catch (InvocationTargetException | NoSuchMethodException |
2964                    IllegalAccessException ex) { return null; }
2965         }
2966         return enumConstants;
2967     }
2968     private volatile transient T[] enumConstants = null;
2969 
2970     /**
2971      * Returns a map from simple name to enum constant.  This package-private
2972      * method is used internally by Enum to implement
2973      *     public static <T extends Enum<T>> T valueOf(Class<T>, String)
2974      * efficiently.  Note that the map is returned by this method is
2975      * created lazily on first use.  Typically it won't ever get created.
2976      */
2977     Map<String, T> enumConstantDirectory() {
2978         if (enumConstantDirectory == null) {
2979             T[] universe = getEnumConstantsShared();
2980             if (universe == null)
2981                 throw new IllegalArgumentException(
2982                     getName() + " is not an enum type");
2983             Map<String, T> m = new HashMap<>(2 * universe.length);
2984             for (T constant : universe)
2985                 m.put(((Enum<?>)constant).name(), constant);
2986             enumConstantDirectory = m;
2987         }
2988         return enumConstantDirectory;
2989     }
2990     private volatile transient Map<String, T> enumConstantDirectory = null;
2991 
2992     /**
2993      * Casts an object to the class or interface represented




2953                                 values.setAccessible(true);
2954                                 return null;
2955                             }
2956                         });
2957                 @SuppressWarnings("unchecked")
2958                 T[] temporaryConstants = (T[])values.invoke(null);
2959                 enumConstants = temporaryConstants;
2960             }
2961             // These can happen when users concoct enum-like classes
2962             // that don't comply with the enum spec.
2963             catch (InvocationTargetException | NoSuchMethodException |
2964                    IllegalAccessException ex) { return null; }
2965         }
2966         return enumConstants;
2967     }
2968     private volatile transient T[] enumConstants = null;
2969 
2970     /**
2971      * Returns a map from simple name to enum constant.  This package-private
2972      * method is used internally by Enum to implement
2973      * {@code public static <T extends Enum<T>> T valueOf(Class<T>, String)}
2974      * efficiently.  Note that the map is returned by this method is
2975      * created lazily on first use.  Typically it won't ever get created.
2976      */
2977     Map<String, T> enumConstantDirectory() {
2978         if (enumConstantDirectory == null) {
2979             T[] universe = getEnumConstantsShared();
2980             if (universe == null)
2981                 throw new IllegalArgumentException(
2982                     getName() + " is not an enum type");
2983             Map<String, T> m = new HashMap<>(2 * universe.length);
2984             for (T constant : universe)
2985                 m.put(((Enum<?>)constant).name(), constant);
2986             enumConstantDirectory = m;
2987         }
2988         return enumConstantDirectory;
2989     }
2990     private volatile transient Map<String, T> enumConstantDirectory = null;
2991 
2992     /**
2993      * Casts an object to the class or interface represented