Search in sources :

Example 46 with CtClass

use of spoon.reflect.declaration.CtClass 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 47 with CtClass

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

the class MetaModelTest method testGetParentRoleHandler.

@Test
public void testGetParentRoleHandler() {
    Launcher launcher = new Launcher();
    Factory factory = launcher.getFactory();
    CtClass<?> type = (CtClass) factory.Core().create(CtClass.class);
    CtField<?> field = factory.Field().create(type, Collections.emptySet(), factory.Type().booleanPrimitiveType(), "someField");
    assertSame(type, field.getDeclaringType());
    // contract: RoleHandlerHelper#getParentRoleHandler returns role handler which handles it's relationship to parent
    assertSame(CtRole.TYPE_MEMBER, RoleHandlerHelper.getRoleHandlerWrtParent(field).getRole());
    assertSame(CtRole.TYPE_MEMBER, field.getRoleInParent());
    // contract: RoleHandlerHelper#getParentRoleHandler returns null if there is no parent
    field.setParent(null);
    assertNull(RoleHandlerHelper.getRoleHandlerWrtParent(field));
    // contract: RoleHandlerHelper#getParentRoleHandler returns null if parent relation cannot be handled in this case
    // parent of new CtClass is root package - there is no way how to modify that
    assertNull(RoleHandlerHelper.getRoleHandlerWrtParent(type));
}
Also used : CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Example 48 with CtClass

use of spoon.reflect.declaration.CtClass 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)

Example 49 with CtClass

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

the class ImportScannerImpl method lookForLocalVariables.

protected Set<String> lookForLocalVariables(CtElement parent) {
    Set<String> result = new HashSet<>();
    // if the first container is the class, then we are not in a block and we can quit now.
    while (parent != null && !(parent instanceof CtBlock)) {
        if (parent instanceof CtClass) {
            return result;
        }
        parent = parent.getParent();
    }
    if (parent != null) {
        CtBlock block = (CtBlock) parent;
        boolean innerClass = false;
        // now we have the first container block, we want to check if we're not in an inner class
        while (parent != null && !(parent instanceof CtClass)) {
            parent = parent.getParent();
        }
        if (parent != null) {
            // let's find the last block BEFORE the class call: some collision could occur because of variables defined in that block
            if (!(parent.getParent() instanceof CtPackage)) {
                while (parent != null && !(parent instanceof CtBlock)) {
                    parent = parent.getParent();
                }
                if (parent != null) {
                    block = (CtBlock) parent;
                }
            }
        }
        AccessibleVariablesFinder avf = new AccessibleVariablesFinder(block);
        List<CtVariable> variables = avf.find();
        for (CtVariable variable : variables) {
            result.add(variable.getSimpleName());
        }
    }
    return result;
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtBlock(spoon.reflect.code.CtBlock) CtVariable(spoon.reflect.declaration.CtVariable) CtPackage(spoon.reflect.declaration.CtPackage) HashSet(java.util.HashSet)

Example 50 with CtClass

use of spoon.reflect.declaration.CtClass 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

CtClass (spoon.reflect.declaration.CtClass)168 Test (org.junit.Test)151 Launcher (spoon.Launcher)102 Factory (spoon.reflect.factory.Factory)84 CtMethod (spoon.reflect.declaration.CtMethod)42 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)22 CtAnnotation (spoon.reflect.declaration.CtAnnotation)19 SpoonModelBuilder (spoon.SpoonModelBuilder)17 CtInvocation (spoon.reflect.code.CtInvocation)16 File (java.io.File)15 CtTypeReference (spoon.reflect.reference.CtTypeReference)15 OuterAnnotation (spoon.test.annotation.testclasses.Foo.OuterAnnotation)15 Annotation (java.lang.annotation.Annotation)14 AbstractFilter (spoon.reflect.visitor.filter.AbstractFilter)14 AnnotationDefaultAnnotation (spoon.test.annotation.testclasses.AnnotationDefaultAnnotation)14 InnerAnnotation (spoon.test.annotation.testclasses.Foo.InnerAnnotation)14 MiddleAnnotation (spoon.test.annotation.testclasses.Foo.MiddleAnnotation)14 GlobalAnnotation (spoon.test.annotation.testclasses.GlobalAnnotation)14 SuperAnnotation (spoon.test.annotation.testclasses.SuperAnnotation)14 TypeAnnotation (spoon.test.annotation.testclasses.TypeAnnotation)14