Skip to content

Commit

Permalink
add a test case on shadow class: Object
Browse files Browse the repository at this point in the history
  • Loading branch information
danglotb committed Oct 17, 2016
1 parent 38cb0c4 commit 2a06d46
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 71 deletions.
2 changes: 0 additions & 2 deletions src/main/java/spoon/reflect/factory/Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,4 @@ public interface Factory {
EvalFactory Eval(); // used 4 times

ConstructorFactory Constructor(); // used 3 times

ShadowFactory Shadow();
}
10 changes: 0 additions & 10 deletions src/main/java/spoon/reflect/factory/FactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,16 +255,6 @@ public TypeFactory Type() {
return type;
}

private transient ShadowFactory shadow;

@Override
public ShadowFactory Shadow() {
if (shadow == null) {
shadow = new ShadowFactory(this);
}
return shadow;
}

/**
* A constructor that takes the parent factory
*/
Expand Down
57 changes: 0 additions & 57 deletions src/main/java/spoon/reflect/factory/ShadowFactory.java

This file was deleted.

17 changes: 15 additions & 2 deletions src/main/java/spoon/reflect/factory/TypeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,21 @@
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.DefaultCoreFactory;
import spoon.support.StandardEnvironment;
import spoon.support.visitor.java.JavaReflectionTreeBuilder;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
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;

/**
* The {@link CtType} sub-factory.
*/
Expand Down Expand Up @@ -73,6 +78,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 @@ -462,12 +469,18 @@ 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 factory.Shadow().get(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);
this.shadowCache.put(cl, newShadowClass);
return newShadowClass;
} else {
return shadowClass;
}
}
return aType;
}


/**
* Gets the declaring type name for a given Java qualified name.
*/
Expand Down
28 changes: 28 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,29 @@ public boolean matches(CtConstructorCall element) {
}).get(0);
assertEquals(launcher.getFactory().Type().objectType(), ctConstructorCall.getExecutable().getParameters().get(9));
}

@Test
public void testShadowType() throws Exception {

/* build a minimalist model to test shadow elements */

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);

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 2a06d46

Please sign in to comment.