Search in sources :

Example 26 with CtExpression

use of spoon.reflect.code.CtExpression in project spoon by INRIA.

the class CtBiScannerGenerator method process.

public void process() {
    final CtLocalVariable<?> peekElement = getFactory().Class().get(GENERATING_BISCANNER_PACKAGE + ".PeekElementTemplate").getMethod("statement").getBody().getStatement(0);
    final CtClass<Object> target = createBiScanner();
    for (CtTypeMember tm : getFactory().Class().get(CtScanner.class).getTypeMembers()) {
        if (!(tm instanceof CtMethod)) {
            continue;
        }
        CtMethod<?> element = (CtMethod) tm;
        if (!element.getSimpleName().startsWith("visitCt")) {
            continue;
        }
        Factory factory = element.getFactory();
        CtMethod<?> clone = factory.Core().clone(element);
        clone.addComment(getFactory().Code().createComment("autogenerated by " + getClass().getSimpleName(), CtComment.CommentType.INLINE));
        // Peek element from Stack.
        final CtLocalVariable<?> peek = factory.Core().clone(peekElement);
        final CtTypeReference type = factory.Core().clone(clone.getParameters().get(0).getType());
        type.getActualTypeArguments().clear();
        peek.getDefaultExpression().addTypeCast(type);
        peek.setType(type);
        clone.getBody().insertBegin(peek);
        for (int i = 2; i < clone.getBody().getStatements().size() - 1; i++) {
            List<CtExpression> invArgs = ((CtInvocation) clone.getBody().getStatement(i)).getArguments();
            if (invArgs.size() <= 1) {
                throw new RuntimeException("You forget the role argument in line " + i + " of method " + element.getSimpleName() + " from " + element.getDeclaringType().getQualifiedName());
            }
            final CtInvocation targetInvocation = (CtInvocation) invArgs.get(1);
            if ("getValue".equals(targetInvocation.getExecutable().getSimpleName()) && "CtLiteral".equals(targetInvocation.getExecutable().getDeclaringType().getSimpleName())) {
                clone.getBody().getStatement(i--).delete();
                continue;
            }
            CtInvocation<?> replace = (CtInvocation<?>) clone.getBody().getStatement(i).clone();
            // Changes to biScan method.
            replace.getExecutable().setSimpleName("biScan");
            // Creates other inv.
            final CtVariableAccess<?> otherRead = factory.Code().createVariableRead(peek.getReference(), false);
            replace.addArgument(factory.Code().createInvocation(otherRead, ((CtInvocation) replace.getArguments().get(1)).getExecutable()));
            if ("Map".equals(targetInvocation.getExecutable().getType().getSimpleName())) {
                ((CtExpression) replace.getArguments().get(1)).replace(factory.Code().createInvocation(targetInvocation, factory.Executable().createReference("List Map#values()")));
                CtInvocation invocation = factory.Code().createInvocation(replace.getArguments().get(2).clone(), factory.Executable().createReference("List Map#values()"));
                replace.getArguments().get(2).replace(invocation);
            }
            clone.getBody().getStatement(i).replace(replace);
        }
        target.addMethod(clone);
    }
}
Also used : CtExpression(spoon.reflect.code.CtExpression) Factory(spoon.reflect.factory.Factory) CtInvocation(spoon.reflect.code.CtInvocation) CtTypeMember(spoon.reflect.declaration.CtTypeMember) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtScanner(spoon.reflect.visitor.CtScanner) CtMethod(spoon.reflect.declaration.CtMethod)

Example 27 with CtExpression

use of spoon.reflect.code.CtExpression in project spoon by INRIA.

the class AccessTest method testCanVisitArrayAccessAndSubClasses.

