jdk/src/share/classes/java/lang/Class.java
changeset 18179 9b6ad451b521
parent 18139 9fb8a2d427b5
child 18276 63b45dc8848c
equal deleted inserted replaced
18178:ee71c923891d 18179:9b6ad451b521
   706      *     <cite>The Java&trade; Virtual Machine Specification</cite>
   706      *     <cite>The Java&trade; Virtual Machine Specification</cite>
   707      * @since 1.5
   707      * @since 1.5
   708      */
   708      */
   709     @SuppressWarnings("unchecked")
   709     @SuppressWarnings("unchecked")
   710     public TypeVariable<Class<T>>[] getTypeParameters() {
   710     public TypeVariable<Class<T>>[] getTypeParameters() {
   711         if (getGenericSignature() != null)
   711         ClassRepository info = getGenericInfo();
   712             return (TypeVariable<Class<T>>[])getGenericInfo().getTypeParameters();
   712         if (info != null)
       
   713             return (TypeVariable<Class<T>>[])info.getTypeParameters();
   713         else
   714         else
   714             return (TypeVariable<Class<T>>[])new TypeVariable<?>[0];
   715             return (TypeVariable<Class<T>>[])new TypeVariable<?>[0];
   715     }
   716     }
   716 
   717 
   717 
   718 
   757      *     instantiated  for any reason
   758      *     instantiated  for any reason
   758      * @return the superclass of the class represented by this object
   759      * @return the superclass of the class represented by this object
   759      * @since 1.5
   760      * @since 1.5
   760      */
   761      */
   761     public Type getGenericSuperclass() {
   762     public Type getGenericSuperclass() {
   762         if (getGenericSignature() != null) {
   763         ClassRepository info = getGenericInfo();
   763             // Historical irregularity:
   764         if (info == null) {
   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();
   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();
   771     }
   776     }
   772 
   777 
   773     /**
   778     /**
   774      * Gets the package for this class.  The class loader of this class is used
   779      * 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
   780      * to find the package.  If the class was loaded by the bootstrap class
   828      * <p> If this object represents a primitive type or void, the method
   833      * <p> If this object represents a primitive type or void, the method
   829      * returns an array of length 0.
   834      * returns an array of length 0.
   830      *
   835      *
   831      * @return an array of interfaces implemented by this class.
   836      * @return an array of interfaces implemented by this class.
   832      */
   837      */
   833     public native Class<?>[] getInterfaces();
   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();
   834 
   855 
   835     /**
   856     /**
   836      * Returns the {@code Type}s representing the interfaces
   857      * Returns the {@code Type}s representing the interfaces
   837      * directly implemented by the class or interface represented by
   858      * directly implemented by the class or interface represented by
   838      * this object.
   859      * this object.
   880      *     type that cannot be instantiated for any reason
   901      *     type that cannot be instantiated for any reason
   881      * @return an array of interfaces implemented by this class
   902      * @return an array of interfaces implemented by this class
   882      * @since 1.5
   903      * @since 1.5
   883      */
   904      */
   884     public Type[] getGenericInterfaces() {
   905     public Type[] getGenericInterfaces() {
   885         if (getGenericSignature() != null)
   906         ClassRepository info = getGenericInfo();
   886             return getGenericInfo().getSuperInterfaces();
   907         return (info == null) ?  getInterfaces() : info.getSuperInterfaces();
   887         else
       
   888             return getInterfaces();
       
   889     }
   908     }
   890 
   909 
   891 
   910 
   892     /**
   911     /**
   893      * Returns the {@code Class} representing the component type of an
   912      * Returns the {@code Class} representing the component type of an
  2311         volatile Constructor<T>[] declaredConstructors;
  2330         volatile Constructor<T>[] declaredConstructors;
  2312         volatile Constructor<T>[] publicConstructors;
  2331         volatile Constructor<T>[] publicConstructors;
  2313         // Intermediate results for getFields and getMethods
  2332         // Intermediate results for getFields and getMethods
  2314         volatile Field[] declaredPublicFields;
  2333         volatile Field[] declaredPublicFields;
  2315         volatile Method[] declaredPublicMethods;
  2334         volatile Method[] declaredPublicMethods;
       
  2335         volatile Class<?>[] interfaces;
       
  2336 
  2316         // Value of classRedefinedCount when we created this ReflectionData instance
  2337         // Value of classRedefinedCount when we created this ReflectionData instance
  2317         final int redefinedCount;
  2338         final int redefinedCount;
  2318 
  2339 
  2319         ReflectionData(int redefinedCount) {
  2340         ReflectionData(int redefinedCount) {
  2320             this.redefinedCount = redefinedCount;
  2341             this.redefinedCount = redefinedCount;
  2386             }
  2407             }
  2387         }
  2408         }
  2388     }
  2409     }
  2389 
  2410 
  2390     // Generic signature handling
  2411     // Generic signature handling
  2391     private native String getGenericSignature();
  2412     private native String getGenericSignature0();
  2392 
  2413 
  2393     // Generic info repository; lazily initialized
  2414     // Generic info repository; lazily initialized
  2394     private transient ClassRepository genericInfo;
  2415     private volatile transient ClassRepository genericInfo;
  2395 
  2416 
  2396     // accessor for factory
  2417     // accessor for factory
  2397     private GenericsFactory getFactory() {
  2418     private GenericsFactory getFactory() {
  2398         // create scope and factory
  2419         // create scope and factory
  2399         return CoreReflectionFactory.make(this, ClassScope.make(this));
  2420         return CoreReflectionFactory.make(this, ClassScope.make(this));
  2400     }
  2421     }
  2401 
  2422 
  2402     // accessor for generic info repository
  2423     // accessor for generic info repository;
       
  2424     // generic info is lazily initialized
  2403     private ClassRepository getGenericInfo() {
  2425     private ClassRepository getGenericInfo() {
  2404         // lazily initialize repository if necessary
  2426         ClassRepository genericInfo = this.genericInfo;
  2405         if (genericInfo == null) {
  2427         if (genericInfo == null) {
  2406             // create and cache generic info repository
  2428             String signature = getGenericSignature0();
  2407             genericInfo = ClassRepository.make(getGenericSignature(),
  2429             if (signature == null) {
  2408                                                getFactory());
  2430                 genericInfo = ClassRepository.NONE;
  2409         }
  2431             } else {
  2410         return genericInfo; //return cached repository
  2432                 genericInfo = ClassRepository.make(signature, getFactory());
       
  2433             }
       
  2434             this.genericInfo = genericInfo;
       
  2435         }
       
  2436         return (genericInfo != ClassRepository.NONE) ? genericInfo : null;
  2411     }
  2437     }
  2412 
  2438 
  2413     // Annotations handling
  2439     // Annotations handling
  2414     private native byte[] getRawAnnotations();
  2440     private native byte[] getRawAnnotations();
  2415     // Since 1.8
  2441     // Since 1.8