Search in sources :

Example 6 with CtCatch

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

the class CtTryImpl method setCatchers.

@Override
public <T extends CtTry> T setCatchers(List<CtCatch> catchers) {
    if (catchers == null || catchers.isEmpty()) {
        this.catchers = CtElementImpl.emptyList();
        return (T) this;
    }
    getFactory().getEnvironment().getModelChangeListener().onListDeleteAll(this, CATCH, this.catchers, new ArrayList<>(this.catchers));
    this.catchers.clear();
    for (CtCatch c : catchers) {
        addCatcher(c);
    }
    return (T) this;
}
Also used : CtCatch(spoon.reflect.code.CtCatch)

Example 7 with CtCatch

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

the class DefaultJavaPrettyPrinter method visitCtTryWithResource.

@Override
public void visitCtTryWithResource(CtTryWithResource tryWithResource) {
    enterCtStatement(tryWithResource);
    printer.writeKeyword("try").writeSpace();
    if (tryWithResource.getResources() != null && !tryWithResource.getResources().isEmpty()) {
        try (ListPrinter lp = elementPrinterHelper.createListPrinter(false, "(", false, false, ";", false, false, ")")) {
            for (CtLocalVariable<?> r : tryWithResource.getResources()) {
                lp.printSeparatorIfAppropriate();
                scan(r);
            }
        }
    }
    printer.writeSpace();
    scan(tryWithResource.getBody());
    for (CtCatch c : tryWithResource.getCatchers()) {
        scan(c);
    }
    if (tryWithResource.getFinalizer() != null) {
        printer.writeSpace().writeKeyword("finally").writeSpace();
        scan(tryWithResource.getFinalizer());
    }
}
Also used : CtCatch(spoon.reflect.code.CtCatch)

Example 8 with CtCatch

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

the class DefaultJavaPrettyPrinter method visitCtTry.

@Override
public void visitCtTry(CtTry tryBlock) {
    enterCtStatement(tryBlock);
    printer.writeKeyword("try").writeSpace();
    scan(tryBlock.getBody());
    for (CtCatch c : tryBlock.getCatchers()) {
        scan(c);
    }
    if (tryBlock.getFinalizer() != null) {
        printer.writeSpace().writeKeyword("finally").writeSpace();
        scan(tryBlock.getFinalizer());
    }
}
Also used : CtCatch(spoon.reflect.code.CtCatch)

Example 9 with CtCatch

use of spoon.reflect.code.CtCatch 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 10 with CtCatch

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

the class CtBodyHolderTest method testTryCatch.

@Test
public void testTryCatch() throws Exception {
    Factory factory = build(ClassWithBodies.class, CWBStatementTemplate.class);
    CtClass<?> cwbClass = (CtClass<?>) factory.Type().get(ClassWithBodies.class);
    assertEquals(2, cwbClass.getMethods().size());
    CtMethod<?> method = cwbClass.getMethod("method2");
    CtBlock<?> methodBody = method.getBody();
    assertTrue(methodBody.getStatement(0) instanceof CtTry);
    CtTry tryStmnt = (CtTry) methodBody.getStatement(0);
    checkCtBody(tryStmnt, "try_body", 0);
    assertEquals(1, tryStmnt.getCatchers().size());
    assertTrue(tryStmnt.getCatchers().get(0) instanceof CtCatch);
    checkCtBody(tryStmnt.getCatchers().get(0), "catch_body", 0);
}
Also used : CtClass(spoon.reflect.declaration.CtClass) ClassWithBodies(spoon.test.ctBodyHolder.testclasses.ClassWithBodies) Factory(spoon.reflect.factory.Factory) CtCatch(spoon.reflect.code.CtCatch) CtTry(spoon.reflect.code.CtTry) Test(org.junit.Test)

Aggregations

CtCatch (spoon.reflect.code.CtCatch)17 Test (org.junit.Test)7 Launcher (spoon.Launcher)4 CtTry (spoon.reflect.code.CtTry)4 Factory (spoon.reflect.factory.Factory)4 File (java.io.File)3 CtCatchVariable (spoon.reflect.code.CtCatchVariable)3 CtStatementList (spoon.reflect.code.CtStatementList)3 CtClass (spoon.reflect.declaration.CtClass)3 CtElement (spoon.reflect.declaration.CtElement)3 CtField (spoon.reflect.declaration.CtField)3 CtTypeReference (spoon.reflect.reference.CtTypeReference)3 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)3 ArrayList (java.util.ArrayList)2 CtStatement (spoon.reflect.code.CtStatement)2 CtExecutable (spoon.reflect.declaration.CtExecutable)2 CtMethod (spoon.reflect.declaration.CtMethod)2 CtPackage (spoon.reflect.declaration.CtPackage)2 CtType (spoon.reflect.declaration.CtType)2 UnionTypeReference (org.eclipse.jdt.internal.compiler.ast.UnionTypeReference)1