Search in sources :

Example 6 with CtTry

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());
}
Also used : ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtTry(spoon.reflect.code.CtTry) Test(org.junit.Test)

Example 7 with CtTry

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());
}
Also used : CtTryWithResource(spoon.reflect.code.CtTryWithResource) CtTry(spoon.reflect.code.CtTry) Test(org.junit.Test)

Example 8 with CtTry

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());
}
Also used : ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtTry(spoon.reflect.code.CtTry) Test(org.junit.Test)

Example 9 with CtTry

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);
    }
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtTry(spoon.reflect.code.CtTry) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 10 with CtTry

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

Aggregations

CtTry (spoon.reflect.code.CtTry)20 Test (org.junit.Test)14 Factory (spoon.reflect.factory.Factory)12 CtMethod (spoon.reflect.declaration.CtMethod)6 CtCatch (spoon.reflect.code.CtCatch)5 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)5 Launcher (spoon.Launcher)4 CtInvocation (spoon.reflect.code.CtInvocation)4 CtLocalVariable (spoon.reflect.code.CtLocalVariable)4 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)4 CtParameter (spoon.reflect.declaration.CtParameter)3 CtTypeReference (spoon.reflect.reference.CtTypeReference)3 CtComment (spoon.reflect.code.CtComment)2 CtConstructorCall (spoon.reflect.code.CtConstructorCall)2 CtDo (spoon.reflect.code.CtDo)2 CtFor (spoon.reflect.code.CtFor)2 CtIf (spoon.reflect.code.CtIf)2 CtReturn (spoon.reflect.code.CtReturn)2 CtSwitch (spoon.reflect.code.CtSwitch)2 CtSynchronized (spoon.reflect.code.CtSynchronized)2