Search in sources :

Example 16 with CtAnonymousExecutable

use of spoon.reflect.declaration.CtAnonymousExecutable in project spoon by INRIA.

the class TargetedExpressionTest method testTargetsOfStaticFieldAccess.

@Test
public void testTargetsOfStaticFieldAccess() 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 CtMethod<?> constructor = type.getMethodsByName("m").get(0);
    final CtThisAccess<Foo> expectedThisAccess = type.getFactory().Code().createThisAccess(expectedType);
    final CtTypeAccess<Foo> expectedTypeAccess = type.getFactory().Code().createTypeAccess(expectedType);
    final CtTypeAccess<Bar> expectedBarTypeAccess = type.getFactory().Code().createTypeAccess(expectedBarType);
    final List<CtFieldAccess<?>> elements = constructor.getElements(new TypeFilter<CtFieldAccess<?>>(CtFieldAccess.class));
    assertEquals(10, elements.size());
    assertEqualsFieldAccess(new ExpectedTargetedExpression().type(CtFieldRead.class).declaringType(expectedType).target(expectedThisAccess).result("this.k"), elements.get(0));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().type(CtFieldRead.class).declaringType(expectedType).target(expectedTypeAccess).result("spoon.test.targeted.testclasses.Foo.k"), elements.get(1));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().type(CtFieldRead.class).declaringType(expectedType).target(expectedTypeAccess).result("spoon.test.targeted.testclasses.Foo.k"), elements.get(2));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().type(CtFieldWrite.class).declaringType(expectedType).target(expectedThisAccess).result("this.k"), elements.get(3));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().type(CtFieldWrite.class).declaringType(expectedType).target(expectedTypeAccess).result("spoon.test.targeted.testclasses.Foo.k"), elements.get(4));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().type(CtFieldWrite.class).declaringType(expectedType).target(expectedTypeAccess).result("spoon.test.targeted.testclasses.Foo.k"), elements.get(5));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().type(CtFieldRead.class).declaringType(expectedBarType).target(expectedBarTypeAccess).result("spoon.test.targeted.testclasses.Bar.FIELD"), elements.get(6));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().type(CtFieldRead.class).declaringType(expectedBarType).target(expectedBarTypeAccess).result("spoon.test.targeted.testclasses.Bar.FIELD"), elements.get(7));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().type(CtFieldWrite.class).declaringType(expectedBarType).target(expectedBarTypeAccess).result("spoon.test.targeted.testclasses.Bar.FIELD"), elements.get(8));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().type(CtFieldWrite.class).declaringType(expectedBarType).target(expectedBarTypeAccess).result("spoon.test.targeted.testclasses.Bar.FIELD"), elements.get(9));
    final CtAnonymousExecutable staticInit = type.getAnonymousExecutables().get(0);
    final List<CtFieldAccess<?>> staticElements = staticInit.getElements(new TypeFilter<>(CtFieldAccess.class));
    assertEquals(1, staticElements.size());
    // Changing behaviour when writing static field, it is now writed using the class name
    assertEqualsFieldAccess(new ExpectedTargetedExpression().type(CtFieldWrite.class).declaringType(expectedType).target(expectedTypeAccess).result("p"), staticElements.get(0));
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) Foo(spoon.test.targeted.testclasses.Foo) Factory(spoon.reflect.factory.Factory) Bar(spoon.test.targeted.testclasses.Bar) CtAnonymousExecutable(spoon.reflect.declaration.CtAnonymousExecutable) Test(org.junit.Test)

Example 17 with CtAnonymousExecutable

use of spoon.reflect.declaration.CtAnonymousExecutable in project spoon by INRIA.

the class AnonymousExecutableTest method testStatements.

@Test
public void testStatements() throws Exception {
    CtType<?> type = build("spoon.test.model", "AnonymousExecutableClass");
    CtAnonymousExecutable anonexec = type.getElements(new TypeFilter<CtAnonymousExecutable>(CtAnonymousExecutable.class)).get(0);
    List<CtStatement> stats = anonexec.getBody().getStatements();
    assertEquals(1, stats.size());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtAnonymousExecutable(spoon.reflect.declaration.CtAnonymousExecutable) Test(org.junit.Test)

Aggregations

CtAnonymousExecutable (spoon.reflect.declaration.CtAnonymousExecutable)17 Test (org.junit.Test)9 CtConstructor (spoon.reflect.declaration.CtConstructor)5 CtMethod (spoon.reflect.declaration.CtMethod)5 Factory (spoon.reflect.factory.Factory)5 CtStatement (spoon.reflect.code.CtStatement)4 CtConstructorCall (spoon.reflect.code.CtConstructorCall)3 CtIf (spoon.reflect.code.CtIf)3 CtSwitch (spoon.reflect.code.CtSwitch)3 CtClass (spoon.reflect.declaration.CtClass)3 CtElement (spoon.reflect.declaration.CtElement)3 ParentNotInitializedException (spoon.reflect.declaration.ParentNotInitializedException)3 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)3 ArrayList (java.util.ArrayList)2 CtBinaryOperator (spoon.reflect.code.CtBinaryOperator)2 CtBlock (spoon.reflect.code.CtBlock)2 CtComment (spoon.reflect.code.CtComment)2 CtConditional (spoon.reflect.code.CtConditional)2 CtDo (spoon.reflect.code.CtDo)2 CtFor (spoon.reflect.code.CtFor)2