Search in sources :

Example 1 with CtTryWithResource

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

the class TryCatchTest method testTryWithOneResource.

@Test
public void testTryWithOneResource() throws Exception {
    CtClass<?> clazz = build("spoon.test.trycatch", "TryCatchResourceClass");
    CtMethod<?> method = clazz.getMethodsByName("readFirstLineFromFile").get(0);
    CtTryWithResource ctTryWithResource = method.getElements(new TypeFilter<CtTryWithResource>(CtTryWithResource.class)).get(0);
    // Checks try has only one resource.
    assertTrue(ctTryWithResource.getResources().size() == 1);
}
Also used : TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtTryWithResource(spoon.reflect.code.CtTryWithResource) Test(org.junit.Test)

Example 2 with CtTryWithResource

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

the class TryCatchTest method testModelBuildingInitializer.

@Test
public void testModelBuildingInitializer() throws Exception {
    CtClass<Main> type = build("spoon.test.trycatch", "Main");
    assertEquals("Main", type.getSimpleName());
    CtMethod<Void> m = type.getMethod("test");
    assertNotNull(m);
    assertEquals(2, m.getBody().getStatements().size());
    assertTrue(m.getBody().getStatements().get(0) instanceof CtTry);
    assertTrue(m.getBody().getStatements().get(1) instanceof CtTryWithResource);
    CtTryWithResource t2 = m.getBody().getStatement(1);
    assertNotNull(t2.getResources());
}
Also used : CtTryWithResource(spoon.reflect.code.CtTryWithResource) CtTry(spoon.reflect.code.CtTry) Test(org.junit.Test)

Example 3 with CtTryWithResource

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

the class TryCatchTest method testTryWithResources.

@Test
public void testTryWithResources() throws Exception {
    CtClass<?> clazz = build("spoon.test.trycatch", "TryCatchResourceClass");
    CtMethod<?> method = clazz.getMethodsByName("writeToFileZipFileContents").get(0);
    CtTryWithResource ctTryWithResource = method.getElements(new TypeFilter<CtTryWithResource>(CtTryWithResource.class)).get(0);
    // Checks try has more than one resource.
    assertTrue(ctTryWithResource.getResources().size() > 1);
}
Also used : TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtTryWithResource(spoon.reflect.code.CtTryWithResource) Test(org.junit.Test)

Example 4 with CtTryWithResource

use of spoon.reflect.code.CtTryWithResource 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 5 with CtTryWithResource

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

the class DefaultCoreFactory method createTryWithResource.

@Override
public CtTryWithResource createTryWithResource() {
    CtTryWithResource e = new CtTryWithResourceImpl();
    e.setFactory(getMainFactory());
    return e;
}
Also used : CtTryWithResourceImpl(spoon.support.reflect.code.CtTryWithResourceImpl) CtTryWithResource(spoon.reflect.code.CtTryWithResource)

Aggregations

CtTryWithResource (spoon.reflect.code.CtTryWithResource)5 Test (org.junit.Test)3 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)2 ArrayList (java.util.ArrayList)1 CtCatch (spoon.reflect.code.CtCatch)1 CtFor (spoon.reflect.code.CtFor)1 CtForEach (spoon.reflect.code.CtForEach)1 CtLocalVariable (spoon.reflect.code.CtLocalVariable)1 CtStatement (spoon.reflect.code.CtStatement)1 CtStatementList (spoon.reflect.code.CtStatementList)1 CtTry (spoon.reflect.code.CtTry)1 CtExecutable (spoon.reflect.declaration.CtExecutable)1 CtField (spoon.reflect.declaration.CtField)1 CtMethod (spoon.reflect.declaration.CtMethod)1 CtPackage (spoon.reflect.declaration.CtPackage)1 CtType (spoon.reflect.declaration.CtType)1 CtVariable (spoon.reflect.declaration.CtVariable)1 CtTypeReference (spoon.reflect.reference.CtTypeReference)1 CtTryWithResourceImpl (spoon.support.reflect.code.CtTryWithResourceImpl)1