Search in sources :

Example 1 with SpoonFile

use of spoon.compiler.SpoonFile in project spoon by INRIA.

the class JDTBasedSpoonCompiler method buildUnits.

/**
 * Build the CompilationUnit found in the source folder
 * @param jdtBuilder The instance of JDTBuilder to prepare the right JDT arguments
 * @param sourcesFolder The source folder
 * @param classpath The complete classpath
 * @param debugMessagePrefix Useful to help debugging
 * @return All compilationUnitDeclaration from JDT found in source folder
 */
protected CompilationUnitDeclaration[] buildUnits(JDTBuilder jdtBuilder, SpoonFolder sourcesFolder, String[] classpath, String debugMessagePrefix) {
    List<SpoonFile> sourceFiles = Collections.unmodifiableList(sourcesFolder.getAllJavaFiles());
    if (sourceFiles.isEmpty()) {
        return EMPTY_RESULT;
    }
    JDTBatchCompiler batchCompiler = createBatchCompiler(new FileCompilerConfig(sourceFiles));
    String[] args;
    if (jdtBuilder == null) {
        args = // 
        new JDTBuilderImpl().classpathOptions(// 
        new ClasspathOptions().encoding(this.getEnvironment().getEncoding().displayName()).classpath(classpath)).complianceOptions(// 
        new ComplianceOptions().compliance(javaCompliance)).advancedOptions(// 
        new AdvancedOptions().preserveUnusedVars().continueExecution().enableJavadoc()).sources(// no sources, handled by the JDTBatchCompiler
        new SourceOptions().sources(sourceFiles)).build();
    } else {
        args = jdtBuilder.build();
    }
    getFactory().getEnvironment().debugMessage(debugMessagePrefix + "build args: " + Arrays.toString(args));
    batchCompiler.configure(args);
    CompilationUnitDeclaration[] units = batchCompiler.getUnits();
    return units;
}
Also used : SourceOptions(spoon.compiler.builder.SourceOptions) ComplianceOptions(spoon.compiler.builder.ComplianceOptions) SpoonFile(spoon.compiler.SpoonFile) ClasspathOptions(spoon.compiler.builder.ClasspathOptions) AdvancedOptions(spoon.compiler.builder.AdvancedOptions) CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) JDTBuilderImpl(spoon.compiler.builder.JDTBuilderImpl)

Example 2 with SpoonFile

use of spoon.compiler.SpoonFile 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 SpoonFile

use of spoon.compiler.SpoonFile in project spoon by INRIA.

the class FileSystemFolder method getFiles.

public List<SpoonFile> getFiles() {
    List<SpoonFile> files;
    files = new ArrayList<>();
    for (File f : file.listFiles()) {
        if (SpoonResourceHelper.isFile(f)) {
            files.add(new FileSystemFile(f));
        }
    }
    return files;
}
Also used : SpoonFile(spoon.compiler.SpoonFile) SpoonFile(spoon.compiler.SpoonFile) File(java.io.File)

Example 4 with SpoonFile

use of spoon.compiler.SpoonFile in project spoon by INRIA.

the class FileCompilerConfig method initializeCompiler.

@Override
public void initializeCompiler(JDTBatchCompiler compiler) {
    JDTBasedSpoonCompiler jdtCompiler = compiler.getJdtCompiler();
    List<CompilationUnit> cuList = new ArrayList<>();
    InputStream inputStream = null;
    try {
        for (SpoonFile f : getFiles(compiler)) {
            if (compiler.filesToBeIgnored.contains(f.getPath())) {
                continue;
            }
            String fName = f.isActualFile() ? f.getPath() : f.getName();
            inputStream = f.getContent();
            char[] content = IOUtils.toCharArray(inputStream, jdtCompiler.getEnvironment().getEncoding());
            cuList.add(new CompilationUnit(content, fName, null));
            IOUtils.closeQuietly(inputStream);
        }
    } catch (Exception e) {
        IOUtils.closeQuietly(inputStream);
        throw new SpoonException(e);
    }
    compiler.setCompilationUnits(cuList.toArray(new CompilationUnit[0]));
}
Also used : CompilationUnit(org.eclipse.jdt.internal.compiler.batch.CompilationUnit) SpoonException(spoon.SpoonException) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) SpoonFile(spoon.compiler.SpoonFile) SpoonException(spoon.SpoonException)

Example 5 with SpoonFile

use of spoon.compiler.SpoonFile in project spoon by INRIA.

the class JDTSnippetCompiler method build.

@Override
public boolean build(JDTBuilder builder) {
    if (factory == null) {
        throw new SpoonException("Factory not initialized");
    }
    boolean srcSuccess;
    List<SpoonFile> allFiles = sources.getAllJavaFiles();
    factory.getEnvironment().debugMessage("compiling sources: " + allFiles);
    long t = System.currentTimeMillis();
    javaCompliance = factory.getEnvironment().getComplianceLevel();
    try {
        srcSuccess = buildSources(builder);
    } finally {
        // remove snippet compilation unit from the cache (to clear memory) and remember it so client can use it
        for (SpoonFile spoonFile : allFiles) {
            if (spoonFile.getName().startsWith(SNIPPET_FILENAME_PREFIX)) {
                snippetCompilationUnit = factory.CompilationUnit().removeFromCache(spoonFile.getName());
            }
        }
    }
    reportProblems(factory.getEnvironment());
    factory.getEnvironment().debugMessage("compiled in " + (System.currentTimeMillis() - t) + " ms");
    return srcSuccess;
}
Also used : SpoonException(spoon.SpoonException) SpoonFile(spoon.compiler.SpoonFile)

Aggregations

SpoonFile (spoon.compiler.SpoonFile)6 File (java.io.File)3 SpoonException (spoon.SpoonException)2 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 CompilationUnitDeclaration (org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)1 CompilationUnit (org.eclipse.jdt.internal.compiler.batch.CompilationUnit)1 InvalidClassPathException (spoon.compiler.InvalidClassPathException)1 SpoonFolder (spoon.compiler.SpoonFolder)1 AdvancedOptions (spoon.compiler.builder.AdvancedOptions)1 ClasspathOptions (spoon.compiler.builder.ClasspathOptions)1 ComplianceOptions (spoon.compiler.builder.ComplianceOptions)1 JDTBuilderImpl (spoon.compiler.builder.JDTBuilderImpl)1 SourceOptions (spoon.compiler.builder.SourceOptions)1 FileSystemFolder (spoon.support.compiler.FileSystemFolder)1