jdk/src/share/classes/java/lang/Class.java
Print this page
@@ -706,12 +706,13 @@
* <cite>The Java™ Virtual Machine Specification</cite>
* @since 1.5
*/
@SuppressWarnings("unchecked")
public TypeVariable<Class<T>>[] getTypeParameters() {
- if (getGenericSignature() != null)
- return (TypeVariable<Class<T>>[])getGenericInfo().getTypeParameters();
+ ClassRepository info = getGenericInfo();
+ if (info != null)
+ return (TypeVariable<Class<T>>[])info.getTypeParameters();
else
return (TypeVariable<Class<T>>[])new TypeVariable<?>[0];
}
@@ -757,19 +758,23 @@
* instantiated for any reason
* @return the superclass of the class represented by this object
* @since 1.5
*/
public Type getGenericSuperclass() {
- if (getGenericSignature() != null) {
+ ClassRepository info = getGenericInfo();
+ if (info == null) {
+ return getSuperclass();
+ }
+
// Historical irregularity:
// Generic signature marks interfaces with superclass = Object
// but this API returns null for interfaces
- if (isInterface())
+ if (isInterface()) {
return null;
- return getGenericInfo().getSuperclass();
- } else
- return getSuperclass();
+ }
+
+ return info.getSuperclass();
}
/**
* Gets the package for this class. The class loader of this class is used
* to find the package. If the class was loaded by the bootstrap class
@@ -828,11 +833,19 @@
* <p> If this object represents a primitive type or void, the method
* returns an array of length 0.
*
* @return an array of interfaces implemented by this class.
*/
- public native Class<?>[] getInterfaces();
+ public Class<?>[] getInterfaces() {
+ ReflectionData<T> rd = reflectionData();
+ if (rd.interfaces == null) {
+ rd.interfaces = getInterfaces0();
+ }
+ return rd.interfaces.clone();
+ }
+
+ private native Class<?>[] getInterfaces0();
/**
* Returns the {@code Type}s representing the interfaces
* directly implemented by the class or interface represented by
* this object.
@@ -880,14 +893,12 @@
* type that cannot be instantiated for any reason
* @return an array of interfaces implemented by this class
* @since 1.5
*/
public Type[] getGenericInterfaces() {
- if (getGenericSignature() != null)
- return getGenericInfo().getSuperInterfaces();
- else
- return getInterfaces();
+ ClassRepository info = getGenericInfo();
+ return (info == null) ? getInterfaces() : info.getSuperInterfaces();
}
/**
* Returns the {@code Class} representing the component type of an
@@ -2311,10 +2322,14 @@
volatile Constructor<T>[] declaredConstructors;
volatile Constructor<T>[] publicConstructors;
// Intermediate results for getFields and getMethods
volatile Field[] declaredPublicFields;
volatile Method[] declaredPublicMethods;
+ volatile Class<?>[] interfaces;
+ volatile boolean genericSignatureResolved;
+ volatile String genericSignature;
+
// Value of classRedefinedCount when we created this ReflectionData instance
final int redefinedCount;
ReflectionData(int redefinedCount) {
this.redefinedCount = redefinedCount;
@@ -2385,31 +2400,46 @@
return rd;
}
}
}
+ private String getGenericSignature() {
+ ReflectionData<T> rd = reflectionData();
+ if (!rd.genericSignatureResolved) {
+ rd.genericSignature = getGenericSignature0();
+ rd.genericSignatureResolved = true;
+ }
+ return rd.genericSignature;
+ }
+
// Generic signature handling
- private native String getGenericSignature();
+ private native String getGenericSignature0();
// Generic info repository; lazily initialized
- private transient ClassRepository genericInfo;
+ private volatile transient ClassRepository genericInfo;
// accessor for factory
private GenericsFactory getFactory() {
// create scope and factory
return CoreReflectionFactory.make(this, ClassScope.make(this));
}
- // accessor for generic info repository
+ // accessor for generic info repository;
+ // generic info is lazily initialized
private ClassRepository getGenericInfo() {
- // lazily initialize repository if necessary
- if (genericInfo == null) {
- // create and cache generic info repository
- genericInfo = ClassRepository.make(getGenericSignature(),
- getFactory());
+ ClassRepository info = genericInfo;
+ if (info != null) {
+ return info;
}
- return genericInfo; //return cached repository
+
+ String signature = getGenericSignature();
+ if (signature == null) {
+ return null;
+ }
+
+ genericInfo = ClassRepository.make(signature, getFactory());
+ return genericInfo;
}
// Annotations handling
private native byte[] getRawAnnotations();
// Since 1.8