use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class TargetedExpressionTest method testTargetsOfFieldAccessInInnerClass.
@Test
public void testTargetsOfFieldAccessInInnerClass() 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 CtTypeReference<SuperClass> expectedSuperClassType = factory.Class().<SuperClass>get(SuperClass.class).getReference();
final CtType<?> innerClass = type.getNestedType("InnerClass");
final CtTypeReference<?> expectedInnerClass = innerClass.getReference();
final CtType<?> nestedTypeScanner = type.getNestedType("1NestedTypeScanner");
final CtTypeReference<?> expectedNested = nestedTypeScanner.getReference();
final CtTypeAccess<Foo> fooTypeAccess = factory.Code().createTypeAccess(expectedType);
final CtThisAccess<Foo> expectedThisAccess = factory.Code().createThisAccess(expectedType);
final CtThisAccess<?> expectedInnerClassAccess = factory.Code().createThisAccess(expectedInnerClass);
final CtThisAccess expectedNestedAccess = factory.Code().createThisAccess(expectedNested);
final CtMethod<?> innerInvMethod = innerClass.getMethodsByName("innerField").get(0);
final List<CtFieldAccess<?>> elements = innerInvMethod.getElements(new TypeFilter<>(CtFieldAccess.class));
assertEquals(6, elements.size());
assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedInnerClass).target(expectedInnerClassAccess).result("this.i"), elements.get(0));
assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedInnerClass).target(expectedInnerClassAccess).result("i"), elements.get(1));
assertEquals(true, elements.get(1).getTarget().isImplicit());
assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("this.i"), elements.get(2));
assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedType).target(fooTypeAccess).result("spoon.test.targeted.testclasses.Foo.k"), elements.get(3));
assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedSuperClassType).target(expectedThisAccess).result("this.o"), elements.get(4));
assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedSuperClassType).target(expectedThisAccess).result("o"), elements.get(5));
final List<CtFieldAccess<?>> newElements = nestedTypeScanner.getMethodsByName("checkField").get(0).getElements(new TypeFilter<>(CtFieldAccess.class));
assertEquals(2, newElements.size());
assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedNested).target(expectedNestedAccess).result("this.type").isLocal(), newElements.get(0));
assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedNested).target(expectedNestedAccess).result("type").isLocal(), newElements.get(1));
}
use of spoon.reflect.factory.Factory 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.factory.Factory 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.factory.Factory 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
}
}
use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class TryCatchTest method testCatchOrder.
@Test
public void testCatchOrder() {
Factory factory = createFactory();
// test the order of the model
CtClass<?> clazz = factory.Code().createCodeSnippetStatement("" + "class X {" + "public void foo() {" + " try{}catch(RuntimeException e){java.lang.System.exit(0);}" + " catch(Exception e){}" + "}" + "};").compile();
CtTry tryStmt = (CtTry) clazz.getElements(new TypeFilter<>(CtTry.class)).get(0);
// the first caught exception is RuntimeException
assertEquals(RuntimeException.class, tryStmt.getCatchers().get(0).getParameter().getType().getActualClass());
assertEquals("java.lang.System.exit(0)", tryStmt.getCatchers().get(0).getBody().getStatement(0).toString());
assertEquals(Exception.class, tryStmt.getCatchers().get(1).getParameter().getType().getActualClass());
}
Aggregations