Search in sources :

Example 41 with SpoonModelBuilder

use of spoon.SpoonModelBuilder in project spoon by INRIA.

the class InterfaceTest method setUp.

@Before
public void setUp() throws Exception {
    final File testDirectory = new File("./src/test/java/spoon/test/interfaces/testclasses/");
    final Launcher launcher = new Launcher();
    this.factory = launcher.createFactory();
    factory.getEnvironment().setComplianceLevel(8);
    SpoonModelBuilder compiler = launcher.createCompiler(this.factory);
    compiler.addInputSource(testDirectory);
    compiler.build();
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Launcher(spoon.Launcher) File(java.io.File) Before(org.junit.Before)

Example 42 with SpoonModelBuilder

use of spoon.SpoonModelBuilder in project spoon by INRIA.

the class ImportScannerTest method testMultiCatchImport.

@Test
public void testMultiCatchImport() throws Exception {
    Launcher spoon = new Launcher();
    Factory factory = spoon.createFactory();
    SpoonModelBuilder compiler = spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/java/spoon/test/imports/testclasses/MultiCatch.java"));
    compiler.build();
    final List<CtClass> classes = Query.getElements(factory, new NamedElementFilter<>(CtClass.class, "MultiCatch"));
    ImportScanner importScanner = new ImportScannerImpl();
    importScanner.computeImports(classes.get(0));
    // as ArithmeticException come from java.lang it is not imported anymore
    // assertTrue( importScanner.isImported( factory.Type().createReference( ArithmeticException.class ) ));
    assertTrue(importScanner.isImported(factory.Type().createReference(AccessControlException.class)));
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtClass(spoon.reflect.declaration.CtClass) ImportScannerImpl(spoon.reflect.visitor.ImportScannerImpl) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) ImportScanner(spoon.reflect.visitor.ImportScanner) MinimalImportScanner(spoon.reflect.visitor.MinimalImportScanner) Test(org.junit.Test)

Example 43 with SpoonModelBuilder

use of spoon.SpoonModelBuilder in project spoon by INRIA.

the class JarTest method testFile.

@Test
public void testFile() throws Exception {
    Launcher launcher = new Launcher();
    SpoonModelBuilder compiler = launcher.createCompiler(launcher.getFactory(), Arrays.asList(SpoonResourceHelper.createFile(new File("./src/test/resources/spoon/test/api/Foo.java"))));
    Assert.assertTrue(compiler.build());
    Assert.assertNotNull(launcher.getFactory().Type().get("Foo"));
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Launcher(spoon.Launcher) VirtualFile(spoon.support.compiler.VirtualFile) File(java.io.File) Test(org.junit.Test)

Example 44 with SpoonModelBuilder

use of spoon.SpoonModelBuilder in project spoon by INRIA.

the class DefaultPrettyPrinterTest method testPrintAClassWithImports.

@Test
public void testPrintAClassWithImports() throws Exception {
    final Launcher launcher = new Launcher();
    final Factory factory = launcher.getFactory();
    factory.getEnvironment().setAutoImports(true);
    final SpoonModelBuilder compiler = launcher.createCompiler();
    compiler.addInputSource(new File("./src/test/java/spoon/test/prettyprinter/testclasses/"));
    compiler.build();
    final String expected = "public class AClass {" + nl + "    public List<?> aMethod() {" + nl + "        return new ArrayList<>();" + nl + "    }" + nl + "" + nl + "    public List<? extends ArrayList> aMethodWithGeneric() {" + nl + "        return new ArrayList<>();" + nl + "    }" + nl + "}";
    final CtClass<?> aClass = (CtClass<?>) factory.Type().get(AClass.class);
    assertEquals(expected, aClass.toString());
    final CtConstructorCall<?> constructorCall = aClass.getElements(new TypeFilter<CtConstructorCall<?>>(CtConstructorCall.class)).get(0);
    final CtTypeReference<?> ctTypeReference = constructorCall.getType().getActualTypeArguments().get(0);
    assertTrue(ctTypeReference.isImplicit());
    assertEquals("Object", ctTypeReference.getSimpleName());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) AClass(spoon.test.prettyprinter.testclasses.AClass) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) File(java.io.File) Test(org.junit.Test)

Example 45 with SpoonModelBuilder

use of spoon.SpoonModelBuilder in project spoon by INRIA.

the class DefaultPrettyPrinterTest method testPrintAMethodWithGeneric.

@Test
public void testPrintAMethodWithGeneric() throws Exception {
    final Launcher launcher = new Launcher();
    final Factory factory = launcher.getFactory();
    factory.getEnvironment().setAutoImports(true);
    final SpoonModelBuilder compiler = launcher.createCompiler();
    compiler.addInputSource(new File("./src/test/java/spoon/test/prettyprinter/testclasses/"));
    compiler.build();
    final CtClass<?> aClass = (CtClass<?>) factory.Type().get(AClass.class);
    final String expected = "public List<? extends ArrayList> aMethodWithGeneric() {" + System.lineSeparator() + "    return new ArrayList<>();" + System.lineSeparator() + "}";
    assertEquals(expected, aClass.getMethodsByName("aMethodWithGeneric").get(0).toString());
    final CtConstructorCall<?> constructorCall = aClass.getElements(new TypeFilter<CtConstructorCall<?>>(CtConstructorCall.class)).get(0);
    final CtTypeReference<?> ctTypeReference = constructorCall.getType().getActualTypeArguments().get(0);
    assertTrue(ctTypeReference.isImplicit());
    assertEquals("Object", ctTypeReference.getSimpleName());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) AClass(spoon.test.prettyprinter.testclasses.AClass) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) File(java.io.File) 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