blob: 2b3a04a090ba3a151952e49ccd50740cf3bf65b0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
import java.lang.reflect.Method;
public class StackError {
public static void main(String args[]) {
new StackError().testEqualsNull();
}
void assertTrue(String msg, boolean b) {}
public void testEqualsNull() {
StackError one = new StackError();
StackError two = new StackError();
assertTrue("equal", one.equals(two)); // does not work
//boolean yes = one.equals(two); // works
}
public boolean equals(Object other) {
return true;
}
}
aspect EqualsContract {
pointcut equalsCall(Object thisOne, Object otherOne):
target(Object+) &&
target(thisOne) &&
call(public boolean equals(Object+)) &&
args(otherOne) &&
!within(EqualsContract);
boolean around(Object thisOne, Object otherOne):
equalsCall(thisOne, otherOne) {
boolean result = proceed(thisOne, otherOne);
Class cls = thisOne.getClass();
String name = cls.getName();
boolean hasHashCode = false;
try {
Method m = cls.getDeclaredMethod("hashCode", null);
String lookFor = "public int " + name + ".hashCode()";
hasHashCode = lookFor.equals(m.toString());
}
catch (NoSuchMethodException nsme) {
}
return result;
}
}
|