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.

OverridingInterfaceObjectMethod.java 954B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import org.aspectj.testing.Tester;
  2. public class OverridingInterfaceObjectMethod {
  3. private static final int VALUE = 10;
  4. public static void main(String[] args) {
  5. Identifiable i = new C();
  6. Tester.checkEqual(i.hashCode(), 42); //System.identityHashCode(i));
  7. i.setId(new Id(VALUE));
  8. Tester.checkEqual(i.hashCode(), VALUE);
  9. }
  10. }
  11. //TODO explore complicated inheritance hierarchies
  12. class C implements Identifiable {}
  13. interface Base { }
  14. interface Identifiable extends Base {
  15. void setId(Id id);
  16. Id getId();
  17. }
  18. class Id {
  19. public Id(int value) {
  20. this.value = value;
  21. }
  22. int value;
  23. }
  24. aspect IdentifiableAspect {
  25. private Id Identifiable.id = null;
  26. public Id Identifiable.getId() {
  27. return this.id;
  28. }
  29. public void Identifiable.setId(Id id) {
  30. this.id = id;
  31. }
  32. public int Identifiable.hashCode() {
  33. return (this.getId() == null)
  34. ? super.hashCode()
  35. : this.getId().value;
  36. }
  37. public int Base.hashCode() {
  38. return 42;
  39. }
  40. }