Search in sources :

Example 1 with FileSystemFolder

use of spoon.support.compiler.FileSystemFolder in project dspot by STAMP-project.

the class DSpotCompiler method compile.

public boolean compile(String pathToAdditionalDependencies) {
    if (this.factory == null) {
        this.factory = this.launcher.getFactory();
    }
    javaCompliance = factory.getEnvironment().getComplianceLevel();
    // environment);
    DSpotJDTBatchCompiler compiler = new DSpotJDTBatchCompiler(this, null);
    final SourceOptions sourcesOptions = new SourceOptions();
    sourcesOptions.sources((new FileSystemFolder(this.sourceOutputDirectory).getAllJavaFiles()));
    this.reportProblems(this.factory.getEnvironment());
    String[] sourcesArray = this.sourceOutputDirectory.getAbsolutePath().split(PATH_SEPARATOR);
    String[] classpath = (this.dependencies + PATH_SEPARATOR + pathToAdditionalDependencies).split(PATH_SEPARATOR);
    String[] finalClasspath = new String[sourcesArray.length + classpath.length];
    System.arraycopy(sourcesArray, 0, finalClasspath, 0, sourcesArray.length);
    System.arraycopy(classpath, 0, finalClasspath, sourcesArray.length, classpath.length);
    final ClasspathOptions classpathOptions = new ClasspathOptions().encoding(getEnvironment().getEncoding().displayName()).classpath(finalClasspath).binaries(getBinaryOutputDirectory());
    final String[] args = // 
    new JDTBuilderImpl().classpathOptions(// 
    classpathOptions).complianceOptions(// 
    new ComplianceOptions().compliance(javaCompliance)).advancedOptions(// 
    new AdvancedOptions().preserveUnusedVars().continueExecution().enableJavadoc()).annotationProcessingOptions(// 
    new AnnotationProcessingOptions().compileProcessors()).sources(// 
    sourcesOptions).build();
    final String[] finalArgs = new String[args.length + 1];
    finalArgs[0] = "-proceedOnError";
    System.arraycopy(args, 0, finalArgs, 1, args.length);
    compiler.compile(finalArgs);
    environment = compiler.getEnvironment();
    return compiler.globalErrorsCount == 0;
}
Also used : FileSystemFolder(spoon.support.compiler.FileSystemFolder)

Example 2 with FileSystemFolder

use of spoon.support.compiler.FileSystemFolder in project spoon by INRIA.

the class StandardEnvironment method verifySourceClasspath.

private void verifySourceClasspath(String[] sourceClasspath) throws InvalidClassPathException {
    for (String classPathElem : sourceClasspath) {
        // preconditions
        File classOrJarFolder = new File(classPathElem);
        if (!classOrJarFolder.exists()) {
            throw new InvalidClassPathException(classPathElem + " does not exist, it is not a valid folder");
        }
        if (classOrJarFolder.isDirectory()) {
            // it should not contain a java file
            SpoonFolder tmp = new FileSystemFolder(classOrJarFolder);
            List<SpoonFile> javaFiles = tmp.getAllJavaFiles();
            if (javaFiles.size() > 0) {
                logger.warn("You're trying to give source code in the classpath, this should be given to " + "addInputSource " + javaFiles);
            }
        } else if (classOrJarFolder.getName().endsWith(".class")) {
            throw new InvalidClassPathException(".class files are not accepted in source classpath.");
        }
    }
}
Also used : InvalidClassPathException(spoon.compiler.InvalidClassPathException) SpoonFolder(spoon.compiler.SpoonFolder) FileSystemFolder(spoon.support.compiler.FileSystemFolder) File(java.io.File) SpoonFile(spoon.compiler.SpoonFile) SpoonFile(spoon.compiler.SpoonFile)

Example 3 with FileSystemFolder

use of spoon.support.compiler.FileSystemFolder in project spoon by INRIA.

the class CompilationTest method testSingleClassLoader.

