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