Skip to content

Commit

Permalink
fix(performance): keep shadow classes (closes #867)
Browse files Browse the repository at this point in the history
  • Loading branch information
danglotb authored and monperrus committed Oct 31, 2016
1 parent 1bb7513 commit d8a536d
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/main/java/spoon/reflect/factory/TypeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
import spoon.reflect.code.CtNewClass;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtTypeParameter;
import spoon.reflect.reference.CtArrayTypeReference;
import spoon.reflect.reference.CtIntersectionTypeReference;
import spoon.reflect.reference.CtTypeParameterReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtScanner;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.DefaultCoreFactory;
import spoon.support.StandardEnvironment;
Expand All @@ -36,8 +38,10 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static spoon.testing.utils.ModelUtils.createFactory;
Expand Down Expand Up @@ -76,6 +80,8 @@ public class TypeFactory extends SubFactory {
public final CtTypeReference<Date> DATE = createReference(Date.class);
public final CtTypeReference<Object> OBJECT = createReference(Object.class);

private final Map<Class<?>, CtType<?>> shadowCache = new HashMap<>();

/**
* Returns a reference on the null type (type of null).
*/
Expand Down Expand Up @@ -465,7 +471,23 @@ private void addNestedType(List<CtType<?>> list, CtType<?> t) {
public <T> CtType<T> get(Class<?> cl) {
final CtType<T> aType = get(cl.getName());
if (aType == null) {
return new JavaReflectionTreeBuilder(createFactory()).scan((Class<T>) cl);
final CtType<T> shadowClass = (CtType<T>) this.shadowCache.get(cl);
if (shadowClass == null) {
final CtType<T> newShadowClass = new JavaReflectionTreeBuilder(createFactory()).scan((Class<T>) cl);
newShadowClass.setFactory(factory);
newShadowClass.accept(new CtScanner() {
@Override
public void scan(CtElement element) {
if (element != null) {
element.setFactory(factory);
}
}
});
this.shadowCache.put(cl, newShadowClass);
return newShadowClass;
} else {
return shadowClass;
}
}
return aType;
}
Expand Down
60 changes: 60 additions & 0 deletions src/test/java/spoon/test/type/TypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@
import spoon.test.type.testclasses.Pozole;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static junit.framework.TestCase.assertFalse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static spoon.testing.utils.ModelUtils.buildClass;
Expand Down Expand Up @@ -294,4 +297,61 @@ public boolean matches(CtConstructorCall element) {
}).get(0);
assertEquals(launcher.getFactory().Type().objectType(), ctConstructorCall.getExecutable().getParameters().get(9));
}

@Test
public void testShadowType() throws Exception {

/* Objects and factory have to be the sames */

Launcher launcher = new Launcher();
launcher.buildModel();

final CtClass<Object> objectCtClass = launcher.getFactory().Class().get(Object.class);
final CtClass<Object> objectCtClass1 = launcher.getFactory().Class().get(Object.class);

assertSame(objectCtClass, objectCtClass1);

assertSame(launcher.getFactory().Class(), objectCtClass.getFactory().Class());
assertSame(launcher.getFactory(), objectCtClass.getFactory());

assertSame(launcher.getFactory().Class(), objectCtClass1.getFactory().Class());
assertSame(launcher.getFactory(), objectCtClass1.getFactory());

assertSame(objectCtClass.getFactory().Class().get(objectCtClass.getActualClass()), objectCtClass);
assertSame(objectCtClass.getFactory().Class().get(Object.class), objectCtClass);

assertSame(objectCtClass1.getFactory().Class().get(objectCtClass1.getActualClass()), objectCtClass1);
assertSame(objectCtClass1.getFactory().Class().get(Object.class), objectCtClass1);

assertTrue(objectCtClass.isShadow());
assertEquals("java.lang.Object", objectCtClass.getQualifiedName());

final CtType<Object> objectCtType = launcher.getFactory().Type().get(Object.class);
final CtType<Object> objectCtType1 = launcher.getFactory().Type().get(Object.class);

assertSame(objectCtType, objectCtType1);

assertSame(launcher.getFactory().Type(), objectCtType.getFactory().Type());
assertSame(launcher.getFactory(), objectCtType.getFactory());

assertSame(launcher.getFactory().Type(), objectCtType1.getFactory().Type());
assertSame(launcher.getFactory(), objectCtType1.getFactory());

assertSame(objectCtType.getFactory().Type().get(objectCtType.getActualClass()), objectCtType);
assertSame(objectCtType.getFactory().Type().get(Object.class), objectCtType);

assertSame(objectCtType1.getFactory().Type().get(objectCtType1.getActualClass()), objectCtType1);
assertSame(objectCtType1.getFactory().Type().get(Object.class), objectCtType1);

assertTrue(objectCtClass.isShadow());
assertEquals("java.lang.Object", objectCtClass.getQualifiedName());

final List<String> methodNameList = Arrays.asList(Object.class.getDeclaredMethods()).stream().map(Method::getName).collect(Collectors.toList());

for (CtMethod<?> ctMethod : objectCtClass.getMethods()) {
assertTrue(methodNameList.contains(ctMethod.getSimpleName()));
assertTrue(ctMethod.getBody().getStatements().isEmpty());
}

}
}

0 comments on commit d8a536d

Please sign in to comment.