Search in sources :

Example 36 with CtClass

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

the class ClassesTest method testIsAnonymousMethodInCtClass.

@Test
public void testIsAnonymousMethodInCtClass() throws Exception {
    CtClass<?> type = build("spoon.test.secondaryclasses.testclasses", "AnonymousClass");
    TreeSet<CtClass<?>> ts = new TreeSet<CtClass<?>>(new CtLineElementComparator());
    ts.addAll(type.getElements(new AbstractFilter<CtClass<?>>(CtClass.class) {

        @Override
        public boolean matches(CtClass<?> element) {
            return element.isAnonymous();
        }
    }));
    List<CtClass<?>> anonymousClass = new ArrayList<CtClass<?>>();
    anonymousClass.addAll(ts);
    assertFalse(type.isAnonymous());
    assertTrue(ts.first().isAnonymous());
    assertTrue(anonymousClass.get(1).isAnonymous());
    assertEquals(2, anonymousClass.size());
    assertEquals(2, ts.size());
    assertEquals("spoon.test.secondaryclasses.testclasses.AnonymousClass$1", anonymousClass.get(0).getQualifiedName());
    assertEquals("spoon.test.secondaryclasses.testclasses.AnonymousClass$2", anonymousClass.get(1).getQualifiedName());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) CtLineElementComparator(spoon.support.comparator.CtLineElementComparator) Test(org.junit.Test)

Example 37 with CtClass

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

the class TargetedExpressionTest method testTargetsOfFieldInAnonymousClass.

@Test
public void testTargetsOfFieldInAnonymousClass() throws Exception {
    final Factory factory = build(Foo.class, Bar.class, SuperClass.class);
    final CtClass<Foo> type = factory.Class().get(Foo.class);
    final CtTypeReference<Foo> expectedType = type.getReference();
    final CtClass<?> anonymousClass = type.getElements(new TypeFilter<CtClass>(CtClass.class) {

        @Override
        public boolean matches(CtClass element) {
            return element.isAnonymous() && super.matches(element);
        }
    }).get(0);
    final CtTypeReference<?> expectedAnonymousType = anonymousClass.getReference();
    final CtThisAccess<Foo> expectedThisAccess = factory.Code().createThisAccess(expectedType);
    final CtThisAccess expectedAnonymousThisAccess = factory.Code().createThisAccess(expectedAnonymousType);
    final CtMethod<?> method = anonymousClass.getMethodsByName("invStatic").get(0);
    final List<CtFieldAccess> elements = method.getElements(new TypeFilter<>(CtFieldAccess.class));
    assertEquals(3, elements.size());
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("this.i"), elements.get(0));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedAnonymousType).target(expectedAnonymousThisAccess).result("this.i"), elements.get(1));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedAnonymousType).target(expectedAnonymousThisAccess).result("i"), elements.get(2));
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) Foo(spoon.test.targeted.testclasses.Foo) Factory(spoon.reflect.factory.Factory) CtThisAccess(spoon.reflect.code.CtThisAccess) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtClass(spoon.reflect.declaration.CtClass) Test(org.junit.Test)

Example 38 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 39 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 40 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)

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