aboutsummaryrefslogtreecommitdiff
path: root/src/lombok/javac/handlers
diff options
context:
space:
mode:
Diffstat (limited to 'src/lombok/javac/handlers')
-rw-r--r--src/lombok/javac/handlers/HandleGetter.java12
-rw-r--r--src/lombok/javac/handlers/HandleSetter.java67
-rw-r--r--src/lombok/javac/handlers/PKG.java6
3 files changed, 79 insertions, 6 deletions
diff --git a/src/lombok/javac/handlers/HandleGetter.java b/src/lombok/javac/handlers/HandleGetter.java
index ef4d9755..781598a2 100644
--- a/src/lombok/javac/handlers/HandleGetter.java
+++ b/src/lombok/javac/handlers/HandleGetter.java
@@ -10,13 +10,12 @@ 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.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCStatement;
import com.sun.tools.javac.tree.JCTree.JCTypeParameter;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
@@ -26,6 +25,7 @@ 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 annotationNode) {
+ //TODO Check for existence of the getter and skip it (+ warn) if it's already there.
if ( annotationNode.up().getKind() != Kind.FIELD ) {
annotationNode.addError("@Getter is only supported on a field.");
return;
@@ -37,16 +37,16 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> {
int access = toJavacModifier(getter.value());
- MethodTree getterMethod = createGetter(access, annotationNode.up(), annotationNode.getTreeMaker());
- javacClassTree.defs = javacClassTree.defs.append((JCTree)getterMethod);
+ JCMethodDecl getterMethod = createGetter(access, annotationNode.up(), annotationNode.getTreeMaker());
+ javacClassTree.defs = javacClassTree.defs.append(getterMethod);
}
- private MethodTree createGetter(int access, JavacAST.Node field, TreeMaker treeMaker) {
+ private JCMethodDecl 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()));
+ Name methodName = field.toName(toGetterName(fieldNode));
JCExpression methodType = fieldNode.type != null ? treeMaker.Type(fieldNode.type) : fieldNode.vartype;
List<JCTypeParameter> methodGenericParams = List.nil();
diff --git a/src/lombok/javac/handlers/HandleSetter.java b/src/lombok/javac/handlers/HandleSetter.java
new file mode 100644
index 00000000..30702701
--- /dev/null
+++ b/src/lombok/javac/handlers/HandleSetter.java
@@ -0,0 +1,67 @@
+package lombok.javac.handlers;
+
+import static lombok.javac.handlers.PKG.toJavacModifier;
+import static lombok.javac.handlers.PKG.toSetterName;
+
+import org.mangosdk.spi.ProviderFor;
+
+import lombok.Setter;
+import lombok.core.AnnotationValues;
+import lombok.core.AST.Kind;
+import lombok.javac.JavacAST;
+import lombok.javac.JavacAnnotationHandler;
+import lombok.javac.JavacAST.Node;
+
+import com.sun.tools.javac.tree.TreeMaker;
+import com.sun.tools.javac.tree.JCTree.JCAnnotation;
+import com.sun.tools.javac.tree.JCTree.JCAssign;
+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.JCFieldAccess;
+import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
+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 HandleSetter implements JavacAnnotationHandler<Setter> {
+ @Override public void handle(AnnotationValues<Setter> annotation, JCAnnotation ast, Node annotationNode) {
+ //TODO Check for existence of the setter and skip it (+ warn) if it's already there.
+ if ( annotationNode.up().getKind() != Kind.FIELD ) {
+ annotationNode.addError("@Setter is only supported on a field.");
+ return;
+ }
+
+ Setter setter = annotation.getInstance();
+
+ JCClassDecl javacClassTree = (JCClassDecl) annotationNode.up().up().get();
+
+ int access = toJavacModifier(setter.value());
+
+ JCMethodDecl setterMethod = createSetter(access, annotationNode.up(), annotationNode.getTreeMaker());
+ javacClassTree.defs = javacClassTree.defs.append(setterMethod);
+ }
+
+ private JCMethodDecl createSetter(int access, JavacAST.Node field, TreeMaker treeMaker) {
+ JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
+
+ JCFieldAccess thisX = treeMaker.Select(treeMaker.Ident(field.toName("this")), fieldDecl.name);
+ JCAssign assign = treeMaker.Assign(thisX, treeMaker.Ident(fieldDecl.name));
+
+ JCBlock methodBody = treeMaker.Block(0, List.<JCStatement>of(treeMaker.Exec(assign)));
+ Name methodName = field.toName(toSetterName(fieldDecl));
+ JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(0, List.<JCAnnotation>nil()), fieldDecl.name, fieldDecl.vartype, null);
+ JCExpression methodType = treeMaker.Type(field.getSymbolTable().voidType);
+
+ List<JCTypeParameter> methodGenericParams = List.nil();
+ List<JCVariableDecl> parameters = List.of(param);
+ List<JCExpression> throwsClauses = List.nil();
+ JCExpression annotationMethodDefaultValue = null;
+
+ return treeMaker.MethodDef(treeMaker.Modifiers(access, List.<JCAnnotation>nil()), methodName, methodType,
+ methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue);
+ }
+}
diff --git a/src/lombok/javac/handlers/PKG.java b/src/lombok/javac/handlers/PKG.java
index 9ba3765a..4ea44274 100644
--- a/src/lombok/javac/handlers/PKG.java
+++ b/src/lombok/javac/handlers/PKG.java
@@ -16,6 +16,12 @@ class PKG {
return TransformationsUtil.toGetterName(fieldName, isBoolean);
}
+ static String toSetterName(JCVariableDecl field) {
+ CharSequence fieldName = field.name;
+
+ return TransformationsUtil.toSetterName(fieldName);
+ }
+
static int toJavacModifier(AccessLevel accessLevel) {
switch ( accessLevel ) {
case MODULE: