Search in sources :

Example 6 with CtAnnotation

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

the class JavaReflectionTreeBuilder method visitAnnotation.

@Override
public void visitAnnotation(final Annotation annotation) {
    final CtAnnotation<Annotation> ctAnnotation = factory.Core().createAnnotation();
    enter(new AnnotationRuntimeBuilderContext(ctAnnotation) {

        @Override
        public void addMethod(CtMethod ctMethod) {
            try {
                Object value = annotation.annotationType().getMethod(ctMethod.getSimpleName()).invoke(annotation);
                ctAnnotation.addValue(ctMethod.getSimpleName(), value);
            } catch (Exception ignore) {
                ctAnnotation.addValue(ctMethod.getSimpleName(), "");
            }
        }
    });
    super.visitAnnotation(annotation);
    exit();
    contexts.peek().addAnnotation(ctAnnotation);
}
Also used : AnnotationRuntimeBuilderContext(spoon.support.visitor.java.internal.AnnotationRuntimeBuilderContext) CtAnnotation(spoon.reflect.declaration.CtAnnotation) Annotation(java.lang.annotation.Annotation) CtMethod(spoon.reflect.declaration.CtMethod)

Example 7 with CtAnnotation

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

the class AnnotationTest method testModelBuildingAnnotationBoundUsage.

@Test
public void testModelBuildingAnnotationBoundUsage() 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");
    assertEquals("Main", type.getSimpleName());
    CtParameter<?> param = type.getElements(new TypeFilter<CtParameter<?>>(CtParameter.class)).get(0);
    assertEquals("a", param.getSimpleName());
    List<CtAnnotation<? extends Annotation>> annotations = param.getAnnotations();
    assertEquals(1, annotations.size());
    CtAnnotation<?> a = annotations.get(0);
    Bound actualAnnotation = (Bound) a.getActualAnnotation();
    assertEquals(8, actualAnnotation.max());
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) Bound(spoon.test.annotation.testclasses.Bound) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) TypeAnnotation(spoon.test.annotation.testclasses.TypeAnnotation) GlobalAnnotation(spoon.test.annotation.testclasses.GlobalAnnotation) SuperAnnotation(spoon.test.annotation.testclasses.SuperAnnotation) AnnotationDefaultAnnotation(spoon.test.annotation.testclasses.AnnotationDefaultAnnotation) InnerAnnotation(spoon.test.annotation.testclasses.Foo.InnerAnnotation) Annotation(java.lang.annotation.Annotation) MiddleAnnotation(spoon.test.annotation.testclasses.Foo.MiddleAnnotation) OuterAnnotation(spoon.test.annotation.testclasses.Foo.OuterAnnotation) CtAnnotation(spoon.reflect.declaration.CtAnnotation) Test(org.junit.Test)

Example 8 with CtAnnotation

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

the class AnnotationTest method testReplaceAnnotationValue.

@Test
public void testReplaceAnnotationValue() 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();
    // contract: test replace of single value
    CtExpression integerValue = a.getValue("integer");
    assertEquals(42, ((CtLiteral<Integer>) integerValue).getValue().intValue());
    assertEquals(42, annot.integer());
    integerValue.replace(factory.createLiteral(17));
    CtExpression newIntegerValue = a.getValue("integer");
    assertEquals(17, ((CtLiteral<Integer>) newIntegerValue).getValue().intValue());
    assertEquals(17, annot.integer());
    // even if second value is null
    try {
        a.getValue("integer").replace(Arrays.asList(factory.createLiteral(18), null));
        fail();
    } catch (SpoonException e) {
    // OK
    }
    // contract: replacing of single value by no value
    a.getValue("integer").delete();
    assertNull(a.getValue("integer"));
    try {
        annot.integer();
        fail();
    } catch (NullPointerException e) {
    // OK - fails because int cannot be null
    }
    // contract: replace with null value means remove
    a.getValue("string").replace((CtElement) null);
    assertNull(a.getValue("string"));
    // contract: check that null value can be returned
    assertNull(annot.string());
    // contract: replace with null value in collection means remove
    a.getValue("clazz").replace(Collections.singletonList(null));
    assertNull(a.getValue("clazz"));
    // contract: check that null value can be returned
    assertNull(annot.clazz());
    // contract: test replace of item in collection
    assertEquals(1, annot.integers().length);
    assertEquals(42, annot.integers()[0]);
    CtNewArray<?> integersNewArray = (CtNewArray) a.getValue("integers");
    integersNewArray.getElements().get(0).replace(Arrays.asList(null, factory.createLiteral(101), null, factory.createLiteral(102)));
    assertEquals(2, annot.integers().length);
    assertEquals(101, annot.integers()[0]);
    assertEquals(102, annot.integers()[1]);
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) AnnotParamTypes(spoon.test.annotation.testclasses.AnnotParamTypes) CtExpression(spoon.reflect.code.CtExpression) SpoonException(spoon.SpoonException) Factory(spoon.reflect.factory.Factory) CtNewArray(spoon.reflect.code.CtNewArray) TypeAnnotation(spoon.test.annotation.testclasses.TypeAnnotation) GlobalAnnotation(spoon.test.annotation.testclasses.GlobalAnnotation) SuperAnnotation(spoon.test.annotation.testclasses.SuperAnnotation) AnnotationDefaultAnnotation(spoon.test.annotation.testclasses.AnnotationDefaultAnnotation) InnerAnnotation(spoon.test.annotation.testclasses.Foo.InnerAnnotation) Annotation(java.lang.annotation.Annotation) MiddleAnnotation(spoon.test.annotation.testclasses.Foo.MiddleAnnotation) OuterAnnotation(spoon.test.annotation.testclasses.Foo.OuterAnnotation) CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtLiteral(spoon.reflect.code.CtLiteral) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 9 with CtAnnotation

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

