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.

CounterTest04.java 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import java.lang.reflect.Field;
  2. import java.lang.reflect.Modifier;
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. /**
  7. * In this test, we have multiple identical cflow() pointcut 'pieces' used in a number of
  8. * pointcuts. We are not naming a cflow() and reusing it, we are just duplicating the
  9. * pointcut in multiple places - can we share counters?
  10. */
  11. public class CounterTest04 {
  12. public static void main(String []argv) {
  13. new CounterTest04().sayMessage();
  14. int ctrs = ReflectionHelper.howManyCflowCounterFields(Cflow1.aspectOf());
  15. // if (ctrs!=2) {
  16. // throw new RuntimeException("Should be two cflow counters, but found: "+ctrs);
  17. // }
  18. int stacks = ReflectionHelper.howManyCflowStackFields(Cflow1.aspectOf());
  19. if (stacks!=2) {
  20. throw new RuntimeException("Should be two cflow stacks, but found: "+stacks);
  21. }
  22. }
  23. public void sayMessage() {
  24. printmsg("Hello "); printmsg("World\n");
  25. }
  26. public void printmsg(String msg) {
  27. System.out.print(msg);
  28. }
  29. }
  30. aspect Cflow1 {
  31. // CflowCounter created for this pointcut should be shared below!
  32. pointcut p1(Object o): cflow(execution(* main(..)) && args(o));
  33. before(Object o): call(* print(..)) && p1(o) {
  34. // Managed by a CflowCounter
  35. }
  36. before(Object o): call(* print(..)) && p1(o) {
  37. // Managed by a CflowCounter
  38. }
  39. before(Object o): execution(* print(..)) && cflow(execution(* main(..)) && target(o)) {
  40. // Managed by a CflowStack - since state is exposed
  41. }
  42. }
  43. class ReflectionHelper {
  44. public static List getCflowfields(Object o,boolean includeCounters,boolean includeStacks) {
  45. List res = new ArrayList();
  46. Class clazz = o.getClass();
  47. Field[] fs = clazz.getDeclaredFields();
  48. for (int i = 0; i < fs.length; i++) {
  49. Field f = fs[i];
  50. if ((f.getType().getName().endsWith("CFlowCounter") && includeCounters) ||
  51. (f.getType().getName().endsWith("CFlowStack") && includeStacks)) {
  52. res.add(f.getType().getName()+":"+f.getName());
  53. }
  54. }
  55. return res;
  56. }
  57. public static int howManyCflowCounterFields(Object o) {
  58. return getCflowfields(o,true,false).size();
  59. }
  60. public static int howManyCflowStackFields(Object o) {
  61. return getCflowfields(o,false,true).size();
  62. }
  63. }