Search in sources :

Example 96 with CtType

use of spoon.reflect.declaration.CtType in project spoon by INRIA.

the class AnnotationTest method testRepeatableAnnotationAreManaged.

@Test
public void testRepeatableAnnotationAreManaged() {
    // contract: when two identical repeatable annotation are used, they should be displayed in two different annotations and not factorized
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/annotation/testclasses/repeatable");
    spoon.buildModel();
    CtType type = spoon.getFactory().Type().get(Repeated.class);
    CtMethod firstMethod = (CtMethod) type.getMethodsByName("method").get(0);
    List<CtAnnotation<?>> annotations = firstMethod.getAnnotations();
    assertEquals(2, annotations.size());
    for (CtAnnotation a : annotations) {
        assertEquals("Tag", a.getAnnotationType().getSimpleName());
    }
    String classContent = type.toString();
    assertTrue("Content of the file: " + classContent, classContent.contains("@spoon.test.annotation.testclasses.repeatable.Tag(\"machin\")"));
    assertTrue("Content of the file: " + classContent, classContent.contains("@spoon.test.annotation.testclasses.repeatable.Tag(\"truc\")"));
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 97 with CtType

use of spoon.reflect.declaration.CtType in project spoon by INRIA.

the class AnnotationTest method testSpoonManageRecursivelyDefinedAnnotation.

@Test
public void testSpoonManageRecursivelyDefinedAnnotation() {
    // contract: Spoon manage to process recursively defined annotation in shadow classes
    Launcher spoon = new Launcher();
    CtType type = spoon.getFactory().Type().get(AliasFor.class);
    assertEquals(3, type.getFields().size());
}
Also used : CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 98 with CtType

use of spoon.reflect.declaration.CtType in project spoon by INRIA.

the class AnnotationTest method testRepeatableAnnotationAreManagedWithArrays.

@Test
public void testRepeatableAnnotationAreManagedWithArrays() {
    // contract: when two identical repeatable annotation with arrays are used, they should be displayed in two different annotations and not factorized
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/annotation/testclasses/repeatandarrays");
    spoon.buildModel();
    CtType type = spoon.getFactory().Type().get(RepeatedArrays.class);
    CtMethod firstMethod = (CtMethod) type.getMethodsByName("method").get(0);
    List<CtAnnotation<?>> annotations = firstMethod.getAnnotations();
    assertEquals(2, annotations.size());
    for (CtAnnotation a : annotations) {
        assertEquals("TagArrays", a.getAnnotationType().getSimpleName());
    }
    String classContent = type.toString();
    assertTrue("Content of the file: " + classContent, classContent.contains("@spoon.test.annotation.testclasses.repeatandarrays.TagArrays({ \"machin\", \"truc\" })"));
    assertTrue("Content of the file: " + classContent, classContent.contains("@spoon.test.annotation.testclasses.repeatandarrays.TagArrays({ \"truc\", \"bidule\" })"));
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 99 with CtType

use of spoon.reflect.declaration.CtType in project spoon by INRIA.

the class AnnotationTest method testCreateRepeatableAnnotationWithArrays.

@Test
public void testCreateRepeatableAnnotationWithArrays() {
    // contract: when using annotate with a repeatable annotation, it will create a new annotation, even if an annotation with an array already exists
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/annotation/testclasses/repeatandarrays");
    spoon.buildModel();
    CtType type = spoon.getFactory().Type().get(Repeated.class);
    CtMethod firstMethod = (CtMethod) type.getMethodsByName("withoutAnnotation").get(0);
    List<CtAnnotation<?>> annotations = firstMethod.getAnnotations();
    assertTrue(annotations.isEmpty());
    spoon.getFactory().Annotation().annotate(firstMethod, TagArrays.class, "value", "foo");
    assertEquals(1, firstMethod.getAnnotations().size());
    spoon.getFactory().Annotation().annotate(firstMethod, TagArrays.class, "value", "bar");
    annotations = firstMethod.getAnnotations();
    assertEquals(2, annotations.size());
    for (CtAnnotation a : annotations) {
        assertEquals("TagArrays", a.getAnnotationType().getSimpleName());
    }
    String classContent = type.toString();
    assertTrue("Content of the file: " + classContent, classContent.contains("@spoon.test.annotation.testclasses.repeatandarrays.TagArrays(\"foo\")"));
    assertTrue("Content of the file: " + classContent, classContent.contains("@spoon.test.annotation.testclasses.repeatandarrays.TagArrays(\"bar\")"));
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 100 with CtType

use of spoon.reflect.declaration.CtType in project spoon by INRIA.

the class TestCompilationUnit method testCompilationUnitDeclaredTypes.

@Test
public void testCompilationUnitDeclaredTypes() throws IOException {
    // contract: the list of declared types should be unmodifiable
    File resource = new File("./src/test/java/spoon/test/model/Foo.java");
    final Launcher launcher = new Launcher();
    launcher.addInputResource(resource.getPath());
    launcher.buildModel();
    CompilationUnit cu = launcher.getFactory().CompilationUnit().getOrCreate(resource.getCanonicalPath());
    assertEquals(3, cu.getDeclaredTypes().size());
    List<CtType<?>> typeList = cu.getDeclaredTypes();
    try {
        typeList.remove(0);
        fail();
    } catch (UnsupportedOperationException e) {
    // do nothing
    }
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) File(java.io.File) Test(org.junit.Test)

Aggregations

CtType (spoon.reflect.declaration.CtType)134 Test (org.junit.Test)67 Launcher (spoon.Launcher)60 ArrayList (java.util.ArrayList)42 CtMethod (spoon.reflect.declaration.CtMethod)38 CtTypeReference (spoon.reflect.reference.CtTypeReference)30 DefaultJavaPrettyPrinter (spoon.reflect.visitor.DefaultJavaPrettyPrinter)20 File (java.io.File)19 Factory (spoon.reflect.factory.Factory)19 PrettyPrinter (spoon.reflect.visitor.PrettyPrinter)19 List (java.util.List)18 Collectors (java.util.stream.Collectors)17 CtField (spoon.reflect.declaration.CtField)17 CtElement (spoon.reflect.declaration.CtElement)16 CtPackage (spoon.reflect.declaration.CtPackage)16 InputConfiguration (fr.inria.diversify.utils.sosiefier.InputConfiguration)14 IOException (java.io.IOException)12 SpoonException (spoon.SpoonException)12 DSpotCompiler (fr.inria.diversify.utils.compilation.DSpotCompiler)11 Set (java.util.Set)11