Search in sources :

Example 1 with CtStatement

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

the class ParentExiter method visitCtCase.

@Override
public <E> void visitCtCase(CtCase<E> caseStatement) {
    final ASTNode node = jdtTreeBuilder.getContextBuilder().stack.peek().node;
    if (node instanceof CaseStatement && ((CaseStatement) node).constantExpression != null && caseStatement.getCaseExpression() == null && child instanceof CtExpression) {
        caseStatement.setCaseExpression((CtExpression<E>) child);
        return;
    } else if (child instanceof CtStatement) {
        caseStatement.addStatement((CtStatement) child);
        return;
    }
    super.visitCtCase(caseStatement);
}
Also used : CtExpression(spoon.reflect.code.CtExpression) CtStatement(spoon.reflect.code.CtStatement) CaseStatement(org.eclipse.jdt.internal.compiler.ast.CaseStatement) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode)

Example 2 with CtStatement

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

the class SnippetCompilationHelper method createWrapperContent.

private static String createWrapperContent(final CtElement element, final Factory f, final CtTypeReference returnType) {
    CtClass<?> w = f.Class().create(WRAPPER_CLASS_NAME);
    CtBlock body = f.Core().createBlock();
    if (element instanceof CtStatement) {
        body.addStatement((CtStatement) element);
    } else if (element instanceof CtExpression) {
        CtReturn ret = f.Core().createReturn();
        ret.setReturnedExpression((CtExpression) element);
        body.addStatement(ret);
    }
    Set<ModifierKind> modifiers = EnumSet.of(ModifierKind.STATIC);
    Set<CtTypeReference<? extends Throwable>> thrownTypes = new HashSet<>();
    thrownTypes.add(f.Class().<Throwable>get(Throwable.class).getReference());
    f.Method().create(w, modifiers, returnType, WRAPPER_METHOD_NAME, CtElementImpl.<CtParameter<?>>emptyList(), thrownTypes, body);
    String contents = w.toString();
    // Clean up (delete wrapper from factory) after it is printed. The DefaultJavaPrettyPrinter needs w in model to be able to print it correctly
    w.getPackage().removeType(w);
    return contents;
}
Also used : CtBlock(spoon.reflect.code.CtBlock) CtStatement(spoon.reflect.code.CtStatement) CtExpression(spoon.reflect.code.CtExpression) CtReturn(spoon.reflect.code.CtReturn) CtTypeReference(spoon.reflect.reference.CtTypeReference) ModifierKind(spoon.reflect.declaration.ModifierKind) HashSet(java.util.HashSet)

Example 3 with CtStatement

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

the class SnippetCompilationHelper method internalCompileStatement.

private static CtStatement internalCompileStatement(CtElement st, CtTypeReference returnType) {
    Factory f = st.getFactory();
    String contents = createWrapperContent(st, f, returnType);
    build(f, contents);
    CtType<?> c = f.Type().get(WRAPPER_CLASS_NAME);
    // Get the part we want
    CtMethod<?> wrapper = c.getMethod(WRAPPER_METHOD_NAME);
    List<CtStatement> statements = wrapper.getBody().getStatements();
    CtStatement ret = statements.get(statements.size() - 1);
    // Clean up
    c.getPackage().removeType(c);
    if (ret instanceof CtClass) {
        CtClass klass = (CtClass) ret;
        ret.getFactory().Package().getRootPackage().addType(klass);
        klass.setSimpleName(klass.getSimpleName().replaceAll("^[0-9]*", ""));
    }
    return ret;
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtStatement(spoon.reflect.code.CtStatement) Factory(spoon.reflect.factory.Factory)

Example 4 with CtStatement

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

the class ContextBuilder method enter.

@SuppressWarnings("unchecked")
void enter(CtElement e, ASTNode node) {
    stack.push(new ASTPair(e, node));
    if (!(e instanceof CtPackage) || (compilationUnitSpoon.getFile() != null && compilationUnitSpoon.getFile().getName().equals(DefaultJavaPrettyPrinter.JAVA_PACKAGE_DECLARATION))) {
        if (compilationunitdeclaration != null && !e.isImplicit()) {
            e.setPosition(this.jdtTreeBuilder.getPositionBuilder().buildPositionCtElement(e, node));
        }
    }
    ASTPair pair = stack.peek();
    CtElement current = pair.element;
    if (current instanceof CtExpression) {
        while (!casts.isEmpty()) {
            ((CtExpression<?>) current).addTypeCast(casts.remove(0));
        }
    }
    if (current instanceof CtStatement && !this.label.isEmpty()) {
        ((CtStatement) current).setLabel(this.label.pop());
    }
    try {
        if (e instanceof CtTypedElement && !(e instanceof CtConstructorCall) && !(e instanceof CtCatchVariable) && node instanceof Expression) {
            if (((CtTypedElement<?>) e).getType() == null) {
                ((CtTypedElement<Object>) e).setType(this.jdtTreeBuilder.getReferencesBuilder().getTypeReference(((Expression) node).resolvedType));
            }
        }
    } catch (UnsupportedOperationException ignore) {
    // For some element, we throw an UnsupportedOperationException when we call setType().
    }
}
Also used : CtExpression(spoon.reflect.code.CtExpression) CtStatement(spoon.reflect.code.CtStatement) CtConstructorCall(spoon.reflect.code.CtConstructorCall) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) CtExpression(spoon.reflect.code.CtExpression) CtElement(spoon.reflect.declaration.CtElement) CtPackage(spoon.reflect.declaration.CtPackage) CtTypedElement(spoon.reflect.declaration.CtTypedElement) CtCatchVariable(spoon.reflect.code.CtCatchVariable)

Example 5 with CtStatement

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

the class CtBlockImpl method setStatements.

@Override
public <T extends CtStatementList> T setStatements(List<CtStatement> statements) {
    if (statements == null || statements.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 s : statements) {
        addStatement(s);
    }
    return (T) this;
}
Also used : STATEMENT(spoon.reflect.path.CtRole.STATEMENT) CtStatement(spoon.reflect.code.CtStatement)

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