Search in sources :

Example 1 with SuperClass

use of spoon.test.targeted.testclasses.SuperClass in project spoon by INRIA.

the class TargetedExpressionTest method testTargetsOfInv.

@Test
public void testTargetsOfInv() throws Exception {
    // contract: Specify declaring type of the executable of an invocation, the target of the invocation and its result.
    final Factory factory = build(Foo.class, Bar.class, SuperClass.class);
    final CtClass<Foo> type = factory.Class().get(Foo.class);
    final CtClass<Foo.Fii.Fuu> fuu = factory.Class().<Foo.Fii.Fuu>get(Foo.Fii.Fuu.class);
    final CtTypeReference<Foo> expectedType = type.getReference();
    final CtTypeReference<Bar> expectedBarType = factory.Class().<Bar>get(Bar.class).getReference();
    final CtTypeReference<SuperClass> expectedSuperClassType = factory.Class().<SuperClass>get(SuperClass.class).getReference();
    final CtTypeReference<Foo.Fii.Fuu> expectedFuuType = fuu.getReference();
    final CtThisAccess<Foo> expectedThisAccess = factory.Code().createThisAccess(expectedType);
    final CtTypeAccess<Foo> fooTypeAccess = factory.Code().createTypeAccess(expectedType);
    final CtTypeAccess<SuperClass> superClassTypeAccess = factory.Code().createTypeAccess(expectedSuperClassType);
    final CtThisAccess<Foo> expectedSuperThisAccess = factory.Code().createThisAccess(expectedType);
    expectedSuperThisAccess.setTarget(superClassTypeAccess);
    final List<CtInvocation<?>> elements = type.getMethodsByName("inv").get(0).getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class));
    assertEquals(7, elements.size());
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(CtConstructorCallImpl.class).result("new spoon.test.targeted.testclasses.Foo(0, 0).method()"), elements.get(0));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(CtFieldReadImpl.class).result("foo.method()"), elements.get(1));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("this.method()"), elements.get(2));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("method()"), elements.get(3));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedBarType).target(CtFieldReadImpl.class).result("bar.methodBar()"), elements.get(4));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedFuuType).target(CtFieldReadImpl.class).result("fuu.method()"), elements.get(5));
    assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedSuperClassType).target(expectedSuperThisAccess).result("superMethod()"), elements.get(6));
    assertEquals(fooTypeAccess.getType().getQualifiedName(), ((CtThisAccess) elements.get(2).getTarget()).getTarget().getType().getQualifiedName());
    assertEquals(fooTypeAccess.getType().getQualifiedName(), ((CtThisAccess) elements.get(3).getTarget()).getTarget().getType().getQualifiedName());
}
Also used : Foo(spoon.test.targeted.testclasses.Foo) SuperClass(spoon.test.targeted.testclasses.SuperClass) Factory(spoon.reflect.factory.Factory) Bar(spoon.test.targeted.testclasses.Bar) CtInvocation(spoon.reflect.code.CtInvocation) Test(org.junit.Test)

Example 2 with SuperClass

use of spoon.test.targeted.testclasses.SuperClass 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 SuperClass

use of spoon.test.targeted.testclasses.SuperClass in project spoon by INRIA.

the class TargetedExpressionTest method testTargetsOfFieldAccess.

@Test
public void testTargetsOfFieldAccess() 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<Bar> expectedBarType = factory.Class().<Bar>get(Bar.class).getReference();
    final CtTypeReference<SuperClass> expectedSuperClassType = factory.Class().<SuperClass>get(SuperClass.class).getReference();
    final CtTypeReference<Foo.Fii.Fuu> expectedFuuType = factory.Class().<Foo.Fii.Fuu>get(Foo.Fii.Fuu.class).getReference();
    final CtMethod<?> fieldMethod = type.getMethodsByName("field").get(0);
    final CtThisAccess<Foo> expectedThisAccess = type.getFactory().Code().createThisAccess(expectedType);
    final List<CtFieldAccess<?>> elements = fieldMethod.getElements(new TypeFilter<CtFieldAccess<?>>(CtFieldAccess.class));
    assertEquals(10, elements.size());
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("this.i"), elements.get(0));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("i"), elements.get(1));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedBarType).target(elements.get(3)).result("this.bar.i"), elements.get(2));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("this.bar"), elements.get(3));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedBarType).target(elements.get(5)).result("bar.i"), elements.get(4));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("bar"), elements.get(5));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedSuperClassType).target(expectedThisAccess).result("this.o"), elements.get(6));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedSuperClassType).target(expectedThisAccess).result("o"), elements.get(7));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedFuuType).target(elements.get(9)).result("fuu.p"), elements.get(8));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("fuu"), elements.get(9));
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) Foo(spoon.test.targeted.testclasses.Foo) SuperClass(spoon.test.targeted.testclasses.SuperClass) Factory(spoon.reflect.factory.Factory) Bar(spoon.test.targeted.testclasses.Bar) Test(org.junit.Test)

Example 4 with SuperClass

use of spoon.test.targeted.testclasses.SuperClass 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

Test (org.junit.Test)4 Factory (spoon.reflect.factory.Factory)4 Foo (spoon.test.targeted.testclasses.Foo)4 SuperClass (spoon.test.targeted.testclasses.SuperClass)4 CtFieldAccess (spoon.reflect.code.CtFieldAccess)2 CtInvocation (spoon.reflect.code.CtInvocation)2 CtThisAccess (spoon.reflect.code.CtThisAccess)2 Bar (spoon.test.targeted.testclasses.Bar)2