提交 4c287313 编写于 作者: S shade

8016236: Class.getGenericInterfaces performance improvement

Summary: cache more reflective data and lookup results.
Reviewed-by: alanb, plevart, psandoz, dl
Contributed-by: NDoug Lea &lt;dl@cs.oswego.edu&gt;, Aleksey Shipilev <aleksey.shipilev@oracle.com>
上级 8c7d41e7
...@@ -708,8 +708,9 @@ public final class Class<T> implements java.io.Serializable, ...@@ -708,8 +708,9 @@ public final class Class<T> implements java.io.Serializable,
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public TypeVariable<Class<T>>[] getTypeParameters() { public TypeVariable<Class<T>>[] getTypeParameters() {
if (getGenericSignature() != null) ClassRepository info = getGenericInfo();
return (TypeVariable<Class<T>>[])getGenericInfo().getTypeParameters(); if (info != null)
return (TypeVariable<Class<T>>[])info.getTypeParameters();
else else
return (TypeVariable<Class<T>>[])new TypeVariable<?>[0]; return (TypeVariable<Class<T>>[])new TypeVariable<?>[0];
} }
...@@ -759,15 +760,19 @@ public final class Class<T> implements java.io.Serializable, ...@@ -759,15 +760,19 @@ public final class Class<T> implements java.io.Serializable,
* @since 1.5 * @since 1.5
*/ */
public Type getGenericSuperclass() { public Type getGenericSuperclass() {
if (getGenericSignature() != null) { ClassRepository info = getGenericInfo();
if (info == null) {
return getSuperclass();
}
// Historical irregularity: // Historical irregularity:
// Generic signature marks interfaces with superclass = Object // Generic signature marks interfaces with superclass = Object
// but this API returns null for interfaces // but this API returns null for interfaces
if (isInterface()) if (isInterface()) {
return null; return null;
return getGenericInfo().getSuperclass(); }
} else
return getSuperclass(); return info.getSuperclass();
} }
/** /**
...@@ -830,7 +835,23 @@ public final class Class<T> implements java.io.Serializable, ...@@ -830,7 +835,23 @@ public final class Class<T> implements java.io.Serializable,
* *
* @return an array of interfaces implemented by this class. * @return an array of interfaces implemented by this class.
*/ */
public native Class<?>[] getInterfaces(); public Class<?>[] getInterfaces() {
ReflectionData<T> rd = reflectionData();
if (rd == null) {
// no cloning required
return getInterfaces0();
} else {
Class<?>[] interfaces = rd.interfaces;
if (interfaces == null) {
interfaces = getInterfaces0();
rd.interfaces = interfaces;
}
// defensively copy before handing over to user code
return interfaces.clone();
}
}
private native Class<?>[] getInterfaces0();
/** /**
* Returns the {@code Type}s representing the interfaces * Returns the {@code Type}s representing the interfaces
...@@ -882,10 +903,8 @@ public final class Class<T> implements java.io.Serializable, ...@@ -882,10 +903,8 @@ public final class Class<T> implements java.io.Serializable,
* @since 1.5 * @since 1.5
*/ */
public Type[] getGenericInterfaces() { public Type[] getGenericInterfaces() {
if (getGenericSignature() != null) ClassRepository info = getGenericInfo();
return getGenericInfo().getSuperInterfaces(); return (info == null) ? getInterfaces() : info.getSuperInterfaces();
else
return getInterfaces();
} }
...@@ -2313,6 +2332,8 @@ public final class Class<T> implements java.io.Serializable, ...@@ -2313,6 +2332,8 @@ public final class Class<T> implements java.io.Serializable,
// Intermediate results for getFields and getMethods // Intermediate results for getFields and getMethods
volatile Field[] declaredPublicFields; volatile Field[] declaredPublicFields;
volatile Method[] declaredPublicMethods; volatile Method[] declaredPublicMethods;
volatile Class<?>[] interfaces;
// Value of classRedefinedCount when we created this ReflectionData instance // Value of classRedefinedCount when we created this ReflectionData instance
final int redefinedCount; final int redefinedCount;
...@@ -2388,10 +2409,10 @@ public final class Class<T> implements java.io.Serializable, ...@@ -2388,10 +2409,10 @@ public final class Class<T> implements java.io.Serializable,
} }
// Generic signature handling // Generic signature handling
private native String getGenericSignature(); private native String getGenericSignature0();
// Generic info repository; lazily initialized // Generic info repository; lazily initialized
private transient ClassRepository genericInfo; private volatile transient ClassRepository genericInfo;
// accessor for factory // accessor for factory
private GenericsFactory getFactory() { private GenericsFactory getFactory() {
...@@ -2399,15 +2420,20 @@ public final class Class<T> implements java.io.Serializable, ...@@ -2399,15 +2420,20 @@ public final class Class<T> implements java.io.Serializable,
return CoreReflectionFactory.make(this, ClassScope.make(this)); 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() { private ClassRepository getGenericInfo() {
// lazily initialize repository if necessary ClassRepository genericInfo = this.genericInfo;
if (genericInfo == null) { if (genericInfo == null) {
// create and cache generic info repository String signature = getGenericSignature0();
genericInfo = ClassRepository.make(getGenericSignature(), if (signature == null) {
getFactory()); genericInfo = ClassRepository.NONE;
} else {
genericInfo = ClassRepository.make(signature, getFactory());
}
this.genericInfo = genericInfo;
} }
return genericInfo; //return cached repository return (genericInfo != ClassRepository.NONE) ? genericInfo : null;
} }
// Annotations handling // Annotations handling
......
...@@ -40,6 +40,8 @@ import java.lang.reflect.Type; ...@@ -40,6 +40,8 @@ import java.lang.reflect.Type;
*/ */
public class ClassRepository extends GenericDeclRepository<ClassSignature> { public class ClassRepository extends GenericDeclRepository<ClassSignature> {
public static final ClassRepository NONE = ClassRepository.make("Ljava/lang/Object;", null);
private Type superclass; // caches the generic superclass info private Type superclass; // caches the generic superclass info
private Type[] superInterfaces; // caches the generic superinterface info private Type[] superInterfaces; // caches the generic superinterface info
......
...@@ -55,7 +55,7 @@ extern jboolean VerifyFixClassname(char *utf_name); ...@@ -55,7 +55,7 @@ extern jboolean VerifyFixClassname(char *utf_name);
static JNINativeMethod methods[] = { static JNINativeMethod methods[] = {
{"getName0", "()" STR, (void *)&JVM_GetClassName}, {"getName0", "()" STR, (void *)&JVM_GetClassName},
{"getSuperclass", "()" CLS, NULL}, {"getSuperclass", "()" CLS, NULL},
{"getInterfaces", "()[" CLS, (void *)&JVM_GetClassInterfaces}, {"getInterfaces0", "()[" CLS, (void *)&JVM_GetClassInterfaces},
{"getClassLoader0", "()" JCL, (void *)&JVM_GetClassLoader}, {"getClassLoader0", "()" JCL, (void *)&JVM_GetClassLoader},
{"isInterface", "()Z", (void *)&JVM_IsInterface}, {"isInterface", "()Z", (void *)&JVM_IsInterface},
{"getSigners", "()[" OBJ, (void *)&JVM_GetClassSigners}, {"getSigners", "()[" OBJ, (void *)&JVM_GetClassSigners},
...@@ -70,7 +70,7 @@ static JNINativeMethod methods[] = { ...@@ -70,7 +70,7 @@ static JNINativeMethod methods[] = {
{"getProtectionDomain0", "()" PD, (void *)&JVM_GetProtectionDomain}, {"getProtectionDomain0", "()" PD, (void *)&JVM_GetProtectionDomain},
{"getDeclaredClasses0", "()[" CLS, (void *)&JVM_GetDeclaredClasses}, {"getDeclaredClasses0", "()[" CLS, (void *)&JVM_GetDeclaredClasses},
{"getDeclaringClass", "()" CLS, (void *)&JVM_GetDeclaringClass}, {"getDeclaringClass", "()" CLS, (void *)&JVM_GetDeclaringClass},
{"getGenericSignature", "()" STR, (void *)&JVM_GetClassSignature}, {"getGenericSignature0", "()" STR, (void *)&JVM_GetClassSignature},
{"getRawAnnotations", "()" BA, (void *)&JVM_GetClassAnnotations}, {"getRawAnnotations", "()" BA, (void *)&JVM_GetClassAnnotations},
{"getConstantPool", "()" CPL, (void *)&JVM_GetClassConstantPool}, {"getConstantPool", "()" CPL, (void *)&JVM_GetClassConstantPool},
{"desiredAssertionStatus0","("CLS")Z",(void *)&JVM_DesiredAssertionStatus}, {"desiredAssertionStatus0","("CLS")Z",(void *)&JVM_DesiredAssertionStatus},
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册