remove caching from ReflectionCache

This commit is contained in:
peter
2013-11-27 10:29:41 +01:00
parent a6f914142c
commit 691aebbfa0
3 changed files with 26 additions and 59 deletions
@@ -15,70 +15,30 @@
*/
package com.intellij.util;
import com.intellij.util.containers.ConcurrentFactoryMap;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.*;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
/**
* Contrary to the name, this class doesn't do any caching. So the usages may be safely dropped in favor of plain reflection calls.
*
* Consider caching higher-level things, if you see reflection in your snapshots.
*
* @author peter
*/
@SuppressWarnings({"MismatchedQueryAndUpdateOfCollection"})
public class ReflectionCache {
private static final ConcurrentFactoryMap<Class,Class> ourSuperClasses = new ConcurrentFactoryMap<Class, Class>() {
@Override
protected Class create(final Class key) {
return key.getSuperclass();
}
};
private static final ConcurrentFactoryMap<Class,Class[]> ourInterfaces = new ConcurrentFactoryMap<Class, Class[]>() {
@Override
@NotNull
protected Class[] create(final Class key) {
Class[] classes = key.getInterfaces();
return classes.length == 0 ? ArrayUtil.EMPTY_CLASS_ARRAY : classes;
}
};
private static final ConcurrentFactoryMap<Class,Boolean> ourIsInterfaces = new ConcurrentFactoryMap<Class, Boolean>() {
@Override
@NotNull
protected Boolean create(final Class key) {
return key.isInterface();
}
};
private static final ConcurrentFactoryMap<Class, TypeVariable[]> ourTypeParameters = new ConcurrentFactoryMap<Class, TypeVariable[]>() {
@Override
@NotNull
protected TypeVariable[] create(final Class key) {
return key.getTypeParameters();
}
};
private static final ConcurrentFactoryMap<Class, Type[]> ourGenericInterfaces = new ConcurrentFactoryMap<Class, Type[]>() {
@Override
@NotNull
protected Type[] create(final Class key) {
return key.getGenericInterfaces();
}
};
private static final ConcurrentFactoryMap<ParameterizedType, Type[]> ourActualTypeArguments = new ConcurrentFactoryMap<ParameterizedType, Type[]>() {
@Override
@NotNull
protected Type[] create(final ParameterizedType key) {
return key.getActualTypeArguments();
}
};
private ReflectionCache() {
}
public static Class getSuperClass(@NotNull Class aClass) {
return ourSuperClasses.get(aClass);
return aClass.getSuperclass();
}
@NotNull
public static Class[] getInterfaces(@NotNull Class aClass) {
return ourInterfaces.get(aClass);
return aClass.getInterfaces();
}
@NotNull
@@ -95,22 +55,22 @@ public class ReflectionCache {
}
public static boolean isInterface(@NotNull Class aClass) {
return ourIsInterfaces.get(aClass);
return aClass.isInterface();
}
@NotNull
public static <T> TypeVariable<Class<T>>[] getTypeParameters(@NotNull Class<T> aClass) {
return ourTypeParameters.get(aClass);
return aClass.getTypeParameters();
}
@NotNull
public static Type[] getGenericInterfaces(@NotNull Class aClass) {
return ourGenericInterfaces.get(aClass);
return aClass.getGenericInterfaces();
}
@NotNull
public static Type[] getActualTypeArguments(@NotNull ParameterizedType type) {
return ourActualTypeArguments.get(type);
return type.getActualTypeArguments();
}
}
@@ -441,7 +441,7 @@ public abstract class DomInvocationHandler<T extends AbstractDomChildDescription
private Converter createConverter(final JavaMethod method) {
final Type returnType = method.getGenericReturnType();
final Type type = returnType == void.class ? method.getGenericParameterTypes()[0] : returnType;
final Class parameter = ReflectionUtil.substituteGenericType(type, myType);
final Class parameter = DomUtil.substituteGenericType(type, myType);
if (parameter == null) {
LOG.error(type + " " + myType);
}
@@ -55,13 +55,16 @@ public class DomUtil {
private static final ConcurrentFactoryMap<Type, Class> ourTypeParameters = new ConcurrentFactoryMap<Type, Class>() {
@NotNull
protected Class create(final Type key) {
final Class<?> result = ReflectionUtil.substituteGenericType(GENERIC_VALUE_TYPE_VARIABLE, key);
final Class<?> result = substituteGenericType(GENERIC_VALUE_TYPE_VARIABLE, key);
return result == null ? DUMMY : result;
}
};
private DomUtil() {
}
private static final ConcurrentFactoryMap<Pair<Type, Type>, Class> ourVariableSubstitutions = new ConcurrentFactoryMap<Pair<Type, Type>, Class>() {
@Nullable
protected Class create(final Pair<Type, Type> key) {
return ReflectionUtil.substituteGenericType(key.first, key.second);
}
};
public static Class extractParameterClassFromGenericType(Type type) {
return getGenericValueParameter(type);
@@ -156,6 +159,10 @@ public class DomUtil {
return description != null ? description.getGetterMethod(description.getValues(parent).indexOf(element)) : null;
}
public static Class<?> substituteGenericType(Type genericType, Type classType) {
return ourVariableSubstitutions.get(Pair.create(genericType, classType));
}
@Nullable
public static Class getGenericValueParameter(Type type) {
final Class aClass = ourTypeParameters.get(type);