Search in sources :

Example 1 with CtThisAccess

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));
}
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 2 with CtThisAccess

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));
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) Foo(spoon.test.targeted.testclasses.Foo) SuperClass(spoon.test.targeted.testclasses.SuperClass) Factory(spoon.reflect.factory.Factory) CtThisAccess(spoon.reflect.code.CtThisAccess) Test(org.junit.Test)

Example 3 with CtThisAccess

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;
}
Also used : CtAssignment(spoon.reflect.code.CtAssignment) CtThisAccess(spoon.reflect.code.CtThisAccess) CtParameter(spoon.reflect.declaration.CtParameter) CtConstructor(spoon.reflect.declaration.CtConstructor)

Example 4 with CtThisAccess

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));
}
Also used : 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) CtInvocation(spoon.reflect.code.CtInvocation) Test(org.junit.Test)

Example 5 with CtThisAccess

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));
}
Also used : Foo(spoon.test.targeted.testclasses.Foo) SuperClass(spoon.test.targeted.testclasses.SuperClass) Factory(spoon.reflect.factory.Factory) CtThisAccess(spoon.reflect.code.CtThisAccess) CtInvocation(spoon.reflect.code.CtInvocation) Test(org.junit.Test)

Aggregations

CtThisAccess (spoon.reflect.code.CtThisAccess)7 Factory (spoon.reflect.factory.Factory)6 Test (org.junit.Test)5 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)4 Foo (spoon.test.targeted.testclasses.Foo)4 CtFieldAccess (spoon.reflect.code.CtFieldAccess)2 CtInvocation (spoon.reflect.code.CtInvocation)2 CtClass (spoon.reflect.declaration.CtClass)2 SuperClass (spoon.test.targeted.testclasses.SuperClass)2 CtAssignment (spoon.reflect.code.CtAssignment)1 CtLocalVariable (spoon.reflect.code.CtLocalVariable)1 CtTargetedExpression (spoon.reflect.code.CtTargetedExpression)1 CtTry (spoon.reflect.code.CtTry)1 CtConstructor (spoon.reflect.declaration.CtConstructor)1 CtParameter (spoon.reflect.declaration.CtParameter)1 Adobada (spoon.test.delete.testclasses.Adobada)1