Search in sources :

Example 1 with CtCatch

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

the class DefaultCoreFactory method createCatch.

public CtCatch createCatch() {
    CtCatch e = new CtCatchImpl();
    e.setFactory(getMainFactory());
    return e;
}
Also used : CtCatchImpl(spoon.support.reflect.code.CtCatchImpl) CtCatch(spoon.reflect.code.CtCatch)

Example 2 with CtCatch

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

the class ExceptionTest method testUnionCatchExceptionInsideLambdaInNoClasspath.

@Test
public void testUnionCatchExceptionInsideLambdaInNoClasspath() {
    // contract: the model should be built when defining a union catch inside a lambda which is not known (noclasspath)
    // and the catch variable types should be the same than outside a lambda
    Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/noclasspath/UnionCatch.java");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.buildModel();
    List<CtCatch> catches = launcher.getFactory().getModel().getElements(new TypeFilter<>(CtCatch.class));
    assertEquals(2, catches.size());
    // inside a lambda
    CtCatchVariable variable1 = catches.get(0).getParameter();
    // outside the lambda
    CtCatchVariable variable2 = catches.get(1).getParameter();
    assertEquals(variable1.getMultiTypes(), variable2.getMultiTypes());
// for now the type of CtCatchVariable is not the same
// this should be fix in the future (see: https://github.com/INRIA/spoon/issues/1420)
// assertEquals(variable2, variable1);
}
Also used : Launcher(spoon.Launcher) CtCatch(spoon.reflect.code.CtCatch) CtCatchVariable(spoon.reflect.code.CtCatchVariable) Test(org.junit.Test)

Example 3 with CtCatch

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

the class TryCatchTest method testTryCatchVariableGetType.

