Search in sources :

Example 21 with SpoonModelBuilder

use of spoon.SpoonModelBuilder in project spoon by INRIA.

the class PackageTest method testAnnotationOnPackage.

@Test
public void testAnnotationOnPackage() throws Exception {
    Launcher launcher = new Launcher();
    Factory factory = launcher.getFactory();
    factory.getEnvironment().setAutoImports(false);
    SpoonModelBuilder compiler = launcher.createCompiler(factory);
    launcher.setSourceOutputDirectory("./target/spooned/");
    compiler.addInputSource(new File("./src/test/java/spoon/test/pkg/testclasses/"));
    compiler.build();
    compiler.generateProcessedSourceFiles(OutputType.CLASSES);
    final SpoonModelBuilder newCompiler = launcher.createCompiler(launcher.createFactory());
    newCompiler.addInputSource(new File("./target/spooned/spoon/test/pkg/testclasses/"));
    try {
        assertTrue(newCompiler.build());
    } catch (Exception ignore) {
        fail();
    }
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) File(java.io.File) Test(org.junit.Test)

Example 22 with SpoonModelBuilder

use of spoon.SpoonModelBuilder in project spoon by INRIA.

the class DefaultPrettyPrinterTest method testPrintAMethodWithImports.

@Test
public void testPrintAMethodWithImports() 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 List<?> aMethod() {" + nl + "    return new ArrayList<>();" + nl + "}";
    final CtClass<?> aClass = (CtClass<?>) factory.Type().get(AClass.class);
    assertEquals(expected, aClass.getMethodsByName("aMethod").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)

Example 23 with SpoonModelBuilder

use of spoon.SpoonModelBuilder in project spoon by INRIA.

the class PropertiesTest method testNonExistingDirectory.

@Test
public void testNonExistingDirectory() throws Exception {
    File tempFile = File.createTempFile("SPOON", "SPOON");
    tempFile.delete();
    Launcher spoon = new Launcher();
    Factory factory = spoon.createFactory();
    SpoonModelBuilder compiler = spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/java/spoon/test/properties/testclasses/Sample.java"));
    compiler.build();
    compiler.instantiateAndProcess(Arrays.asList(SimpleProcessor.class.getName()));
    assertEquals(factory.getEnvironment().getErrorCount(), 0);
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) File(java.io.File) Test(org.junit.Test)

Example 24 with SpoonModelBuilder

use of spoon.SpoonModelBuilder in project spoon by INRIA.

the class VisibilityTest method testVisibilityOfClassesNamedByClassesInJavaLangPackage.

@Test
public void testVisibilityOfClassesNamedByClassesInJavaLangPackage() throws Exception {
    final File sourceOutputDir = new File("target/spooned/spoon/test/visibility_package/testclasses");
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setAutoImports(true);
    launcher.getEnvironment().setSourceOutputDirectory(sourceOutputDir);
    final Factory factory = launcher.getFactory();
    final SpoonModelBuilder compiler = launcher.createCompiler();
    compiler.addInputSource(new File("./src/test/java/spoon/test/visibility/testclasses/"));
    compiler.setSourceOutputDirectory(sourceOutputDir);
    compiler.build();
    compiler.generateProcessedSourceFiles(OutputType.CLASSES);
    // Class must be imported.
    final CtClass<?> aDouble = (CtClass<?>) factory.Type().get(spoon.test.visibility.testclasses.internal.Double.class);
    assertNotNull(aDouble);
    assertEquals(spoon.test.visibility.testclasses.internal.Double.class, aDouble.getActualClass());
    // Class mustn't be imported.
    final CtClass<?> aFloat = (CtClass<?>) factory.Type().get(spoon.test.visibility.testclasses.Float.class);
    assertNotNull(aFloat);
    assertEquals(spoon.test.visibility.testclasses.Float.class, aFloat.getActualClass());
    canBeBuilt(new File("./target/spooned/spoon/test/visibility_package/testclasses/"), 7);
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Factory(spoon.reflect.factory.Factory) CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) File(java.io.File) Test(org.junit.Test)

Example 25 with SpoonModelBuilder

use of spoon.SpoonModelBuilder in project dspot by STAMP-project.

the class DSpotCompiler method compile.

public static boolean compile(String pathToSources, String dependencies, File binaryOutputDirectory) {
    Launcher launcher = new Launcher();
    if (Main.verbose) {
        launcher.getEnvironment().setLevel("DEBUG");
    }
    launcher.getEnvironment().setNoClasspath(true);
    launcher.getEnvironment().setCommentEnabled(true);
    launcher.getEnvironment().setOutputType(OutputType.CLASSES);
    if (!new File("target/dspot/dependencies/compare").exists()) {
        DSpotUtils.copyPackageFromResources("fr/inria/diversify/compare/", "MethodsHandler", "ObjectLog", "Observation", "Utils");
    }
    String[] sourcesArray = (pathToSources + PATH_SEPARATOR).split(PATH_SEPARATOR);
    Arrays.stream(sourcesArray).forEach(launcher::addInputResource);
    String[] dependenciesArray = dependencies.split(PATH_SEPARATOR);
    launcher.getModelBuilder().setSourceClasspath(dependenciesArray);
    launcher.buildModel();
    SpoonModelBuilder modelBuilder = launcher.getModelBuilder();
    modelBuilder.setBinaryOutputDirectory(binaryOutputDirectory);
    return modelBuilder.compile(SpoonModelBuilder.InputType.CTTYPES);
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Launcher(spoon.Launcher) File(java.io.File)

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