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™ 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™ 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™ 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™ 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™ 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 if (rd.interfaces == null) {
845 rd.interfaces = getInterfaces0();
846 }
847 // defensively copy before handing over to user code
848 return rd.interfaces.clone();
849 }
850 }
851
852 private native Class<?>[] getInterfaces0();
853
854 /**
855 * Returns the {@code Type}s representing the interfaces
856 * directly implemented by the class or interface represented by
857 * this object.
858 *
859 * <p>If a superinterface is a parameterized type, the
860 * {@code Type} object returned for it must accurately reflect
861 * the actual type parameters used in the source code. The
862 * parameterized type representing each superinterface is created
863 * if it had not been created before. See the declaration of
864 * {@link java.lang.reflect.ParameterizedType ParameterizedType}
865 * for the semantics of the creation process for parameterized
866 * types.
867 *
868 * <p> If this object represents a class, the return value is an
869 * array containing objects representing all interfaces
870 * implemented by the class. The order of the interface objects in
871 * the array corresponds to the order of the interface names in
872 * the {@code implements} clause of the declaration of the class
884 * <p>If this object represents a class or interface that
885 * implements no interfaces, the method returns an array of length
886 * 0.
887 *
888 * <p>If this object represents a primitive type or void, the
889 * method returns an array of length 0.
890 *
891 * @throws java.lang.reflect.GenericSignatureFormatError
892 * if the generic class signature does not conform to the format
893 * specified in
894 * <cite>The Java™ Virtual Machine Specification</cite>
895 * @throws TypeNotPresentException if any of the generic
896 * superinterfaces refers to a non-existent type declaration
897 * @throws java.lang.reflect.MalformedParameterizedTypeException
898 * if any of the generic superinterfaces refer to a parameterized
899 * type that cannot be instantiated for any reason
900 * @return an array of interfaces implemented by this class
901 * @since 1.5
902 */
903 public Type[] getGenericInterfaces() {
904 ClassRepository info = getGenericInfo();
905 return (info == null) ? getInterfaces() : info.getSuperInterfaces();
906 }
907
908
909 /**
910 * Returns the {@code Class} representing the component type of an
911 * array. If this class does not represent an array class this method
912 * returns null.
913 *
914 * @return the {@code Class} representing the component type of this
915 * class if this class is an array
916 * @see java.lang.reflect.Array
917 * @since JDK1.1
918 */
919 public native Class<?> getComponentType();
920
921
922 /**
923 * Returns the Java language modifiers for this class or interface, encoded
924 * in an integer. The modifiers consist of the Java Virtual Machine's
925 * constants for {@code public}, {@code protected},
2313 }
2314
2315 /**
2316 * Reflection support.
2317 */
2318
2319 // Caches for certain reflective results
2320 private static boolean useCaches = true;
2321
2322 // reflection data that might get invalidated when JVM TI RedefineClasses() is called
2323 static class ReflectionData<T> {
2324 volatile Field[] declaredFields;
2325 volatile Field[] publicFields;
2326 volatile Method[] declaredMethods;
2327 volatile Method[] publicMethods;
2328 volatile Constructor<T>[] declaredConstructors;
2329 volatile Constructor<T>[] publicConstructors;
2330 // Intermediate results for getFields and getMethods
2331 volatile Field[] declaredPublicFields;
2332 volatile Method[] declaredPublicMethods;
2333 volatile Class<?>[] interfaces;
2334
2335 // Value of classRedefinedCount when we created this ReflectionData instance
2336 final int redefinedCount;
2337
2338 ReflectionData(int redefinedCount) {
2339 this.redefinedCount = redefinedCount;
2340 }
2341
2342 // initialize Unsafe machinery here, since we need to call Class.class instance method
2343 // and have to avoid calling it in the static initializer of the Class class...
2344 private static final Unsafe unsafe;
2345 // offset of Class.reflectionData instance field
2346 private static final long reflectionDataOffset;
2347
2348 static {
2349 unsafe = Unsafe.getUnsafe();
2350 // bypass caches
2351 Field reflectionDataField = searchFields(Class.class.getDeclaredFields0(false),
2352 "reflectionData");
2353 if (reflectionDataField == null) {
2354 throw new Error("No reflectionData field found in java.lang.Class");
2390 if (!useCaches) return null;
2391
2392 while (true) {
2393 ReflectionData<T> rd = new ReflectionData<>(classRedefinedCount);
2394 // try to CAS it...
2395 if (ReflectionData.compareAndSwap(this, oldReflectionData, new SoftReference<>(rd))) {
2396 return rd;
2397 }
2398 // else retry
2399 oldReflectionData = this.reflectionData;
2400 classRedefinedCount = this.classRedefinedCount;
2401 if (oldReflectionData != null &&
2402 (rd = oldReflectionData.get()) != null &&
2403 rd.redefinedCount == classRedefinedCount) {
2404 return rd;
2405 }
2406 }
2407 }
2408
2409 // Generic signature handling
2410 private native String getGenericSignature0();
2411
2412 // Generic info repository; lazily initialized
2413 private volatile transient ClassRepository genericInfo;
2414
2415 // accessor for factory
2416 private GenericsFactory getFactory() {
2417 // create scope and factory
2418 return CoreReflectionFactory.make(this, ClassScope.make(this));
2419 }
2420
2421 private static class ClassRepositoryHolder {
2422 static final ClassRepository NONE = ClassRepository.make("Ljava/lang/Object;", null);
2423 }
2424
2425 // accessor for generic info repository;
2426 // generic info is lazily initialized
2427 private ClassRepository getGenericInfo() {
2428 ClassRepository genericInfo = this.genericInfo;
2429 if (genericInfo == null) {
2430 String signature = getGenericSignature0();
2431 if (signature == null) {
2432 genericInfo = ClassRepositoryHolder.NONE;
2433 } else {
2434 genericInfo = ClassRepository.make(signature, getFactory());
2435 }
2436 this.genericInfo = genericInfo;
2437 }
2438 return (genericInfo != ClassRepositoryHolder.NONE) ? genericInfo : null;
2439 }
2440
2441 // Annotations handling
2442 private native byte[] getRawAnnotations();
2443 // Since 1.8
2444 native byte[] getRawTypeAnnotations();
2445 static byte[] getExecutableTypeAnnotationBytes(Executable ex) {
2446 return getReflectionFactory().getExecutableTypeAnnotationBytes(ex);
2447 }
2448
2449 native ConstantPool getConstantPool();
2450
2451 //
2452 //
2453 // java.lang.reflect.Field handling
2454 //
2455 //
2456
2457 // Returns an array of "root" fields. These Field objects must NOT
2458 // be propagated to the outside world, but must instead be copied
|