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.

Code.aj 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package test;
  2. import org.aspectj.lang.annotation.Aspect;
  3. import org.aspectj.lang.annotation.DeclareParents;
  4. interface A {
  5. void doA();
  6. }
  7. interface BBad extends A {
  8. void doB();
  9. }
  10. interface BGood extends A {
  11. void doB();
  12. void doA();
  13. }
  14. class TargetBad { }
  15. class TargetGood { }
  16. @Aspect
  17. class DeclareParentsAspect {
  18. // @DeclareParents(value = "test.TargetGood", defaultImpl = BImplGood.class)
  19. // private BGood bGood;
  20. @DeclareParents(value = "test.TargetBad", defaultImpl = BImplGood.class)
  21. private BBad bBad;
  22. public static class BImplGood implements BGood, BBad {
  23. public void doB() {
  24. System.out.println("doB");
  25. }
  26. public void doA() {
  27. System.out.println("doA");
  28. }
  29. }
  30. }
  31. public class Code {
  32. public static void main(String... args) {
  33. /*
  34. {
  35. TargetGood target = new TargetGood();
  36. BGood b = (BGood) target;
  37. b.doB();
  38. b.doA();
  39. }
  40. */
  41. {
  42. TargetBad target = new TargetBad();
  43. BBad b = (BBad) target;
  44. b.doB();
  45. /*
  46. The following line is the problem.
  47. The Generated class should refer to ajc$test_DeclareParentsAspect$test_BBad
  48. Instead...
  49. Exception in thread "main" java.lang.NoSuchFieldError: ajc$test_DeclareParentsAspect$test_A
  50. at test.TargetBad.doA(TargetBad.java:1)
  51. at test.Main.main(Main.java:21)
  52. */
  53. b.doA();
  54. }
  55. }
  56. }