Search in sources :

Example 26 with CtExecutableReference

use of spoon.reflect.reference.CtExecutableReference in project spoon by INRIA.

the class ParentTest method testParentOfCtExecutableReference.

@Test
public void testParentOfCtExecutableReference() throws Exception {
    // contract: parent of a executable reference is the element which call getExecutable().
    final Factory factory = build(Tacos.class);
    final CtType<Tacos> aTacos = factory.Type().get(Tacos.class);
    final CtInvocation inv = aTacos.getMethodsByName("m3").get(0).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
    final CtExecutableReference oldExecutable = inv.getExecutable();
    assertNotNull(oldExecutable.getParent());
    assertEquals(inv, oldExecutable.getParent());
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) Tacos(spoon.test.replace.testclasses.Tacos) Factory(spoon.reflect.factory.Factory) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) Test(org.junit.Test)

Example 27 with CtExecutableReference

use of spoon.reflect.reference.CtExecutableReference in project spoon by INRIA.

the class QualifiedThisRefTest method testPrintCtFieldAccessWorkEvenWhenParentNotInitialized.

@Test
public void testPrintCtFieldAccessWorkEvenWhenParentNotInitialized() throws Exception {
    CtClass zeclass = factory.Class().get(QualifiedThisRef.class);
    List<CtMethod> methods = zeclass.getMethodsByName("bla");
    assertEquals(1, methods.size());
    CtStatement invocation = methods.get(0).getBody().getStatement(0);
    assertTrue(invocation instanceof CtInvocation);
    CtInvocation<?> arg0 = (CtInvocation) invocation;
    CtExpression param = arg0.getArguments().get(0);
    CtExecutableReference execref = factory.Core().createExecutableReference();
    execref.setDeclaringType(factory.Type().createReference("java.util.Map"));
    execref.setSimpleName("exorcise");
    execref.setStatic(true);
    CtTypeReference tmp = param.getType();
    CtExpression arg = null;
    CtFieldReference ctfe = factory.createFieldReference();
    ctfe.setSimpleName("class");
    ctfe.setDeclaringType(tmp.box());
    arg = factory.Core().createFieldRead();
    ((CtFieldAccessImpl) arg).setVariable(ctfe);
    CtLiteral location = factory.Core().createLiteral();
    location.setType(factory.Type().createReference(String.class));
    CtTypeReference tmpref = factory.Core().clone(tmp);
    CtInvocation invoc = factory.Core().createInvocation();
    invoc.setExecutable(execref);
    invoc.setArguments(Arrays.asList(new CtExpression[] { param, arg, location }));
    execref.setActualTypeArguments(Arrays.asList(new CtTypeReference<?>[] { tmpref }));
    // succeeds
    arg0.getArguments().set(0, invoc);
    DefaultJavaPrettyPrinter printer = new DefaultJavaPrettyPrinter(factory.getEnvironment());
    printer.visitCtClass(zeclass);
    assertFalse(printer.getResult().isEmpty());
}
Also used : CtExpression(spoon.reflect.code.CtExpression) CtFieldReference(spoon.reflect.reference.CtFieldReference) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) CtClass(spoon.reflect.declaration.CtClass) CtInvocation(spoon.reflect.code.CtInvocation) CtLiteral(spoon.reflect.code.CtLiteral) CtStatement(spoon.reflect.code.CtStatement) CtFieldAccessImpl(spoon.support.reflect.code.CtFieldAccessImpl) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

CtExecutableReference (spoon.reflect.reference.CtExecutableReference)27 Test (org.junit.Test)13 CtInvocation (spoon.reflect.code.CtInvocation)9 CtTypeReference (spoon.reflect.reference.CtTypeReference)9 Factory (spoon.reflect.factory.Factory)8 Launcher (spoon.Launcher)7 CtMethod (spoon.reflect.declaration.CtMethod)7 ArrayList (java.util.ArrayList)5 CtElement (spoon.reflect.declaration.CtElement)5 CtExecutable (spoon.reflect.declaration.CtExecutable)5 CtExpression (spoon.reflect.code.CtExpression)4 CtFieldReference (spoon.reflect.reference.CtFieldReference)4 List (java.util.List)3 CtType (spoon.reflect.declaration.CtType)3 CtReference (spoon.reflect.reference.CtReference)3 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)3 HashSet (java.util.HashSet)2 CtFieldAccess (spoon.reflect.code.CtFieldAccess)2 CtLiteral (spoon.reflect.code.CtLiteral)2 CtStatement (spoon.reflect.code.CtStatement)2