@Test
public void testSingleClassLoader() throws Exception {
    /*
		 *  contract: the environment exposes a classloader configured by the spoonclass path, 
		 *  there is one class loader, so the loaded classes are compatible
		 */
    Launcher launcher = new Launcher();
    launcher.addInputResource(new FileSystemFolder("./src/test/resources/classloader-test"));
    File outputBinDirectory = new File("./target/classloader-test");
    if (!outputBinDirectory.exists()) {
        outputBinDirectory.mkdirs();
    }
    launcher.setBinaryOutputDirectory(outputBinDirectory);
    launcher.getModelBuilder().build();
    CtTypeReference<?> mIFoo = launcher.getFactory().Type().createReference("spoontest.IFoo");
    CtTypeReference<?> mFoo = launcher.getFactory().Type().createReference("spoontest.Foo");
    assertTrue("Foo subtype of IFoo", mFoo.isSubtypeOf(mIFoo));
    launcher.getModelBuilder().compile(SpoonModelBuilder.InputType.FILES);
    // Create new launcher which uses classes compiled by previous launcher.
    // It simulates the classes without sources, which has to be accessed using reflection
    launcher = new Launcher();
    // not in the classpath
    try {
        Class.forName("spoontest.IFoo");
        fail();
    } catch (ClassNotFoundException expected) {
    }
    // not in the spoon classpath before setting it
    try {
        launcher.getEnvironment().getInputClassLoader().loadClass("spoontest.IFoo");
        fail();
    } catch (ClassNotFoundException expected) {
    }
    launcher.getEnvironment().setSourceClasspath(new String[] { outputBinDirectory.getAbsolutePath() });
    mIFoo = launcher.getFactory().Type().createReference("spoontest.IFoo");
    mFoo = launcher.getFactory().Type().createReference("spoontest.Foo");
    // if it fails then it is because each class is loaded by different class loader
    assertTrue("Foo subtype of IFoo", mFoo.isSubtypeOf(mIFoo));
    // not in the spoon classpath before setting it
    Class<?> ifoo = launcher.getEnvironment().getInputClassLoader().loadClass("spoontest.IFoo");
    Class<?> foo = launcher.getEnvironment().getInputClassLoader().loadClass("spoontest.Foo");
    assertTrue(ifoo.isAssignableFrom(foo));
    assertTrue(ifoo.getClassLoader() == foo.getClassLoader());
}
Also used : Launcher(spoon.Launcher) FileSystemFolder(spoon.support.compiler.FileSystemFolder) File(java.io.File) SpoonClassNotFoundException(spoon.support.SpoonClassNotFoundException) Test(org.junit.Test)

Example 4 with FileSystemFolder

use of spoon.support.compiler.FileSystemFolder in project spoon by INRIA.

the class SpoonMetaModel method createFactory.

private static Factory createFactory(File spoonJavaSourcesDirectory) {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.getEnvironment().setCommentEnabled(true);
    // // Spoon model interfaces
    Arrays.asList("spoon/reflect/code", "spoon/reflect/declaration", "spoon/reflect/reference", "spoon/support/reflect/declaration", "spoon/support/reflect/code", "spoon/support/reflect/reference").forEach(path -> {
        launcher.addInputResource(new FileSystemFolder(new File(spoonJavaSourcesDirectory, path)));
    });
    launcher.buildModel();
    return launcher.getFactory();
}
Also used : Launcher(spoon.Launcher) FileSystemFolder(spoon.support.compiler.FileSystemFolder) File(java.io.File)

Example 5 with FileSystemFolder

use of spoon.support.compiler.FileSystemFolder in project spoon by INRIA.

the class ResourceTest method testFilteringFolder.

@Test
public void testFilteringFolder() throws Exception {
    // contract: one can filter input files
    // the core of this test is the removeIfMatches at the end
    // all files
    SpoonModelBuilder mb = new Launcher().getModelBuilder();
    FilteringFolder resources = new FilteringFolder();
    resources.addFolder(new FileSystemFolder("src/test/java/spoon/test/visibility/"));
    mb.addInputSource(resources);
    mb.build();
    int nbAll = mb.getFactory().getModel().getAllTypes().size();
    assertEquals(12, nbAll);
    SpoonModelBuilder mb2 = new Launcher().getModelBuilder();
    FilteringFolder resources2 = new FilteringFolder();
    resources2.addFolder(new FileSystemFolder("src/test/java/spoon/test/visibility/packageprotected/"));
    mb2.addInputSource(resources2);
    mb2.build();
    int nbPackageProtected = mb2.getFactory().getModel().getAllTypes().size();
    assertEquals(2, nbPackageProtected);
    // now the core of this test
    SpoonModelBuilder mb3 = new Launcher().getModelBuilder();
    FilteringFolder resources3 = new FilteringFolder();
    resources3.addFolder(new FileSystemFolder("src/test/java/spoon/test/visibility/"));
    // we remove a number of input resources
    resources3.removeAllThatMatch(".*packageprotected.*");
    mb3.addInputSource(resources3);
    mb3.build();
    assertEquals(nbAll - nbPackageProtected, mb3.getFactory().getModel().getAllTypes().size());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Launcher(spoon.Launcher) FilteringFolder(spoon.support.compiler.FilteringFolder) FileSystemFolder(spoon.support.compiler.FileSystemFolder) Test(org.junit.Test)

Aggregations

FileSystemFolder (spoon.support.compiler.FileSystemFolder)9 File (java.io.File)6 Test (org.junit.Test)6 Launcher (spoon.Launcher)4 SpoonFolder (spoon.compiler.SpoonFolder)3 FileSystemFile (spoon.support.compiler.FileSystemFile)2 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 LauncherTest (spoon.LauncherTest)1 SpoonModelBuilder (spoon.SpoonModelBuilder)1 InvalidClassPathException (spoon.compiler.InvalidClassPathException)1 SpoonFile (spoon.compiler.SpoonFile)1 CtEnum (spoon.reflect.declaration.CtEnum)1 Factory (spoon.reflect.factory.Factory)1 SpoonClassNotFoundException (spoon.support.SpoonClassNotFoundException)1 FilteringFolder (spoon.support.compiler.FilteringFolder)1 VirtualFolder (spoon.support.compiler.VirtualFolder)1