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.

TestUtils.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* *******************************************************************
  2. * Copyright (c) 2008 Contributors
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Andy Clement initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver;
  13. import java.lang.reflect.Modifier;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. public class TestUtils {
  17. private static final String[] ZERO_STRINGS = new String[0];
  18. // For stringifying a delegate - extracted from AbstractReferenceTypeDelegate, not fixed up
  19. // /**
  20. // * Create the string representation for a delegate, allowing us to
  21. // * more easily compare delegate implementations.
  22. // */
  23. // public String stringifyDelegate() {
  24. //
  25. // StringBuffer result = new StringBuffer();
  26. // result.append("=== Delegate for "+getResolvedTypeX().getName()+"\n");
  27. //
  28. // result.append("isAspect?"+isAspect()+"\n");
  29. // result.append("isAnnotationStyleAspect?"+isAnnotationStyleAspect()+"\n");
  30. // result.append("isInterface?"+isInterface()+"\n");
  31. // result.append("isEnum?"+isEnum()+"\n");
  32. // result.append("isClass?"+isClass()+"\n");
  33. // result.append("-\n");
  34. // result.append("isAnnotation?"+isAnnotation()+"\n");
  35. // result.append("retentionPolicy="+getRetentionPolicy()+"\n");
  36. // result.append("canAnnotationTargetType?"+canAnnotationTargetType()+"\n");
  37. // AnnotationTargetKind[] kinds = getAnnotationTargetKinds();
  38. // if (kinds!=null && kinds.length>0) {
  39. // result.append("annotationTargetKinds:[");
  40. // for (int i = 0; i < kinds.length; i++) {
  41. // AnnotationTargetKind kind = kinds[i];
  42. // result.append(kind);
  43. // if ((i+1)<kinds.length) result.append(" ");
  44. // }
  45. // result.append("]\n");
  46. // }
  47. // result.append("isAnnotationWithRuntimeRetention?"+isAnnotationWithRuntimeRetention()+"\n");
  48. // result.append("-\n");
  49. //
  50. // result.append("isAnonymous?"+isAnonymous()+"\n");
  51. // result.append("isNested?"+isNested()+"\n");
  52. // result.append("-\n");
  53. //
  54. // result.append("isGeneric?"+isGeneric()+"\n");
  55. // result.append("declaredGenericSignature="+getDeclaredGenericSignature()+"\n");
  56. // result.append("-\n");
  57. //
  58. // AnnotationX[] axs = getAnnotations();
  59. // if (axs!=null && axs.length>0) {
  60. // result.append("getAnnotations() returns: "+axs.length+" annotations\n");
  61. // for (int i = 0; i < axs.length; i++) {
  62. // AnnotationX annotationX = axs[i];
  63. // result.append(" #"+i+") "+annotationX+"\n");
  64. // }
  65. // } else {
  66. // result.append("getAnnotations() returns nothing\n");
  67. // }
  68. // ResolvedType[] axtypes = getAnnotationTypes();
  69. // if (axtypes!=null && axtypes.length>0) {
  70. // result.append("getAnnotationTypes() returns: "+axtypes.length+" annotations\n");
  71. // for (int i = 0; i < axtypes.length; i++) {
  72. // ResolvedType annotation = axtypes[i];
  73. // result.append(" #"+i+") "+annotation+":"+annotation.getClass()+"\n");
  74. // }
  75. // } else {
  76. // result.append("getAnnotationTypes() returns nothing\n");
  77. // }
  78. //
  79. // result.append("isExposedToWeaver?"+isExposedToWeaver()+"\n");
  80. // result.append("getSuperclass?"+getSuperclass()+"\n");
  81. // result.append("getResolvedTypeX?"+getResolvedTypeX()+"\n");
  82. // result.append("--\n");
  83. //
  84. // ResolvedMember[] fields = getDeclaredFields();
  85. // if (fields!=null && fields.length>0) {
  86. // result.append("The fields: "+fields.length+"\n");
  87. // for (int i = 0; i < fields.length; i++) {
  88. // ResolvedMember member = fields[i];
  89. // result.append("f"+i+") "+member.toDebugString()+"\n");
  90. // }
  91. // }
  92. // ResolvedMember[] methods = getDeclaredMethods();
  93. // if (methods!=null && methods.length>0) {
  94. // result.append("The methods: "+methods.length+"\n");
  95. // for (int i = 0; i < methods.length; i++) {
  96. // ResolvedMember member = methods[i];
  97. // result.append("m"+i+") "+member.toDebugString()+"\n");
  98. // }
  99. // }
  100. // ResolvedType[] interfaces = getDeclaredInterfaces();
  101. // if (interfaces!=null && interfaces.length>0) {
  102. // result.append("The interfaces: "+interfaces.length+"\n");
  103. // for (int i = 0; i < interfaces.length; i++) {
  104. // ResolvedType member = interfaces[i];
  105. // result.append("i"+i+") "+member+"\n");
  106. // }
  107. // }
  108. //
  109. // result.append("getModifiers?"+getModifiers()+"\n");
  110. //
  111. // result.append("perclause="+getPerClause()+"\n");
  112. //
  113. // result.append("aj:weaverstate="+getWeaverState()+"\n");
  114. //
  115. // ResolvedMember[] pointcuts = getDeclaredPointcuts();
  116. // if (pointcuts!=null && pointcuts.length>0) {
  117. // result.append("The pointcuts: "+pointcuts.length+"\n");
  118. //
  119. // // Sort the damn things
  120. // List sortedSetOfPointcuts = new ArrayList();
  121. // for (int i = 0; i < pointcuts.length; i++) {sortedSetOfPointcuts.add(pointcuts[i]);}
  122. // Collections.sort(sortedSetOfPointcuts);
  123. //
  124. // int i =0;
  125. // for (Iterator iter = sortedSetOfPointcuts.iterator(); iter.hasNext();) {
  126. // ResolvedMember member = (ResolvedMember) iter.next();
  127. // result.append("p"+i+") "+member.toDebugString()+"\n");
  128. // i++;
  129. // }
  130. // }
  131. //
  132. // Collection declares = getDeclares();
  133. // if (declares.size()>0) {
  134. // result.append("The declares: "+declares.size()+"\n");
  135. //
  136. // // // Sort the damn things
  137. // // List sortedSetOfPointcuts = new ArrayList();
  138. // // for (int i = 0; i < pointcuts.length; i++) {sortedSetOfPointcuts.add(pointcuts[i]);}
  139. // // Collections.sort(sortedSetOfPointcuts);
  140. //
  141. // int i=0;
  142. // for (Iterator iter = declares.iterator(); iter.hasNext();) {
  143. // Declare dec = (Declare) iter.next();
  144. // result.append("d"+i+") "+dec.toString()+"\n");
  145. // i++;
  146. // }
  147. // }
  148. //
  149. // TypeVariable[] tv = getTypeVariables();
  150. // if (tv!=null && tv.length>0) {
  151. // result.append("The type variables: "+tv.length+"\n");
  152. // for (int i = 0; i < tv.length; i++) {
  153. // result.append("tv"+i+") "+tv[i]+"\n");
  154. // }
  155. // }
  156. //
  157. // Collection tmungers = getTypeMungers();
  158. // if (tmungers.size()>0) {
  159. // List sorted = new ArrayList();
  160. // sorted.addAll(tmungers);
  161. // Collections.sort(sorted,new Comparator() {
  162. // public int compare(Object arg0, Object arg1) {
  163. // return arg0.toString().compareTo(arg1.toString());
  164. // }
  165. // });
  166. // result.append("The type mungers: "+tmungers.size()+"\n");
  167. // int i=0;
  168. // for (Iterator iter = sorted.iterator(); iter.hasNext();) {
  169. // ConcreteTypeMunger mun = (ConcreteTypeMunger) iter.next();
  170. // result.append("tm"+i+") "+mun.toString()+"\n");
  171. // i++;
  172. // }
  173. // }
  174. //
  175. // result.append("doesNotExposeShadowMungers?"+doesNotExposeShadowMungers()+"\n");
  176. //
  177. // Collection pas = getPrivilegedAccesses();
  178. // if (pas!=null && pas.size()>0) {
  179. // // List sorted = new ArrayList();
  180. // // sorted.addAll(tmungers);
  181. // // Collections.sort(sorted,new Comparator() {
  182. // // public int compare(Object arg0, Object arg1) {
  183. // // return arg0.toString().compareTo(arg1.toString());
  184. // // }
  185. // // });
  186. // result.append("The privileged accesses: "+pas.size()+"\n");
  187. // int i=0;
  188. // for (Iterator iter = pas.iterator(); iter.hasNext();) {
  189. // ResolvedMember mun = (ResolvedMember) iter.next();
  190. // result.append("tm"+i+") "+mun.toDebugString()+"\n");
  191. // i++;
  192. // }
  193. // }
  194. //
  195. // // public Collection getPrivilegedAccesses();
  196. // // public boolean hasAnnotation(UnresolvedType ofType);
  197. // result.append("===");
  198. // return result.toString();
  199. // }
  200. /**
  201. * Build a member from a string representation: <blockquote>
  202. *
  203. * <pre>
  204. * static? TypeName TypeName.Id
  205. * </pre>
  206. *
  207. * </blockquote>
  208. */
  209. public static MemberImpl fieldFromString(String str) {
  210. str = str.trim();
  211. final int len = str.length();
  212. int i = 0;
  213. int mods = 0;
  214. if (str.startsWith("static", i)) {
  215. mods = Modifier.STATIC;
  216. i += 6;
  217. while (Character.isWhitespace(str.charAt(i)))
  218. i++;
  219. }
  220. int start = i;
  221. while (!Character.isWhitespace(str.charAt(i)))
  222. i++;
  223. UnresolvedType retTy = UnresolvedType.forName(str.substring(start, i));
  224. start = i;
  225. i = str.lastIndexOf('.');
  226. UnresolvedType declaringTy = UnresolvedType.forName(str.substring(start, i).trim());
  227. start = ++i;
  228. String name = str.substring(start, len).trim();
  229. return new MemberImpl(Member.FIELD, declaringTy, mods, retTy, name, UnresolvedType.NONE);
  230. }
  231. /**
  232. * Build a member from a string representation: <blockquote>
  233. *
  234. * <pre>
  235. * (static|interface|private)? TypeName TypeName . Id ( TypeName , ...)
  236. * </pre>
  237. *
  238. * </blockquote>
  239. */
  240. public static Member methodFromString(String str) {
  241. str = str.trim();
  242. // final int len = str.length();
  243. int i = 0;
  244. int mods = 0;
  245. if (str.startsWith("static", i)) {
  246. mods = Modifier.STATIC;
  247. i += 6;
  248. } else if (str.startsWith("interface", i)) {
  249. mods = Modifier.INTERFACE;
  250. i += 9;
  251. } else if (str.startsWith("private", i)) {
  252. mods = Modifier.PRIVATE;
  253. i += 7;
  254. }
  255. while (Character.isWhitespace(str.charAt(i)))
  256. i++;
  257. int start = i;
  258. while (!Character.isWhitespace(str.charAt(i)))
  259. i++;
  260. UnresolvedType returnTy = UnresolvedType.forName(str.substring(start, i));
  261. start = i;
  262. i = str.indexOf('(', i);
  263. i = str.lastIndexOf('.', i);
  264. UnresolvedType declaringTy = UnresolvedType.forName(str.substring(start, i).trim());
  265. start = ++i;
  266. i = str.indexOf('(', i);
  267. String name = str.substring(start, i).trim();
  268. start = ++i;
  269. i = str.indexOf(')', i);
  270. String[] paramTypeNames = parseIds(str.substring(start, i).trim());
  271. return MemberImpl.method(declaringTy, mods, returnTy, name, UnresolvedType.forNames(paramTypeNames));
  272. }
  273. public static String[] parseIds(String str) {
  274. if (str.length() == 0)
  275. return ZERO_STRINGS;
  276. List<String> l = new ArrayList<String>();
  277. int start = 0;
  278. while (true) {
  279. int i = str.indexOf(',', start);
  280. if (i == -1) {
  281. l.add(str.substring(start).trim());
  282. break;
  283. }
  284. l.add(str.substring(start, i).trim());
  285. start = i + 1;
  286. }
  287. return (String[]) l.toArray(new String[l.size()]);
  288. }
  289. }