aboutsummaryrefslogtreecommitdiff
path: root/src/core/lombok/javac
diff options
context:
space:
mode:
authorReinier Zwitserloot <reinier@zwitserloot.com>2010-07-21 10:51:09 +0200
committerReinier Zwitserloot <reinier@zwitserloot.com>2010-07-21 10:51:09 +0200
commit91bb3455da2913c81745d2a7f7e5b42839964f58 (patch)
treed01d7683b1ad91ada20df34d69ef648aaceb0815 /src/core/lombok/javac
parentc06660bd186c7ae8215a822c4eceab097407eeda (diff)
downloadlombok-91bb3455da2913c81745d2a7f7e5b42839964f58.tar.gz
lombok-91bb3455da2913c81745d2a7f7e5b42839964f58.tar.bz2
lombok-91bb3455da2913c81745d2a7f7e5b42839964f58.zip
Added using .getX() instead of using .x in equals, hashCode, and toString. Also updated changelog as well as the docs.
Also updated usage examples for @EqualsAndHashCode, @ToString, and @Data, which also contained some other minor issues (such as missing this. qualifiers). Still to do is to detect that getters don't exist _yet_ but will later due to @Getter or @Data.
Diffstat (limited to 'src/core/lombok/javac')
-rw-r--r--src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java2
-rw-r--r--src/core/lombok/javac/handlers/JavacHandlerUtil.java57
2 files changed, 48 insertions, 11 deletions
diff --git a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java
index 66d2308a..20ede725 100644
--- a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java
+++ b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java
@@ -281,7 +281,7 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd
maker.Apply(List.<JCExpression>nil(), hcMethod, List.of(fieldAccessor)));
} else /* objects */ {
/* this.fieldName == null ? 0 : this.fieldName.hashCode() */
- JCExpression hcCall = maker.Apply(List.<JCExpression>nil(), maker.Select(fieldAccessor, typeNode.toName("hashCode")),
+ JCExpression hcCall = maker.Apply(List.<JCExpression>nil(), maker.Select(createFieldAccessor(maker, fieldNode, useFieldsDirectly), typeNode.toName("hashCode")),
List.<JCExpression>nil());
JCExpression thisEqualsNull = maker.Binary(JCTree.EQ, fieldAccessor, maker.Literal(TypeTags.BOT, null));
intoResult = intoResult.append(
diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
index 84388c30..cb2697f1 100644
--- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java
+++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
@@ -41,6 +41,7 @@ import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.tree.JCTree.JCExpression;
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.JCStatement;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.util.List;
@@ -269,15 +270,26 @@ public class JavacHandlerUtil {
}
}
- /**
- * Creates an expression that reads the field. Will either be {@code this.field} or {@code this.getField()} depending on whether or not there's a getter.
- */
- static JCExpression createFieldAccessor(TreeMaker maker, JavacNode field, boolean useFieldsDirectly) {
- return createFieldAccessor(maker, field, useFieldsDirectly, maker.Ident(field.toName("this")));
- }
-
- static JCExpression createFieldAccessor(TreeMaker maker, JavacNode field, boolean useFieldsDirectly, JCExpression receiver) {
- return maker.Select(receiver, ((JCVariableDecl)field.get()).name);
+ private static JCMethodDecl findGetter(JavacNode field) {
+ JCVariableDecl decl = (JCVariableDecl)field.get();
+ JavacNode typeNode = field.up();
+ for (String potentialGetterName : toAllGetterNames(decl)) {
+ switch (methodExists(potentialGetterName, typeNode, false)) {
+ case EXISTS_BY_LOMBOK:
+ case EXISTS_BY_USER:
+ for (JavacNode potentialGetter : typeNode.down()) {
+ if (potentialGetter.getKind() != Kind.METHOD) continue;
+ JCMethodDecl method = (JCMethodDecl) potentialGetter.get();
+ /** 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 null;
}
/**
@@ -286,7 +298,32 @@ public class JavacHandlerUtil {
* @see #createFieldAccessor(TreeMaker, JavacNode)
*/
static JCExpression getFieldType(JavacNode field, boolean useFieldsDirectly) {
- return ((JCVariableDecl)field.get()).vartype;
+ JCMethodDecl getter = useFieldsDirectly ? null : findGetter(field);
+
+ if (getter == null) {
+ return ((JCVariableDecl)field.get()).vartype;
+ }
+
+ return getter.restype;
+ }
+
+ /**
+ * Creates an expression that reads the field. Will either be {@code this.field} or {@code this.getField()} depending on whether or not there's a getter.
+ */
+ static JCExpression createFieldAccessor(TreeMaker maker, JavacNode field, boolean useFieldsDirectly) {
+ return createFieldAccessor(maker, field, useFieldsDirectly, maker.Ident(field.toName("this")));
+ }
+
+ static JCExpression createFieldAccessor(TreeMaker maker, JavacNode field, boolean useFieldsDirectly, JCExpression receiver) {
+ JCMethodDecl getter = useFieldsDirectly ? null : findGetter(field);
+
+ if (getter == null) {
+ return maker.Select(receiver, ((JCVariableDecl)field.get()).name);
+ }
+
+ JCMethodInvocation call = maker.Apply(List.<JCExpression>nil(),
+ maker.Select(receiver, getter.name), List.<JCExpression>nil());
+ return call;
}
/**