I added this code to the org.exolab.castor.builder.SourceFactory class:
/**
* Create an "hashCode" method on the given JClass.
*
* @param jclass the JClass in wich we create the hashCode method.
*/
public static void createHashCodeMethod(JClass jclass) {
if (jclass == null) {
throw new IllegalArgumentException("JClass must not be
null");
}
// The argument is not null
JField[] fields = jclass.getFields();
// Creates the method signature
JMethod jMethod = new JMethod(JType.Int, "hashCode");
jMethod.setComment("Override the java.lang.Object.hashCode
method");
// The hashCode method has no arguments
jclass.addMethod(jMethod);
JSourceCode jsc = jMethod.getSourceCode();
// The following steps came from Effective Java Programming
Language
Guide by Joshua Bloch, chapter 3
jsc.add("int result = 17;");
for (int i = 0; i <fields.length; i++) {
JField temp = fields[i];
// If the field is an object the hashCode method is
called recursively
JType type = temp.getType();
String name = temp.getName();
if (type.isPrimitive()) {
if (type == JType.Boolean) {
jsc.add("result = 37 * result + (" +
name + "?0:1);");
} else
if ((type == JType.Byte) ||
(type == JType.Short) ||
(type == JType.Int)) {
jsc.add("result = 37 * result + (int)"
+ name + ";");
} else if (type == JType.Long) {
jsc.add("result = 37 * result + (int)("
+ name + "^(" + name +
">>>32));");
} else if (type == JType.Float) {
jsc.add("result = 37 * result +
Float.floatToIntBits(" + name + ");");
} else if (type == JType.Double) {
jsc.add("long tmp =
Double.doubleToLongBits(" + name + ");");
jsc.add("result = 37 * result +
(int)(tmp^(tmp>>>32));");
}
} else {
// Calculates hashCode in a recursive manner.
jsc.add("result = 37 * result + " + name +
".hashCode();");
}
}
jsc.add("");
jsc.add("return result;");
} //createHashCodeMethod
and into the createSourceCode() method I've added the following control
//create equals() method?
if (component.hasEquals()) {
createEqualsMethod(jClass);
createHashCodeMethod(jClass);
}
since the hashCode method *must* be added when the equals method is
created (see Effective Java Programming Language Guide by Joshua Bloch,
chapter 3).
Best regards,
Michele
Ralf Joachim wrote:
> Hi Michele,
>
> SourceGenerator seams not to be able to generate hashcode() / equals()
> methods at the moment. In general I don't see a reason why it should not
> be possible to improve SourceGenerator to also generate them. As with
> other open source projects we are open for contributions. So you are
> open to volunteer on this addition.
>
> Regards
> Ralf
> Castor JDO, committer
>
>
> Michele Mazzucco schrieb:
>> Hi all,
>>
>> is it possible to "autogenerate" the hashCode() method on java objects
>> created from the xml schema?, otherwise checks on the server side could
>> return wrong values (i.e. checking if an object sent by by client is
>> stored in a collection hosted by the server side).
>>
>>
>> Thanks in advance,
>> Michele
>>
>> -------------------------------------------------
>> If you wish to unsubscribe from this list, please send an empty
>> message to the following address:
>>
>> [EMAIL PROTECTED]
>> -------------------------------------------------
>
> -------------------------------------------------
> If you wish to unsubscribe from this list, please send an empty message
> to the following address:
>
> [EMAIL PROTECTED]
> -------------------------------------------------
>
-------------------------------------------------
If you wish to unsubscribe from this list, please
send an empty message to the following address:
[EMAIL PROTECTED]
-------------------------------------------------