You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/01/31 18:55:36 UTC

[groovy] 06/06: GROOVY-9575: `ASTNode#hashCode` does not conform to the general contract

This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 491d3f2c481297f65a457110ef28d1ba13fff47d
Author: Anders Wallgren <aw...@cloudbees.com>
AuthorDate: Tue May 26 19:18:37 2020 -0700

    GROOVY-9575: `ASTNode#hashCode` does not conform to the general contract
    
    (cherry picked from commit eb8539f45daf1b980b99ddd824cb05ed2e14cc1f)
    
    Conflicts:
    	src/main/java/org/codehaus/groovy/ast/ASTNode.java
---
 src/main/java/org/codehaus/groovy/ast/ASTNode.java | 45 ++++++++--------------
 src/test/org/codehaus/groovy/ast/ASTNodeTest.java  | 33 ++++++++++++++++
 2 files changed, 50 insertions(+), 28 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/ast/ASTNode.java b/src/main/java/org/codehaus/groovy/ast/ASTNode.java
index 3b68f1e..598d9af 100644
--- a/src/main/java/org/codehaus/groovy/ast/ASTNode.java
+++ b/src/main/java/org/codehaus/groovy/ast/ASTNode.java
@@ -23,7 +23,6 @@ import org.codehaus.groovy.util.ListHashMap;
 
 import java.util.Collections;
 import java.util.Map;
-import java.util.Objects;
 
 /**
  * Base class for any AST node. This class supports basic information used in all nodes of the AST:
@@ -31,15 +30,15 @@ import java.util.Objects;
  * <li> line and column number information. Usually a node represents a certain
  * area in a text file determined by a starting position and an ending position.
  * For nodes that do not represent this, this information will be -1. A node can
- * also be configured in its line/col information using another node through 
+ * also be configured in its line/col information using another node through
  * setSourcePosition(otherNode).</li>
- * <li> every node can store meta data. A phase operation or transform can use 
- * this to transport arbitrary information to another phase operation or 
+ * <li> every node can store meta data. A phase operation or transform can use
+ * this to transport arbitrary information to another phase operation or
  * transform. The only requirement is that the other phase operation or transform
- * runs after the part storing the information. If the information transport is 
- * done it is strongly recommended to remove that meta data.</li> 
+ * runs after the part storing the information. If the information transport is
+ * done it is strongly recommended to remove that meta data.</li>
  * <li> a text representation of this node trough getText(). This was in the
- * past used for assertion messages. Since the usage of power asserts this 
+ * past used for assertion messages. Since the usage of power asserts this
  * method will not be called for this purpose anymore and might be removed in
  * future versions of Groovy</li>
  * </ul>
@@ -91,13 +90,13 @@ public class ASTNode {
     public void setLastColumnNumber(int lastColumnNumber) {
         this.lastColumnNumber = lastColumnNumber;
     }
-    
+
     /**
      * Sets the source position using another ASTNode.
      * The sourcePosition consists of a line/column pair for
      * the start and a line/column pair for the end of the
-     * expression or statement 
-     * 
+     * expression or statement
+     *
      * @param node - the node used to configure the position information
      */
     public void setSourcePosition(ASTNode node) {
@@ -106,10 +105,10 @@ public class ASTNode {
         this.lastColumnNumber = node.getLastColumnNumber();
         this.lineNumber = node.getLineNumber();
     }
-    
+
     /**
-     * Gets the node meta data. 
-     * 
+     * Gets the node meta data.
+     *
      * @param key - the meta data key
      * @return the node meta data value for this key
      */
@@ -133,13 +132,13 @@ public class ASTNode {
         }
         metaDataMap.putAll(other.metaDataMap);
     }
-    
+
     /**
-     * Sets the node meta data. 
-     * 
+     * Sets the node meta data.
+     *
      * @param key - the meta data key
      * @param value - the meta data value
-     * @throws GroovyBugError if key is null or there is already meta 
+     * @throws GroovyBugError if key is null or there is already meta
      *                        data under that key
      */
     public void setNodeMetaData(Object key, Object value) {
@@ -169,7 +168,7 @@ public class ASTNode {
 
     /**
      * Removes a node meta data entry.
-     * 
+     *
      * @param key - the meta data key
      * @throws GroovyBugError if the key is null
      */
@@ -195,14 +194,4 @@ public class ASTNode {
     public ListHashMap getMetaDataMap() {
         return metaDataMap;
     }
-
-    @Override
-    public boolean equals(Object o) {
-        return this == o;
-    }
-
-    @Override
-    public int hashCode() {
-        return Objects.hash(lineNumber, columnNumber, lastLineNumber, lastColumnNumber);
-    }
 }
diff --git a/src/test/org/codehaus/groovy/ast/ASTNodeTest.java b/src/test/org/codehaus/groovy/ast/ASTNodeTest.java
new file mode 100644
index 0000000..c069b4b
--- /dev/null
+++ b/src/test/org/codehaus/groovy/ast/ASTNodeTest.java
@@ -0,0 +1,33 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.codehaus.groovy.ast;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public final class ASTNodeTest {
+    @Test
+    public void testHashCode() {
+        ASTNode astNode = new ASTNode();
+        int hashcode = astNode.hashCode();
+        astNode.setLineNumber(astNode.getLineNumber() + 10);
+        assertEquals("hashCode is consistent", hashcode, astNode.hashCode());
+    }
+}