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;
}
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);
}
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());
}
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;
}
}
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;
}
Aggregations