use of com.sun.tools.javac.file.JavacFileManager in project ceylon-compiler by ceylon.
the class Test method createFileManager.
JavacFileManager createFileManager(boolean useOptimizedZip, boolean useSymbolFile) {
Context c = new Context();
Options options = Options.instance(c);
options.put("useOptimizedZip", Boolean.toString(useOptimizedZip));
if (!useSymbolFile) {
options.put("ignore.symbol.file", "true");
}
return new JavacFileManager(c, false, null);
}
use of com.sun.tools.javac.file.JavacFileManager in project ceylon-compiler by ceylon.
the class LanguageCompiler method getPackage.
private String getPackage(JavaFileObject file) throws IOException {
Iterable<? extends File> prefixes = ((JavacFileManager) fileManager).getLocation(StandardLocation.SOURCE_PATH);
// Figure out the package name by stripping the "-src" prefix and
// extracting
// the package part of the fullname.
String filePath = file.toUri().getPath();
// go absolute
filePath = new File(filePath).getCanonicalPath();
int srcDirLength = 0;
for (File prefixFile : prefixes) {
String prefix = prefixFile.getCanonicalPath();
if (filePath.startsWith(prefix) && prefix.length() > srcDirLength) {
srcDirLength = prefix.length();
}
}
if (srcDirLength > 0) {
String fullname = filePath.substring(srcDirLength);
assert fullname.endsWith(".ceylon");
fullname = fullname.substring(0, fullname.length() - ".ceylon".length());
fullname = fullname.replace(File.separator, ".");
if (fullname.startsWith("."))
fullname = fullname.substring(1);
String packageName = Convert.packagePart(fullname);
if (!packageName.equals(""))
return packageName;
}
return null;
}
use of com.sun.tools.javac.file.JavacFileManager in project ceylon-compiler by ceylon.
the class LanguageCompiler method getSrcDir.
// FIXME: this function is terrible, possibly refactor it with getPackage?
private File getSrcDir(File sourceFile) throws IOException {
Iterable<? extends File> prefixes = ((JavacFileManager) fileManager).getLocation(StandardLocation.SOURCE_PATH);
File srcDirFile = FileUtil.selectPath(prefixes, sourceFile.getPath());
if (srcDirFile != null) {
return srcDirFile;
} else {
// This error should have been caught by the tool chain
throw new RuntimeException(sourceFile.getPath() + " is not in the current source path");
}
}
use of com.sun.tools.javac.file.JavacFileManager in project ceylon-compiler by ceylon.
the class AptJavaCompiler method compile.
/** Main method: compile a list of files, return all compiled classes
* @param filenames The names of all files to be compiled.
*/
public List<ClassSymbol> compile(List<String> filenames, Map<String, String> origOptions, ClassLoader aptCL, AnnotationProcessorFactory providedFactory, java.util.Set<Class<? extends AnnotationProcessorFactory>> productiveFactories, java.util.Set<java.io.File> aggregateGenFiles) throws Throwable {
// it has been used before.
assert !hasBeenUsed : "attempt to reuse JavaCompiler";
hasBeenUsed = true;
this.aggregateGenFiles = aggregateGenFiles;
long msec = System.currentTimeMillis();
ListBuffer<ClassSymbol> classes = new ListBuffer<ClassSymbol>();
try {
JavacFileManager fm = (JavacFileManager) fileManager;
//parse all files
ListBuffer<JCCompilationUnit> trees = new ListBuffer<JCCompilationUnit>();
for (List<String> l = filenames; l.nonEmpty(); l = l.tail) {
if (classesAsDecls) {
if (!l.head.endsWith(".java")) {
// process as class file
ClassSymbol cs = reader.enterClass(names.fromString(l.head));
try {
cs.complete();
} catch (Symbol.CompletionFailure cf) {
bark.aptError("CantFindClass", l);
continue;
}
// add to list of classes
classes.append(cs);
continue;
}
}
JavaFileObject fo = fm.getJavaFileObjectsFromStrings(List.of(l.head)).iterator().next();
trees.append(parse(fo));
}
//enter symbols for all files
List<JCCompilationUnit> roots = trees.toList();
if (errorCount() == 0) {
boolean prev = bark.setDiagnosticsIgnored(true);
try {
enter.main(roots);
} finally {
bark.setDiagnosticsIgnored(prev);
}
}
if (errorCount() == 0) {
apt.main(roots, classes, origOptions, aptCL, providedFactory, productiveFactories);
genSourceFileNames.addAll(apt.getSourceFileNames());
genClassFileNames.addAll(apt.getClassFileNames());
}
} catch (Abort ex) {
}
if (verbose)
log.printVerbose("total", Long.toString(System.currentTimeMillis() - msec));
chk.reportDeferredDiagnostics();
printCount("error", errorCount());
printCount("warn", warningCount());
return classes.toList();
}
use of com.sun.tools.javac.file.JavacFileManager in project ceylon-compiler by ceylon.
the class JavacProcessingEnvironment method handleServiceLoaderUnavailability.
/**
* Returns an empty processor iterator if no processors are on the
* relevant path, otherwise if processors are present, logs an
* error. Called when a service loader is unavailable for some
* reason, either because a service loader class cannot be found
* or because a security policy prevents class loaders from being
* created.
*
* @param key The resource key to use to log an error message
* @param e If non-null, pass this exception to Abort
*/
private Iterator<Processor> handleServiceLoaderUnavailability(String key, Exception e) {
JavaFileManager fileManager = context.get(JavaFileManager.class);
if (fileManager instanceof JavacFileManager) {
StandardJavaFileManager standardFileManager = (JavacFileManager) fileManager;
Iterable<? extends File> workingPath = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH) ? standardFileManager.getLocation(ANNOTATION_PROCESSOR_PATH) : standardFileManager.getLocation(CLASS_PATH);
if (needClassLoader(options.get(PROCESSOR), workingPath))
handleException(key, e);
} else {
handleException(key, e);
}
java.util.List<Processor> pl = Collections.emptyList();
return pl.iterator();
}
Aggregations