Search in sources :

Example 31 with CtStatement

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

the class VisitorPartialEvaluator method visitCtInvocation.

public <T> void visitCtInvocation(CtInvocation<T> invocation) {
    CtInvocation<T> i = invocation.getFactory().Core().createInvocation();
    i.setExecutable(invocation.getExecutable());
    i.setTypeCasts(invocation.getTypeCasts());
    boolean constant = true;
    i.setTarget(evaluate(invocation.getTarget()));
    if ((i.getTarget() != null) && !(i.getTarget() instanceof CtLiteral)) {
        constant = false;
    }
    for (CtExpression<?> e : invocation.getArguments()) {
        CtExpression<?> re = evaluate(e);
        if (!(re instanceof CtLiteral)) {
            constant = false;
        }
        i.addArgument(re);
    }
    // do not partially evaluate super(...)
    if (i.getExecutable().getSimpleName().equals(CtExecutableReference.CONSTRUCTOR_NAME)) {
        setResult(i);
        return;
    }
    if (constant) {
        CtExecutable<?> executable = invocation.getExecutable().getDeclaration();
        CtType<?> aType = invocation.getParent(CtType.class);
        CtTypeReference<?> execDeclaringType = invocation.getExecutable().getDeclaringType();
        // (including to superclasses)
        if (executable != null && aType != null && invocation.getType() != null && execDeclaringType != null && execDeclaringType.isSubtypeOf(aType.getReference())) {
            CtBlock<?> b = evaluate(executable.getBody());
            flowEnded = false;
            CtStatement last = b.getStatements().get(b.getStatements().size() - 1);
            if ((last != null) && (last instanceof CtReturn)) {
                if (((CtReturn<?>) last).getReturnedExpression() instanceof CtLiteral) {
                    setResult(((CtReturn<?>) last).getReturnedExpression());
                    return;
                }
            }
        } else {
            // try to completely evaluate
            T r = null;
            try {
                // System.err.println("invocking "+i);
                r = RtHelper.invoke(i);
                if (isLiteralType(r)) {
                    CtLiteral<T> l = invocation.getFactory().Core().createLiteral();
                    l.setValue(r);
                    setResult(l);
                    return;
                }
            } catch (Exception e) {
            }
        }
    }
    setResult(i);
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) CtStatement(spoon.reflect.code.CtStatement) CtReturn(spoon.reflect.code.CtReturn)

Example 32 with CtStatement

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

the class EqualsChecker method scanCtStatement.

@Override
public void scanCtStatement(CtStatement s) {
    final CtStatement peek = (CtStatement) this.other;
    final String leftLabel = s.getLabel();
    final String rightLabel = peek.getLabel();
    if (leftLabel == null && rightLabel == null) {
        super.scanCtStatement(s);
        return;
    }
    if (leftLabel == null || !leftLabel.equals(rightLabel)) {
        isNotEqual = true;
        return;
    }
    super.scanCtStatement(s);
}
Also used : CtStatement(spoon.reflect.code.CtStatement)

Example 33 with CtStatement

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

the class CtStatementListImpl method insertBegin.

@Override
public <T extends CtStatementList> T insertBegin(CtStatementList statements) {
    ensureModifiableStatementsList();
    for (CtStatement statement : statements.getStatements()) {
        statement.setParent(this);
        this.addStatement(0, statement);
    }
    if (isImplicit() && this.statements.size() > 1) {
        setImplicit(false);
    }
    return (T) this;
}
Also used : STATEMENT(spoon.reflect.path.CtRole.STATEMENT) CtStatement(spoon.reflect.code.CtStatement)

Example 34 with CtStatement

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

the class CtStatementListImpl method setStatements.

@Override
public <T extends CtStatementList> T setStatements(List<CtStatement> stmts) {
    if (stmts == null || stmts.isEmpty()) {
        this.statements = CtElementImpl.emptyList();
        return (T) this;
    }
    getFactory().getEnvironment().getModelChangeListener().onListDeleteAll(this, STATEMENT, this.statements, new ArrayList<>(this.statements));
    this.statements.clear();
    for (CtStatement stmt : stmts) {
        addStatement(stmt);
    }
    return (T) this;
}
Also used : STATEMENT(spoon.reflect.path.CtRole.STATEMENT) CtStatement(spoon.reflect.code.CtStatement)

Example 35 with CtStatement

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

the class AnnotationTest method testAnnotationIntrospection.

@Test
public void testAnnotationIntrospection() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/AnnotationIntrospection.java");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    CtClass<Object> aClass = factory.Class().get(AnnotationIntrospection.class);
    CtMethod<?> mMethod = aClass.getMethod("m");
    CtStatement statement = mMethod.getBody().getStatement(1);
    assertEquals("annotation.equals(null)", statement.toString());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Aggregations

CtStatement (spoon.reflect.code.CtStatement)84 Test (org.junit.Test)55 Factory (spoon.reflect.factory.Factory)35 CtMethod (spoon.reflect.declaration.CtMethod)23 Launcher (spoon.Launcher)20 CtBlock (spoon.reflect.code.CtBlock)17 CtIf (spoon.reflect.code.CtIf)11 CtExpression (spoon.reflect.code.CtExpression)10 ArrayList (java.util.ArrayList)9 CtElement (spoon.reflect.declaration.CtElement)9 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)9 CtInvocation (spoon.reflect.code.CtInvocation)8 CtTypeReference (spoon.reflect.reference.CtTypeReference)7 CtCase (spoon.reflect.code.CtCase)6 CtStatementList (spoon.reflect.code.CtStatementList)6 STATEMENT (spoon.reflect.path.CtRole.STATEMENT)6 FileSystemFile (spoon.support.compiler.FileSystemFile)6 Adobada (spoon.test.delete.testclasses.Adobada)6 CtCodeSnippetStatement (spoon.reflect.code.CtCodeSnippetStatement)5 CtLocalVariable (spoon.reflect.code.CtLocalVariable)5