views:

142

answers:

2

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

A: 

The __equalsCal behave as a Cache for the last call to Equals. So, if two calls in the same instance to equals method are invoked with the same parameter the method will be faster

EDIT: Sorry this would be true if the

__equalsCalc = null;

would not be there, but rechecking your code, probably my answer is not valid.

David Santamaria
Nope, it gets set to null before the return statement
Jon Skeet
Yep, just realize about that in the moment of Post the answer. Edit the answer for not confuse anybody.
David Santamaria
I was thinking the same first. When I saw the null-assignment I was completely puzzled and decided to post to post to SO
Mo
+3  A: 

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.

Jon Skeet
I assume, it has something to do with recursion... But I can't figure out what exactly it tries to accomplish
Mo
BTW, seems like is code generated by WSDL2Java ( http://www.google.com/search?q=__equalsCalc )
David Santamaria