jdk/src/share/classes/java/lang/Class.java
changeset 18276 63b45dc8848c
parent 18263 69df685432b7
parent 18179 9b6ad451b521
child 18546 862067c6481c
equal deleted inserted replaced
18275:9e7a5558965d 18276:63b45dc8848c
   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
  2394         volatile Constructor<T>[] declaredConstructors;
  2413         volatile Constructor<T>[] declaredConstructors;
  2395         volatile Constructor<T>[] publicConstructors;
  2414         volatile Constructor<T>[] publicConstructors;
  2396         // Intermediate results for getFields and getMethods
  2415         // Intermediate results for getFields and getMethods
  2397         volatile Field[] declaredPublicFields;
  2416         volatile Field[] declaredPublicFields;
  2398         volatile Method[] declaredPublicMethods;
  2417         volatile Method[] declaredPublicMethods;
       
  2418         volatile Class<?>[] interfaces;
       
  2419 
  2399         // Value of classRedefinedCount when we created this ReflectionData instance
  2420         // Value of classRedefinedCount when we created this ReflectionData instance
  2400         final int redefinedCount;
  2421         final int redefinedCount;
  2401 
  2422 
  2402         ReflectionData(int redefinedCount) {
  2423         ReflectionData(int redefinedCount) {
  2403             this.redefinedCount = redefinedCount;
  2424             this.redefinedCount = redefinedCount;
  2469             }
  2490             }
  2470         }
  2491         }
  2471     }
  2492     }
  2472 
  2493 
  2473     // Generic signature handling
  2494     // Generic signature handling
  2474     private native String getGenericSignature();
  2495     private native String getGenericSignature0();
  2475 
  2496 
  2476     // Generic info repository; lazily initialized
  2497     // Generic info repository; lazily initialized
  2477     private transient ClassRepository genericInfo;
  2498     private volatile transient ClassRepository genericInfo;
  2478 
  2499 
  2479     // accessor for factory
  2500     // accessor for factory
  2480     private GenericsFactory getFactory() {
  2501     private GenericsFactory getFactory() {
  2481         // create scope and factory
  2502         // create scope and factory
  2482         return CoreReflectionFactory.make(this, ClassScope.make(this));
  2503         return CoreReflectionFactory.make(this, ClassScope.make(this));
  2483     }
  2504     }
  2484 
  2505 
  2485     // accessor for generic info repository
  2506     // accessor for generic info repository;
       
  2507     // generic info is lazily initialized
  2486     private ClassRepository getGenericInfo() {
  2508     private ClassRepository getGenericInfo() {
  2487         // lazily initialize repository if necessary
  2509         ClassRepository genericInfo = this.genericInfo;
  2488         if (genericInfo == null) {
  2510         if (genericInfo == null) {
  2489             // create and cache generic info repository
  2511             String signature = getGenericSignature0();
  2490             genericInfo = ClassRepository.make(getGenericSignature(),
  2512             if (signature == null) {
  2491                                                getFactory());
  2513                 genericInfo = ClassRepository.NONE;
  2492         }
  2514             } else {
  2493         return genericInfo; //return cached repository
  2515                 genericInfo = ClassRepository.make(signature, getFactory());
       
  2516             }
       
  2517             this.genericInfo = genericInfo;
       
  2518         }
       
  2519         return (genericInfo != ClassRepository.NONE) ? genericInfo : null;
  2494     }
  2520     }
  2495 
  2521 
  2496     // Annotations handling
  2522     // Annotations handling
  2497     private native byte[] getRawAnnotations();
  2523     private native byte[] getRawAnnotations();
  2498     // Since 1.8
  2524     // Since 1.8