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;
}
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.");
}
}
}
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;
}
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]));
}
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;
}
Aggregations