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.

ReflectOnAtAspectJDeclareParents.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import org.aspectj.lang.reflect.*;
  2. import java.util.*;
  3. import java.lang.reflect.*;
  4. public class ReflectOnAtAspectJDeclareParents {
  5. public static void main(String[] args) {
  6. new ReflectOnAtAspectJDeclareParents().runTests();
  7. }
  8. public void runTests() {
  9. AjType<AtAspectJDeclareParents> ajType = AjTypeSystem.getAjType(AtAspectJDeclareParents.class);
  10. testDeclareParents(ajType);
  11. testDeclaredInterTypeMethods(ajType);
  12. testInterTypeMethods(ajType);
  13. testDeclaredInterTypeFields(ajType);
  14. testInterTypeFields(ajType);
  15. }
  16. private void testDeclareParents(AjType<AtAspectJDeclareParents> ajType) {
  17. DeclareParents[] dps = ajType.getDeclareParents();
  18. assertEquals(1,dps.length,"number of declare parents");
  19. System.out.println(dps[0]);
  20. }
  21. private void testDeclaredInterTypeMethods(AjType<AtAspectJDeclareParents> ajType) {
  22. InterTypeMethodDeclaration[] itdms = ajType.getDeclaredITDMethods();
  23. assertEquals(2,itdms.length,"number of declared ITD methods");
  24. Set s = new TreeSet(new Comparator<InterTypeMethodDeclaration>() {
  25. public int compare(InterTypeMethodDeclaration m1, InterTypeMethodDeclaration m2) {
  26. if (m1 == m2) return 0;
  27. int vis = (m1.getModifiers() - m2.getModifiers());
  28. if (vis != 0) return vis;
  29. int name = (m1.getName().compareTo(m2.getName()));
  30. if (name != 0) return name;
  31. try {
  32. return (
  33. m1.getTargetType().getJavaClass().getName().compareTo(
  34. m2.getTargetType().getJavaClass().getName())
  35. );
  36. } catch (ClassNotFoundException ex) {
  37. throw new RuntimeException(ex);
  38. }
  39. }
  40. });
  41. for (InterTypeMethodDeclaration itdm : itdms) { s.add(itdm); }
  42. for (Object o : s) { System.out.println(o); }
  43. try {
  44. InterTypeMethodDeclaration shouldFind = ajType.getDeclaredITDMethod("getX",AjTypeSystem.getAjType(I.class));
  45. System.out.println(shouldFind);
  46. } catch (NoSuchMethodException ex) {
  47. throw new RuntimeException("getITDMethod failed");
  48. }
  49. try {
  50. ajType.getDeclaredITDMethod("getP",AjTypeSystem.getAjType(I.class));
  51. throw new RuntimeException("failed to fail in getting ITDMethod");
  52. } catch (NoSuchMethodException ex) {
  53. // good!
  54. }
  55. }
  56. private void testInterTypeMethods(AjType<AtAspectJDeclareParents> ajType) {
  57. InterTypeMethodDeclaration[] itdms = ajType.getITDMethods();
  58. assertEquals(2,itdms.length,"number of ITD methods");
  59. Set s = new TreeSet(new Comparator<InterTypeMethodDeclaration>() {
  60. public int compare(InterTypeMethodDeclaration m1, InterTypeMethodDeclaration m2) {
  61. if (m1 == m2) return 0;
  62. int vis = (m1.getModifiers() - m2.getModifiers());
  63. if (vis != 0) return vis;
  64. int name = (m1.getName().compareTo(m2.getName()));
  65. if (name != 0) return name;
  66. try {
  67. return (
  68. m1.getTargetType().getJavaClass().getName().compareTo(
  69. m2.getTargetType().getJavaClass().getName())
  70. );
  71. } catch (ClassNotFoundException ex) {
  72. throw new RuntimeException(ex);
  73. }
  74. }
  75. });
  76. for (InterTypeMethodDeclaration itdm : itdms) { s.add(itdm); }
  77. for (Object o : s) { System.out.println(o); }
  78. try {
  79. InterTypeMethodDeclaration shouldFind = ajType.getITDMethod("getX",AjTypeSystem.getAjType(I.class));
  80. System.out.println(shouldFind);
  81. } catch (NoSuchMethodException ex) {
  82. throw new RuntimeException("getITDMethod failed");
  83. }
  84. try {
  85. ajType.getITDMethod("getX",AjTypeSystem.getAjType(C.class));
  86. throw new RuntimeException("failed to fail in getting ITDMethod");
  87. } catch (NoSuchMethodException ex) {
  88. // good!
  89. }
  90. }
  91. private void testDeclaredInterTypeFields(AjType<AtAspectJDeclareParents> ajType) {
  92. InterTypeFieldDeclaration[] itdfs = ajType.getDeclaredITDFields();
  93. assertEquals(1,itdfs.length,"number of declared ITD fields");
  94. System.out.println(itdfs[0]);
  95. try {
  96. InterTypeFieldDeclaration shouldFind = ajType.getDeclaredITDField("x",AjTypeSystem.getAjType(I.class));
  97. System.out.println(shouldFind);
  98. } catch (NoSuchFieldException ex) {
  99. throw new RuntimeException("getITDField failed");
  100. }
  101. try {
  102. ajType.getDeclaredITDField("p",AjTypeSystem.getAjType(C.class));
  103. throw new RuntimeException("failed to fail in getting ITDField");
  104. } catch (NoSuchFieldException ex) {
  105. // good!
  106. }
  107. }
  108. private void testInterTypeFields(AjType<AtAspectJDeclareParents> ajType) {
  109. InterTypeFieldDeclaration[] itdfs = ajType.getITDFields();
  110. assertEquals(0,itdfs.length,"number of declared ITD fields");
  111. }
  112. private void assertEquals(int x, int y, String msg) {
  113. if (x != y) {
  114. throw new RuntimeException(msg + " expecting '" + x + "' but was '" + y + "'");
  115. }
  116. }
  117. }