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

Print this page




 691     native ClassLoader getClassLoader0();
 692 
 693 
 694     /**
 695      * Returns an array of {@code TypeVariable} objects that represent the
 696      * type variables declared by the generic declaration represented by this
 697      * {@code GenericDeclaration} object, in declaration order.  Returns an
 698      * array of length 0 if the underlying generic declaration declares no type
 699      * variables.
 700      *
 701      * @return an array of {@code TypeVariable} objects that represent
 702      *     the type variables declared by this generic declaration
 703      * @throws java.lang.reflect.GenericSignatureFormatError if the generic
 704      *     signature of this generic declaration does not conform to
 705      *     the format specified in
 706      *     <cite>The Java&trade; Virtual Machine Specification</cite>
 707      * @since 1.5
 708      */
 709     @SuppressWarnings("unchecked")
 710     public TypeVariable<Class<T>>[] getTypeParameters() {
 711         if (getGenericSignature() != null)
 712             return (TypeVariable<Class<T>>[])getGenericInfo().getTypeParameters();

 713         else
 714             return (TypeVariable<Class<T>>[])new TypeVariable<?>[0];
 715     }
 716 
 717 
 718     /**
 719      * Returns the {@code Class} representing the superclass of the entity
 720      * (class, interface, primitive type or void) represented by this
 721      * {@code Class}.  If this {@code Class} represents either the
 722      * {@code Object} class, an interface, a primitive type, or void, then
 723      * null is returned.  If this object represents an array class then the
 724      * {@code Class} object representing the {@code Object} class is
 725      * returned.
 726      *
 727      * @return the superclass of the class represented by this object.
 728      */
 729     public native Class<? super T> getSuperclass();
 730 
 731 
 732     /**


 742      * java.lang.reflect.ParameterizedType ParameterizedType} for the
 743      * semantics of the creation process for parameterized types.  If
 744      * this {@code Class} represents either the {@code Object}
 745      * class, an interface, a primitive type, or void, then null is
 746      * returned.  If this object represents an array class then the
 747      * {@code Class} object representing the {@code Object} class is
 748      * returned.
 749      *
 750      * @throws java.lang.reflect.GenericSignatureFormatError if the generic
 751      *     class signature does not conform to the format specified in
 752      *     <cite>The Java&trade; Virtual Machine Specification</cite>
 753      * @throws TypeNotPresentException if the generic superclass
 754      *     refers to a non-existent type declaration
 755      * @throws java.lang.reflect.MalformedParameterizedTypeException if the
 756      *     generic superclass refers to a parameterized type that cannot be
 757      *     instantiated  for any reason
 758      * @return the superclass of the class represented by this object
 759      * @since 1.5
 760      */
 761     public Type getGenericSuperclass() {
 762         if (getGenericSignature() != null) {




 763             // Historical irregularity:
 764             // Generic signature marks interfaces with superclass = Object
 765             // but this API returns null for interfaces
 766             if (isInterface())
 767                 return null;
 768             return getGenericInfo().getSuperclass();
 769         } else
 770             return getSuperclass();
 771     }
 772 
 773     /**
 774      * Gets the package for this class.  The class loader of this class is used
 775      * to find the package.  If the class was loaded by the bootstrap class
 776      * loader the set of packages loaded from CLASSPATH is searched to find the
 777      * package of the class. Null is returned if no package object was created
 778      * by the class loader of this class.
 779      *
 780      * <p> Packages have attributes for versions and specifications only if the
 781      * information was defined in the manifests that accompany the classes, and
 782      * if the class loader created the package instance with the attributes
 783      * from the manifest.
 784      *
 785      * @return the package of the class, or null if no package
 786      *         information is available from the archive or codebase.
 787      */
 788     public Package getPackage() {
 789         return Package.getPackage(this);
 790     }


 813      * <blockquote>
 814      * {@code s.getClass().getInterfaces()[1]}
 815      * </blockquote>
 816      * is the {@code Class} object that represents interface
 817      * {@code DessertTopping}.
 818      *
 819      * <p> If this object represents an interface, the array contains objects
 820      * representing all interfaces extended by the interface. The order of the
 821      * interface objects in the array corresponds to the order of the interface
 822      * names in the {@code extends} clause of the declaration of the
 823      * interface represented by this object.
 824      *
 825      * <p> If this object represents a class or interface that implements no
 826      * interfaces, the method returns an array of length 0.
 827      *
 828      * <p> If this object represents a primitive type or void, the method
 829      * returns an array of length 0.
 830      *
 831      * @return an array of interfaces implemented by this class.
 832      */
 833     public native Class<?>[] getInterfaces();
















 834 
 835     /**
 836      * Returns the {@code Type}s representing the interfaces
 837      * directly implemented by the class or interface represented by
 838      * this object.
 839      *
 840      * <p>If a superinterface is a parameterized type, the
 841      * {@code Type} object returned for it must accurately reflect
 842      * the actual type parameters used in the source code. The
 843      * parameterized type representing each superinterface is created
 844      * if it had not been created before. See the declaration of
 845      * {@link java.lang.reflect.ParameterizedType ParameterizedType}
 846      * for the semantics of the creation process for parameterized
 847      * types.
 848      *
 849      * <p> If this object represents a class, the return value is an
 850      * array containing objects representing all interfaces
 851      * implemented by the class. The order of the interface objects in
 852      * the array corresponds to the order of the interface names in
 853      * the {@code implements} clause of the declaration of the class


 865      * <p>If this object represents a class or interface that
 866      * implements no interfaces, the method returns an array of length
 867      * 0.
 868      *
 869      * <p>If this object represents a primitive type or void, the
 870      * method returns an array of length 0.
 871      *
 872      * @throws java.lang.reflect.GenericSignatureFormatError
 873      *     if the generic class signature does not conform to the format
 874      *     specified in
 875      *     <cite>The Java&trade; Virtual Machine Specification</cite>
 876      * @throws TypeNotPresentException if any of the generic
 877      *     superinterfaces refers to a non-existent type declaration
 878      * @throws java.lang.reflect.MalformedParameterizedTypeException
 879      *     if any of the generic superinterfaces refer to a parameterized
 880      *     type that cannot be instantiated for any reason
 881      * @return an array of interfaces implemented by this class
 882      * @since 1.5
 883      */
 884     public Type[] getGenericInterfaces() {
 885         if (getGenericSignature() != null)
 886             return getGenericInfo().getSuperInterfaces();
 887         else
 888             return getInterfaces();
 889     }
 890 
 891 
 892     /**
 893      * Returns the {@code Class} representing the component type of an
 894      * array.  If this class does not represent an array class this method
 895      * returns null.
 896      *
 897      * @return the {@code Class} representing the component type of this
 898      * class if this class is an array
 899      * @see     java.lang.reflect.Array
 900      * @since JDK1.1
 901      */
 902     public native Class<?> getComponentType();
 903 
 904 
 905     /**
 906      * Returns the Java language modifiers for this class or interface, encoded
 907      * in an integer. The modifiers consist of the Java Virtual Machine's
 908      * constants for {@code public}, {@code protected},


2296     }
2297 
2298     /**
2299      * Reflection support.
2300      */
2301 
2302     // Caches for certain reflective results
2303     private static boolean useCaches = true;
2304 
2305     // reflection data that might get invalidated when JVM TI RedefineClasses() is called
2306     static class ReflectionData<T> {
2307         volatile Field[] declaredFields;
2308         volatile Field[] publicFields;
2309         volatile Method[] declaredMethods;
2310         volatile Method[] publicMethods;
2311         volatile Constructor<T>[] declaredConstructors;
2312         volatile Constructor<T>[] publicConstructors;
2313         // Intermediate results for getFields and getMethods
2314         volatile Field[] declaredPublicFields;
2315         volatile Method[] declaredPublicMethods;


2316         // Value of classRedefinedCount when we created this ReflectionData instance
2317         final int redefinedCount;
2318 
2319         ReflectionData(int redefinedCount) {
2320             this.redefinedCount = redefinedCount;
2321         }
2322 
2323         // initialize Unsafe machinery here, since we need to call Class.class instance method
2324         // and have to avoid calling it in the static initializer of the Class class...
2325         private static final Unsafe unsafe;
2326         // offset of Class.reflectionData instance field
2327         private static final long reflectionDataOffset;
2328 
2329         static {
2330             unsafe = Unsafe.getUnsafe();
2331             // bypass caches
2332             Field reflectionDataField = searchFields(Class.class.getDeclaredFields0(false),
2333                                                      "reflectionData");
2334             if (reflectionDataField == null) {
2335                 throw new Error("No reflectionData field found in java.lang.Class");


2371         if (!useCaches) return null;
2372 
2373         while (true) {
2374             ReflectionData<T> rd = new ReflectionData<>(classRedefinedCount);
2375             // try to CAS it...
2376             if (ReflectionData.compareAndSwap(this, oldReflectionData, new SoftReference<>(rd))) {
2377                 return rd;
2378             }
2379             // else retry
2380             oldReflectionData = this.reflectionData;
2381             classRedefinedCount = this.classRedefinedCount;
2382             if (oldReflectionData != null &&
2383                 (rd = oldReflectionData.get()) != null &&
2384                 rd.redefinedCount == classRedefinedCount) {
2385                 return rd;
2386             }
2387         }
2388     }
2389 
2390     // Generic signature handling
2391     private native String getGenericSignature();
2392 
2393     // Generic info repository; lazily initialized
2394     private transient ClassRepository genericInfo;
2395 
2396     // accessor for factory
2397     private GenericsFactory getFactory() {
2398         // create scope and factory
2399         return CoreReflectionFactory.make(this, ClassScope.make(this));
2400     }
2401 
2402     // accessor for generic info repository

2403     private ClassRepository getGenericInfo() {
2404         // lazily initialize repository if necessary
2405         if (genericInfo == null) {
2406             // create and cache generic info repository
2407             genericInfo = ClassRepository.make(getGenericSignature(),
2408                                                getFactory());




2409         }
2410         return genericInfo; //return cached repository
2411     }
2412 
2413     // Annotations handling
2414     private native byte[] getRawAnnotations();
2415     // Since 1.8
2416     native byte[] getRawTypeAnnotations();
2417     static byte[] getExecutableTypeAnnotationBytes(Executable ex) {
2418         return getReflectionFactory().getExecutableTypeAnnotationBytes(ex);
2419     }
2420 
2421     native ConstantPool getConstantPool();
2422 
2423     //
2424     //
2425     // java.lang.reflect.Field handling
2426     //
2427     //
2428 
2429     // Returns an array of "root" fields. These Field objects must NOT
2430     // be propagated to the outside world, but must instead be copied




 691     native ClassLoader getClassLoader0();
 692 
 693 
 694     /**
 695      * Returns an array of {@code TypeVariable} objects that represent the
 696      * type variables declared by the generic declaration represented by this
 697      * {@code GenericDeclaration} object, in declaration order.  Returns an
 698      * array of length 0 if the underlying generic declaration declares no type
 699      * variables.
 700      *
 701      * @return an array of {@code TypeVariable} objects that represent
 702      *     the type variables declared by this generic declaration
 703      * @throws java.lang.reflect.GenericSignatureFormatError if the generic
 704      *     signature of this generic declaration does not conform to
 705      *     the format specified in
 706      *     <cite>The Java&trade; Virtual Machine Specification</cite>
 707      * @since 1.5
 708      */
 709     @SuppressWarnings("unchecked")
 710     public TypeVariable<Class<T>>[] getTypeParameters() {
 711         ClassRepository info = getGenericInfo();
 712         if (info != null)
 713             return (TypeVariable<Class<T>>[])info.getTypeParameters();
 714         else
 715             return (TypeVariable<Class<T>>[])new TypeVariable<?>[0];
 716     }
 717 
 718 
 719     /**
 720      * Returns the {@code Class} representing the superclass of the entity
 721      * (class, interface, primitive type or void) represented by this
 722      * {@code Class}.  If this {@code Class} represents either the
 723      * {@code Object} class, an interface, a primitive type, or void, then
 724      * null is returned.  If this object represents an array class then the
 725      * {@code Class} object representing the {@code Object} class is
 726      * returned.
 727      *
 728      * @return the superclass of the class represented by this object.
 729      */
 730     public native Class<? super T> getSuperclass();
 731 
 732 
 733     /**


 743      * java.lang.reflect.ParameterizedType ParameterizedType} for the
 744      * semantics of the creation process for parameterized types.  If
 745      * this {@code Class} represents either the {@code Object}
 746      * class, an interface, a primitive type, or void, then null is
 747      * returned.  If this object represents an array class then the
 748      * {@code Class} object representing the {@code Object} class is
 749      * returned.
 750      *
 751      * @throws java.lang.reflect.GenericSignatureFormatError if the generic
 752      *     class signature does not conform to the format specified in
 753      *     <cite>The Java&trade; Virtual Machine Specification</cite>
 754      * @throws TypeNotPresentException if the generic superclass
 755      *     refers to a non-existent type declaration
 756      * @throws java.lang.reflect.MalformedParameterizedTypeException if the
 757      *     generic superclass refers to a parameterized type that cannot be
 758      *     instantiated  for any reason
 759      * @return the superclass of the class represented by this object
 760      * @since 1.5
 761      */
 762     public Type getGenericSuperclass() {
 763         ClassRepository info = getGenericInfo();
 764         if (info == null) {
 765             return getSuperclass();
 766         }
 767 
 768         // Historical irregularity:
 769         // Generic signature marks interfaces with superclass = Object
 770         // but this API returns null for interfaces
 771         if (isInterface()) {
 772             return null;
 773         }
 774 
 775         return info.getSuperclass();
 776     }
 777 
 778     /**
 779      * Gets the package for this class.  The class loader of this class is used
 780      * to find the package.  If the class was loaded by the bootstrap class
 781      * loader the set of packages loaded from CLASSPATH is searched to find the
 782      * package of the class. Null is returned if no package object was created
 783      * by the class loader of this class.
 784      *
 785      * <p> Packages have attributes for versions and specifications only if the
 786      * information was defined in the manifests that accompany the classes, and
 787      * if the class loader created the package instance with the attributes
 788      * from the manifest.
 789      *
 790      * @return the package of the class, or null if no package
 791      *         information is available from the archive or codebase.
 792      */
 793     public Package getPackage() {
 794         return Package.getPackage(this);
 795     }


 818      * <blockquote>
 819      * {@code s.getClass().getInterfaces()[1]}
 820      * </blockquote>
 821      * is the {@code Class} object that represents interface
 822      * {@code DessertTopping}.
 823      *
 824      * <p> If this object represents an interface, the array contains objects
 825      * representing all interfaces extended by the interface. The order of the
 826      * interface objects in the array corresponds to the order of the interface
 827      * names in the {@code extends} clause of the declaration of the
 828      * interface represented by this object.
 829      *
 830      * <p> If this object represents a class or interface that implements no
 831      * interfaces, the method returns an array of length 0.
 832      *
 833      * <p> If this object represents a primitive type or void, the method
 834      * returns an array of length 0.
 835      *
 836      * @return an array of interfaces implemented by this class.
 837      */
 838     public Class<?>[] getInterfaces() {
 839         ReflectionData<T> rd = reflectionData();
 840         if (rd == null) {
 841             // no cloning required
 842             return getInterfaces0();
 843         } else {
 844             Class<?>[] interfaces = rd.interfaces;
 845             if (interfaces == null) {
 846                 interfaces = getInterfaces0();
 847                 rd.interfaces = interfaces;
 848             }
 849             // defensively copy before handing over to user code
 850             return interfaces.clone();
 851         }
 852     }
 853 
 854     private native Class<?>[] getInterfaces0();
 855 
 856     /**
 857      * Returns the {@code Type}s representing the interfaces
 858      * directly implemented by the class or interface represented by
 859      * this object.
 860      *
 861      * <p>If a superinterface is a parameterized type, the
 862      * {@code Type} object returned for it must accurately reflect
 863      * the actual type parameters used in the source code. The
 864      * parameterized type representing each superinterface is created
 865      * if it had not been created before. See the declaration of
 866      * {@link java.lang.reflect.ParameterizedType ParameterizedType}
 867      * for the semantics of the creation process for parameterized
 868      * types.
 869      *
 870      * <p> If this object represents a class, the return value is an
 871      * array containing objects representing all interfaces
 872      * implemented by the class. The order of the interface objects in
 873      * the array corresponds to the order of the interface names in
 874      * the {@code implements} clause of the declaration of the class


 886      * <p>If this object represents a class or interface that
 887      * implements no interfaces, the method returns an array of length
 888      * 0.
 889      *
 890      * <p>If this object represents a primitive type or void, the
 891      * method returns an array of length 0.
 892      *
 893      * @throws java.lang.reflect.GenericSignatureFormatError
 894      *     if the generic class signature does not conform to the format
 895      *     specified in
 896      *     <cite>The Java&trade; Virtual Machine Specification</cite>
 897      * @throws TypeNotPresentException if any of the generic
 898      *     superinterfaces refers to a non-existent type declaration
 899      * @throws java.lang.reflect.MalformedParameterizedTypeException
 900      *     if any of the generic superinterfaces refer to a parameterized
 901      *     type that cannot be instantiated for any reason
 902      * @return an array of interfaces implemented by this class
 903      * @since 1.5
 904      */
 905     public Type[] getGenericInterfaces() {
 906         ClassRepository info = getGenericInfo();
 907         return (info == null) ?  getInterfaces() : info.getSuperInterfaces();


 908     }
 909 
 910 
 911     /**
 912      * Returns the {@code Class} representing the component type of an
 913      * array.  If this class does not represent an array class this method
 914      * returns null.
 915      *
 916      * @return the {@code Class} representing the component type of this
 917      * class if this class is an array
 918      * @see     java.lang.reflect.Array
 919      * @since JDK1.1
 920      */
 921     public native Class<?> getComponentType();
 922 
 923 
 924     /**
 925      * Returns the Java language modifiers for this class or interface, encoded
 926      * in an integer. The modifiers consist of the Java Virtual Machine's
 927      * constants for {@code public}, {@code protected},


2315     }
2316 
2317     /**
2318      * Reflection support.
2319      */
2320 
2321     // Caches for certain reflective results
2322     private static boolean useCaches = true;
2323 
2324     // reflection data that might get invalidated when JVM TI RedefineClasses() is called
2325     static class ReflectionData<T> {
2326         volatile Field[] declaredFields;
2327         volatile Field[] publicFields;
2328         volatile Method[] declaredMethods;
2329         volatile Method[] publicMethods;
2330         volatile Constructor<T>[] declaredConstructors;
2331         volatile Constructor<T>[] publicConstructors;
2332         // Intermediate results for getFields and getMethods
2333         volatile Field[] declaredPublicFields;
2334         volatile Method[] declaredPublicMethods;
2335         volatile Class<?>[] interfaces;
2336 
2337         // Value of classRedefinedCount when we created this ReflectionData instance
2338         final int redefinedCount;
2339 
2340         ReflectionData(int redefinedCount) {
2341             this.redefinedCount = redefinedCount;
2342         }
2343 
2344         // initialize Unsafe machinery here, since we need to call Class.class instance method
2345         // and have to avoid calling it in the static initializer of the Class class...
2346         private static final Unsafe unsafe;
2347         // offset of Class.reflectionData instance field
2348         private static final long reflectionDataOffset;
2349 
2350         static {
2351             unsafe = Unsafe.getUnsafe();
2352             // bypass caches
2353             Field reflectionDataField = searchFields(Class.class.getDeclaredFields0(false),
2354                                                      "reflectionData");
2355             if (reflectionDataField == null) {
2356                 throw new Error("No reflectionData field found in java.lang.Class");


2392         if (!useCaches) return null;
2393 
2394         while (true) {
2395             ReflectionData<T> rd = new ReflectionData<>(classRedefinedCount);
2396             // try to CAS it...
2397             if (ReflectionData.compareAndSwap(this, oldReflectionData, new SoftReference<>(rd))) {
2398                 return rd;
2399             }
2400             // else retry
2401             oldReflectionData = this.reflectionData;
2402             classRedefinedCount = this.classRedefinedCount;
2403             if (oldReflectionData != null &&
2404                 (rd = oldReflectionData.get()) != null &&
2405                 rd.redefinedCount == classRedefinedCount) {
2406                 return rd;
2407             }
2408         }
2409     }
2410 
2411     // Generic signature handling
2412     private native String getGenericSignature0();
2413 
2414     // Generic info repository; lazily initialized
2415     private volatile transient ClassRepository genericInfo;
2416 
2417     // accessor for factory
2418     private GenericsFactory getFactory() {
2419         // create scope and factory
2420         return CoreReflectionFactory.make(this, ClassScope.make(this));
2421     }
2422 
2423     // accessor for generic info repository;
2424     // generic info is lazily initialized
2425     private ClassRepository getGenericInfo() {
2426         ClassRepository genericInfo = this.genericInfo;
2427         if (genericInfo == null) {
2428             String signature = getGenericSignature0();
2429             if (signature == null) {
2430                 genericInfo = ClassRepository.NONE;
2431             } else {
2432                 genericInfo = ClassRepository.make(signature, getFactory());
2433             }
2434             this.genericInfo = genericInfo;
2435         }
2436         return (genericInfo != ClassRepository.NONE) ? genericInfo : null;
2437     }
2438 
2439     // Annotations handling
2440     private native byte[] getRawAnnotations();
2441     // Since 1.8
2442     native byte[] getRawTypeAnnotations();
2443     static byte[] getExecutableTypeAnnotationBytes(Executable ex) {
2444         return getReflectionFactory().getExecutableTypeAnnotationBytes(ex);
2445     }
2446 
2447     native ConstantPool getConstantPool();
2448 
2449     //
2450     //
2451     // java.lang.reflect.Field handling
2452     //
2453     //
2454 
2455     // Returns an array of "root" fields. These Field objects must NOT
2456     // be propagated to the outside world, but must instead be copied