diff --git a/platform/util/src/com/intellij/util/ReflectionCache.java b/platform/util/src/com/intellij/util/ReflectionCache.java index 9fc96728bf7e..1a948882e4b3 100644 --- a/platform/util/src/com/intellij/util/ReflectionCache.java +++ b/platform/util/src/com/intellij/util/ReflectionCache.java @@ -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 ourSuperClasses = new ConcurrentFactoryMap() { - @Override - protected Class create(final Class key) { - return key.getSuperclass(); - } - }; - private static final ConcurrentFactoryMap ourInterfaces = new ConcurrentFactoryMap() { - @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 ourIsInterfaces = new ConcurrentFactoryMap() { - @Override - @NotNull - protected Boolean create(final Class key) { - return key.isInterface(); - } - }; - private static final ConcurrentFactoryMap ourTypeParameters = new ConcurrentFactoryMap() { - @Override - @NotNull - protected TypeVariable[] create(final Class key) { - return key.getTypeParameters(); - } - }; - private static final ConcurrentFactoryMap ourGenericInterfaces = new ConcurrentFactoryMap() { - @Override - @NotNull - protected Type[] create(final Class key) { - return key.getGenericInterfaces(); - } - }; - private static final ConcurrentFactoryMap ourActualTypeArguments = new ConcurrentFactoryMap() { - @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 TypeVariable>[] getTypeParameters(@NotNull Class 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(); } } diff --git a/xml/dom-impl/src/com/intellij/util/xml/impl/DomInvocationHandler.java b/xml/dom-impl/src/com/intellij/util/xml/impl/DomInvocationHandler.java index d1d85ccecbdf..98666dfc780f 100644 --- a/xml/dom-impl/src/com/intellij/util/xml/impl/DomInvocationHandler.java +++ b/xml/dom-impl/src/com/intellij/util/xml/impl/DomInvocationHandler.java @@ -441,7 +441,7 @@ public abstract class DomInvocationHandler ourTypeParameters = new ConcurrentFactoryMap() { @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, Class> ourVariableSubstitutions = new ConcurrentFactoryMap, Class>() { + @Nullable + protected Class create(final Pair 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);