@Test
public void testCanVisitArrayAccessAndSubClasses() throws Exception {
    final Factory factory = build(ArrayAccessSample.class);
    final List<CtArrayRead<?>> arraysRead = Query.getElements(factory, new AbstractFilter<CtArrayRead<?>>(CtArrayRead.class) {

        @Override
        public boolean matches(CtArrayRead<?> element) {
            return super.matches(element);
        }
    });
    assertEquals(2, arraysRead.size());
    final List<CtArrayWrite<?>> arraysWrite = Query.getElements(factory, new AbstractFilter<CtArrayWrite<?>>(CtArrayWrite.class) {

        @Override
        public boolean matches(CtArrayWrite<?> element) {
            return super.matches(element);
        }
    });
    assertEquals(1, arraysWrite.size());
    final List<CtArrayAccess<?, CtExpression<?>>> arraysAccess = Query.getElements(factory, new AbstractFilter<CtArrayAccess<?, CtExpression<?>>>(CtArrayAccess.class) {

        @Override
        public boolean matches(CtArrayAccess<?, CtExpression<?>> element) {
            return super.matches(element);
        }
    });
    assertEquals(3, arraysAccess.size());
}
Also used : CtExpression(spoon.reflect.code.CtExpression) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtArrayWrite(spoon.reflect.code.CtArrayWrite) CtArrayAccess(spoon.reflect.code.CtArrayAccess) CtArrayRead(spoon.reflect.code.CtArrayRead) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 28 with CtExpression

use of spoon.reflect.code.CtExpression in project spoon by INRIA.

the class ParentTest method testGetParentWithFilter.

@Test
public void testGetParentWithFilter() throws Exception {
    // addType should set Parent
    CtClass<Foo> clazz = (CtClass<Foo>) factory.Class().getAll().get(0);
    CtMethod<Object> m = clazz.getMethod("m");
    // get three = "" in one = two = three = "";
    CtExpression statement = ((CtAssignment) ((CtAssignment) m.getBody().getStatement(3)).getAssignment()).getAssignment();
    CtPackage ctPackage = statement.getParent(new TypeFilter<CtPackage>(CtPackage.class));
    assertEquals(Foo.class.getPackage().getName(), ctPackage.getQualifiedName());
    CtStatement ctStatement = statement.getParent(new AbstractFilter<CtStatement>(CtStatement.class) {

        @Override
        public boolean matches(CtStatement element) {
            return element.getParent() instanceof CtStatementList && super.matches(element);
        }
    });
    // the filter has to return one = two = three = ""
    assertEquals(m.getBody().getStatement(3), ctStatement);
    m = clazz.getMethod("internalClass");
    CtStatement ctStatement1 = m.getElements(new AbstractFilter<CtStatement>(CtStatement.class) {

        @Override
        public boolean matches(CtStatement element) {
            return element instanceof CtLocalVariable && super.matches(element);
        }
    }).get(0);
    // get the top class
    ctStatement1.getParent(CtType.class);
    CtType parent = ctStatement1.getParent(new AbstractFilter<CtType>(CtType.class) {

        @Override
        public boolean matches(CtType element) {
            return !element.isAnonymous() && element.isTopLevel() && super.matches(element);
        }
    });
    assertEquals(clazz, parent);
    assertNotEquals(ctStatement1.getParent(CtType.class), parent);
    // not present element
    CtWhile ctWhile = ctStatement1.getParent(new TypeFilter<CtWhile>(CtWhile.class));
    assertEquals(null, ctWhile);
    CtStatement statementParent = statement.getParent(new AbstractFilter<CtStatement>(CtStatement.class) {

        @Override
        public boolean matches(CtStatement element) {
            return true;
        }
    });
    // getParent must not return the current element
    assertNotEquals(statement, statementParent);
}
Also used : CtAssignment(spoon.reflect.code.CtAssignment) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtExpression(spoon.reflect.code.CtExpression) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtWhile(spoon.reflect.code.CtWhile) CtClass(spoon.reflect.declaration.CtClass) CtType(spoon.reflect.declaration.CtType) CtStatement(spoon.reflect.code.CtStatement) CtPackage(spoon.reflect.declaration.CtPackage) CtStatementList(spoon.reflect.code.CtStatementList) Test(org.junit.Test)

Example 29 with CtExpression

use of spoon.reflect.code.CtExpression in project spoon by INRIA.

the class PositionTest method testPositionField.

