For the first time, I have to implement my own proxy classes using the standard JDK Dynamic Proxy. It works quite well, with one exception: method equals(...).
Suppose we have a simple interface like this, which we want a proxy:
public interface MyInterface {
public String getID();
public void setID(String id);
}
... and our implementation looks like this (standard Java Bean with generated hashCode()and equals):
public class MyImplementation implements MyInterface {
private String id;
public String getID() { return this.id; }
public void setID(String id) { this.id = id; }
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (this.databaseId == null ? 0 :
this.id.hashCode());
return result;
}
public final boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (this.getClass() != obj.getClass()) {
return false;
}
MyImplementation other = (MyImplementation) obj;
if (this.databaseId == null) {
if (other.databaseId != null) {
return false;
}
} else if (!this.databaseId.equals(other.databaseId)) {
return false;
}
return true;
}
}
The problem is that when I create a proxy server, the method is equals(...)no longer symmetrical:
original.equals(original);
proxy.equals(original);
original.equals(proxy);
proxy.equals(proxy);
This is also stated in this article .
My question is: if I want all four “equal” cases to deliver true, what is the best (that is, the safest and least intrusive) way to do this?