Skip to content

Commit

Permalink
Use cache of ReflectionProvider to accelerate lookup for field hiding
Browse files Browse the repository at this point in the history
inheritance of an implicit declared collection, array or map. Closes #60
and #61.
  • Loading branch information
joehni committed Nov 2, 2016
1 parent 8810f2c commit ef3aa68
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion xstream/src/java/com/thoughtworks/xstream/XStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ private Mapper buildMapper() {
mapper = new FieldAliasingMapper(mapper);
mapper = new AttributeAliasingMapper(mapper);
mapper = new SystemAttributeAliasingMapper(mapper);
mapper = new ImplicitCollectionMapper(mapper);
mapper = new ImplicitCollectionMapper(mapper, reflectionProvider);
mapper = new OuterClassMapper(mapper);
mapper = new ArrayMapper(mapper);
mapper = new DefaultImplementationsMapper(mapper);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2005 Joe Walnes.
* Copyright (C) 2006, 2007, 2009, 2011, 2012, 2013, 2014, 2015 XStream Committers.
* Copyright (C) 2006, 2007, 2009, 2011, 2012, 2013, 2014, 2015, 2016 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
Expand All @@ -18,37 +18,32 @@
import java.util.Map;

import com.thoughtworks.xstream.InitializationException;
import com.thoughtworks.xstream.converters.reflection.ReflectionProvider;
import com.thoughtworks.xstream.core.util.Primitives;


public class ImplicitCollectionMapper extends MapperWrapper {

public ImplicitCollectionMapper(final Mapper wrapped) {
private final ReflectionProvider reflectionProvider;

public ImplicitCollectionMapper(final Mapper wrapped, final ReflectionProvider reflectionProvider) {
super(wrapped);
this.reflectionProvider = reflectionProvider;
}

private final Map<Class<?>, ImplicitCollectionMapperForClass> classNameToMapper = new HashMap<>();

private ImplicitCollectionMapperForClass getMapper(final Class<?> declaredFor, final String fieldName) {
Class<?> definedIn = declaredFor;
final Field field = fieldName != null ? reflectionProvider.getFieldOrNull(definedIn, fieldName) : null;
final Class<?> inheritanceStop = field != null ? field.getDeclaringClass() : null;
while (definedIn != null) {
final ImplicitCollectionMapperForClass mapper = classNameToMapper.get(definedIn);
if (mapper != null) {
return mapper;
} else {
if (fieldName != null) {
try {
// do not continue search for a hidden field
final Field field = definedIn.getDeclaredField(fieldName);
if (field != null && !Modifier.isStatic(field.getModifiers())) {
return null;
}
} catch (final SecurityException e) {
throw new InitializationException("Access denied for field with implicit collection", e);
} catch (final NoSuchFieldException e) {
// OK, we can continue the search in the class hierarchy
}
}
}
if (definedIn == inheritanceStop) {
break;
}
definedIn = definedIn.getSuperclass();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2005 Joe Walnes.
* Copyright (C) 2006, 2007, 2011, 2013 XStream Committers.
* Copyright (C) 2006, 2007, 2011, 2013, 2016 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
Expand All @@ -20,13 +20,14 @@
import com.thoughtworks.acceptance.objects.SampleMaps;
import com.thoughtworks.acceptance.objects.Software;
import com.thoughtworks.xstream.core.ClassLoaderReference;
import com.thoughtworks.xstream.core.JVM;

import junit.framework.TestCase;

public class ImplicitCollectionMapperTest extends TestCase {

private ImplicitCollectionMapper implicitCollections = new ImplicitCollectionMapper(
new DefaultMapper(new ClassLoaderReference(null)));
new DefaultMapper(new ClassLoaderReference(null)), JVM.newReflectionProvider());

public void testAllowsFieldsToBeMarkedAsImplicitCollectionsToBeAdded() {
implicitCollections.add(SampleLists.class, "good", null);
Expand Down

0 comments on commit ef3aa68

Please sign in to comment.