Search in sources :

Example 16 with CtTry

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

the class TemplateTest method testExtensionBlock.

@Test
public void testExtensionBlock() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.addInputResource("./src/test/java/spoon/test/template/testclasses/logger/Logger.java");
    launcher.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/logger/LoggerTemplate.java"));
    launcher.addProcessor(new LoggerTemplateProcessor());
    launcher.getEnvironment().setSourceClasspath(System.getProperty("java.class.path").split(File.pathSeparator));
    try {
        launcher.run();
    } catch (ClassCastException ignored) {
        fail();
    }
    final CtClass<Logger> aLogger = launcher.getFactory().Class().get(Logger.class);
    final CtMethod aMethod = aLogger.getMethodsByName("enter").get(0);
    assertTrue(aMethod.getBody().getStatement(0) instanceof CtTry);
    final CtTry aTry = (CtTry) aMethod.getBody().getStatement(0);
    assertTrue(aTry.getFinalizer().getStatement(0) instanceof CtInvocation);
    assertEquals("spoon.test.template.testclasses.logger.Logger.exit(\"enter\")", aTry.getFinalizer().getStatement(0).toString());
    assertTrue(aTry.getBody().getStatement(0) instanceof CtInvocation);
    assertEquals("spoon.test.template.testclasses.logger.Logger.enter(\"Logger\", \"enter\")", aTry.getBody().getStatement(0).toString());
    assertTrue(aTry.getBody().getStatements().size() > 1);
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) LoggerTemplateProcessor(spoon.test.template.testclasses.logger.LoggerTemplateProcessor) Launcher(spoon.Launcher) FileSystemFile(spoon.support.compiler.FileSystemFile) Logger(spoon.test.template.testclasses.logger.Logger) CtTry(spoon.reflect.code.CtTry) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 17 with CtTry

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

the class TryCatchTest method testCatchWithExplicitFinalVariable.

@Test
public void testCatchWithExplicitFinalVariable() throws IOException {
    // contract: explicit final modifier are possible in catch variables and should be kept when pretty-printing
    String inputResource = "./src/test/java/spoon/test/trycatch/testclasses/Bar.java";
    Launcher launcher = new Launcher();
    launcher.addInputResource(inputResource);
    launcher.getEnvironment().setComplianceLevel(5);
    launcher.buildModel();
    CtTry tryStmt = launcher.getModel().getElements(new TypeFilter<>(CtTry.class)).get(0);
    List<CtCatch> catchers = tryStmt.getCatchers();
    assertEquals(1, catchers.size());
    CtCatchVariable<?> catchVariable = catchers.get(0).getParameter();
    assertTrue(catchVariable.hasModifier(ModifierKind.FINAL));
    Set<CtExtendedModifier> extendedModifierSet = catchVariable.getExtendedModifiers();
    assertEquals(1, extendedModifierSet.size());
    assertEquals(new CtExtendedModifier(ModifierKind.FINAL, false), extendedModifierSet.iterator().next());
    launcher = new Launcher();
    launcher.addInputResource(inputResource);
    launcher.setSourceOutputDirectory("./target/spoon-trycatch");
    launcher.getEnvironment().setShouldCompile(true);
    launcher.getEnvironment().setComplianceLevel(5);
    launcher.run();
    File f = new File("target/spoon-trycatch/spoon/test/trycatch/testclasses/Bar.java");
    String content = StringUtils.join(Files.readAllLines(f.toPath()), "\n");
    assertTrue(content.contains("catch (final java.lang.Exception e)"));
}
Also used : Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtCatch(spoon.reflect.code.CtCatch) CtTry(spoon.reflect.code.CtTry) CtExtendedModifier(spoon.support.reflect.CtExtendedModifier) File(java.io.File) Test(org.junit.Test)

Example 18 with CtTry

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

the class TryCatchTest method testExceptionJava7.

@Test
public void testExceptionJava7() {
    Factory factory = createFactory();
    CtClass<?> clazz = factory.Code().createCodeSnippetStatement("" + "class X {" + "public void foo() {" + " try{}catch(RuntimeException | Error 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());
    assertEquals(Throwable.class, catchers.get(0).getParameter().getType().getActualClass());
    assertEquals(2, catchers.get(0).getParameter().getMultiTypes().size());
    assertEquals(RuntimeException.class, catchers.get(0).getParameter().getMultiTypes().get(0).getActualClass());
    assertEquals(Error.class, catchers.get(0).getParameter().getMultiTypes().get(1).getActualClass());
    // the code of the catch block is duplicated
    assertEquals("java.lang.System.exit(0)", catchers.get(0).getBody().getStatement(0).toString());
}
Also used : 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 19 with CtTry

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

the class MainGenerator method createLargeTryCatchBlock.

private static CtTry createLargeTryCatchBlock(Set<CtTypeReference<? extends Throwable>> thrownTypes, Factory factory) {
    final CtTry aTry = factory.createTry();
    thrownTypes.stream().forEach(ctTypeReference -> addCatchGivenExceptionToTry(ctTypeReference, factory, aTry, ctTypeReference.getSimpleName()));
    return aTry;
}
Also used : CtTry(spoon.reflect.code.CtTry)

Example 20 with CtTry

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

the class MainGenerator method removeNonStaticElement.

private static void removeNonStaticElement(final CtMethod<?> mainMethod, final CtType testClass) {
    final CtBlock<?> body = mainMethod.getBody();
    final Factory factory = mainMethod.getFactory();
    // 1 create a local variable of the test class
    final CtLocalVariable localVariableOfTestClass = factory.createLocalVariable(testClass.getReference(), Character.toLowerCase(testClass.getSimpleName().charAt(0)) + testClass.getSimpleName().substring(1), factory.createConstructorCall(testClass.getReference()));
    // 2 invoke setUp(@Before) at the begin if present
    final CtTry wrappedBefore = wrapInTryCatchMethodWithSpecificAnnotation(testClass, factory, localVariableOfTestClass, "org.junit.Before");
    if (wrappedBefore != null) {
        body.insertBegin(wrappedBefore);
    }
    // 3 invoke tearDown(@After) at the end of the block
    final CtTry wrappedAfter = wrapInTryCatchMethodWithSpecificAnnotation(testClass, factory, localVariableOfTestClass, "org.junit.After");
    if (wrappedAfter != null) {
        body.insertEnd(wrappedAfter);
    }
    // 4 replaces all non-static accesses to accesses on the local variable created at the first step
    body.getElements(new TypeFilter<CtTargetedExpression>(CtTargetedExpression.class) {

        @Override
        public boolean matches(CtTargetedExpression element) {
            return element.getTarget() instanceof CtThisAccess;
        }
    }).stream().map(CtTargetedExpression::getTarget).forEach(target -> target.replace(factory.createVariableRead(localVariableOfTestClass.getReference(), false)));
    body.insertBegin(localVariableOfTestClass);
}
Also used : Factory(spoon.reflect.factory.Factory) CtTargetedExpression(spoon.reflect.code.CtTargetedExpression) CtThisAccess(spoon.reflect.code.CtThisAccess) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtTry(spoon.reflect.code.CtTry) CtLocalVariable(spoon.reflect.code.CtLocalVariable)

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