aboutsummaryrefslogtreecommitdiff
path: root/src/core/lombok/javac/handlers
diff options
context:
space:
mode:
authorRoel Spilker <r.spilker@gmail.com>2010-11-10 01:44:04 +0100
committerRoel Spilker <r.spilker@gmail.com>2010-11-10 01:44:04 +0100
commitfe7f0db2fce7b4c80853b9aed100908ff1f55f40 (patch)
tree41f7c7a289237153307402b7ebe25e372254ae40 /src/core/lombok/javac/handlers
parent46d471e9c3dc32b03c34804df1819739a4dffc50 (diff)
downloadlombok-fe7f0db2fce7b4c80853b9aed100908ff1f55f40.tar.gz
lombok-fe7f0db2fce7b4c80853b9aed100908ff1f55f40.tar.bz2
lombok-fe7f0db2fce7b4c80853b9aed100908ff1f55f40.zip
From now on it is possible to specify annotations to be placed on the method
or the parameter of the generated getter/setter methods.
Diffstat (limited to 'src/core/lombok/javac/handlers')
-rw-r--r--src/core/lombok/javac/handlers/HandleData.java2
-rw-r--r--src/core/lombok/javac/handlers/HandleGetter.java26
-rw-r--r--src/core/lombok/javac/handlers/HandleSetter.java63
-rw-r--r--src/core/lombok/javac/handlers/JavacHandlerUtil.java40
4 files changed, 67 insertions, 64 deletions
diff --git a/src/core/lombok/javac/handlers/HandleData.java b/src/core/lombok/javac/handlers/HandleData.java
index 4c63e8f0..6767f073 100644
--- a/src/core/lombok/javac/handlers/HandleData.java
+++ b/src/core/lombok/javac/handlers/HandleData.java
@@ -59,7 +59,7 @@ public class HandleData implements JavacAnnotationHandler<Data> {
// TODO move this to the end OR move it to the top in eclipse.
new HandleConstructor().generateRequiredArgsConstructor(typeNode, AccessLevel.PUBLIC, staticConstructorName, true);
new HandleGetter().generateGetterForType(typeNode, annotationNode, AccessLevel.PUBLIC, true);
- new HandleSetter().generateSetterForType(typeNode, annotationNode, AccessLevel.PUBLIC, true, List.<JCExpression>nil(), List.<JCExpression>nil());
+ new HandleSetter().generateSetterForType(typeNode, annotationNode, AccessLevel.PUBLIC, true);
new HandleEqualsAndHashCode().generateEqualsAndHashCodeForType(typeNode, annotationNode);
new HandleToString().generateToStringForType(typeNode, annotationNode);
diff --git a/src/core/lombok/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java
index ac3a16a1..79e842dc 100644
--- a/src/core/lombok/javac/handlers/HandleGetter.java
+++ b/src/core/lombok/javac/handlers/HandleGetter.java
@@ -78,7 +78,7 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> {
}
for (JavacNode field : typeNode.down()) {
- if (fieldQualifiesForGetterGeneration(field)) generateGetterForField(field, errorNode.get(), level);
+ if (fieldQualifiesForGetterGeneration(field)) generateGetterForField(field, errorNode.get(), level, List.<JCExpression>nil());
}
return true;
@@ -109,7 +109,7 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> {
* @param fieldNode The node representing the field you want a getter for.
* @param pos The node responsible for generating the getter (the {@code @Data} or {@code @Getter} annotation).
*/
- public void generateGetterForField(JavacNode fieldNode, DiagnosticPosition pos, AccessLevel level) {
+ public void generateGetterForField(JavacNode fieldNode, DiagnosticPosition pos, AccessLevel level, List<JCExpression> onMethod) {
for (JavacNode child : fieldNode.down()) {
if (child.getKind() == Kind.ANNOTATION) {
if (Javac.annotationTypeMatches(Getter.class, child)) {
@@ -119,7 +119,7 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> {
}
}
- createGetterForField(level, fieldNode, fieldNode, false);
+ createGetterForField(level, fieldNode, fieldNode, false, onMethod);
}
@Override public boolean handle(AnnotationValues<Getter> annotation, JCAnnotation ast, JavacNode annotationNode) {
@@ -131,25 +131,28 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> {
if (level == AccessLevel.NONE) return true;
if (node == null) return false;
+
+ List<JCExpression> onMethod = getAndRemoveAnnotationParameter(ast, "onMethod");
if (node.getKind() == Kind.FIELD) {
- return createGetterForFields(level, fields, annotationNode, true);
+ return createGetterForFields(level, fields, annotationNode, true, onMethod);
}
if (node.getKind() == Kind.TYPE) {
+ if (!onMethod.isEmpty()) annotationNode.addError("'onMethod' is not supported for @Getter on a type.");
return generateGetterForType(node, annotationNode, level, false);
}
return false;
}
- private boolean createGetterForFields(AccessLevel level, Collection<JavacNode> fieldNodes, JavacNode errorNode, boolean whineIfExists) {
+ private boolean createGetterForFields(AccessLevel level, Collection<JavacNode> fieldNodes, JavacNode errorNode, boolean whineIfExists, List<JCExpression> onMethod) {
for (JavacNode fieldNode : fieldNodes) {
- createGetterForField(level, fieldNode, errorNode, whineIfExists);
+ createGetterForField(level, fieldNode, errorNode, whineIfExists, onMethod);
}
return true;
}
private boolean createGetterForField(AccessLevel level,
- JavacNode fieldNode, JavacNode errorNode, boolean whineIfExists) {
+ JavacNode fieldNode, JavacNode errorNode, boolean whineIfExists, List<JCExpression> onMethod) {
if (fieldNode.getKind() != Kind.FIELD) {
errorNode.addError("@Getter is only supported on a class or a field.");
return true;
@@ -178,12 +181,12 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> {
long access = toJavacModifier(level) | (fieldDecl.mods.flags & Flags.STATIC);
- injectMethod(fieldNode.up(), createGetter(access, fieldNode, fieldNode.getTreeMaker()));
+ injectMethod(fieldNode.up(), createGetter(access, fieldNode, fieldNode.getTreeMaker(), onMethod));
return true;
}
- private JCMethodDecl createGetter(long access, JavacNode field, TreeMaker treeMaker) {
+ private JCMethodDecl createGetter(long access, JavacNode field, TreeMaker treeMaker, List<JCExpression> onMethod) {
JCVariableDecl fieldNode = (JCVariableDecl) field.get();
JCExpression fieldRef = createFieldAccessor(treeMaker, field, true);
JCStatement returnStatement = treeMaker.Return(fieldRef);
@@ -199,7 +202,10 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> {
List<JCAnnotation> nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN);
List<JCAnnotation> nullables = findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN);
- return treeMaker.MethodDef(treeMaker.Modifiers(access, nonNulls.appendList(nullables)), methodName, methodType,
+
+ List<JCAnnotation> annsOnMethod = copyAnnotations(onMethod).appendList(nonNulls).appendList(nullables);
+
+ return treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, methodType,
methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue);
}
}
diff --git a/src/core/lombok/javac/handlers/HandleSetter.java b/src/core/lombok/javac/handlers/HandleSetter.java
index b8c05a9c..af6f546d 100644
--- a/src/core/lombok/javac/handlers/HandleSetter.java
+++ b/src/core/lombok/javac/handlers/HandleSetter.java
@@ -32,8 +32,8 @@ import javax.lang.model.type.TypeVisitor;
import lombok.AccessLevel;
import lombok.Setter;
-import lombok.core.AST.Kind;
import lombok.core.AnnotationValues;
+import lombok.core.AST.Kind;
import lombok.core.handlers.TransformationsUtil;
import lombok.javac.Javac;
import lombok.javac.JavacAnnotationHandler;
@@ -44,28 +44,26 @@ import org.mangosdk.spi.ProviderFor;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.TypeTags;
+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.JCIdent;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
-import com.sun.tools.javac.tree.JCTree.JCNewArray;
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.tree.TreeMaker;
-import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.Name;
+import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
/**
* Handles the {@code lombok.Setter} annotation for javac.
*/
@ProviderFor(JavacAnnotationHandler.class)
public class HandleSetter implements JavacAnnotationHandler<Setter> {
- public boolean generateSetterForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean checkForTypeLevelSetter, List<JCExpression> onMethod, List<JCExpression> onParam) {
+ public boolean generateSetterForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean checkForTypeLevelSetter) {
if (checkForTypeLevelSetter) {
if (typeNode != null) for (JavacNode child : typeNode.down()) {
if (child.getKind() == Kind.ANNOTATION) {
@@ -97,7 +95,7 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> {
//Skip final fields.
if ((fieldDecl.mods.flags & Flags.FINAL) != 0) continue;
- generateSetterForField(field, errorNode.get(), level, onMethod, onParam);
+ generateSetterForField(field, errorNode.get(), level, List.<JCExpression>nil(), List.<JCExpression>nil());
}
return true;
}
@@ -140,47 +138,17 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> {
if (level == AccessLevel.NONE) return true;
if (node == null) return false;
- List<JCExpression> params = List.nil();
- List<JCExpression> onMethodList = List.nil();
- List<JCExpression> onParamList = List.nil();
-
- for (JCExpression param : ast.args) {
- System.out.println("T: " + param.getClass());
- if (param instanceof JCAssign) {
- JCAssign assign = (JCAssign) param;
- System.out.println("TL: " + assign.lhs.getClass());
- if (assign.lhs instanceof JCIdent) {
- JCIdent name = (JCIdent) assign.lhs;
- if ("onMethod".equals(name.name.toString())) {
- if (assign.rhs instanceof JCNewArray) {
- onMethodList = ((JCNewArray) assign.rhs).elems;
- } else {
- onMethodList = onMethodList.append(assign.rhs);
- }
- continue;
- } else if ("onParam".equals(name.name.toString())) {
- if (assign.rhs instanceof JCNewArray) {
- onParamList = ((JCNewArray) assign.rhs).elems;
- } else {
- onParamList = onParamList.append(assign.rhs);
- }
- continue;
- }
- }
- }
-
- params = params.append(param);
- }
-
- System.out.println("IN HANDLE SETTER5");
- ast.args = params;
- System.out.println("Args now: " + ast.args);
+ List<JCExpression> onParamList = getAndRemoveAnnotationParameter(ast, "onParam");
+ List<JCExpression> onMethodList = getAndRemoveAnnotationParameter(ast, "onMethod");
if (node.getKind() == Kind.FIELD) {
return createSetterForFields(level, fields, annotationNode, true, onMethodList, onParamList);
}
if (node.getKind() == Kind.TYPE) {
- return generateSetterForType(node, annotationNode, level, false, onMethodList, onParamList);
+ if (!onMethodList.isEmpty()) annotationNode.addError("'onMethod' is not supported for @Setter on a type.");
+ if (!onParamList.isEmpty()) annotationNode.addError("'onParam' is not supported for @Setter on a type.");
+
+ return generateSetterForType(node, annotationNode, level, false);
}
return false;
}
@@ -261,15 +229,6 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> {
methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue);
}
- private static List<JCAnnotation> copyAnnotations(List<JCExpression> in) {
- List<JCAnnotation> out = List.nil();
- for (JCExpression expr : in) {
- if (!(expr instanceof JCAnnotation)) continue;
- out = out.append((JCAnnotation) expr.clone());
- }
- return out;
- }
-
private static class JCNoType extends Type implements NoType {
public JCNoType(int tag) {
super(tag, null);
diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
index 5dacf2ca..bf356853 100644
--- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java
+++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
@@ -1,5 +1,5 @@
/*
- * Copyright © 2009-2010 Reinier Zwitserloot and Roel Spilker.
+ * Copyright © 2009-2010 Reinier Zwitserloot, Roel Spilker and Robbert Jan Grootjans.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -42,12 +42,15 @@ import com.sun.tools.javac.tree.JCTree.JCLiteral;
import com.sun.tools.javac.tree.JCTree.JCModifiers;
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.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.tree.JCTree.JCExpression;
+import com.sun.tools.javac.tree.JCTree.JCIdent;
import com.sun.tools.javac.tree.JCTree.JCImport;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
+import com.sun.tools.javac.tree.JCTree.JCNewArray;
import com.sun.tools.javac.tree.JCTree.JCStatement;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.util.List;
@@ -567,4 +570,39 @@ public class JavacHandlerUtil {
return problematic;
}
+
+ static List<JCExpression> getAndRemoveAnnotationParameter(JCAnnotation ast, String parameterName) {
+ List<JCExpression> params = List.nil();
+ List<JCExpression> result = List.nil();
+
+ for (JCExpression param : ast.args) {
+ if (param instanceof JCAssign) {
+ JCAssign assign = (JCAssign) param;
+ if (assign.lhs instanceof JCIdent) {
+ JCIdent ident = (JCIdent) assign.lhs;
+ if (parameterName.equals(ident.name.toString())) {
+ if (assign.rhs instanceof JCNewArray) {
+ result = ((JCNewArray) assign.rhs).elems;
+ } else {
+ result = result.append(assign.rhs);
+ }
+ continue;
+ }
+ }
+ }
+
+ params = params.append(param);
+ }
+ ast.args = params;
+ return result;
+ }
+
+ static List<JCAnnotation> copyAnnotations(List<JCExpression> in) {
+ List<JCAnnotation> out = List.nil();
+ for (JCExpression expr : in) {
+ if (!(expr instanceof JCAnnotation)) continue;
+ out = out.append((JCAnnotation) expr.clone());
+ }
+ return out;
+ }
}