Added the ability to get the Context object via JavacAST, useful for various things, and specifically requested by David McCullars, who is working on rolling their own lombok extensions.

See: http://groups.google.com/group/project-lombok/browse_thread/thread/a73972d4011a3e21
This commit is contained in:
Reinier Zwitserloot committed 2009-10-18 17:48:27 +02:00
1 parent 4da08ce3cc
commit 17c8f5a019
3 files changed
+27 -9

No files matched your search

+15 -8
View File
@@ -34,7 +34,6 @@ import lombok.core.AST;
import com.sun.source.util.Trees;
import com.sun.tools.javac.code.Symtab;
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
@@ -47,6 +46,7 @@ import com.sun.tools.javac.tree.JCTree.JCImport;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCStatement;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Log;
import com.sun.tools.javac.util.Name;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
@@ -61,23 +61,30 @@ public class JavacAST extends AST<JavacAST, JavacNode, JCTree> {
private final TreeMaker treeMaker;
private final Symtab symtab;
private final Log log;
private final Context context;
/**
* Creates a new JavacAST of the provided Compilation Unit.
*
* @param trees The trees instance to use to inspect the compilation unit. Generate via:
* {@code Trees.getInstance(env)}
* @param env The ProcessingEnvironment object passed e.g. to an annotation processor.
* @param messager A Messager for warning and error reporting.
* @param context A Context object for interfacing with the compiler.
* @param top The compilation unit, which serves as the top level node in the tree to be built.
*/
public JavacAST(Trees trees, JavacProcessingEnvironment env, JCCompilationUnit top) {
public JavacAST(Trees trees, Messager messager, Context context, JCCompilationUnit top) {
super(top.sourcefile == null ? null : top.sourcefile.toString());
setTop(buildCompilationUnit(top));
this.messager = env.getMessager();
this.log = Log.instance(env.getContext());
this.nameTable = Name.Table.instance(env.getContext());
this.treeMaker = TreeMaker.instance(env.getContext());
this.symtab = Symtab.instance(env.getContext());
this.context = context;
this.messager = messager;
this.log = Log.instance(context);
this.nameTable = Name.Table.instance(context);
this.treeMaker = TreeMaker.instance(context);
this.symtab = Symtab.instance(context);
}
public Context getContext() {
return context;
}
/** {@inheritDoc} */
+10
View File
@@ -36,6 +36,7 @@ import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Name;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
@@ -163,6 +164,15 @@ public class JavacNode extends lombok.core.LombokNode<JavacAST, JavacNode, JCTre
return ast.getSymbolTable();
}
/**
* Convenient shortcut to the owning JavacAST object's getContext method.
*
* @see JavacAST#getContext()
*/
public Context getContext() {
return ast.getContext();
}
/**
* Convenient shortcut to the owning JavacAST object's toName method.
*
+2 -1
View File
@@ -93,7 +93,8 @@ public class Processor extends AbstractProcessor {
List<JavacAST> asts = new ArrayList<JavacAST>();
for (JCCompilationUnit unit : units.keySet()) asts.add(new JavacAST(trees, processingEnv, unit));
for (JCCompilationUnit unit : units.keySet()) asts.add(
new JavacAST(trees, processingEnv.getMessager(), processingEnv.getContext(), unit));
handlers.skipPrintAST();
for (JavacAST ast : asts) {