Search in sources :

Example 1 with CtAnnotation

use of spoon.reflect.declaration.CtAnnotation in project dspot by STAMP-project.

the class AmplificationHelper method cloneTestMethod.

/**
 * Clones a test method.
 *
 * Performs necessary integration with JUnit and adds timeout.
 *
 * @param method Method to be cloned
 * @param suffix Suffix for the cloned method's name
 * @return The cloned method
 */
private static CtMethod cloneTestMethod(CtMethod method, String suffix) {
    CtMethod cloned_method = cloneMethod(method, suffix);
    CtAnnotation testAnnotation = cloned_method.getAnnotations().stream().filter(annotation -> annotation.toString().contains("Test")).findFirst().orElse(null);
    if (testAnnotation != null) {
        cloned_method.removeAnnotation(testAnnotation);
    }
    testAnnotation = method.getFactory().Core().createAnnotation();
    CtTypeReference<Object> ref = method.getFactory().Core().createTypeReference();
    ref.setSimpleName("Test");
    CtPackageReference refPackage = method.getFactory().Core().createPackageReference();
    refPackage.setSimpleName("org.junit");
    ref.setPackage(refPackage);
    testAnnotation.setAnnotationType(ref);
    Map<String, Object> elementValue = new HashMap<>();
    elementValue.put("timeout", timeOutInMs);
    testAnnotation.setElementValues(elementValue);
    cloned_method.addAnnotation(testAnnotation);
    cloned_method.addThrownType(method.getFactory().Type().createReference(Exception.class));
    return cloned_method;
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtPackageReference(spoon.reflect.reference.CtPackageReference) CtMethod(spoon.reflect.declaration.CtMethod)

Example 2 with CtAnnotation

use of spoon.reflect.declaration.CtAnnotation in project dspot by STAMP-project.

the class AmplificationHelper method cloneMethod.

/**
 * Clones a method.
 *
 * @param method Method to be cloned
 * @param suffix Suffix for the cloned method's name
 * @return The cloned method
 */
private static CtMethod cloneMethod(CtMethod method, String suffix) {
    CtMethod cloned_method = method.clone();
    // rename the clone
    cloned_method.setSimpleName(method.getSimpleName() + (suffix.isEmpty() ? "" : suffix + cloneNumber));
    cloneNumber++;
    CtAnnotation toRemove = cloned_method.getAnnotations().stream().filter(annotation -> annotation.toString().contains("Override")).findFirst().orElse(null);
    if (toRemove != null) {
        cloned_method.removeAnnotation(toRemove);
    }
    return cloned_method;
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtMethod(spoon.reflect.declaration.CtMethod)

Example 3 with CtAnnotation

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

the class ParentExiter method scanCtElement.

@Override
public void scanCtElement(CtElement e) {
    if (child instanceof CtAnnotation && this.jdtTreeBuilder.getContextBuilder().annotationValueName.isEmpty()) {
        // we check if the current element can have the annotation attached
        CtAnnotatedElementType annotatedElementType = CtAnnotation.getAnnotatedElementTypeForCtElement(e);
        annotatedElementType = (e instanceof CtTypeParameter || e instanceof CtTypeParameterReference) ? CtAnnotatedElementType.TYPE_USE : annotatedElementType;
        // in case of noclasspath, we cannot be 100% sure, so we guess it must be attached...
        if (this.jdtTreeBuilder.getFactory().getEnvironment().getNoClasspath() || (annotatedElementType != null && JDTTreeBuilderQuery.hasAnnotationWithType((Annotation) childJDT, annotatedElementType))) {
            e.addAnnotation((CtAnnotation<?>) child);
        }
        // in this case the annotation should be (also) attached to the type
        if (e instanceof CtTypedElement && JDTTreeBuilderQuery.hasAnnotationWithType((Annotation) childJDT, CtAnnotatedElementType.TYPE_USE)) {
            List<CtAnnotation> annotations = new ArrayList<>();
            if (!annotationsMap.containsKey(e)) {
                annotationsMap.put((CtTypedElement<?>) e, annotations);
            } else {
                annotations = annotationsMap.get(e);
            }
            annotations.add((CtAnnotation) child.clone());
            annotationsMap.put((CtTypedElement<?>) e, annotations);
        }
    }
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) CtAnnotatedElementType(spoon.reflect.declaration.CtAnnotatedElementType) ArrayList(java.util.ArrayList) CtTypedElement(spoon.reflect.declaration.CtTypedElement) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) CtAnnotation(spoon.reflect.declaration.CtAnnotation)

Example 4 with CtAnnotation

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

the class MetaModelTest method elementAnnotationAdaptedRoleTest.

@Test
public void elementAnnotationAdaptedRoleTest() {
    Launcher launcher = new Launcher();
    Factory factory = launcher.getFactory();
    CtClass<?> type = (CtClass) factory.Core().create(CtClass.class);
    CtAnnotation<?> annotation = factory.Annotation().annotate(type, Parameter.class, "value", "abc");
    // check adaptation of attribute to modifiable List
    List<CtAnnotation<?>> value = RoleHandlerHelper.getRoleHandler(type.getClass(), CtRole.ANNOTATION).asList(type);
    assertEquals(1, value.size());
    assertSame(annotation, value.get(0));
    // check we can remove from this collection
    value.remove(annotation);
    assertEquals(0, value.size());
    assertEquals(0, ((List) type.getValueByRole(CtRole.ANNOTATION)).size());
    // check we can add to this collection
    value.add(annotation);
    assertEquals(1, value.size());
    assertSame(annotation, value.get(0));
    assertEquals(1, ((List) type.getValueByRole(CtRole.ANNOTATION)).size());
    assertEquals(annotation, ((List) type.getValueByRole(CtRole.ANNOTATION)).get(0));
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtAnnotation(spoon.reflect.declaration.CtAnnotation) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Example 5 with CtAnnotation

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

the class MetaModelTest method elementAnnotationRoleHandlerTest.

@Test
public void elementAnnotationRoleHandlerTest() {
    Launcher launcher = new Launcher();
    Factory factory = launcher.getFactory();
    CtClass<?> type = (CtClass) factory.Core().create(CtClass.class);
    CtAnnotation<?> annotation = factory.Annotation().annotate(type, Parameter.class, "value", "abc");
    // check contract of low level RoleHandler
    RoleHandler roleHandler = RoleHandlerHelper.getRoleHandler(type.getClass(), CtRole.ANNOTATION);
    assertNotNull(roleHandler);
    assertEquals(CtElement.class, roleHandler.getTargetType());
    assertSame(CtRole.ANNOTATION, roleHandler.getRole());
    assertSame(ContainerKind.LIST, roleHandler.getContainerKind());
    assertEquals(CtAnnotation.class, roleHandler.getValueClass());
    // check getting value using role handler
    List<CtAnnotation<?>> value = roleHandler.getValue(type);
    assertEquals(1, value.size());
    assertSame(annotation, value.get(0));
    // check we have got direct readonly List
    try {
        value.remove(annotation);
        fail();
    } catch (Exception e) {
        this.getClass();
    }
    // check setValueByRole
    roleHandler.setValue(type, Collections.emptyList());
    value = roleHandler.getValue(type);
    assertEquals(0, value.size());
    roleHandler.setValue(type, Collections.singletonList(annotation));
    value = roleHandler.getValue(type);
    assertEquals(1, value.size());
    assertSame(annotation, value.get(0));
    try {
        // contract value must be a list of annotation. One annotation is not actually OK. This contract might be changed in future
        roleHandler.setValue(type, annotation);
        fail();
    } catch (ClassCastException e) {
    // OK
    }
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtAnnotation(spoon.reflect.declaration.CtAnnotation) RoleHandler(spoon.reflect.meta.RoleHandler) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) SpoonException(spoon.SpoonException) 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