use of spoon.reflect.code.CtTry in project spoon by INRIA.
the class TryCatchTest method testCatchOrder.
@Test
public void testCatchOrder() {
Factory factory = createFactory();
// test the order of the model
CtClass<?> clazz = factory.Code().createCodeSnippetStatement("" + "class X {" + "public void foo() {" + " try{}catch(RuntimeException e){java.lang.System.exit(0);}" + " catch(Exception e){}" + "}" + "};").compile();
CtTry tryStmt = (CtTry) clazz.getElements(new TypeFilter<>(CtTry.class)).get(0);
// the first caught exception is RuntimeException
assertEquals(RuntimeException.class, tryStmt.getCatchers().get(0).getParameter().getType().getActualClass());
assertEquals("java.lang.System.exit(0)", tryStmt.getCatchers().get(0).getBody().getStatement(0).toString());
assertEquals(Exception.class, tryStmt.getCatchers().get(1).getParameter().getType().getActualClass());
}
use of spoon.reflect.code.CtTry 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());
}
use of spoon.reflect.code.CtTry in project spoon by INRIA.
the class TryCatchTest method testFullyQualifiedException.
@Test
public void testFullyQualifiedException() {
Factory factory = createFactory();
// test the order of the model
CtClass<?> clazz = factory.Code().createCodeSnippetStatement("" + "class X {" + "public void foo() {" + " try{}catch(java.lang.RuntimeException e){}" + "}};").compile();
CtTry tryStmt = (CtTry) clazz.getElements(new TypeFilter<>(CtTry.class)).get(0);
assertEquals(1, tryStmt.getCatchers().size());
}
use of spoon.reflect.code.CtTry in project spoon by INRIA.
the class TryCatchTest method testRethrowingExceptionsJava7.
@Test
public void testRethrowingExceptionsJava7() throws Exception {
CtClass<?> clazz = build("spoon.test.trycatch", "RethrowingClass");
CtMethod<?> method = (CtMethod<?>) clazz.getMethods().toArray()[0];
Set<CtTypeReference<? extends Throwable>> thrownTypes = method.getThrownTypes();
// Checks we throw 2 exceptions and not one.
assertEquals(2, thrownTypes.size());
CtTry ctTry = clazz.getElements(new TypeFilter<CtTry>(CtTry.class)).get(0);
Class<? extends CtCatchVariableReference> exceptionClass = ctTry.getCatchers().get(0).getParameter().getReference().getClass();
// Checks the exception in the catch isn't on the signature of the method.
for (CtTypeReference<? extends Throwable> thrownType : thrownTypes) {
assertNotEquals(thrownType.getClass(), exceptionClass);
}
}
use of spoon.reflect.code.CtTry 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());
}
Aggregations