Search in sources :

Example 16 with CtCatch

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

the class TryCatchTest method testMultiTryCatchWithCustomExceptions.

@Test
public void testMultiTryCatchWithCustomExceptions() throws Exception {
    final Launcher launcher = new Launcher();
    final SpoonModelBuilder compiler = launcher.createCompiler();
    compiler.addInputSource(new File("./src/test/java/spoon/test/trycatch/testclasses/"));
    compiler.build();
    Factory factory = compiler.getFactory();
    final CtClass<?> foo = (CtClass<?>) factory.Type().get(Foo.class);
    final CtCatch ctCatch = foo.getElements(new AbstractFilter<CtCatch>(CtCatch.class) {

        @Override
        public boolean matches(CtCatch element) {
            return true;
        }
    }).get(0);
    final String expected = "catch (spoon.test.trycatch.testclasses.internal.MyException | spoon.test.trycatch.testclasses.internal.MyException2 ignore) {" + System.lineSeparator() + "}";
    assertEquals(expected, ctCatch.toString());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtClass(spoon.reflect.declaration.CtClass) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) Foo(spoon.test.trycatch.testclasses.Foo) Launcher(spoon.Launcher) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtCatch(spoon.reflect.code.CtCatch) File(java.io.File) Test(org.junit.Test)

Example 17 with CtCatch

use of spoon.reflect.code.CtCatch in project Ex2Amplifier by STAMP-project.

the class MainGenerator method addCatchGivenExceptionToTry.

private static void addCatchGivenExceptionToTry(CtTypeReference exceptionType, Factory factory, CtTry aTry, String suffixNameException) {
    final CtCatch aCatch = factory.createCatch();
    final CtCatchVariable catchVariable = factory.createCatchVariable();
    catchVariable.setSimpleName("__exceptionEx2Amplifier" + suffixNameException);
    catchVariable.setType(exceptionType);
    aCatch.setParameter(catchVariable);
    final CtThrow aThrow = factory.createThrow();
    aThrow.setThrownExpression(factory.createConstructorCall(factory.Type().createReference("java.lang.RuntimeException"), factory.createVariableRead(factory.createCatchVariableReference(catchVariable), false)));
    aCatch.setBody(aThrow);
    aTry.addCatcher(aCatch);
}
Also used : CtThrow(spoon.reflect.code.CtThrow) 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