Provides access to functionality specific to the JDK Java Compiler, javac.
Method from com.sun.tools.javac.api.JavacTaskImpl Detail: |
public Iterable<Element> analyze() throws IOException {
return analyze(null);
}
|
public Iterable<Element> analyze(Iterable<TypeElement> classes) throws IOException {
enter(null); // ensure all classes have been entered
final ListBuffer< Element > results = new ListBuffer< Element >();
try {
if (classes == null) {
handleFlowResults(compiler.flow(compiler.attribute(compiler.todo)), results);
} else {
Filter f = new Filter() {
public void process(Env< AttrContext > env) {
handleFlowResults(compiler.flow(compiler.attribute(env)), results);
}
};
f.run(compiler.todo, classes);
}
} finally {
compiler.log.flush();
}
return results;
}
Complete all analysis on the given classes.
This can be used to ensure that all compile time errors are reported.
The classes must have previously been returned from #enter .
If null is specified, all outstanding classes will be analyzed. |
public JavaFileObject asJavaFileObject(File file) {
JavacFileManager fm = (JavacFileManager)context.get(JavaFileManager.class);
return fm.getRegularFile(file);
}
|
public Boolean call() {
if (!used.getAndSet(true)) {
initContext();
notYetEntered = new HashMap< JavaFileObject, JCCompilationUnit >();
compilerMain.setAPIMode(true);
result = compilerMain.compile(args, context, fileObjects, processors);
cleanup();
return result == 0;
} else {
throw new IllegalStateException("multiple calls to method 'call'");
}
}
|
void cleanup() {
if (compiler != null)
compiler.close();
compiler = null;
compilerMain = null;
args = null;
context = null;
fileObjects = null;
notYetEntered = null;
}
|
public Iterable<TypeElement> enter() throws IOException {
return enter(null);
}
Translate all the abstract syntax trees to elements. |
public Iterable<TypeElement> enter(Iterable<CompilationUnitTree> trees) throws IOException {
prepareCompiler();
ListBuffer< JCCompilationUnit > roots = null;
if (trees == null) {
// If there are still files which were specified to be compiled
// (i.e. in fileObjects) but which have not yet been entered,
// then we make sure they have been parsed and add them to the
// list to be entered.
if (notYetEntered.size() > 0) {
if (!parsed)
parse(); // TODO would be nice to specify files needed to be parsed
for (JavaFileObject file: fileObjects) {
JCCompilationUnit unit = notYetEntered.remove(file);
if (unit != null) {
if (roots == null)
roots = new ListBuffer< JCCompilationUnit >();
roots.append(unit);
}
}
notYetEntered.clear();
}
}
else {
for (CompilationUnitTree cu : trees) {
if (cu instanceof JCCompilationUnit) {
if (roots == null)
roots = new ListBuffer< JCCompilationUnit >();
roots.append((JCCompilationUnit)cu);
notYetEntered.remove(cu.getSourceFile());
}
else
throw new IllegalArgumentException(cu.toString());
}
}
if (roots == null)
return List.nil();
try {
List< JCCompilationUnit > units = compiler.enterTrees(roots.toList());
if (notYetEntered.isEmpty())
compiler = compiler.processAnnotations(units);
ListBuffer< TypeElement > elements = new ListBuffer< TypeElement >();
for (JCCompilationUnit unit : units) {
for (JCTree node : unit.defs) {
if (node.getTag() == JCTree.CLASSDEF) {
JCClassDecl cdef = (JCClassDecl) node;
if (cdef.sym != null) // maybe null if errors in anno processing
elements.append(cdef.sym);
}
}
}
return elements.toList();
}
finally {
compiler.log.flush();
}
}
Translate the given abstract syntax trees to elements. |
public Iterable<JavaFileObject> generate() throws IOException {
return generate(null);
}
|
public Iterable<JavaFileObject> generate(Iterable<TypeElement> classes) throws IOException {
final ListBuffer< JavaFileObject > results = new ListBuffer< JavaFileObject >();
try {
analyze(null); // ensure all classes have been parsed, entered, and analyzed
if (classes == null) {
compiler.generate(compiler.desugar(genList), results);
genList.clear();
}
else {
Filter f = new Filter() {
public void process(Env< AttrContext > env) {
compiler.generate(compiler.desugar(ListBuffer.of(env)), results);
}
};
f.run(genList, classes);
}
if (genList.isEmpty()) {
compiler.reportDeferredDiagnostics();
cleanup();
}
}
finally {
if (compiler != null)
compiler.log.flush();
}
return results;
}
Generate code corresponding to the given classes.
The classes must have previously been returned from #enter .
If there are classes outstanding to be analyzed, that will be done before
any classes are generated.
If null is specified, code will be generated for all outstanding classes. |
public Context getContext() {
return context;
}
For internal use only. This method will be
removed without warning. |
public JavacElements getElements() {
if (context == null)
throw new IllegalStateException();
return JavacElements.instance(context);
}
|
public TypeMirror getTypeMirror(Iterable<Tree> path) {
// TODO: Should complete attribution if necessary
Tree last = null;
for (Tree node : path)
last = node;
return ((JCTree)last).type;
}
|
public JavacTypes getTypes() {
if (context == null)
throw new IllegalStateException();
return JavacTypes.instance(context);
}
|
public Iterable<CompilationUnitTree> parse() throws IOException {
try {
prepareCompiler();
List< JCCompilationUnit > units = compiler.parseFiles(fileObjects);
for (JCCompilationUnit unit: units) {
JavaFileObject file = unit.getSourceFile();
if (notYetEntered.containsKey(file))
notYetEntered.put(file, unit);
}
return units;
}
finally {
parsed = true;
if (compiler != null && compiler.log != null)
compiler.log.flush();
}
}
Parse the specified files returning a list of abstract syntax trees. |
public Type parseType(String expr,
TypeElement scope) {
if (expr == null || expr.equals(""))
throw new IllegalArgumentException();
compiler = JavaCompiler.instance(context);
JavaFileObject prev = compiler.log.useSource(null);
ParserFactory parserFactory = ParserFactory.instance(context);
Attr attr = Attr.instance(context);
try {
CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length());
Parser parser = parserFactory.newParser(buf, false, false, false);
JCTree tree = parser.parseType();
return attr.attribType(tree, (Symbol.TypeSymbol)scope);
} finally {
compiler.log.useSource(prev);
}
}
For internal use only. This method will be
removed without warning. |
public Iterable<Tree> pathFor(CompilationUnitTree unit,
Tree node) {
return TreeInfo.pathFor((JCTree) node, (JCTree.JCCompilationUnit) unit).reverse();
}
|
public void setLocale(Locale locale) {
if (used.get())
throw new IllegalStateException();
this.locale = locale;
}
|
public void setProcessors(Iterable<Processor> processors) {
processors.getClass(); // null check
// not mt-safe
if (used.get())
throw new IllegalStateException();
this.processors = processors;
}
|
public void setTaskListener(TaskListener taskListener) {
this.taskListener = taskListener;
}
|
public void updateContext(Context newContext) {
context = newContext;
}
For internal use only. This method will be
removed without warning. |