From f36be2eb01bcbff01a513d64bff2d1aba54460b1 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Tue, 16 Jun 2009 03:04:46 +0200 Subject: Implemented a lot of stuff for javac, but we ran into 2 major issues still to be implemented: 1. The visit mode of a lombok handler (does not trigger off of annotations, instead sees every field, method, type, and statement), needs to be coded, 2. triggering off of annotations via APT's annotation handling system skips method-local classes. We'll need to recode this via an AST visitor like we need for issue #1 Other than that, triggering off of annotations works swimmingly! --- src/lombok/javac/handlers/HandleGetter_javac.java | 60 +++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/lombok/javac/handlers/HandleGetter_javac.java (limited to 'src/lombok/javac/handlers/HandleGetter_javac.java') diff --git a/src/lombok/javac/handlers/HandleGetter_javac.java b/src/lombok/javac/handlers/HandleGetter_javac.java new file mode 100644 index 00000000..77ce2b4f --- /dev/null +++ b/src/lombok/javac/handlers/HandleGetter_javac.java @@ -0,0 +1,60 @@ +package lombok.javac.handlers; + +import static lombok.javac.handlers.PKG.*; + +import javax.lang.model.element.Element; + +import lombok.Getter; +import lombok.javac.JavacAnnotationHandler; +import lombok.javac.JavacNode; + +import org.mangosdk.spi.ProviderFor; + +import com.sun.source.tree.MethodTree; +import com.sun.tools.javac.code.Symbol; +import com.sun.tools.javac.code.Type; +import com.sun.tools.javac.tree.JCTree; +import com.sun.tools.javac.tree.TreeMaker; +import com.sun.tools.javac.tree.JCTree.JCAnnotation; +import com.sun.tools.javac.tree.JCTree.JCBlock; +import com.sun.tools.javac.tree.JCTree.JCClassDecl; +import com.sun.tools.javac.tree.JCTree.JCExpression; +import com.sun.tools.javac.tree.JCTree.JCStatement; +import com.sun.tools.javac.tree.JCTree.JCTypeParameter; +import com.sun.tools.javac.tree.JCTree.JCVariableDecl; +import com.sun.tools.javac.util.List; +import com.sun.tools.javac.util.Name; + +@ProviderFor(JavacAnnotationHandler.class) +public class HandleGetter_javac implements JavacAnnotationHandler { + @Override public void handle(JavacNode node, Getter getter) { + System.out.println("PING: " + node.getJavacAST()); + if ( !node.getJavacAST().getKind().isField() ) { + node.addError("@Getter is only supported on a field."); + return; + } + + JCClassDecl javacClassTree = node.getEnclosingType(); + + int access = toJavacModifier(getter.value()); + + MethodTree getterMethod = createGetter(access, node.getJavacAST(), node.createTreeMaker(), node.createNameTable()); + javacClassTree.defs = javacClassTree.defs.append((JCTree)getterMethod); + } + + private MethodTree createGetter(int access, Element field, TreeMaker treeMaker, Name.Table nameTable) { + JCStatement returnStatement = treeMaker.Return(treeMaker.Ident((Symbol)field)); + + JCBlock methodBody = treeMaker.Block(0, List.of(returnStatement)); + Name methodName = Name.fromString(nameTable, toGetterName(field)); + JCExpression methodType = treeMaker.Type((Type)field.asType()); + + List methodGenericParams = List.nil(); + List parameters = List.nil(); + List throwsClauses = List.nil(); + JCExpression annotationMethodDefaultValue = null; + + return treeMaker.MethodDef(treeMaker.Modifiers(access, List.nil()), methodName, methodType, + methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue); + } +} -- cgit