Search in sources :

Example 46 with CtStatement

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

the class PositionTest method getPositionOfImplicitBlock.

@Test
public void getPositionOfImplicitBlock() {
    // contract: position of implicit block in if (cond) [implicit block] else [implicit block] should be the source position of implicit block content.
    Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/position/testclasses/ImplicitBlock.java");
    launcher.buildModel();
    CtIf ifElement = launcher.getModel().getElements(new TypeFilter<CtIf>(CtIf.class)).get(0);
    CtStatement thenStatement = ifElement.getThenStatement();
    assertTrue(thenStatement instanceof CtBlock);
    CtBlock thenBlock = (CtBlock) thenStatement;
    SourcePosition positionThen = thenBlock.getPosition();
    CtStatement returnStatement = thenBlock.getStatement(0);
    assertEquals(returnStatement.getPosition(), positionThen);
    assertEquals("ImplicitBlock.java", positionThen.getFile().getName());
    assertEquals(7, positionThen.getLine());
    CtStatement elseStatement = ifElement.getElseStatement();
    assertTrue(elseStatement instanceof CtBlock);
    CtBlock elseBlock = (CtBlock) elseStatement;
    SourcePosition positionElse = elseBlock.getPosition();
    CtStatement otherReturnStatement = elseBlock.getStatement(0);
    assertEquals(otherReturnStatement.getPosition(), positionElse);
    assertEquals("ImplicitBlock.java", positionThen.getFile().getName());
    assertEquals(8, positionElse.getLine());
    assertNotEquals(returnStatement, otherReturnStatement);
}
Also used : CtBlock(spoon.reflect.code.CtBlock) CtStatement(spoon.reflect.code.CtStatement) DeclarationSourcePosition(spoon.reflect.cu.position.DeclarationSourcePosition) BodyHolderSourcePosition(spoon.reflect.cu.position.BodyHolderSourcePosition) SourcePosition(spoon.reflect.cu.SourcePosition) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Example 47 with CtStatement

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

the class DefaultPrettyPrinterTest method testTernaryParenthesesOnLocalVariable.

@Test
public void testTernaryParenthesesOnLocalVariable() {
    // Spooning the code snippet
    Launcher launcher = new Launcher();
    CtCodeSnippetStatement snippet = launcher.getFactory().Code().createCodeSnippetStatement("final int foo = (new Object() instanceof Object ? new Object().equals(null) : new Object().equals(new Object())) ? 0 : new Object().hashCode();");
    CtStatement compile = snippet.compile();
    // Pretty-printing the Spooned code snippet and compiling the resulting code.
    snippet = launcher.getFactory().Code().createCodeSnippetStatement(compile.toString());
    assertEquals(compile, snippet.compile());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement) Test(org.junit.Test)

Example 48 with CtStatement

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

the class DefaultJavaPrettyPrinter method visitCtCase.

@Override
@SuppressWarnings("rawtypes")
public <E> void visitCtCase(CtCase<E> caseStatement) {
    enterCtStatement(caseStatement);
    if (caseStatement.getCaseExpression() != null) {
        printer.writeKeyword("case").writeSpace();
        // writing enum case expression
        if (caseStatement.getCaseExpression() instanceof CtFieldAccess) {
            final CtFieldReference variable = ((CtFieldAccess) caseStatement.getCaseExpression()).getVariable();
            // In noclasspath mode, we don't have always the type of the declaring type.
            if (variable.getType() != null && variable.getDeclaringType() != null && variable.getType().getQualifiedName().equals(variable.getDeclaringType().getQualifiedName())) {
                printer.writeIdentifier(variable.getSimpleName());
            } else {
                scan(caseStatement.getCaseExpression());
            }
        } else {
            scan(caseStatement.getCaseExpression());
        }
    } else {
        printer.writeKeyword("default");
    }
    printer.writeSpace().writeSeparator(":").incTab();
    for (CtStatement statement : caseStatement.getStatements()) {
        printer.writeln();
        elementPrinterHelper.writeStatement(statement);
    }
    printer.decTab();
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) CtStatement(spoon.reflect.code.CtStatement) CtFieldReference(spoon.reflect.reference.CtFieldReference)

Example 49 with CtStatement

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

the class AccessibleVariablesFinder method getVariable.

