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");


2370                                                 int classRedefinedCount) {
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.interfaces == null) {
 841             rd.interfaces = getInterfaces0();
 842         }
 843         return rd.interfaces.clone();
 844     }
 845 
 846     private native Class<?>[] getInterfaces0();
 847 
 848     /**
 849      * Returns the {@code Type}s representing the interfaces
 850      * directly implemented by the class or interface represented by
 851      * this object.
 852      *
 853      * <p>If a superinterface is a parameterized type, the
 854      * {@code Type} object returned for it must accurately reflect
 855      * the actual type parameters used in the source code. The
 856      * parameterized type representing each superinterface is created
 857      * if it had not been created before. See the declaration of
 858      * {@link java.lang.reflect.ParameterizedType ParameterizedType}
 859      * for the semantics of the creation process for parameterized
 860      * types.
 861      *
 862      * <p> If this object represents a class, the return value is an
 863      * array containing objects representing all interfaces
 864      * implemented by the class. The order of the interface objects in
 865      * the array corresponds to the order of the interface names in
 866      * the {@code implements} clause of the declaration of the class


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


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


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


2385                                                 int classRedefinedCount) {
2386         if (!useCaches) return null;
2387 
2388         while (true) {
2389             ReflectionData<T> rd = new ReflectionData<>(classRedefinedCount);
2390             // try to CAS it...
2391             if (ReflectionData.compareAndSwap(this, oldReflectionData, new SoftReference<>(rd))) {
2392                 return rd;
2393             }
2394             // else retry
2395             oldReflectionData = this.reflectionData;
2396             classRedefinedCount = this.classRedefinedCount;
2397             if (oldReflectionData != null &&
2398                 (rd = oldReflectionData.get()) != null &&
2399                 rd.redefinedCount == classRedefinedCount) {
2400                 return rd;
2401             }
2402         }
2403     }
2404 
2405     private String getGenericSignature() {
2406         ReflectionData<T> rd = reflectionData();
2407         if (!rd.genericSignatureResolved) {
2408             rd.genericSignature = getGenericSignature0();
2409             rd.genericSignatureResolved = true;
2410         }
2411         return rd.genericSignature;
2412     }
2413 
2414     // Generic signature handling
2415     private native String getGenericSignature0();
2416 
2417     // Generic info repository; lazily initialized
2418     private volatile transient ClassRepository genericInfo;
2419 
2420     // accessor for factory
2421     private GenericsFactory getFactory() {
2422         // create scope and factory
2423         return CoreReflectionFactory.make(this, ClassScope.make(this));
2424     }
2425 
2426     // accessor for generic info repository;
2427     // generic info is lazily initialized
2428     private ClassRepository getGenericInfo() {
2429         ClassRepository info = genericInfo;
2430         if (info != null) {
2431             return info;


2432         }
2433 
2434         String signature = getGenericSignature();
2435         if (signature == null) {
2436             return null;
2437         }
2438 
2439         genericInfo = ClassRepository.make(signature, getFactory());
2440         return genericInfo;
2441     }
2442 
2443     // Annotations handling
2444     private native byte[] getRawAnnotations();
2445     // Since 1.8
2446     native byte[] getRawTypeAnnotations();
2447     static byte[] getExecutableTypeAnnotationBytes(Executable ex) {
2448         return getReflectionFactory().getExecutableTypeAnnotationBytes(ex);
2449     }
2450 
2451     native ConstantPool getConstantPool();
2452 
2453     //
2454     //
2455     // java.lang.reflect.Field handling
2456     //
2457     //
2458 
2459     // Returns an array of "root" fields. These Field objects must NOT
2460     // be propagated to the outside world, but must instead be copied