use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class CloneReferenceTest method testGetDeclarationAfterClone.
@Test
public void testGetDeclarationAfterClone() throws Exception {
// contract: all variable references of the clone (but fields) should point to the variable of the clone
Launcher spoon = new Launcher();
List<String> names = Arrays.asList("f1", "f2", "a", "b", "x", "param", "e");
spoon.addInputResource("./src/test/resources/noclasspath/A2.java");
spoon.getEnvironment().setComplianceLevel(8);
spoon.getEnvironment().setNoClasspath(true);
spoon.buildModel();
final CtClass<Object> a = spoon.getFactory().Class().get("A2");
// test before clone
for (String name : names) {
CtVariable var1 = findVariable(a, name);
CtVariable var2 = findReference(a, name).getDeclaration();
assertTrue(var1 == var2);
}
CtClass b = a.clone();
// test after clone
for (String name : names) {
CtVariable var1 = findVariable(b, name);
CtVariableReference refVar1 = findReference(b, name);
CtVariable var2 = refVar1.getDeclaration();
assertTrue("Var1 and var2 are not the same element", var1 == var2);
}
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class CloneReferenceTest method testGetDeclarationOfFieldAfterClone.
@Test
public void testGetDeclarationOfFieldAfterClone() throws Exception {
// contract: all field references of the clone point to the old class
// behaviour changed on https://github.com/INRIA/spoon/pull/1215
Launcher spoon = new Launcher();
String name = "field";
spoon.addInputResource("./src/test/resources/noclasspath/A2.java");
spoon.getEnvironment().setComplianceLevel(8);
spoon.getEnvironment().setNoClasspath(true);
spoon.buildModel();
final CtClass<Object> a = spoon.getFactory().Class().get("A2");
// test before clone
CtField oldVar1 = (CtField) findVariable(a, name);
CtField oldVar2 = (CtField) findReference(a, name).getDeclaration();
assertTrue(oldVar1 == oldVar2);
CtClass b = a.clone();
// test after clone
CtField var1 = (CtField) findVariable(b, name);
CtVariableReference refVar1 = findReference(b, name);
CtField var2 = (CtField) refVar1.getDeclaration();
assertTrue(var1 != var2);
assertTrue(var2 == oldVar1);
assertTrue(var1.getParent(CtClass.class) == b);
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class ProcessingTest method testProcessorWithGenericType.
@Test
public void testProcessorWithGenericType() {
// contract: we can use generic type for another abstract processor
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/imports/testclasses");
CtClassProcessor classProcessor = new CtClassProcessor();
spoon.addProcessor(classProcessor);
spoon.run();
assertFalse(classProcessor.elements.isEmpty());
for (CtType type : classProcessor.elements) {
assertTrue("Type " + type.getSimpleName() + " is not a class", type instanceof CtClass);
}
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class DefaultPrettyPrinterTest method testPrintAMethodWithImports.
@Test
public void testPrintAMethodWithImports() throws Exception {
final Launcher launcher = new Launcher();
final Factory factory = launcher.getFactory();
factory.getEnvironment().setAutoImports(true);
final SpoonModelBuilder compiler = launcher.createCompiler();
compiler.addInputSource(new File("./src/test/java/spoon/test/prettyprinter/testclasses/"));
compiler.build();
final String expected = "public List<?> aMethod() {" + nl + " return new ArrayList<>();" + nl + "}";
final CtClass<?> aClass = (CtClass<?>) factory.Type().get(AClass.class);
assertEquals(expected, aClass.getMethodsByName("aMethod").get(0).toString());
final CtConstructorCall<?> constructorCall = aClass.getElements(new TypeFilter<CtConstructorCall<?>>(CtConstructorCall.class)).get(0);
final CtTypeReference<?> ctTypeReference = constructorCall.getType().getActualTypeArguments().get(0);
assertTrue(ctTypeReference.isImplicit());
assertEquals("Object", ctTypeReference.getSimpleName());
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class TypeTest method testIntersectionTypeReferenceInGenericsAndCasts.
@Test
public void testIntersectionTypeReferenceInGenericsAndCasts() throws Exception {
final String target = "./target/type";
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/type/testclasses");
launcher.setSourceOutputDirectory(target);
launcher.getEnvironment().setNoClasspath(true);
launcher.run();
final CtClass<Pozole> aPozole = launcher.getFactory().Class().get(Pozole.class);
final CtMethod<?> prepare = aPozole.getMethodsByName("prepare").get(0);
// Intersection type in generic types.
final List<CtClass> localTypes = prepare.getElements(new TypeFilter<>(CtClass.class));
assertEquals(1, localTypes.size());
// New type parameter declaration.
final CtTypeParameter typeParameter = localTypes.get(0).getFormalCtTypeParameters().get(0);
assertNotNull(typeParameter);
assertEquals("T", typeParameter.getSimpleName());
assertIntersectionTypeForPozolePrepareMethod(aPozole, typeParameter.getSuperclass());
// Intersection type in casts.
final List<CtLambda<?>> lambdas = prepare.getElements(new TypeFilter<CtLambda<?>>(CtLambda.class));
assertEquals(1, lambdas.size());
assertEquals(1, lambdas.get(0).getTypeCasts().size());
assertTrue(lambdas.get(0).getTypeCasts().get(0) instanceof CtIntersectionTypeReference);
final CtIntersectionTypeReference<?> intersectionType = lambdas.get(0).getTypeCasts().get(0).asCtIntersectionTypeReference();
assertEquals("java.lang.Runnable & java.io.Serializable", intersectionType.toString());
assertEquals(aPozole.getFactory().Type().createReference(Runnable.class), intersectionType.getBounds().stream().collect(Collectors.toList()).get(0));
assertEquals(aPozole.getFactory().Type().createReference(Serializable.class), intersectionType.getBounds().stream().collect(Collectors.toList()).get(1));
canBeBuilt(target, 8, true);
}
Aggregations