private List<CtVariable> getVariable(final CtElement parent) {
    final List<CtVariable> variables = new ArrayList<>();
    if (parent == null) {
        return variables;
    }
    class VariableScanner extends CtInheritanceScanner {

        @Override
        public void visitCtStatementList(CtStatementList e) {
            for (int i = 0; i < e.getStatements().size(); i++) {
                CtStatement ctStatement = e.getStatements().get(i);
                if (ctStatement.getPosition() == null) {
                }
                if (ctStatement.getPosition() != null && ctStatement.getPosition().getSourceStart() > expression.getPosition().getSourceEnd()) {
                    break;
                }
                if (ctStatement instanceof CtVariable) {
                    variables.add((CtVariable) ctStatement);
                }
            }
            super.visitCtStatementList(e);
        }

        @Override
        public <T> void scanCtType(CtType<T> type) {
            List<CtField<?>> fields = type.getFields();
            for (int i = 0; i < fields.size(); i++) {
                CtField<?> ctField = fields.get(i);
                if (ctField.hasModifier(ModifierKind.PUBLIC) || ctField.hasModifier(ModifierKind.PROTECTED)) {
                    variables.add(ctField);
                } else if (ctField.hasModifier(ModifierKind.PRIVATE)) {
                    if (expression.hasParent(type)) {
                        variables.add(ctField);
                    }
                } else if (expression.getParent(CtPackage.class).equals(type.getParent(CtPackage.class))) {
                    // default visibility
                    variables.add(ctField);
                }
            }
            CtTypeReference<?> superclass = type.getSuperclass();
            if (superclass != null) {
                variables.addAll(getVariable(superclass.getTypeDeclaration()));
            }
            Set<CtTypeReference<?>> superInterfaces = type.getSuperInterfaces();
            for (Iterator<CtTypeReference<?>> iterator = superInterfaces.iterator(); iterator.hasNext(); ) {
                CtTypeReference<?> typeReference = iterator.next();
                variables.addAll(getVariable(typeReference.getTypeDeclaration()));
            }
            super.scanCtType(type);
        }

        @Override
        public void visitCtTryWithResource(CtTryWithResource e) {
            variables.addAll(e.getResources());
            super.visitCtTryWithResource(e);
        }

        @Override
        public void scanCtExecutable(CtExecutable e) {
            variables.addAll(e.getParameters());
            super.scanCtExecutable(e);
        }

        @Override
        public void visitCtFor(CtFor e) {
            for (CtStatement ctStatement : e.getForInit()) {
                this.scan(ctStatement);
            }
            super.visitCtFor(e);
        }

        @Override
        public void visitCtForEach(CtForEach e) {
            variables.add(e.getVariable());
            super.visitCtForEach(e);
        }

        @Override
        public void visitCtMethod(CtMethod e) {
            this.scan(e.getBody());
            super.visitCtMethod(e);
        }

        @Override
        public void visitCtLocalVariable(CtLocalVariable e) {
            variables.add(e);
            super.visitCtLocalVariable(e);
        }

        @Override
        public void visitCtCatch(CtCatch e) {
            variables.add(e.getParameter());
            super.visitCtCatch(e);
        }
    }
    new VariableScanner().scan(parent);
    return variables;
}
Also used : ArrayList(java.util.ArrayList) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtExecutable(spoon.reflect.declaration.CtExecutable) CtForEach(spoon.reflect.code.CtForEach) CtType(spoon.reflect.declaration.CtType) CtStatement(spoon.reflect.code.CtStatement) CtField(spoon.reflect.declaration.CtField) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtVariable(spoon.reflect.declaration.CtVariable) CtStatementList(spoon.reflect.code.CtStatementList) CtPackage(spoon.reflect.declaration.CtPackage) CtTryWithResource(spoon.reflect.code.CtTryWithResource) CtCatch(spoon.reflect.code.CtCatch) CtFor(spoon.reflect.code.CtFor) CtMethod(spoon.reflect.declaration.CtMethod)

Example 50 with CtStatement

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

the class DeleteTest method testDeleteAStatementInMethod.

@Test
public void testDeleteAStatementInMethod() throws Exception {
    final Factory factory = build(Adobada.class);
    final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
    final CtMethod method = adobada.getMethod("m");
    assertEquals(2, method.getBody().getStatements().size());
    final CtStatement statement = method.getBody().getStatement(1);
    statement.delete();
    assertEquals(1, method.getBody().getStatements().size());
    assertFalse(method.getBody().getStatements().contains(statement));
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Factory(spoon.reflect.factory.Factory) Adobada(spoon.test.delete.testclasses.Adobada) CtMethod(spoon.reflect.declaration.CtMethod) 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