use of spoon.reflect.code.CtThisAccess 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.code.CtThisAccess 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.code.CtThisAccess in project spoon by INRIA.
the class ReplaceScanner method updateConstructor.
private CtParameter<?> updateConstructor(CtClass listener, CtTypeReference type) {
final CtConstructor ctConstructor = (CtConstructor) listener.getConstructors().toArray(new CtConstructor[listener.getConstructors().size()])[0];
CtAssignment assign = (CtAssignment) ctConstructor.getBody().getStatement(1);
CtThisAccess fieldAccess = (CtThisAccess) ((CtFieldAccess) assign.getAssigned()).getTarget();
((CtTypeAccess) fieldAccess.getTarget()).getAccessedType().setImplicit(true);
final CtParameter<?> aParameter = (CtParameter<?>) ctConstructor.getParameters().get(0);
aParameter.setType(type);
return aParameter;
}
use of spoon.reflect.code.CtThisAccess in project spoon by INRIA.
the class TargetedExpressionTest method testTargetsOfInvInAnonymousClass.
@Test
public void testTargetsOfInvInAnonymousClass() throws Exception {
// contract: Specify declaring type of the executable of an invocation, the target of the invocation, and its result. All this in an anonymous class.
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("m").get(0);
final List<CtInvocation> elements = method.getElements(new TypeFilter<>(CtInvocation.class));
assertEquals(2, elements.size());
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("this.invStatic()"), elements.get(0));
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedAnonymousType).target(expectedAnonymousThisAccess).result("this.invStatic()"), elements.get(1));
}
use of spoon.reflect.code.CtThisAccess in project spoon by INRIA.
the class TargetedExpressionTest method testTargetsOfInvInInnerClass.
@Test
public void testTargetsOfInvInInnerClass() throws Exception {
// contract: Specify declaring type of the executable of an invocation, the target of the invocation and its result. All this in an innerclass.
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 expectedThisAccess = factory.Code().createThisAccess(expectedType);
final CtThisAccess expectedSuperThisAccess = factory.Code().createThisAccess(expectedSuperClassType);
final CtThisAccess<?> expectedInnerClassAccess = factory.Code().createThisAccess(expectedInnerClass);
final CtThisAccess expectedNestedAccess = factory.Code().createThisAccess(expectedNested);
final CtMethod<?> innerInvMethod = innerClass.getMethodsByName("innerInv").get(0);
final List<CtInvocation<?>> elements = innerInvMethod.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class));
assertEquals(8, elements.size());
expectedThisAccess.setType(expectedInnerClass);
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("inv()"), elements.get(0));
expectedThisAccess.setType(expectedType);
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("this.inv()"), elements.get(1));
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(fooTypeAccess).result("spoon.test.targeted.testclasses.Foo.staticMethod()"), elements.get(2));
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(fooTypeAccess).result("spoon.test.targeted.testclasses.Foo.staticMethod()"), elements.get(3));
expectedSuperThisAccess.setType(expectedInnerClass);
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedSuperClassType).target(expectedSuperThisAccess).result("superMethod()"), elements.get(4));
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedSuperClassType).target(expectedThisAccess).result("this.superMethod()"), elements.get(5));
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedInnerClass).target(expectedInnerClassAccess).result("method()"), elements.get(6));
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedInnerClass).target(expectedInnerClassAccess).result("this.method()"), elements.get(7));
final List<CtInvocation> newElements = nestedTypeScanner.getMethodsByName("checkType").get(0).getElements(new TypeFilter<>(CtInvocation.class));
assertEquals(1, newElements.size());
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedNested).target(expectedNestedAccess).result("this.checkType(type)"), newElements.get(0));
}
Aggregations