You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StackError.java 1.1KB

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