Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Major
-
Resolution: Won't Fix
-
Affects Version/s: 1.0-beta-6
-
Fix Version/s: None
-
Component/s: groovy-jdk
-
Labels:None
Description
println "${2}".hashCode() == "2".hashCode()
prints
false
println "${2}" == "2"
prints true
The hashCodes should be identical.
I reproduced this issue with the test case below and found a little more information. The equals() and hashCode() methods seem to be consistent with each other but inconsistent with ==. This is probably because "2" is a java.lang.String and "${2}" is a groovy.lang.GString.
void testEqualsTemplateToLiteral() {
def template = "${2}"
def literal = "2"
// prints "class java.lang.String"
print("literal: ${literal.getClass()}\n")
// prints "class GStringTest$67"
print("template: ${template.getClass()}\n")
// succeeds
assertTrue("template == literal not true", template == literal)
// these all fail
assertTrue("literal not equal to template", literal.equals(template))
assertTrue("template not equal to literal", template.equals(literal))
assertEquals("hash codes not equal", literal.hashCode(), template.hashCode())
}