use of spoon.SpoonModelBuilder in project spoon by INRIA.
the class TryCatchTest method testCompileMultiTryCatchWithCustomExceptions.
@Test
public void testCompileMultiTryCatchWithCustomExceptions() throws Exception {
spoon.Launcher.main(new String[] { "-i", "src/test/java/spoon/test/trycatch/testclasses", "-o", "target/spooned" });
final Launcher launcher = new Launcher();
final SpoonModelBuilder newCompiler = launcher.createCompiler();
newCompiler.addInputSource(new File("./target/spooned/spoon/test/trycatch/testclasses/"));
try {
assertTrue(newCompiler.build());
} catch (Exception e) {
fail(e.getMessage());
}
}
use of spoon.SpoonModelBuilder 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());
}
use of spoon.SpoonModelBuilder in project spoon by INRIA.
the class ImportTest method testImportOfAnInnerClassInAClassPackage.
@Test
public void testImportOfAnInnerClassInAClassPackage() throws Exception {
Launcher spoon = new Launcher();
spoon.setArgs(new String[] { "--output-type", "nooutput" });
Factory factory = spoon.createFactory();
SpoonModelBuilder compiler = spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/java/spoon/test/imports/testclasses/internal/PublicSuperClass.java", "./src/test/java/spoon/test/imports/testclasses/DefaultClientClass.java"));
compiler.build();
final CtClass<?> client = (CtClass<?>) factory.Type().get("spoon.test.imports.testclasses.DefaultClientClass");
final CtMethod<?> methodVisit = client.getMethodsByName("visit").get(0);
final CtType<Object> innerClass = factory.Type().get("spoon.test.imports.testclasses.DefaultClientClass$InnerClass");
assertEquals("Type of the method must to be InnerClass accessed via DefaultClientClass.", innerClass, methodVisit.getType().getDeclaration());
}
use of spoon.SpoonModelBuilder in project spoon by INRIA.
the class ImportTest method testImportOfAnInnerClassInASuperClassAvailableInLibrary.
@Test
public void testImportOfAnInnerClassInASuperClassAvailableInLibrary() throws Exception {
SpoonModelBuilder comp = new Launcher().createCompiler();
List<SpoonResource> fileToBeSpooned = SpoonResourceHelper.resources("./src/test/resources/visibility/YamlRepresenter.java");
assertEquals(1, fileToBeSpooned.size());
comp.addInputSources(fileToBeSpooned);
List<SpoonResource> classpath = SpoonResourceHelper.resources("./src/test/resources/visibility/snakeyaml-1.9.jar");
assertEquals(1, classpath.size());
comp.setSourceClasspath(classpath.get(0).getPath());
comp.build();
Factory factory = comp.getFactory();
CtType<?> theClass = factory.Type().get("visibility.YamlRepresenter");
final CtClass<?> innerClass = theClass.getNestedType("RepresentConfigurationSection");
String expected = "visibility.YamlRepresenter.RepresentConfigurationSection";
assertEquals(expected, innerClass.getReference().toString());
expected = "org.yaml.snakeyaml.representer.Representer.RepresentMap";
assertEquals(expected, innerClass.getSuperclass().toString());
}
use of spoon.SpoonModelBuilder in project spoon by INRIA.
the class ImportTest method testAnotherMissingImport.
@Test
public void testAnotherMissingImport() throws Exception {
Launcher spoon = new Launcher();
spoon.setArgs(new String[] { "--output-type", "nooutput" });
Factory factory = spoon.createFactory();
factory.getEnvironment().setNoClasspath(true);
factory.getEnvironment().setLevel("OFF");
SpoonModelBuilder compiler = spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/resources/import-resources/fr/inria/AnotherMissingImport.java"));
compiler.build();
List<CtMethod> methods = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "doSomething"));
List<CtParameter> parameters = methods.get(0).getParameters();
CtTypeReference<?> type = parameters.get(0).getType();
assertEquals("SomeType", type.getSimpleName());
assertEquals("externallib", type.getPackage().getSimpleName());
CtMethod<?> mainMethod = factory.Class().getAll().get(0).getMethodsByName("main").get(0);
List<CtStatement> statements = mainMethod.getBody().getStatements();
CtStatement invocationStatement = statements.get(1);
assertTrue(invocationStatement instanceof CtInvocation);
CtInvocation invocation = (CtInvocation) invocationStatement;
CtExecutableReference executableReference = invocation.getExecutable();
assertEquals("doSomething(externallib.SomeType)", executableReference.getSignature());
assertSame(methods.get(0), executableReference.getDeclaration());
}
Aggregations