use of com.sun.tools.javac.util.ClientCodeException in project intellij-community by JetBrains.
the class JavacReferenceCollectorListener method finished.
@Override
public void finished(TaskEvent e) {
// Should be initialized only when JavaCompiler was created (jdk 6-7).
// Otherwise JavacReferenceCollectorListener will not be loaded to javac Context.
initializeUtilitiesIfNeeded();
try {
if (e.getKind() == TaskEvent.Kind.ANALYZE) {
// javac creates an event on each processed top level declared class not file
final CompilationUnitTree unit = e.getCompilationUnit();
final String fileName = e.getSourceFile().getName();
Tree declarationToProcess = myTreeUtility.getTree(e.getTypeElement());
boolean collectImportsData;
boolean addedToCache = true;
ReferenceCollector incompletelyProcessedFile = myIncompletelyProcessedFiles.get(fileName);
if (incompletelyProcessedFile == null) {
final int declarationCount = unit.getTypeDecls().size();
incompletelyProcessedFile = new ReferenceCollector(declarationCount, fileName, unit);
if (declarationCount == 1 && declarationToProcess != null) {
addedToCache = false;
} else {
myIncompletelyProcessedFiles.put(fileName, incompletelyProcessedFile);
}
collectImportsData = true;
} else {
collectImportsData = false;
}
final boolean isFileDataComplete;
if (incompletelyProcessedFile.decrementRemainDeclarationsAndGet(declarationToProcess) == 0) {
if (addedToCache) {
myIncompletelyProcessedFiles.remove(fileName);
}
isFileDataComplete = true;
} else {
isFileDataComplete = false;
}
if (collectImportsData) {
scanImports(unit, incompletelyProcessedFile.myFileData.getRefs(), incompletelyProcessedFile);
if (myDivideImportRefs) {
scanImports(unit, incompletelyProcessedFile.myFileData.getImportRefs(), incompletelyProcessedFile);
}
}
myAstScanner.scan(declarationToProcess, incompletelyProcessedFile);
if (isFileDataComplete) {
for (AnnotationTree annotation : unit.getPackageAnnotations()) {
myAstScanner.scan(annotation, incompletelyProcessedFile);
}
myDataConsumer.consume(incompletelyProcessedFile.myFileData);
}
}
} catch (Exception ex) {
throw new ClientCodeException(ex);
}
}
use of com.sun.tools.javac.util.ClientCodeException in project ceylon-compiler by ceylon.
the class Main method compile.
/**
* Programmatic interface for main function.
* @param args The command line parameters.
*/
public int compile(String[] args, Context context, List<JavaFileObject> fileObjects, Iterable<? extends Processor> processors) {
if (options == null) {
// creates a new one
options = Options.instance(context);
}
filenames = new ListBuffer<File>();
classnames = new ListBuffer<String>();
exitState = null;
JavaCompiler comp = null;
/* TODO: Logic below about what is an acceptable command line should be
* updated to take annotation processing semantics into account. */
try {
if (args.length == 0 && fileObjects.isEmpty()) {
help();
this.exitState = ExitState.cmderror();
return EXIT_CMDERR;
}
List<File> filenames = processArgs(args);
if (filenames == null) {
// null signals an error in options, abort
this.exitState = ExitState.cmderror();
return EXIT_CMDERR;
} else if (filenames.isEmpty() && fileObjects.isEmpty() && classnames.isEmpty()) {
// or version info
if (options.get("-help") != null || options.get("-jhelp") != null || options.get("-X") != null || options.get("-version") != null || options.get("-fullversion") != null)
return EXIT_OK;
error("err.no.source.files");
this.exitState = ExitState.cmderror();
return EXIT_CMDERR;
}
// Set up the timer *after* we've processed to options
// because it needs to know if we need logging or not
timer = Timer.instance(context);
timer.init();
boolean forceStdOut = options.get("stdout") != null;
if (forceStdOut) {
out.flush();
out = new PrintWriter(System.out, true);
}
context.put(Log.outKey, out);
fileManager = context.get(JavaFileManager.class);
comp = LanguageCompiler.instance(context);
if (comp == null) {
this.exitState = ExitState.systemError(null, null);
return EXIT_SYSERR;
}
if (!classnames.isEmpty()) {
filenames = addModuleFiles(filenames);
classnames.clear();
}
if (!filenames.isEmpty()) {
// add filenames to fileObjects
List<JavaFileObject> otherFiles = List.nil();
JavacFileManager dfm = (JavacFileManager) fileManager;
for (JavaFileObject fo : dfm.getJavaFileObjectsFromFiles(filenames)) {
otherFiles = otherFiles.append(fo);
}
fileObjects = fileObjects.prependList(otherFiles);
}
if (fileObjects.isEmpty()) {
error("err.no.source.files");
this.exitState = ExitState.cmderror();
return EXIT_CMDERR;
}
comp.compile(fileObjects, classnames.toList(), processors);
int errorCount = comp.errorCount();
//ceylonBackendErrors = comp.log instanceof CeylonLog ? ((CeylonLog)comp.log).ceylonBackendErrors() : false;
if (errorCount != 0) {
this.exitState = ExitState.error(comp);
return EXIT_ERROR;
}
} catch (IOException ex) {
ioMessage(ex);
this.exitState = ExitState.systemError(null, ex);
return EXIT_SYSERR;
} catch (OutOfMemoryError ex) {
resourceMessage(ex);
this.exitState = ExitState.systemError(null, ex);
return EXIT_SYSERR;
} catch (StackOverflowError ex) {
resourceMessage(ex);
this.exitState = ExitState.systemError(null, ex);
return EXIT_SYSERR;
} catch (FatalError ex) {
this.exitState = ExitState.systemError(comp, ex);
if (this.exitState.javacExitCode == EXIT_SYSERR) {
feMessage(ex);
}
return this.exitState.javacExitCode;
} catch (AnnotationProcessingError ex) {
apMessage(ex);
this.exitState = ExitState.systemError(null, ex);
return EXIT_SYSERR;
} catch (ClientCodeException ex) {
// and javax.tools.JavaCompiler.CompilationTask#call
throw new RuntimeException(ex.getCause());
} catch (PropagatedException ex) {
throw ex.getCause();
} catch (RepositoryException ex) {
throw new EnvironmentException(ex);
} catch (Throwable ex) {
// exceptions.
if (comp == null || comp.errorCount() == 0 || options == null || options.get("dev") != null) {
bugMessage(ex);
}
this.exitState = ExitState.abnormal(comp, ex, options);
return EXIT_ABNORMAL;
} finally {
if (comp != null)
comp.close();
filenames = null;
options = null;
if (timer != null) {
timer.end();
}
timer = null;
}
this.exitState = ExitState.ok();
return EXIT_OK;
}
use of com.sun.tools.javac.util.ClientCodeException in project ceylon-compiler by ceylon.
the class JavacTool method getTask.
public JavacTask getTask(Writer out, JavaFileManager fileManager, DiagnosticListener<? super JavaFileObject> diagnosticListener, Iterable<String> options, Iterable<String> classes, Iterable<? extends JavaFileObject> compilationUnits) {
try {
Context context = new Context();
ClientCodeWrapper ccw = ClientCodeWrapper.instance(context);
final String kindMsg = "All compilation units must be of SOURCE kind";
if (options != null)
for (String option : options) // null check
option.getClass();
if (classes != null) {
for (String cls : classes) if (// implicit null check
!SourceVersion.isName(cls))
throw new IllegalArgumentException("Not a valid class name: " + cls);
}
if (compilationUnits != null) {
// implicit null check
compilationUnits = ccw.wrapJavaFileObjects(compilationUnits);
for (JavaFileObject cu : compilationUnits) {
if (cu.getKind() != JavaFileObject.Kind.SOURCE)
throw new IllegalArgumentException(kindMsg);
}
}
if (diagnosticListener != null)
context.put(DiagnosticListener.class, ccw.wrap(diagnosticListener));
if (out == null)
context.put(Log.outKey, new PrintWriter(System.err, true));
else
context.put(Log.outKey, new PrintWriter(out, true));
if (fileManager == null)
fileManager = getStandardFileManager(diagnosticListener, null, null);
fileManager = ccw.wrap(fileManager);
context.put(JavaFileManager.class, fileManager);
processOptions(context, fileManager, options);
Main compiler = new Main("javacTask", context.get(Log.outKey));
return new JavacTaskImpl(compiler, options, context, classes, compilationUnits);
} catch (ClientCodeException ex) {
throw new RuntimeException(ex.getCause());
}
}
Aggregations