@Test
public void testTryCatchVariableGetType() throws Exception {
    Factory factory = createFactory();
    CtClass<?> clazz = factory.Code().createCodeSnippetStatement("" + "class X {" + "public void foo() {" + " try{}catch(RuntimeException e){System.exit(0);}" + "}" + "};").compile();
    CtTry tryStmt = (CtTry) clazz.getElements(new TypeFilter<>(CtTry.class)).get(0);
    List<CtCatch> catchers = tryStmt.getCatchers();
    assertEquals(1, catchers.size());
    CtCatchVariable<?> catchVariable = catchers.get(0).getParameter();
    assertEquals(RuntimeException.class, catchVariable.getType().getActualClass());
    assertEquals(1, catchVariable.getMultiTypes().size());
    assertEquals(RuntimeException.class, catchVariable.getMultiTypes().get(0).getActualClass());
    // contract: the manipulation with catch variable type is possible
    catchVariable.setType((CtTypeReference) factory.Type().createReference(IllegalArgumentException.class));
    assertEquals(IllegalArgumentException.class, catchVariable.getType().getActualClass());
    // contract setType influences multitypes
    assertEquals(1, catchVariable.getMultiTypes().size());
    assertEquals(IllegalArgumentException.class, catchVariable.getMultiTypes().get(0).getActualClass());
    catchVariable.setMultiTypes(Collections.singletonList((CtTypeReference) factory.Type().createReference(UnsupportedOperationException.class)));
    assertEquals(UnsupportedOperationException.class, catchVariable.getType().getActualClass());
    // contract setType influences multitypes
    assertEquals(1, catchVariable.getMultiTypes().size());
    assertEquals(UnsupportedOperationException.class, catchVariable.getMultiTypes().get(0).getActualClass());
    catchVariable.setMultiTypes(Arrays.asList(factory.Type().createReference(UnsupportedOperationException.class), factory.Type().createReference(IllegalArgumentException.class)));
    assertEquals(2, catchVariable.getMultiTypes().size());
    assertEquals(UnsupportedOperationException.class, catchVariable.getMultiTypes().get(0).getActualClass());
    assertEquals(IllegalArgumentException.class, catchVariable.getMultiTypes().get(1).getActualClass());
    // contract setMultiTypes influences types, which contains common super class of all multi types
    assertEquals(RuntimeException.class, catchVariable.getType().getActualClass());
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtCatch(spoon.reflect.code.CtCatch) CtTry(spoon.reflect.code.CtTry) Test(org.junit.Test)

Example 4 with CtCatch

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

the class PotentialVariableDeclarationFunction method apply.

@Override
public void apply(CtElement input, CtConsumer<Object> outputConsumer) {
    isTypeOnTheWay = false;
    isInStaticScope = false;
    // Search previous siblings for element which may represents the declaration of this local variable
    CtQuery siblingsQuery = input.getFactory().createQuery().map(new SiblingsFunction().mode(SiblingsFunction.Mode.PREVIOUS)).select(new TypeFilter<>(CtVariable.class));
    if (variableName != null) {
        // variable name is defined so we have to search only for variables with that name
        siblingsQuery = siblingsQuery.select(new NamedElementFilter<>(CtNamedElement.class, variableName));
    }
    CtElement scopeElement = input;
    // Search input and then all parents until first CtPackage for element which may represents the declaration of this local variable
    while (scopeElement != null && !(scopeElement instanceof CtPackage) && scopeElement.isParentInitialized()) {
        CtElement parent = scopeElement.getParent();
        if (parent instanceof CtType<?>) {
            isTypeOnTheWay = true;
            // visit each CtField of `parent` CtType
            CtQuery q = parent.map(new AllTypeMembersFunction(CtField.class));
            q.forEach((CtField<?> field) -> {
                if (isInStaticScope && field.hasModifier(ModifierKind.STATIC) == false) {
                    /*
						 * the variable reference is used in static scope,
						 * but the field is not static - ignore it
						 */
                    return;
                }
                // else send field as potential variable declaration
                if (sendToOutput(field, outputConsumer)) {
                    // and terminate the internal query q if outer query is already terminated
                    q.terminate();
                }
            });
            if (query.isTerminated()) {
                return;
            }
        } else if (parent instanceof CtBodyHolder || parent instanceof CtStatementList) {
            // visit all previous CtVariable siblings of scopeElement element in parent BodyHolder or Statement list
            siblingsQuery.setInput(scopeElement).forEach(outputConsumer);
            if (query.isTerminated()) {
                return;
            }
            // visit parameters of CtCatch and CtExecutable (method, lambda)
            if (parent instanceof CtCatch) {
                CtCatch ctCatch = (CtCatch) parent;
                if (sendToOutput(ctCatch.getParameter(), outputConsumer)) {
                    return;
                }
            } else if (parent instanceof CtExecutable) {
                CtExecutable<?> exec = (CtExecutable<?>) parent;
                for (CtParameter<?> param : exec.getParameters()) {
                    if (sendToOutput(param, outputConsumer)) {
                        return;
                    }
                }
            }
        }
        if (parent instanceof CtModifiable) {
            isInStaticScope = isInStaticScope || ((CtModifiable) parent).hasModifier(ModifierKind.STATIC);
        }
        scopeElement = parent;
    }
}
Also used : CtElement(spoon.reflect.declaration.CtElement) CtQuery(spoon.reflect.visitor.chain.CtQuery) CtExecutable(spoon.reflect.declaration.CtExecutable) CtBodyHolder(spoon.reflect.code.CtBodyHolder) CtType(spoon.reflect.declaration.CtType) CtField(spoon.reflect.declaration.CtField) CtVariable(spoon.reflect.declaration.CtVariable) CtPackage(spoon.reflect.declaration.CtPackage) CtStatementList(spoon.reflect.code.CtStatementList) CtCatch(spoon.reflect.code.CtCatch) CtModifiable(spoon.reflect.declaration.CtModifiable)

Example 5 with CtCatch

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

the class CtCatchVariableReferenceImpl method getDeclaration.

@Override
public CtCatchVariable<T> getDeclaration() {
    CtElement element = this;
    String name = getSimpleName();
    CtCatchVariable var;
    try {
        do {
            CtCatch catchBlock = element.getParent(CtCatch.class);
            if (catchBlock == null) {
                return null;
            }
            var = catchBlock.getParameter();
            element = catchBlock;
        } while (!name.equals(var.getSimpleName()));
    } catch (ParentNotInitializedException e) {
        return null;
    }
    return var;
}
Also used : ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) CtElement(spoon.reflect.declaration.CtElement) CtCatch(spoon.reflect.code.CtCatch) CtCatchVariable(spoon.reflect.code.CtCatchVariable)

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