Search in sources :

Example 1 with AnnotParamTypes

use of spoon.test.annotation.testclasses.AnnotParamTypes 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 2 with AnnotParamTypes

use of spoon.test.annotation.testclasses.AnnotParamTypes 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());
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) AnnotParamTypes(spoon.test.annotation.testclasses.AnnotParamTypes) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) 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

Annotation (java.lang.annotation.Annotation)2 Test (org.junit.Test)2 Launcher (spoon.Launcher)2 CtAnnotation (spoon.reflect.declaration.CtAnnotation)2 Factory (spoon.reflect.factory.Factory)2 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)2 AnnotParamTypes (spoon.test.annotation.testclasses.AnnotParamTypes)2 AnnotationDefaultAnnotation (spoon.test.annotation.testclasses.AnnotationDefaultAnnotation)2 InnerAnnotation (spoon.test.annotation.testclasses.Foo.InnerAnnotation)2 MiddleAnnotation (spoon.test.annotation.testclasses.Foo.MiddleAnnotation)2 OuterAnnotation (spoon.test.annotation.testclasses.Foo.OuterAnnotation)2 GlobalAnnotation (spoon.test.annotation.testclasses.GlobalAnnotation)2 SuperAnnotation (spoon.test.annotation.testclasses.SuperAnnotation)2 TypeAnnotation (spoon.test.annotation.testclasses.TypeAnnotation)2 SpoonException (spoon.SpoonException)1 CtExpression (spoon.reflect.code.CtExpression)1 CtLiteral (spoon.reflect.code.CtLiteral)1 CtNewArray (spoon.reflect.code.CtNewArray)1