groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject [1/2] groovy git commit: GROOVY-6245: @EqualsAndHashCode assumes get style getters for boolean properties (port from master)
Date Sat, 06 Aug 2016 01:35:53 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_4_X ac5ec9dbe -> 32fc63ee6


GROOVY-6245: @EqualsAndHashCode assumes get style getters for boolean properties (port from
master)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/8a861aaf
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/8a861aaf
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/8a861aaf

Branch: refs/heads/GROOVY_2_4_X
Commit: 8a861aaf9130a43b9d10b579f710edb5ca43a7b5
Parents: ac5ec9d
Author: paulk <paulk@asert.com.au>
Authored: Fri Aug 5 19:22:31 2016 +1000
Committer: paulk <paulk@asert.com.au>
Committed: Sat Aug 6 11:26:40 2016 +1000

----------------------------------------------------------------------
 .../codehaus/groovy/ast/tools/GeneralUtils.java | 49 +++++++++++++++-----
 .../EqualsAndHashCodeASTTransformation.java     |  2 +-
 .../transform/ToStringASTTransformation.java    |  2 +-
 3 files changed, 40 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/8a861aaf/src/main/org/codehaus/groovy/ast/tools/GeneralUtils.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/ast/tools/GeneralUtils.java b/src/main/org/codehaus/groovy/ast/tools/GeneralUtils.java
index 9f63d16..fc8f78a 100644
--- a/src/main/org/codehaus/groovy/ast/tools/GeneralUtils.java
+++ b/src/main/org/codehaus/groovy/ast/tools/GeneralUtils.java
@@ -70,11 +70,6 @@ import java.util.Set;
 
 /**
  * Handy methods when working with the Groovy AST
- *
- * @author Guillaume Laforge
- * @author Paul King
- * @author Andre Steingress
- * @author Graeme Rocher
  */
 public class GeneralUtils {
     public static final Token ASSIGN = Token.newSymbol(Types.ASSIGN, -1, -1);
@@ -481,8 +476,8 @@ public class GeneralUtils {
     }
 
     public static BooleanExpression hasSamePropertyX(PropertyNode pNode, Expression other)
{
-        String getterName = getGetterName(pNode);
-        return sameX(callThisX(getterName), callX(other, getterName));
+        ClassNode cNode = pNode.getDeclaringClass();
+        return sameX(getterThisX(cNode, pNode), getterX(cNode, other, pNode));
     }
 
     public static Statement ifElseS(Expression cond, Statement thenStmt, Statement elseStmt)
{
@@ -633,22 +628,54 @@ public class GeneralUtils {
     }
 
     /**
+     * @deprecated use getterThisX instead
+     */
+    @Deprecated
+    public static Expression getterX(ClassNode annotatedNode, PropertyNode pNode) {
+        ClassNode owner = pNode.getDeclaringClass();
+        if (annotatedNode.equals(owner)) {
+            String getterName = "get" + MetaClassHelper.capitalize(pNode.getName());
+            if (ClassHelper.boolean_TYPE.equals(pNode.getOriginType())) {
+                getterName = "is" + MetaClassHelper.capitalize(pNode.getName());
+            }
+            return callX(new VariableExpression("this"), getterName, ArgumentListExpression.EMPTY_ARGUMENTS);
+        }
+        return propX(new VariableExpression("this"), pNode.getName());
+    }
+
+    /**
      * This method is similar to {@link #propX(Expression, Expression)} but will make sure
that if the property
      * being accessed is defined inside the classnode provided as a parameter, then a getter
call is generated
      * instead of a field access.
+     *
      * @param annotatedNode the class node where the property node is accessed from
      * @param pNode the property being accessed
      * @return a method call expression or a property expression
      */
-    public static Expression getterX(ClassNode annotatedNode, PropertyNode pNode) {
+    public static Expression getterThisX(ClassNode annotatedNode, PropertyNode pNode) {
+        return getterX(annotatedNode, new VariableExpression("this"), pNode);
+    }
+
+    /**
+     * This method is similar to {@link #propX(Expression, Expression)} but will make sure
that if the property
+     * being accessed is defined inside the classnode provided as a parameter, then a getter
call is generated
+     * instead of a field access.
+     *
+     * @param annotatedNode the class node where the property node is accessed from
+     * @param receiver the object having the property
+     * @param pNode the property being accessed
+     * @return a method call expression or a property expression
+     */
+    public static Expression getterX(ClassNode annotatedNode, Expression receiver, PropertyNode
pNode) {
         ClassNode owner = pNode.getDeclaringClass();
         if (annotatedNode.equals(owner)) {
             String getterName = "get" + MetaClassHelper.capitalize(pNode.getName());
-            if (ClassHelper.boolean_TYPE.equals(pNode.getOriginType())) {
+            boolean existingExplicitGetter = annotatedNode.getGetterMethod(getterName) !=
null;
+            if (ClassHelper.boolean_TYPE.equals(pNode.getOriginType()) && !existingExplicitGetter)
{
                 getterName = "is" + MetaClassHelper.capitalize(pNode.getName());
             }
-            return callX(new VariableExpression("this"), getterName, ArgumentListExpression.EMPTY_ARGUMENTS);
+            return callX(receiver, getterName);
         }
-        return propX(new VariableExpression("this"), pNode.getName());
+        return propX(receiver, pNode.getName());
     }
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/8a861aaf/src/main/org/codehaus/groovy/transform/EqualsAndHashCodeASTTransformation.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/transform/EqualsAndHashCodeASTTransformation.java
b/src/main/org/codehaus/groovy/transform/EqualsAndHashCodeASTTransformation.java
index a440b52..24eaf52 100644
--- a/src/main/org/codehaus/groovy/transform/EqualsAndHashCodeASTTransformation.java
+++ b/src/main/org/codehaus/groovy/transform/EqualsAndHashCodeASTTransformation.java
@@ -126,7 +126,7 @@ public class EqualsAndHashCodeASTTransformation extends AbstractASTTransformatio
         for (PropertyNode pNode : pList) {
             if (shouldSkip(pNode.getName(), excludes, includes)) continue;
             // _result = HashCodeHelper.updateHash(_result, getProperty()) // plus self-reference
checking
-            Expression getter = getterX(cNode, pNode);
+            Expression getter = getterThisX(cNode, pNode);
             final Expression current = callX(HASHUTIL_TYPE, "updateHash", args(result, getter));
             body.addStatement(ifS(
                     notX(sameX(getter, varX("this"))),

http://git-wip-us.apache.org/repos/asf/groovy/blob/8a861aaf/src/main/org/codehaus/groovy/transform/ToStringASTTransformation.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/transform/ToStringASTTransformation.java b/src/main/org/codehaus/groovy/transform/ToStringASTTransformation.java
index 4ed3303..ff2484f 100644
--- a/src/main/org/codehaus/groovy/transform/ToStringASTTransformation.java
+++ b/src/main/org/codehaus/groovy/transform/ToStringASTTransformation.java
@@ -160,7 +160,7 @@ public class ToStringASTTransformation extends AbstractASTTransformation
{
         }
         for (PropertyNode pNode : pList) {
             if (shouldSkip(pNode.getName(), excludes, includes)) continue;
-            Expression getter = getterX(cNode, pNode);
+            Expression getter = getterThisX(cNode, pNode);
 
             appendValue(body, result, first, getter, pNode.getName(), includeNames, ignoreNulls);
         }


Mime
View raw message