Generated equals in Webservice Stub

Tags: , ,



All the generated webservice-stubs from our backend have an equals-method similar to this one:

private java.lang.Object __equalsCalc = null;
public synchronized boolean equals(java.lang.Object obj) {
    if (!(obj instanceof PropertyData)) return false;
    PropertyData other = (PropertyData) obj;
    if (obj == null) return false;
    if (this == obj) return true;
    if (__equalsCalc != null) {
        return (__equalsCalc == obj);
    }
    __equalsCalc = obj;
    boolean _equals;
    _equals = true && 
        ((this.key==null && other.getKey()==null) || 
         (this.key!=null &&
          this.key.equals(other.getKey()))) &&
        ((this.value==null && other.getValue()==null) || 
         (this.value!=null &&
          this.value.equals(other.getValue())));
    __equalsCalc = null;
    return _equals;
}

Can someone please explain to me the purpoise of __equalsCalc? I just don’t get it. It is not used somewhere else in the class. The way I see it, it is not null exactly during the calculation of the “equality”. However the equals is declared synchronized. Therefore only one thread can be in it at any given time. I can’t see, why if (__equalsCalc != null) should ever be true.

Please show me my stupid misunderstanding 😉

EDIT: I am new to the project and therefore my answer may be wrong. But if I trace it correctly, the method is generated by axis-wsdl2java

Answer

I suspect it’s trying to stop recursion – if something has itself as its key, you don’t want it to recurse forever.

However, at that point it uses reference equality for the test, which is possibly not desirable.

What’s generating this code? Is it a home-grown system, or a public one? If it’s home-grown, is the developer responsible still at the company?

EDIT: Okay, now that we know it’s WSDL2Java, the answer is (sort of) in the source code:

    // The __equalsCalc field and synchronized method are necessary
    // in case the object has direct or indirect references to itself.

and then:

    // Have we been here before ? return true if yes otherwise false
    pw.println("        if (__equalsCalc != null) {");
    pw.println("            return (__equalsCalc == obj);");
    pw.println("        }");

Not the most explanatory of comments, but at least they show we were on the right track thinking about recursion.



Source: stackoverflow