Search in sources :

Example 76 with CtClass

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);
    }
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtVariableReference(spoon.reflect.reference.CtVariableReference) CtVariable(spoon.reflect.declaration.CtVariable) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 77 with CtClass

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);
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtVariableReference(spoon.reflect.reference.CtVariableReference) CtField(spoon.reflect.declaration.CtField) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 78 with CtClass

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);
    }
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) CtClassProcessor(spoon.test.processing.testclasses.CtClassProcessor) Test(org.junit.Test)

Example 79 with 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());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) AClass(spoon.test.prettyprinter.testclasses.AClass) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) File(java.io.File) Test(org.junit.Test)

Example 80 with CtClass

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);
}
Also used : Serializable(java.io.Serializable) CtLambda(spoon.reflect.code.CtLambda) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) CtIntersectionTypeReference(spoon.reflect.reference.CtIntersectionTypeReference) CtClass(spoon.reflect.declaration.CtClass) Pozole(spoon.test.type.testclasses.Pozole) Launcher(spoon.Launcher) Test(org.junit.Test)

Aggregations

CtClass (spoon.reflect.declaration.CtClass)168 Test (org.junit.Test)151 Launcher (spoon.Launcher)102 Factory (spoon.reflect.factory.Factory)84 CtMethod (spoon.reflect.declaration.CtMethod)42 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)22 CtAnnotation (spoon.reflect.declaration.CtAnnotation)19 SpoonModelBuilder (spoon.SpoonModelBuilder)17 CtInvocation (spoon.reflect.code.CtInvocation)16 File (java.io.File)15 CtTypeReference (spoon.reflect.reference.CtTypeReference)15 OuterAnnotation (spoon.test.annotation.testclasses.Foo.OuterAnnotation)15 Annotation (java.lang.annotation.Annotation)14 AbstractFilter (spoon.reflect.visitor.filter.AbstractFilter)14 AnnotationDefaultAnnotation (spoon.test.annotation.testclasses.AnnotationDefaultAnnotation)14 InnerAnnotation (spoon.test.annotation.testclasses.Foo.InnerAnnotation)14 MiddleAnnotation (spoon.test.annotation.testclasses.Foo.MiddleAnnotation)14 GlobalAnnotation (spoon.test.annotation.testclasses.GlobalAnnotation)14 SuperAnnotation (spoon.test.annotation.testclasses.SuperAnnotation)14 TypeAnnotation (spoon.test.annotation.testclasses.TypeAnnotation)14