aboutsummaryrefslogtreecommitdiff
path: root/src/lombok/javac/handlers/HandleGetter.java
diff options
context:
space:
mode:
authorReinier Zwitserloot <reinier@tipit.to>2009-06-17 20:12:03 +0200
committerReinier Zwitserloot <reinier@tipit.to>2009-06-17 20:12:03 +0200
commite737d64242329391ef681e51b36886aaf4d6fe35 (patch)
treefbebb5d68f9441079ea1e056fd1a1e937eb0613f /src/lombok/javac/handlers/HandleGetter.java
parentdb2326714f796b8e7100edc0388e54b2ba3e5139 (diff)
downloadlombok-e737d64242329391ef681e51b36886aaf4d6fe35.tar.gz
lombok-e737d64242329391ef681e51b36886aaf4d6fe35.tar.bz2
lombok-e737d64242329391ef681e51b36886aaf4d6fe35.zip
Renamed the Handler implementations.
Diffstat (limited to 'src/lombok/javac/handlers/HandleGetter.java')
-rw-r--r--src/lombok/javac/handlers/HandleGetter.java60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/lombok/javac/handlers/HandleGetter.java b/src/lombok/javac/handlers/HandleGetter.java
new file mode 100644
index 00000000..6e695c63
--- /dev/null
+++ b/src/lombok/javac/handlers/HandleGetter.java
@@ -0,0 +1,60 @@
+package lombok.javac.handlers;
+
+import static lombok.javac.handlers.PKG.*;
+
+import lombok.Getter;
+import lombok.core.AnnotationValues;
+import lombok.core.AST.Kind;
+import lombok.javac.JavacAnnotationHandler;
+import lombok.javac.JavacAST;
+
+import org.mangosdk.spi.ProviderFor;
+
+import com.sun.source.tree.MethodTree;
+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 implements JavacAnnotationHandler<Getter> {
+ @Override public void handle(AnnotationValues<Getter> annotation, JCAnnotation ast, JavacAST.Node node) {
+ if ( node.up().getKind() != Kind.FIELD ) {
+ node.addError("@Getter is only supported on a field.");
+ return;
+ }
+
+ Getter getter = annotation.getInstance();
+
+ JCClassDecl javacClassTree = (JCClassDecl) node.up().up().get();
+
+ int access = toJavacModifier(getter.value());
+
+ MethodTree getterMethod = createGetter(access, node.up(), node.getTreeMaker());
+ javacClassTree.defs = javacClassTree.defs.append((JCTree)getterMethod);
+ }
+
+ private MethodTree createGetter(int access, JavacAST.Node field, TreeMaker treeMaker) {
+ JCVariableDecl fieldNode = (JCVariableDecl) field.get();
+ JCStatement returnStatement = treeMaker.Return(treeMaker.Ident(fieldNode.getName()));
+
+ JCBlock methodBody = treeMaker.Block(0, List.of(returnStatement));
+ Name methodName = field.toName(toGetterName((JCVariableDecl)field.get()));
+ JCExpression methodType = fieldNode.type != null ? treeMaker.Type(fieldNode.type) : fieldNode.vartype;
+
+ List<JCTypeParameter> methodGenericParams = List.nil();
+ List<JCVariableDecl> parameters = List.nil();
+ List<JCExpression> throwsClauses = List.nil();
+ JCExpression annotationMethodDefaultValue = null;
+
+ return treeMaker.MethodDef(treeMaker.Modifiers(access, List.<JCAnnotation>nil()), methodName, methodType,
+ methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue);
+ }
+}