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.

Coverage.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import org.aspectj.testing.Tester;
  2. import org.aspectj.lang.*;
  3. import org.aspectj.lang.reflect.*;
  4. import java.util.*;
  5. public class Coverage extends Args {
  6. public static void main(String[] args) {
  7. Args.args = args;
  8. Args.c = new C();
  9. Args.c.x += 1;
  10. Args.c.mumble("foo");
  11. }
  12. }
  13. class C {
  14. public int x;
  15. void mumble(String arg1) {
  16. Args.arg1 = arg1;
  17. try {
  18. throw new Exception("test");
  19. } catch (Exception e) {
  20. }
  21. }
  22. }
  23. class Args {
  24. public static String[] args;
  25. public static String arg1;
  26. public static C c;
  27. }
  28. class SubC {
  29. }
  30. aspect JoinPoints extends Helper {
  31. before(): set(* C.*) {
  32. JoinPoint jp = thisJoinPoint;
  33. p("sets", jp);
  34. a(jp.getKind(),"field-set");
  35. Integer integer = null;
  36. Object o = null;
  37. try {
  38. o = jp.getTarget().getClass().getField
  39. (jp.getSignature().getName()).get(jp.getTarget());
  40. } catch (Exception e) { a(e); return; }
  41. try {
  42. integer = (Integer)o;
  43. } catch (ClassCastException cce) {
  44. a(ni(o, Integer.class, "set-1"));
  45. return;
  46. }
  47. a(integer.intValue() == 0, "set value i != 0");
  48. o = jp.getArgs()[0];
  49. try {
  50. integer = (Integer) o;
  51. } catch (ClassCastException cce) {
  52. a(ni(o, Integer.class, "set-2"));
  53. return;
  54. }
  55. a(integer.intValue() == 1, "set newvalue i != 1");
  56. Object ex = jp.getThis();
  57. JoinPoint.StaticPart sp = jp.getStaticPart();
  58. FieldSignature fs = null;
  59. Signature sig = jp.getSignature();
  60. try {
  61. fs = (FieldSignature)sig;
  62. } catch (ClassCastException cce) {
  63. a(ni(sig, FieldSignature.class, "set-1"));
  64. }
  65. }
  66. before(): get(* C.*) {
  67. JoinPoint jp = thisJoinPoint;
  68. p("gets", jp);
  69. a(jp.getKind(),"field-get");
  70. Integer integer = null;
  71. Object o = null;
  72. try {
  73. o = jp.getTarget().getClass().getField
  74. (jp.getSignature().getName()).get(jp.getTarget());
  75. } catch (Exception e) { a(e); }
  76. try {
  77. integer = (Integer) o;
  78. } catch (ClassCastException cce) {
  79. a(ni(o, Integer.class, "get"));
  80. return;
  81. }
  82. a(integer.intValue() == 0, "get value i != 0");
  83. Object ex = jp.getThis();
  84. JoinPoint.StaticPart sp = jp.getStaticPart();
  85. FieldSignature fs = null;
  86. Signature sig = jp.getSignature();
  87. try {
  88. fs = (FieldSignature)sig;
  89. } catch (ClassCastException cce) {
  90. a(ni(sig, FieldSignature.class, "get"));
  91. return;
  92. }
  93. }
  94. before(): execution(* C.*(..)) {
  95. JoinPoint jp = thisJoinPoint;
  96. p("executions", jp);
  97. a(jp.getKind(), "method-execution");
  98. Signature sig = jp.getSignature();
  99. CodeSignature cs = null;
  100. try {
  101. cs = (CodeSignature)sig;
  102. } catch (ClassCastException cce) {
  103. a(ni(sig, CodeSignature.class, "execution"));
  104. }
  105. String name = cs.getName();
  106. got(name + ".execution");
  107. if (name.equals("mumble")) {
  108. Object[] params = jp.getArgs();
  109. Object target = jp.getThis();
  110. String arg1 = null;
  111. try {
  112. arg1 = (String) params[0];
  113. } catch (ArrayIndexOutOfBoundsException ae) {
  114. a("params.size < 1");
  115. return;
  116. } catch (ClassCastException cce) {
  117. a(ni(params[0], String.class));
  118. return;
  119. }
  120. a(target == Args.c, target + " != " + Args.c + ", should be c - 2");
  121. }
  122. }
  123. before(): call(C.new(..)) {
  124. JoinPoint jp = thisJoinPoint;
  125. p("calls", jp);
  126. a(jp.getKind(),"constructor-call");
  127. Signature sig = jp.getSignature();
  128. ConstructorSignature cs = null;
  129. try {
  130. cs = (ConstructorSignature)sig;
  131. } catch (ClassCastException cce) {
  132. a(ni(cs, ConstructorSignature.class));
  133. }
  134. Object[] params = jp.getArgs();
  135. Object target = jp.getThis();
  136. String name = cs.getName();
  137. a(name, "<init>");
  138. a(new Integer(params.length), new Integer(0));
  139. a(target == Args.c, target + " != " + Args.c + ", should be c - 3");
  140. }
  141. before(): execution(C.new(..)) {
  142. JoinPoint jp = thisJoinPoint;
  143. p("executions", jp);
  144. a(jp.getKind(),"constructor-execution");
  145. Signature sig = jp.getSignature();
  146. ConstructorSignature cs = null;
  147. try {
  148. cs = (ConstructorSignature)sig;
  149. } catch (ClassCastException cce) {
  150. a(ni(sig, ConstructorSignature.class));
  151. }
  152. Object[] params = jp.getArgs();
  153. Object target = jp.getThis();
  154. String name = cs.getName();
  155. a(name, "<init>");
  156. a(new Integer(params.length), new Integer(0));
  157. }
  158. before(): handler(*) && !within(JoinPoints) {
  159. JoinPoint jp = thisJoinPoint;
  160. p("handle", jp);
  161. a(jp.getKind(),"exception-handler");
  162. Signature sig = jp.getSignature();
  163. CatchClauseSignature ccs = null;
  164. try {
  165. ccs = (CatchClauseSignature)sig;
  166. } catch (ClassCastException cce) {
  167. a(ni(sig, CatchClauseSignature.class));
  168. }
  169. Throwable t = null;
  170. try {
  171. t = (Throwable)jp.getArgs()[0];
  172. } catch (ArrayIndexOutOfBoundsException _) {
  173. a("handlers out of bounds");
  174. } catch (ClassCastException __) {
  175. a(ni(jp.getArgs()[0], Throwable.class, "handlers"));
  176. }
  177. a(t.getMessage(), "test");
  178. Object ex = jp.getThis();
  179. a(ex, Args.c);
  180. JoinPoint.StaticPart sp = jp.getStaticPart();
  181. }
  182. }
  183. abstract aspect Helper {
  184. final static String[] strings = {
  185. "mumble.execution",
  186. "calls",
  187. "executions",
  188. "gets",
  189. "sets",
  190. "executions",
  191. "handle",
  192. };
  193. static Map hash = new HashMap();
  194. static List names = new Vector();
  195. static {
  196. for (int i = 0; i < strings.length; i++) {
  197. need(strings[i]);
  198. }
  199. }
  200. after(): execution(* main(..)) {
  201. checkAll();
  202. }
  203. static void checkAll() {
  204. Iterator iter = names.iterator();
  205. while (iter.hasNext()) {
  206. Object method = iter.next();
  207. Object call = hash.get(method);
  208. a(call != null, method + " was not called");
  209. }
  210. }
  211. static void got(Object method) {
  212. hash.put(method, method);
  213. }
  214. static void need(Object method) {
  215. names.add(method);
  216. }
  217. static void a(Object o1, Object o2) {
  218. String msg = "" + o1 + " != " + o2;
  219. a(o1, o2, msg);
  220. }
  221. static void a(Object o1, Object o2, String msg) {
  222. if (o1 != null) a(o1.equals(o2), msg);
  223. else if (o2 != null) a(o2.equals(o1), msg);
  224. }
  225. static void a(boolean b, Object o) {
  226. Tester.check(b, o + "");
  227. }
  228. static void a(Object o) {
  229. a(false, o);
  230. }
  231. static String ni(Object o, Class c, String s) {
  232. return ni("(" + s + ") " + o, c);
  233. }
  234. static String ni(Object o, Class c) {
  235. return o +
  236. (o == null ? " is null " : " is a " + o.getClass()) +
  237. " is not a " + c;
  238. }
  239. static boolean verbose = false;
  240. static void p(Object o) {
  241. if (verbose) System.out.println(o);
  242. }
  243. static void p(Object o, JoinPoint jp) {
  244. got(o);
  245. }
  246. static void po(Object o) {
  247. boolean ov = verbose;
  248. verbose = true;
  249. p(o);
  250. verbose = ov;
  251. }
  252. }