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());
}
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));
}
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));
}
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));
}
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
}
}
Aggregations