aboutsummaryrefslogtreecommitdiff
path: root/src/core/lombok/javac/handlers/JavacHandlerUtil.java
diff options
context:
space:
mode:
authorReinier Zwitserloot <reinier@zwitserloot.com>2010-07-22 14:19:23 +0200
committerReinier Zwitserloot <reinier@zwitserloot.com>2010-07-22 14:19:23 +0200
commitceda2e5efe229650d4e95de6b8a2632d9f616592 (patch)
tree1af7964267dc2ce40e0efb02ee767d58303bfab5 /src/core/lombok/javac/handlers/JavacHandlerUtil.java
parent868d8b0f93c0801f638b8c5523291aacd35d9ce2 (diff)
downloadlombok-ceda2e5efe229650d4e95de6b8a2632d9f616592.tar.gz
lombok-ceda2e5efe229650d4e95de6b8a2632d9f616592.tar.bz2
lombok-ceda2e5efe229650d4e95de6b8a2632d9f616592.zip
toString(), equals(), and hashCode() now use getX() instead of x if either it exists OR it will be generated by some other lombok annotation, addressing issue #110.
code deduplication by removing HandleData's scanning for fields, which is now no longer done; the sub-parts of Data (Getter, Setter, RequiredArgsConstructor, etc) take care of it now. fix for class-level @Getter/@Setter, which used to go for every field. Now they skip the usual fields (static, for setters final, and $ prefixed fields). Bugfix for @Data not recognizing that it should let field-level @Getter/@Setter take care of generating the getter/setter for multi field declarations (@Getter int x, y);
Diffstat (limited to 'src/core/lombok/javac/handlers/JavacHandlerUtil.java')
-rw-r--r--src/core/lombok/javac/handlers/JavacHandlerUtil.java60
1 files changed, 55 insertions, 5 deletions
diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
index cb2697f1..09d5c3fe 100644
--- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java
+++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
@@ -21,12 +21,18 @@
*/
package lombok.javac.handlers;
+import static lombok.javac.Javac.annotationTypeMatches;
+
import java.lang.annotation.Annotation;
import java.util.regex.Pattern;
import lombok.AccessLevel;
+import lombok.Data;
+import lombok.Getter;
+import lombok.core.AnnotationValues;
import lombok.core.AST.Kind;
import lombok.core.handlers.TransformationsUtil;
+import lombok.javac.Javac;
import lombok.javac.JavacNode;
import com.sun.tools.javac.code.Flags;
@@ -270,7 +276,17 @@ public class JavacHandlerUtil {
}
}
- private static JCMethodDecl findGetter(JavacNode field) {
+ private static class GetterMethod {
+ private final Name name;
+ private final JCExpression type;
+
+ GetterMethod(Name name, JCExpression type) {
+ this.name = name;
+ this.type = type;
+ }
+ }
+
+ private static GetterMethod findGetter(JavacNode field) {
JCVariableDecl decl = (JCVariableDecl)field.get();
JavacNode typeNode = field.up();
for (String potentialGetterName : toAllGetterNames(decl)) {
@@ -280,15 +296,49 @@ public class JavacHandlerUtil {
for (JavacNode potentialGetter : typeNode.down()) {
if (potentialGetter.getKind() != Kind.METHOD) continue;
JCMethodDecl method = (JCMethodDecl) potentialGetter.get();
+ if (!method.name.contentEquals(potentialGetterName)) continue;
/** static getX() methods don't count. */
if ((method.mods.flags & Flags.STATIC) != 0) continue;
/** Nor do getters with a non-empty parameter list. */
if (method.params != null && method.params.size() > 0) continue;
- return method;
+ return new GetterMethod(method.name, method.restype);
+ }
+ }
+ }
+
+ // Check if the field has a @Getter annotation.
+
+ boolean hasGetterAnnotation = false;
+
+ for (JavacNode child : field.down()) {
+ if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Getter.class, child)) {
+ AnnotationValues<Getter> ann = Javac.createAnnotation(Getter.class, child);
+ if (ann.getInstance().value() == AccessLevel.NONE) return null; //Definitely WONT have a getter.
+ hasGetterAnnotation = true;
+ }
+ }
+
+ // Check if the class has a @Getter annotation.
+
+ if (!hasGetterAnnotation && new HandleGetter().fieldQualifiesForGetterGeneration(field)) {
+ //Check if the class has @Getter or @Data annotation.
+
+ JavacNode containingType = field.up();
+ if (containingType != null) for (JavacNode child : containingType.down()) {
+ if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Data.class, child)) hasGetterAnnotation = true;
+ if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Getter.class, child)) {
+ AnnotationValues<Getter> ann = Javac.createAnnotation(Getter.class, child);
+ if (ann.getInstance().value() == AccessLevel.NONE) return null; //Definitely WONT have a getter.
+ hasGetterAnnotation = true;
}
}
}
+ if (hasGetterAnnotation) {
+ String getterName = toGetterName(decl);
+ return new GetterMethod(field.toName(getterName), decl.vartype);
+ }
+
return null;
}
@@ -298,13 +348,13 @@ public class JavacHandlerUtil {
* @see #createFieldAccessor(TreeMaker, JavacNode)
*/
static JCExpression getFieldType(JavacNode field, boolean useFieldsDirectly) {
- JCMethodDecl getter = useFieldsDirectly ? null : findGetter(field);
+ GetterMethod getter = useFieldsDirectly ? null : findGetter(field);
if (getter == null) {
return ((JCVariableDecl)field.get()).vartype;
}
- return getter.restype;
+ return getter.type;
}
/**
@@ -315,7 +365,7 @@ public class JavacHandlerUtil {
}
static JCExpression createFieldAccessor(TreeMaker maker, JavacNode field, boolean useFieldsDirectly, JCExpression receiver) {
- JCMethodDecl getter = useFieldsDirectly ? null : findGetter(field);
+ GetterMethod getter = useFieldsDirectly ? null : findGetter(field);
if (getter == null) {
return maker.Select(receiver, ((JCVariableDecl)field.get()).name);