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.

ReflectOnCodeStyleITDs.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import org.aspectj.lang.reflect.*;
  2. import java.util.*;
  3. import java.lang.reflect.*;
  4. public class ReflectOnCodeStyleITDs {
  5. public static void main(String[] args) {
  6. new ReflectOnCodeStyleITDs().runTests();
  7. }
  8. public void runTests() {
  9. AjType<InterTypeDeclarations> ajType = AjTypeSystem.getAjType(InterTypeDeclarations.class);
  10. testDeclaredInterTypeConstructors(ajType);
  11. testInterTypeConstructors(ajType);
  12. testDeclaredInterTypeMethods(ajType);
  13. testInterTypeMethods(ajType);
  14. testDeclaredInterTypeFields(ajType);
  15. testInterTypeFields(ajType);
  16. }
  17. private void testDeclaredInterTypeConstructors(AjType<InterTypeDeclarations> ajType) {
  18. InterTypeConstructorDeclaration[] itdcs = ajType.getDeclaredITDConstructors();
  19. assertEquals(3,itdcs.length,"number of declared ITD constructors");
  20. InterTypeConstructorDeclaration publicITDC = null;
  21. InterTypeConstructorDeclaration defaultITDC = null;
  22. InterTypeConstructorDeclaration privateITDC = null;
  23. for(InterTypeConstructorDeclaration itdc : itdcs) {
  24. if (Modifier.isPublic(itdc.getModifiers())) {
  25. publicITDC = itdc;
  26. } else if (Modifier.isPrivate(itdc.getModifiers())) {
  27. privateITDC = itdc;
  28. } else {
  29. defaultITDC = itdc;
  30. }
  31. }
  32. System.out.println(publicITDC);
  33. System.out.println(defaultITDC);
  34. System.out.println(privateITDC);
  35. try {
  36. InterTypeConstructorDeclaration shouldFind = ajType.getDeclaredITDConstructor(AjTypeSystem.getAjType(C.class), AjTypeSystem.getAjType(int.class));
  37. System.out.println(shouldFind);
  38. } catch (NoSuchMethodException ex) {
  39. throw new RuntimeException("getDeclaredITDConstructor failed");
  40. }
  41. try {
  42. ajType.getDeclaredITDConstructor(AjTypeSystem.getAjType(I.class), AjTypeSystem.getAjType(int.class));
  43. throw new RuntimeException("failed to fail in getting DeclaredITDConstructor #1");
  44. } catch (NoSuchMethodException ex) {
  45. // good!
  46. }
  47. try {
  48. ajType.getDeclaredITDConstructor(AjTypeSystem.getAjType(C.class), AjTypeSystem.getAjType(String.class));
  49. throw new RuntimeException("failed to fail in getting DeclaredITDConstructor #2");
  50. } catch (NoSuchMethodException ex) {
  51. // good!
  52. }
  53. }
  54. private void testInterTypeConstructors(AjType<InterTypeDeclarations> ajType) {
  55. InterTypeConstructorDeclaration[] itdcs = ajType.getITDConstructors();
  56. assertEquals(1,itdcs.length,"number of ITD constructors");
  57. System.out.println(itdcs[0]);
  58. AjType<?> intClass = AjTypeSystem.getAjType(int.class);
  59. try {
  60. InterTypeConstructorDeclaration shouldFind = ajType.getITDConstructor(AjTypeSystem.getAjType(C.class), intClass, intClass, intClass);
  61. System.out.println(shouldFind);
  62. } catch (NoSuchMethodException ex) {
  63. throw new RuntimeException("getITDConstructor failed");
  64. }
  65. try {
  66. ajType.getITDConstructor(AjTypeSystem.getAjType(C.class), AjTypeSystem.getAjType(int.class));
  67. throw new RuntimeException("failed to fail in getting ITDConstructor");
  68. } catch (NoSuchMethodException ex) {
  69. // good!
  70. }
  71. }
  72. private void testDeclaredInterTypeMethods(AjType<InterTypeDeclarations> ajType) {
  73. InterTypeMethodDeclaration[] itdms = ajType.getDeclaredITDMethods();
  74. assertEquals(6,itdms.length,"number of declared ITD methods");
  75. Set s = new TreeSet(new Comparator<InterTypeMethodDeclaration>() {
  76. public int compare(InterTypeMethodDeclaration m1, InterTypeMethodDeclaration m2) {
  77. if (m1 == m2) return 0;
  78. int vis = (m1.getModifiers() - m2.getModifiers());
  79. if (vis != 0) return vis;
  80. int name = (m1.getName().compareTo(m2.getName()));
  81. if (name != 0) return name;
  82. try {
  83. return (
  84. m1.getTargetType().getJavaClass().getName().compareTo(
  85. m2.getTargetType().getJavaClass().getName())
  86. );
  87. } catch (ClassNotFoundException ex) {
  88. throw new RuntimeException(ex);
  89. }
  90. }
  91. });
  92. for (InterTypeMethodDeclaration itdm : itdms) { s.add(itdm); }
  93. for (Object o : s) { System.out.println(o); }
  94. try {
  95. InterTypeMethodDeclaration shouldFind = ajType.getDeclaredITDMethod("getX",AjTypeSystem.getAjType(C.class));
  96. System.out.println(shouldFind);
  97. } catch (NoSuchMethodException ex) {
  98. throw new RuntimeException("getITDMethod failed");
  99. }
  100. try {
  101. ajType.getDeclaredITDMethod("getP",AjTypeSystem.getAjType(C.class));
  102. throw new RuntimeException("failed to fail in getting ITDMethod");
  103. } catch (NoSuchMethodException ex) {
  104. // good!
  105. }
  106. }
  107. private void testInterTypeMethods(AjType<InterTypeDeclarations> ajType) {
  108. InterTypeMethodDeclaration[] itdms = ajType.getITDMethods();
  109. assertEquals(2,itdms.length,"number of ITD methods");
  110. Set s = new TreeSet(new Comparator<InterTypeMethodDeclaration>() {
  111. public int compare(InterTypeMethodDeclaration m1, InterTypeMethodDeclaration m2) {
  112. if (m1 == m2) return 0;
  113. int vis = (m1.getModifiers() - m2.getModifiers());
  114. if (vis != 0) return vis;
  115. int name = (m1.getName().compareTo(m2.getName()));
  116. if (name != 0) return name;
  117. try {
  118. return (
  119. m1.getTargetType().getJavaClass().getName().compareTo(
  120. m2.getTargetType().getJavaClass().getName())
  121. );
  122. } catch (ClassNotFoundException ex) {
  123. throw new RuntimeException(ex);
  124. }
  125. }
  126. });
  127. for (InterTypeMethodDeclaration itdm : itdms) { s.add(itdm); }
  128. for (Object o : s) { System.out.println(o); }
  129. try {
  130. InterTypeMethodDeclaration shouldFind = ajType.getITDMethod("getZ",AjTypeSystem.getAjType(C.class));
  131. System.out.println(shouldFind);
  132. } catch (NoSuchMethodException ex) {
  133. throw new RuntimeException("getITDMethod failed");
  134. }
  135. try {
  136. ajType.getITDMethod("getX",AjTypeSystem.getAjType(C.class));
  137. throw new RuntimeException("failed to fail in getting ITDMethod");
  138. } catch (NoSuchMethodException ex) {
  139. // good!
  140. }
  141. }
  142. private void testDeclaredInterTypeFields(AjType<InterTypeDeclarations> ajType) {
  143. InterTypeFieldDeclaration[] itdfs = ajType.getDeclaredITDFields();
  144. assertEquals(6,itdfs.length,"number of declared ITD fields");
  145. Set s = new TreeSet(new Comparator<InterTypeFieldDeclaration>() {
  146. public int compare(InterTypeFieldDeclaration m1, InterTypeFieldDeclaration m2) {
  147. if (m1 == m2) return 0;
  148. int vis = (m1.getModifiers() - m2.getModifiers());
  149. if (vis != 0) return vis;
  150. int name = (m1.getName().compareTo(m2.getName()));
  151. if (name != 0) return name;
  152. try {
  153. return (
  154. m1.getTargetType().getJavaClass().getName().compareTo(
  155. m2.getTargetType().getJavaClass().getName())
  156. );
  157. } catch (ClassNotFoundException ex) {
  158. throw new RuntimeException(ex);
  159. }
  160. }
  161. });
  162. for (InterTypeFieldDeclaration itdf : itdfs) { s.add(itdf); }
  163. for (Object o : s) { System.out.println(o); }
  164. try {
  165. InterTypeFieldDeclaration shouldFind = ajType.getDeclaredITDField("x",AjTypeSystem.getAjType(C.class));
  166. System.out.println(shouldFind);
  167. } catch (NoSuchFieldException ex) {
  168. throw new RuntimeException("getITDField failed");
  169. }
  170. try {
  171. ajType.getDeclaredITDField("p",AjTypeSystem.getAjType(C.class));
  172. throw new RuntimeException("failed to fail in getting ITDField");
  173. } catch (NoSuchFieldException ex) {
  174. // good!
  175. }
  176. }
  177. private void testInterTypeFields(AjType<InterTypeDeclarations> ajType) {
  178. InterTypeFieldDeclaration[] itdfs = ajType.getITDFields();
  179. assertEquals(2,itdfs.length,"number of declared ITD fields");
  180. Set s = new TreeSet(new Comparator<InterTypeFieldDeclaration>() {
  181. public int compare(InterTypeFieldDeclaration m1, InterTypeFieldDeclaration m2) {
  182. if (m1 == m2) return 0;
  183. int vis = (m1.getModifiers() - m2.getModifiers());
  184. if (vis != 0) return vis;
  185. int name = (m1.getName().compareTo(m2.getName()));
  186. if (name != 0) return name;
  187. try {
  188. return (
  189. m1.getTargetType().getJavaClass().getName().compareTo(
  190. m2.getTargetType().getJavaClass().getName())
  191. );
  192. } catch (ClassNotFoundException ex) {
  193. throw new RuntimeException(ex);
  194. }
  195. }
  196. });
  197. for (InterTypeFieldDeclaration itdf : itdfs) { s.add(itdf); }
  198. for (Object o : s) { System.out.println(o); }
  199. try {
  200. InterTypeFieldDeclaration shouldFind = ajType.getITDField("z",AjTypeSystem.getAjType(C.class));
  201. System.out.println(shouldFind);
  202. } catch (NoSuchFieldException ex) {
  203. throw new RuntimeException("getITDField failed");
  204. }
  205. try {
  206. ajType.getITDField("x",AjTypeSystem.getAjType(C.class));
  207. throw new RuntimeException("failed to fail in getting ITDField");
  208. } catch (NoSuchFieldException ex) {
  209. // good!
  210. }
  211. }
  212. private void assertEquals(int x, int y, String msg) {
  213. if (x != y) {
  214. throw new RuntimeException(msg + " expecting '" + x + "' but was '" + y + "'");
  215. }
  216. }
  217. }