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.

CounterTest03.java 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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, a cflow() pointcut is named and then reused. It refers to state and so
  8. * we must manage it with a CFlowStack - can we share the stacks?
  9. */
  10. public class CounterTest03 {
  11. public static void main(String []argv) {
  12. new CounterTest03().sayMessage();
  13. int ctrs = ReflectionHelper.howManyCflowCounterFields(Cflow1.aspectOf());
  14. if (ctrs!=0) {
  15. throw new RuntimeException("Should be zero cflow counters, but found: "+ctrs);
  16. }
  17. int stacks = ReflectionHelper.howManyCflowStackFields(Cflow1.aspectOf());
  18. if (stacks!=1) {
  19. throw new RuntimeException("Should be one cflow stacks, but found: "+stacks);
  20. }
  21. }
  22. public void sayMessage() {
  23. printmsg("Hello "); printmsg("World\n");
  24. }
  25. public void printmsg(String msg) {
  26. System.out.print(msg);
  27. }
  28. }
  29. aspect Cflow1 {
  30. // CflowCounter created for this pointcut should be shared below!
  31. pointcut p1(Object o): cflow(execution(* main(..)) && args(o));
  32. before(Object o): call(* print(..)) && p1(o) {
  33. // Managed by a CflowCounter
  34. }
  35. before(Object o): call(* print(..)) && p1(o) {
  36. // Managed by a CflowCounter
  37. }
  38. // before(Object o): execution(* print(..)) && cflow(execution(* main(..)) && target(o)) {
  39. // // Managed by a CflowStack - since state is exposed
  40. // }
  41. }
  42. class ReflectionHelper {
  43. public static List getCflowfields(Object o,boolean includeCounters,boolean includeStacks) {
  44. List res = new ArrayList();
  45. Class clazz = o.getClass();
  46. Field[] fs = clazz.getDeclaredFields();
  47. for (int i = 0; i < fs.length; i++) {
  48. Field f = fs[i];
  49. if ((f.getType().getName().endsWith("CFlowCounter") && includeCounters) ||
  50. (f.getType().getName().endsWith("CFlowStack") && includeStacks)) {
  51. res.add(f.getType().getName()+":"+f.getName());
  52. }
  53. }
  54. return res;
  55. }
  56. public static int howManyCflowCounterFields(Object o) {
  57. return getCflowfields(o,true,false).size();
  58. }
  59. public static int howManyCflowStackFields(Object o) {
  60. return getCflowfields(o,false,true).size();
  61. }
  62. }