@Test
public void testPositionField() throws Exception {
    final Factory build = build(FooField.class);
    final CtType<FooField> foo = build.Type().get(FooField.class);
    String classContent = getClassContent(foo);
    DeclarationSourcePosition position1 = (DeclarationSourcePosition) foo.getField("field1").getPosition();
    assertEquals(5, position1.getLine());
    assertEquals(5, position1.getEndLine());
    assertEquals(68, position1.getSourceStart());
    assertEquals(95, position1.getSourceEnd());
    assertEquals("public final int field1 = 0;", contentAtPosition(classContent, position1));
    assertEquals("field1", contentAtPosition(classContent, position1.getNameStart(), position1.getNameEnd()));
    assertEquals("public final", contentAtPosition(classContent, position1.getModifierSourceStart(), position1.getModifierSourceEnd()));
    DeclarationSourcePosition position2 = (DeclarationSourcePosition) foo.getField("field2").getPosition();
    assertEquals(7, position2.getLine());
    assertEquals(8, position2.getEndLine());
    assertEquals(99, position2.getSourceStart());
    assertEquals(116, position2.getSourceEnd());
    assertEquals("int field2 =\n" + "\t\t\t0;", contentAtPosition(classContent, position2));
    assertEquals("field2", contentAtPosition(classContent, position2.getNameStart(), position2.getNameEnd()));
    assertEquals("", contentAtPosition(classContent, position2.getModifierSourceStart(), position2.getModifierSourceEnd()));
    CtAssignment m = foo.getMethod("m").getBody().getStatement(0);
    CtFieldAccess assigned = (CtFieldAccess) m.getAssigned();
    SourcePosition position3 = assigned.getPosition();
    assertEquals(13, position3.getLine());
    assertEquals(13, position3.getEndLine());
    assertEquals(168, position3.getSourceStart());
    assertEquals(184, position3.getSourceEnd());
    assertEquals("FooField.f.field2", contentAtPosition(classContent, position3));
    CtFieldAccess target = (CtFieldAccess) assigned.getTarget();
    SourcePosition position4 = target.getPosition();
    assertEquals(13, position4.getLine());
    assertEquals(13, position4.getEndLine());
    assertEquals(168, position4.getSourceStart());
    assertEquals(177, position4.getSourceEnd());
    assertEquals("FooField.f", contentAtPosition(classContent, position4));
    CtExpression typeAccess = target.getTarget();
    SourcePosition position5 = typeAccess.getPosition();
    assertEquals(13, position5.getLine());
    assertEquals(13, position5.getEndLine());
    assertEquals(168, position5.getSourceStart());
    assertEquals(175, position5.getSourceEnd());
    assertEquals("FooField", contentAtPosition(classContent, position5));
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) CtAssignment(spoon.reflect.code.CtAssignment) CtExpression(spoon.reflect.code.CtExpression) DeclarationSourcePosition(spoon.reflect.cu.position.DeclarationSourcePosition) BodyHolderSourcePosition(spoon.reflect.cu.position.BodyHolderSourcePosition) SourcePosition(spoon.reflect.cu.SourcePosition) Factory(spoon.reflect.factory.Factory) FooField(spoon.test.position.testclasses.FooField) DeclarationSourcePosition(spoon.reflect.cu.position.DeclarationSourcePosition) Test(org.junit.Test)

Example 30 with CtExpression

use of spoon.reflect.code.CtExpression 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

CtExpression (spoon.reflect.code.CtExpression)33 Factory (spoon.reflect.factory.Factory)19 Test (org.junit.Test)16 CtTypeReference (spoon.reflect.reference.CtTypeReference)11 CtStatement (spoon.reflect.code.CtStatement)10 CtInvocation (spoon.reflect.code.CtInvocation)6 CtMethod (spoon.reflect.declaration.CtMethod)6 AbstractTest (fr.inria.AbstractTest)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 CtAssignment (spoon.reflect.code.CtAssignment)5 CtLiteral (spoon.reflect.code.CtLiteral)5 CtNewArray (spoon.reflect.code.CtNewArray)5 Launcher (spoon.Launcher)4 CtConstructorCall (spoon.reflect.code.CtConstructorCall)4 CtLocalVariable (spoon.reflect.code.CtLocalVariable)4 CtClass (spoon.reflect.declaration.CtClass)4 CtElement (spoon.reflect.declaration.CtElement)4 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)4 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)4