use of org.eclipse.ceylon.javax.tools.JavaFileObject in project ceylon by eclipse.
the class PathFileObject method getCharContent.
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
CharBuffer cb = fileManager.getCachedContent(this);
if (cb == null) {
InputStream in = openInputStream();
try {
ByteBuffer bb = fileManager.makeByteBuffer(in);
JavaFileObject prev = fileManager.log.useSource(this);
try {
cb = fileManager.decode(bb, ignoreEncodingErrors);
} finally {
fileManager.log.useSource(prev);
}
fileManager.recycleByteBuffer(bb);
if (!ignoreEncodingErrors) {
fileManager.cache(this, cb);
}
} finally {
in.close();
}
}
return cb;
}
use of org.eclipse.ceylon.javax.tools.JavaFileObject in project ceylon by eclipse.
the class JavacMessager method printMessage.
/**
* Prints a message of the specified kind at the location of the
* annotation value inside the annotation mirror of the annotated
* element.
*
* @param kind the kind of message
* @param msg the message, or an empty string if none
* @param e the annotated element
* @param a the annotation containing the annotaiton value
* @param v the annotation value to use as a position hint
*/
public void printMessage(Diagnostic.Kind kind, CharSequence msg, Element e, AnnotationMirror a, AnnotationValue v) {
JavaFileObject oldSource = null;
JavaFileObject newSource = null;
JCDiagnostic.DiagnosticPosition pos = null;
JavacElements elemUtils = processingEnv.getElementUtils();
Pair<JCTree, JCCompilationUnit> treeTop = elemUtils.getTreeAndTopLevel(e, a, v);
if (treeTop != null) {
newSource = treeTop.snd.sourcefile;
if (newSource != null) {
// save the old version and reinstate it later
oldSource = log.useSource(newSource);
pos = treeTop.fst.pos();
}
}
try {
switch(kind) {
case ERROR:
errorCount++;
boolean prev = log.multipleErrors;
log.multipleErrors = true;
try {
log.error(pos, "proc.messager", msg.toString());
} finally {
log.multipleErrors = prev;
}
break;
case WARNING:
warningCount++;
log.warning(pos, "proc.messager", msg.toString());
break;
case MANDATORY_WARNING:
warningCount++;
log.mandatoryWarning(pos, "proc.messager", msg.toString());
break;
default:
log.note(pos, "proc.messager", msg.toString());
break;
}
} finally {
// reinstate the saved version, only if it was saved earlier
if (newSource != null)
log.useSource(oldSource);
}
}
use of org.eclipse.ceylon.javax.tools.JavaFileObject in project ceylon by eclipse.
the class JavacProcessingEnvironment method doProcessing.
// TODO: internal catch clauses?; catch and rethrow an annotation
// processing error
public JavaCompiler doProcessing(Context context, List<JCCompilationUnit> roots, List<ClassSymbol> classSymbols, Iterable<? extends PackageSymbol> pckSymbols, Log.DeferredDiagnosticHandler deferredDiagnosticHandler) {
log = Log.instance(context);
Set<PackageSymbol> specifiedPackages = new LinkedHashSet<PackageSymbol>();
for (PackageSymbol psym : pckSymbols) specifiedPackages.add(psym);
this.specifiedPackages = Collections.unmodifiableSet(specifiedPackages);
Round round = new Round(context, roots, classSymbols, deferredDiagnosticHandler);
boolean errorStatus;
boolean moreToDo;
do {
// Run processors for round n
round.run(false, false);
// Processors for round n have run to completion.
// Check for errors and whether there is more work to do.
errorStatus = round.unrecoverableError();
moreToDo = moreToDo();
round.showDiagnostics(errorStatus || showResolveErrors);
// Set up next round.
// Copy mutable collections returned from filer.
round = round.next(new LinkedHashSet<JavaFileObject>(filer.getGeneratedSourceFileObjects()), new LinkedHashMap<String, JavaFileObject>(filer.getGeneratedClasses()));
// Check for errors during setup.
if (round.unrecoverableError())
errorStatus = true;
} while (moreToDo && !errorStatus);
// run last round
round.run(true, errorStatus);
round.showDiagnostics(true);
filer.warnIfUnclosedFiles();
warnIfUnmatchedOptions();
/*
* If an annotation processor raises an error in a round,
* that round runs to completion and one last round occurs.
* The last round may also occur because no more source or
* class files have been generated. Therefore, if an error
* was raised on either of the last *two* rounds, the compile
* should exit with a nonzero exit code. The current value of
* errorStatus holds whether or not an error was raised on the
* second to last round; errorRaised() gives the error status
* of the last round.
*/
if (messager.errorRaised() || werror && round.warningCount() > 0 && round.errorCount() > 0)
errorStatus = true;
Set<JavaFileObject> newSourceFiles = new LinkedHashSet<JavaFileObject>(filer.getGeneratedSourceFileObjects());
roots = cleanTrees(round.roots);
JavaCompiler compiler = round.finalCompiler();
// Ceylon: we need to call parseFiles even if we did not add anything, to reset
// module stuff
// if (newSourceFiles.size() > 0)
roots = roots.appendList(compiler.parseFiles(newSourceFiles));
errorStatus = errorStatus || (compiler.errorCount() > 0);
// Free resources
this.close();
if (!taskListener.isEmpty())
taskListener.finished(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING));
if (errorStatus) {
if (compiler.errorCount() == 0)
compiler.log.nerrors++;
return compiler;
}
compiler.enterTreesIfNeeded(roots);
return compiler;
}
use of org.eclipse.ceylon.javax.tools.JavaFileObject in project ceylon by eclipse.
the class MandatoryWarningHandler method report.
/**
* Report a mandatory warning.
*/
public void report(DiagnosticPosition pos, String msg, Object... args) {
JavaFileObject currentSource = log.currentSourceFile();
if (verbose) {
if (sourcesWithReportedWarnings == null)
sourcesWithReportedWarnings = new HashSet<JavaFileObject>();
if (log.nwarnings < log.MaxWarnings) {
// generate message and remember the source file
logMandatoryWarning(pos, msg, args);
sourcesWithReportedWarnings.add(currentSource);
} else if (deferredDiagnosticKind == null) {
// set up deferred message
if (sourcesWithReportedWarnings.contains(currentSource)) {
// more errors in a file that already has reported warnings
deferredDiagnosticKind = DeferredDiagnosticKind.ADDITIONAL_IN_FILE;
} else {
// warnings in a new source file
deferredDiagnosticKind = DeferredDiagnosticKind.IN_FILE;
}
deferredDiagnosticSource = currentSource;
deferredDiagnosticArg = currentSource;
} else if ((deferredDiagnosticKind == DeferredDiagnosticKind.IN_FILE || deferredDiagnosticKind == DeferredDiagnosticKind.ADDITIONAL_IN_FILE) && !equal(deferredDiagnosticSource, currentSource)) {
// additional errors in more than one source file
deferredDiagnosticKind = DeferredDiagnosticKind.ADDITIONAL_IN_FILES;
deferredDiagnosticArg = null;
}
} else {
if (deferredDiagnosticKind == null) {
// warnings in a single source
deferredDiagnosticKind = DeferredDiagnosticKind.IN_FILE;
deferredDiagnosticSource = currentSource;
deferredDiagnosticArg = currentSource;
} else if (deferredDiagnosticKind == DeferredDiagnosticKind.IN_FILE && !equal(deferredDiagnosticSource, currentSource)) {
// warnings in multiple source files
deferredDiagnosticKind = DeferredDiagnosticKind.IN_FILES;
deferredDiagnosticArg = null;
}
}
}
use of org.eclipse.ceylon.javax.tools.JavaFileObject in project ceylon by eclipse.
the class LanguageCompiler method addModuleDescriptors.
// This is a bit of a hack, but if we got passed a list of resources
// without any accompaning source files we'll not be able to determine
// the module to which the resource files belong. So to try to fix that
// we see if a module file exists in the source folders and add it to
// the list of source files
private List<JavaFileObject> addModuleDescriptors(List<JavaFileObject> sourceFiles, List<JavaFileObject> resourceFiles) {
List<JavaFileObject> result = sourceFiles;
JavacFileManager dfm = (JavacFileManager) fileManager;
for (JavaFileObject fo : resourceFiles) {
String resName = JarUtils.toPlatformIndependentPath(dfm.getLocation(CeylonLocation.RESOURCE_PATH), fo.getName());
JavaFileObject moduleFile = findModuleDescriptorForFile(new File(resName));
if (moduleFile != null && !result.contains(moduleFile)) {
result = result.append(moduleFile);
}
}
return result;
}
Aggregations