the class AnnotationTest method testGetAnnotationFromParameter.

@Test
public void testGetAnnotationFromParameter() {
    // contract: Java 8 receiver parameters are handled
    Launcher spoon = new Launcher();
    spoon.addInputResource("src/test/resources/noclasspath/Initializer.java");
    String output = "target/spooned-" + this.getClass().getSimpleName() + "-firstspoon/";
    spoon.setSourceOutputDirectory(output);
    spoon.getEnvironment().setNoClasspath(true);
    Factory factory = spoon.getFactory();
    spoon.buildModel();
    List<CtMethod> methods = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "setField"));
    assertThat(methods.size(), is(1));
    CtMethod methodSet = methods.get(0);
    assertThat(methodSet.getSimpleName(), is("setField"));
    List<CtParameter> parameters = methodSet.getParameters();
    assertThat(parameters.size(), is(1));
    CtParameter thisParameter = parameters.get(0);
    assertThat(thisParameter.getSimpleName(), is("this"));
    CtTypeReference thisParamType = thisParameter.getType();
    assertThat(thisParamType.getSimpleName(), is("Initializer"));
    List<CtAnnotation<?>> annotations = thisParameter.getType().getAnnotations();
    assertThat(annotations.size(), is(2));
    CtAnnotation unknownInit = annotations.get(0);
    CtAnnotation raw = annotations.get(1);
    assertThat(unknownInit.getAnnotationType().getSimpleName(), is("UnknownInitialization"));
    assertThat(raw.getAnnotationType().getSimpleName(), is("Raw"));
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtParameter(spoon.reflect.declaration.CtParameter) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 10 with CtAnnotation

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

the class AnnotationTest method testRepeatSameAnnotationOnLocalVariable.

@Test
public void testRepeatSameAnnotationOnLocalVariable() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/AnnotationsRepeated.java");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    final CtClass<?> ctClass = (CtClass<?>) factory.Type().get(AnnotationsRepeated.class);
    final CtMethod<?> method = ctClass.getMethodsByName("methodWithLocalVariable").get(0);
    final CtLocalVariable<?> ctLocalVariable = method.getBody().getElements(new AbstractFilter<CtLocalVariable<?>>(CtLocalVariable.class) {

        @Override
        public boolean matches(CtLocalVariable<?> element) {
            return true;
        }
    }).get(0);
    final List<CtAnnotation<? extends Annotation>> annotations = ctLocalVariable.getAnnotations();
    assertEquals("Local variable must to have multi annotation of the same type", 2, annotations.size());
    assertEquals("Type of the first annotation is AnnotationRepeated", AnnotationRepeated.class, annotations.get(0).getAnnotationType().getActualClass());
    assertEquals("Type of the second annotation is AnnotationRepeated", AnnotationRepeated.class, annotations.get(1).getAnnotationType().getActualClass());
    assertEquals("Argument of the first annotation is \"Local 1\"", "Local 1", ((CtLiteral) annotations.get(0).getValue("value")).getValue());
    assertEquals("Argument of the second annotation is \"Local 2\"", "Local 2", ((CtLiteral) annotations.get(1).getValue("value")).getValue());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtAnnotation(spoon.reflect.declaration.CtAnnotation) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) AnnotationsRepeated(spoon.test.annotation.testclasses.AnnotationsRepeated) CtLocalVariable(spoon.reflect.code.CtLocalVariable) TypeAnnotation(spoon.test.annotation.testclasses.TypeAnnotation) GlobalAnnotation(spoon.test.annotation.testclasses.GlobalAnnotation) SuperAnnotation(spoon.test.annotation.testclasses.SuperAnnotation) AnnotationDefaultAnnotation(spoon.test.annotation.testclasses.AnnotationDefaultAnnotation) InnerAnnotation(spoon.test.annotation.testclasses.Foo.InnerAnnotation) Annotation(java.lang.annotation.Annotation) MiddleAnnotation(spoon.test.annotation.testclasses.Foo.MiddleAnnotation) OuterAnnotation(spoon.test.annotation.testclasses.Foo.OuterAnnotation) CtAnnotation(spoon.reflect.declaration.CtAnnotation) Test(org.junit.Test)

Aggregations

CtAnnotation (spoon.reflect.declaration.CtAnnotation)43 Test (org.junit.Test)37 Launcher (spoon.Launcher)35 Factory (spoon.reflect.factory.Factory)30 Annotation (java.lang.annotation.Annotation)20 CtClass (spoon.reflect.declaration.CtClass)19 GlobalAnnotation (spoon.test.annotation.testclasses.GlobalAnnotation)19 TypeAnnotation (spoon.test.annotation.testclasses.TypeAnnotation)19 AnnotationDefaultAnnotation (spoon.test.annotation.testclasses.AnnotationDefaultAnnotation)18 InnerAnnotation (spoon.test.annotation.testclasses.Foo.InnerAnnotation)18 MiddleAnnotation (spoon.test.annotation.testclasses.Foo.MiddleAnnotation)18 OuterAnnotation (spoon.test.annotation.testclasses.Foo.OuterAnnotation)18 SuperAnnotation (spoon.test.annotation.testclasses.SuperAnnotation)18 CtMethod (spoon.reflect.declaration.CtMethod)12 CtTypeReference (spoon.reflect.reference.CtTypeReference)8 CtType (spoon.reflect.declaration.CtType)6 AnnotationsRepeated (spoon.test.annotation.testclasses.AnnotationsRepeated)6 AbstractFilter (spoon.reflect.visitor.filter.AbstractFilter)4 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)4 SpoonException (spoon.SpoonException)3