Search in sources :

Example 36 with SpoonModelBuilder

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());
    }
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Launcher(spoon.Launcher) File(java.io.File) IOException(java.io.IOException) Test(org.junit.Test)

Example 37 with SpoonModelBuilder

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

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());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Example 39 with SpoonModelBuilder

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());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) SpoonResource(spoon.compiler.SpoonResource) Test(org.junit.Test)

Example 40 with SpoonModelBuilder

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());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Factory(spoon.reflect.factory.Factory) CtParameter(spoon.reflect.declaration.CtParameter) CtInvocation(spoon.reflect.code.CtInvocation) CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

SpoonModelBuilder (spoon.SpoonModelBuilder)45 Launcher (spoon.Launcher)43 Test (org.junit.Test)35 Factory (spoon.reflect.factory.Factory)29 File (java.io.File)17 CtClass (spoon.reflect.declaration.CtClass)16 CtMethod (spoon.reflect.declaration.CtMethod)6 SpoonResource (spoon.compiler.SpoonResource)5 CtInvocation (spoon.reflect.code.CtInvocation)5 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)4 DefaultCoreFactory (spoon.support.DefaultCoreFactory)4 JDTSnippetCompiler (spoon.support.compiler.jdt.JDTSnippetCompiler)4 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)4 Before (org.junit.Before)3 FileNotFoundException (java.io.FileNotFoundException)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 CtParameterRemoveRefactoring (spoon.refactoring.CtParameterRemoveRefactoring)2 CtNewClass (spoon.reflect.code.CtNewClass)2 CtExecutable (spoon.reflect.declaration.CtExecutable)2 FactoryImpl (spoon.reflect.factory.FactoryImpl)2