HashcodeBuilder is broken for Annotations
-----------------------------------------
Key: LANG-403
URL: https://issues.apache.org/jira/browse/LANG-403
Project: Commons Lang
Issue Type: Bug
Affects Versions: 2.3
Reporter: Jay Liang
HashCodeBuilder does not produce a correct hashCode that conforms to java
language specs. Running the following code snippets, i get
@Simple$CustomAnnotation(name=blah, type=class java.lang.String) hashCode=
895255138
[EMAIL PROTECTED],_type=class java.lang.String] hashCode= 122852694
hashCode should be 895255138
The hashCode should computed according to
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/annotation/Annotation.html#hashCode()
for annotations.
===============
public class Simple{
@BindingAnnotation
@Retention(RetentionPolicy.RUNTIME)
@Target({FIELD, METHOD, PARAMETER})
private @interface CustomAnnotation{
public String name();
public Class<?> type();
}
private static class CustomAnnotationImpl implements CustomAnnotation{
final String _name;
final Class<?> _type;
public CustomAnnotationImpl(String name, Class<?> type) {
_name = Objects.nonNull(name, "property name should not be null");
_type = Objects.nonNull(type, "property type should not be null");
}
public String name() {
return _name;
}
public Class<?> type() {
return _type;
}
public Class<? extends Annotation> annotationType() {
return CustomAnnotation.class;
}
@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
/**
* This conform to specs defined at
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/annotation/Annotation.html#hashCode
* @return good annotation hashCode
*/
public int goodHashCode() {
return (127 * "name".hashCode() ^ _name.hashCode()) + (127
*"type".hashCode() ^ _type.hashCode());
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
private static class Test{
@CustomAnnotation(name="blah", type=String.class)
String _blah;
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
public static void main(String[] args) throws Exception{
for(Field f : Test.class.getDeclaredFields()){
for(Annotation a : f.getAnnotations()){
System.out.println(a + " hashCode= " + a.hashCode());
CustomAnnotationImpl c = new Simple.CustomAnnotationImpl("blah",
String.class);
System.out.println(c + " hashCode= " + c.hashCode());
System.out.println("hashCode should be "+ c.goodHashCode());
}
}
}
}
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.