cache the generics & accessors that XmlSerializer uses

This commit is contained in:
peter
2011-07-09 23:43:53 +02:00
parent 9cf4a1b33f
commit b4c8faf1fd
2 changed files with 16 additions and 2 deletions
@@ -22,6 +22,7 @@ import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.JDOMUtil;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.reference.SoftReference;
import com.intellij.util.ArrayUtil;
import com.intellij.util.containers.MultiMap;
import com.intellij.util.xmlb.annotations.*;
@@ -35,10 +36,13 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
class BeanBinding implements Binding {
private static final Logger LOG = Logger.getInstance("#com.intellij.util.xmlb.BeanBinding");
private static final ConcurrentHashMap<Class, SoftReference<List<Accessor>>> ourAccessorCache = new ConcurrentHashMap<Class, SoftReference<List<Accessor>>>();
private final String myTagName;
private final Map<Binding, Accessor> myPropertyBindings = new HashMap<Binding, Accessor>();
private final List<Binding> myPropertyBindingsList = new ArrayList<Binding>();
@@ -196,7 +200,13 @@ class BeanBinding implements Binding {
@NotNull
static List<Accessor> getAccessors(Class<?> aClass) {
final List<Accessor> accessors = Lists.newArrayList();
final SoftReference<List<Accessor>> reference = ourAccessorCache.get(aClass);
List<Accessor> accessors = reference == null ? null : reference.get();
if (accessors != null) {
return accessors;
}
accessors = Lists.newArrayList();
final Map<String, Pair<Method, Method>> candidates = Maps.newTreeMap(); // (name,(getter,setter))
for (Method method : aClass.getMethods()) {
@@ -230,6 +240,8 @@ class BeanBinding implements Binding {
}
}
ourAccessorCache.put(aClass, new SoftReference<List<Accessor>>(accessors));
return accessors;
}
@@ -32,6 +32,7 @@ class PropertyAccessor implements Accessor {
private final Class<?> myType;
private final Method myReadMethod;
private final Method myWriteMethod;
private final Type myGenericType;
public PropertyAccessor(PropertyDescriptor descriptor) {
this(descriptor.getName(), descriptor.getPropertyType(), descriptor.getReadMethod(), descriptor.getWriteMethod());
@@ -42,6 +43,7 @@ class PropertyAccessor implements Accessor {
myType = type;
myReadMethod = readMethod;
myWriteMethod = writeMethod;
myGenericType = myReadMethod.getGenericReturnType();
}
public Object read(Object o) {
@@ -91,7 +93,7 @@ class PropertyAccessor implements Accessor {
}
public Type getGenericType() {
return myReadMethod.getGenericReturnType();
return myGenericType;
}
@NonNls