commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Piotr Kaczmarski (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (LANG-1039) HashCodeBuilder.reflectionHashCode(Object object) returns always the same result for any array
Date Thu, 25 Sep 2014 09:14:34 GMT

    [ https://issues.apache.org/jira/browse/LANG-1039?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14147565#comment-14147565
] 

Piotr Kaczmarski commented on LANG-1039:
----------------------------------------

The Javadoc for method HashCodeBuilder.reflectionHashCode states:
{quote}"This method uses reflection to build a valid hash code."{quote}
And a valid hashCode implementation indeed doesn't have to produce distinct values for different
instances:
{quote}
As much as is reasonably practical, the hashCode method defined by class Object does return
distinct integers for distinct objects. (This is typically implemented by converting the internal
address of the object into an integer, but this implementation technique is not required by
the JavaTM programming language.)
{quote}

However in this particular implementation we can apply "the principle of least astonishment":
{quote}
If a necessary feature has a high astonishment factor, it may be necessary to redesign the
feature.
{quote}

One can be astonished seeing how this method returns the same value (namely 17) for every
array, no matter how big or of what type.

> HashCodeBuilder.reflectionHashCode(Object object) returns always the same result for
any array
> ----------------------------------------------------------------------------------------------
>
>                 Key: LANG-1039
>                 URL: https://issues.apache.org/jira/browse/LANG-1039
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.builder.*
>    Affects Versions: 3.3.2
>         Environment: Windows 7, Java 6
>            Reporter: Bartosz Paszkowski
>
> HashCodeBuilder.reflectionHashCode(Object object) returns always the same result for
any array. The result is 17.
> There is no information in javadoc, that this method works in that way.
> The same situation in previous versions.
> *Example:*
> {code}
> public class HashCodeBuilderTest {
>     
>     public static void main(String[] args) {
>         
>         System.out.println(HashCodeBuilder.reflectionHashCode(new double[] {1, 1}));
>         System.out.println(HashCodeBuilder.reflectionHashCode(new double[] {2, 2}));
>         System.out.println(HashCodeBuilder.reflectionHashCode(new int[] {3, 3}));
>         System.out.println(HashCodeBuilder.reflectionHashCode(new int[] {4, 4}));
>         System.out.println(HashCodeBuilder.reflectionHashCode(new Long[] {5L, 5L}));
>         System.out.println(HashCodeBuilder.reflectionHashCode(new Double[] {null, null}));
>         System.out.println(HashCodeBuilder.reflectionHashCode(new Object[] {Boolean.FALSE,
1L, null}));
>         
>     }
> }
> {code}
> *Output:*
> {code}
> 17
> 17
> 17
> 17
> 17
> 17
> 17
> {code}
> *Fix example 1*
> First check if argument obj in HashCodeBuilder.reflectionHashCode(Object obj) is an array
and than use java.util.Arrays.hashCode(Object[] array)



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message