Name: dsR10051 Date: 02/21/2002
Method hashCode() is not declared in classes
java.beans.PropertyDescriptor and java.beans.IndexedPropertyDescriptor
It violates the general contract of this method that declares
the equality of hash codes if objects are equals.
This contract is explained in javadoc for java.lang.Object.hashCode():
"
/**
* Returns a hash code value for the object. This method is
* supported for the benefit of hashtables such as those provided by
* <code>java.util.Hashtable</code>.
* <p>
* The general contract of <code>hashCode</code> is:
* <ul>
... skipped
* <li>If two objects are equal according to the <tt>equals(Object)</tt>
* method, then calling the <code>hashCode</code> method on each of
* the two objects must produce the same integer result.
... skipped
*/
public native int hashCode();
"
Here is minimized test:
--- IndexedPropertyDescriptorTest02.java ---
import java.beans.*;
public class IndexedPropertyDescriptorTest02 {
public static void main(String[] args) {
PropertyDescriptor pd = null;
PropertyDescriptor obj = null;
try {
pd = new IndexedPropertyDescriptor("child", Wombat.class);
obj = new IndexedPropertyDescriptor("child", Wombat.class);
} catch (IntrospectionException ie) {
System.out.println("Unexpected IntrospectionException");
return;
}
if (pd.equals(obj) && (pd.hashCode() != obj.hashCode())) {
System.out.println("IndexedPropertyDescriptor objects are equals, but their hash codes are different");
} else {
System.out.println("OKAY");
}
try {
pd = new PropertyDescriptor("child", Wombat.class);
obj = new PropertyDescriptor("child", Wombat.class);
} catch (IntrospectionException ie) {
System.out.println("Unexpected IntrospectionException");
return;
}
if (pd.equals(obj) && (pd.hashCode() != obj.hashCode())) {
System.out.println("PropertyDescriptor objects are equals, but their hash codes are different");
} else {
System.out.println("OKAY");
}
}
}
--- Wombat.java ---
public class Wombat {
Wombat[] child = new Wombat[10];
public Wombat getChild(int index) {
return child[index];
}
public void setChild(int index, Wombat w) {
child[index] = w;
}
public Wombat[] getChild() {
return child;
}
public void setChild(Wombat[] child) {
this.child = child;
}
}
--- Output ---
% /set/java/jdk1.4.1/solaris/bin/java -version
java version "1.4.1-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-beta-b02)
Java HotSpot(TM) Client VM (build 1.4.1-beta-b02, mixed mode)
% /set/java/jdk1.4.1/solaris/bin/java IndexedPropertyDescriptorTest02
IndexedPropertyDescriptor objects are equals, but their hash codes are different
PropertyDescriptor objects are equals, but their hash codes are different
======================================================================