use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.
the class AnnotationTest method testUsageOfTypeAnnotationInExtendsImplementsOfAClass.
@Test
public void testUsageOfTypeAnnotationInExtendsImplementsOfAClass() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/AnnotationsAppliedOnAnyTypeInAClass.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
final CtClass<?> ctClass = (CtClass<?>) factory.Type().get("spoon.test.annotation.testclasses.AnnotationsAppliedOnAnyTypeInAClass");
final CtClass<?> innerClass = ctClass.getElements(new NamedElementFilter<>(CtClass.class, "DummyClass")).get(0);
final CtTypeReference<?> extendsActual = innerClass.getSuperclass();
final List<CtAnnotation<? extends Annotation>> extendsTypeAnnotations = extendsActual.getAnnotations();
final String superClassExpected = "spoon.test.annotation.testclasses.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "AnnotArrayInnerClass";
assertEquals("Extends with a type annotation must have it in its model", 1, extendsTypeAnnotations.size());
assertEquals("Type annotation on a extends must be typed by TypeAnnotation", TypeAnnotation.class, extendsTypeAnnotations.get(0).getAnnotationType().getActualClass());
assertEquals(CtAnnotatedElementType.TYPE_USE, extendsTypeAnnotations.get(0).getAnnotatedElementType());
assertEquals("Extends with an type annotation must be well printed", superClassExpected, extendsActual.toString());
final Set<CtTypeReference<?>> superInterfaces = innerClass.getSuperInterfaces();
final CtTypeReference<?> firstSuperInterface = superInterfaces.toArray(new CtTypeReference<?>[0])[0];
final List<CtAnnotation<? extends Annotation>> implementsTypeAnnotations = firstSuperInterface.getAnnotations();
final String superInterfaceExpected = "spoon.test.annotation.testclasses.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "BasicAnnotation";
assertEquals("Implements with a type annotation must have it in its model", 1, implementsTypeAnnotations.size());
assertEquals("Type annotation on a extends must be typed by TypeAnnotation", TypeAnnotation.class, implementsTypeAnnotations.get(0).getAnnotationType().getActualClass());
assertEquals(CtAnnotatedElementType.TYPE_USE, implementsTypeAnnotations.get(0).getAnnotatedElementType());
assertEquals("Extends with an type annotation must be well printed", superInterfaceExpected, firstSuperInterface.toString());
final CtEnum<?> enumActual = ctClass.getElements(new NamedElementFilter<>(CtEnum.class, "DummyEnum")).get(0);
final Set<CtTypeReference<?>> superInterfacesOfEnum = enumActual.getSuperInterfaces();
final CtTypeReference<?> firstSuperInterfaceOfEnum = superInterfacesOfEnum.toArray(new CtTypeReference<?>[0])[0];
final List<CtAnnotation<? extends Annotation>> enumTypeAnnotations = firstSuperInterfaceOfEnum.getAnnotations();
final String enumExpected = "public enum DummyEnum implements spoon.test.annotation.testclasses.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "BasicAnnotation {" + System.lineSeparator() + " ;" + System.lineSeparator() + "}";
assertEquals("Implements in a enum with a type annotation must have it in its model", 1, enumTypeAnnotations.size());
assertEquals("Type annotation on a implements in a enum must be typed by TypeAnnotation", TypeAnnotation.class, enumTypeAnnotations.get(0).getAnnotationType().getActualClass());
assertEquals(CtAnnotatedElementType.TYPE_USE, enumTypeAnnotations.get(0).getAnnotatedElementType());
assertEquals("Implements in a enum with an type annotation must be well printed", enumExpected, enumActual.toString());
final CtInterface<?> interfaceActual = ctClass.getElements(new NamedElementFilter<>(CtInterface.class, "DummyInterface")).get(0);
final Set<CtTypeReference<?>> superInterfacesOfInterface = interfaceActual.getSuperInterfaces();
final CtTypeReference<?> firstSuperInterfaceOfInterface = superInterfacesOfInterface.toArray(new CtTypeReference<?>[0])[0];
final List<CtAnnotation<? extends Annotation>> interfaceTypeAnnotations = firstSuperInterfaceOfInterface.getAnnotations();
final String interfaceExpected = "public interface DummyInterface extends spoon.test.annotation.testclasses.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "BasicAnnotation {}";
assertEquals("Implements in a interface with a type annotation must have it in its model", 1, interfaceTypeAnnotations.size());
assertEquals("Type annotation on a implements in a enum must be typed by TypeAnnotation", TypeAnnotation.class, interfaceTypeAnnotations.get(0).getAnnotationType().getActualClass());
assertEquals(CtAnnotatedElementType.TYPE_USE, interfaceTypeAnnotations.get(0).getAnnotatedElementType());
assertEquals("Implements in a interface with an type annotation must be well printed", interfaceExpected, interfaceActual.toString());
}
use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.
the class AnnotationTest method testAnnotationParameterTypes.
@Test
public void testAnnotationParameterTypes() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/Main.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
CtType<?> type = factory.Type().get("spoon.test.annotation.testclasses.Main");
CtMethod<?> m1 = type.getElements(new NamedElementFilter<>(CtMethod.class, "m1")).get(0);
List<CtAnnotation<? extends Annotation>> annotations = m1.getAnnotations();
assertEquals(1, annotations.size());
CtAnnotation<?> a = annotations.get(0);
AnnotParamTypes annot = (AnnotParamTypes) a.getActualAnnotation();
assertEquals(42, annot.integer());
assertEquals(1, annot.integers().length);
assertEquals(42, annot.integers()[0]);
assertEquals("Hello World!", annot.string());
assertEquals(2, annot.strings().length);
assertEquals("Hello", annot.strings()[0]);
assertEquals("World", annot.strings()[1]);
assertEquals(Integer.class, annot.clazz());
assertEquals(2, annot.classes().length);
assertEquals(Integer.class, annot.classes()[0]);
assertEquals(String.class, annot.classes()[1]);
assertEquals(true, annot.b());
assertEquals('c', annot.c());
assertEquals(42, annot.byt());
assertEquals((short) 42, annot.s());
assertEquals(42, annot.l());
assertEquals(3.14f, annot.f(), 0f);
assertEquals(3.14159, annot.d(), 0);
assertEquals(AnnotParamTypeEnum.G, annot.e());
assertEquals("dd", annot.ia().value());
CtMethod<?> m2 = type.getElements(new NamedElementFilter<>(CtMethod.class, "m2")).get(0);
annotations = m2.getAnnotations();
assertEquals(1, annotations.size());
a = annotations.get(0);
annot = (AnnotParamTypes) a.getActualAnnotation();
assertEquals(42, annot.integer());
assertEquals(1, annot.integers().length);
assertEquals(42, annot.integers()[0]);
assertEquals("Hello World!", annot.string());
assertEquals(2, annot.strings().length);
assertEquals("Hello", annot.strings()[0]);
assertEquals("world", annot.strings()[1]);
assertEquals(false, annot.b());
assertEquals(42, annot.byt());
assertEquals((short) 42, annot.s());
assertEquals(42, annot.l());
assertEquals(3.14f, annot.f(), 0f);
assertEquals(3.14159, annot.d(), 0);
assertEquals(AnnotParamTypeEnum.G, annot.e());
assertEquals("dd", annot.ia().value());
// tests binary expressions
CtMethod<?> m3 = type.getElements(new NamedElementFilter<>(CtMethod.class, "m3")).get(0);
annotations = m3.getAnnotations();
assertEquals(1, annotations.size());
a = annotations.get(0);
annot = (AnnotParamTypes) a.getActualAnnotation();
assertEquals(45, annot.integer());
assertEquals(2, annot.integers().length);
assertEquals(40, annot.integers()[0]);
assertEquals(42 * 3, annot.integers()[1]);
assertEquals("Hello World!concatenated", annot.string());
assertEquals(2, annot.strings().length);
assertEquals("Helloconcatenated", annot.strings()[0]);
assertEquals("worldconcatenated", annot.strings()[1]);
assertEquals(true, annot.b());
assertEquals(42 ^ 1, annot.byt());
assertEquals((short) 42 / 2, annot.s());
assertEquals(43, annot.l());
assertEquals(3.14f * 2f, annot.f(), 0f);
assertEquals(3.14159d / 3d, annot.d(), 0);
assertEquals(AnnotParamTypeEnum.G, annot.e());
assertEquals("dddd", annot.ia().value());
}
use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.
the class AnnotationTest method testSpoonSpoonResult.
@Test
public void testSpoonSpoonResult() throws Exception {
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/annotation/testclasses/dropwizard/GraphiteReporterFactory.java");
String output = "target/spooned-" + this.getClass().getSimpleName() + "-firstspoon/";
spoon.setSourceOutputDirectory(output);
Factory factory = spoon.getFactory();
spoon.run();
Launcher spoon2 = new Launcher();
spoon2.addInputResource(output + "/spoon/test/annotation/testclasses/dropwizard/GraphiteReporterFactory.java");
// spoon2.addInputResource("./src/test/java/spoon/test/annotation/testclasses/PortRange.java");
spoon2.buildModel();
List<CtMethod<?>> methods = spoon2.getModel().getElements(new NamedElementFilter(CtMethod.class, "getPort"));
assertEquals("Number of method getPort should be 1", 1, methods.size());
CtMethod getport = methods.get(0);
CtTypeReference returnType = getport.getType();
List<CtAnnotation<?>> annotations = returnType.getAnnotations();
assertEquals("Number of annotation for return type of method getPort should be 1", 1, annotations.size());
CtAnnotation annotation = annotations.get(0);
assertEquals("Annotation should be @spoon.test.annotation.testclasses.PortRange", "spoon.test.annotation.testclasses.PortRange", annotation.getAnnotationType().getQualifiedName());
}
use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.
the class AnnotationTest method testUsageOfTypeAnnotationWithGenericTypesInClassDeclaration.
@Test
public void testUsageOfTypeAnnotationWithGenericTypesInClassDeclaration() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/AnnotationsAppliedOnAnyTypeInAClass.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
final CtClass<?> ctClass = (CtClass<?>) factory.Type().get("spoon.test.annotation.testclasses.AnnotationsAppliedOnAnyTypeInAClass");
final CtClass<?> genericClass = ctClass.getElements(new NamedElementFilter<>(CtClass.class, "DummyGenericClass")).get(0);
// New type parameter declaration.
final List<CtTypeParameter> typeParameters = genericClass.getFormalCtTypeParameters();
assertEquals("Generic class has 2 generics parameters.", 2, typeParameters.size());
assertEquals("First generic type must have type annotation", "@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "T", typeParameters.get(0).toString());
assertEquals("Second generic type must have type annotation", "@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "K", typeParameters.get(1).toString());
final CtTypeReference<?> superInterface = genericClass.getSuperInterfaces().toArray(new CtTypeReference<?>[0])[0];
final String expected = "spoon.test.annotation.testclasses.BasicAnnotation<@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "T>";
assertEquals("Super interface has a generic type with type annotation", expected, superInterface.toString());
}
use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.
the class InitializerTest method testModelBuildingInitializer.
@Test
public void testModelBuildingInitializer() throws Exception {
CtClass<?> type = build("spoon.test.initializers", "InstanceInitializers");
assertEquals("InstanceInitializers", type.getSimpleName());
CtField<?> k = type.getElements(new NamedElementFilter<>(CtField.class, "k")).get(0);
assertTrue(k.getDefaultExpression() instanceof CtConstructorCall);
CtField<?> l = type.getElements(new NamedElementFilter<>(CtField.class, "l")).get(0);
assertTrue(l.getDefaultExpression() instanceof CtConstructorCall);
CtField<?> x = type.getElements(new NamedElementFilter<>(CtField.class, "x")).get(0);
assertTrue(x.getDefaultExpression() == null);
CtField<?> y = type.getElements(new NamedElementFilter<>(CtField.class, "y")).get(0);
assertTrue(y.getDefaultExpression() instanceof CtLiteral);
CtField<?> z = type.getElements(new NamedElementFilter<>(CtField.class, "z")).get(0);
assertTrue(z.getDefaultExpression().toString().equals("5"));
// static initializer
CtAnonymousExecutable ex = type.getElements(new TypeFilter<CtAnonymousExecutable>(CtAnonymousExecutable.class)).get(0);
assertEquals("x = 3", ex.getBody().getStatements().get(0).toString());
}
Aggregations