Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CounterTest01.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 testcase we are using cflow() with a pointcut that doesn't include state -
  8. * this should be managed by our new CflowCounter rather than a CflowStack.
  9. *
  10. * Because the two cflow pointcuts are identical (both are 'cflow(execution(* main(..))' it also
  11. * means we can share a single counter for both of them !
  12. */
  13. public class CounterTest01 {
  14. public static void main(String []argv) {
  15. new CounterTest01().sayMessage();
  16. int ctrs = ReflectionHelper.howManyCflowCounterFields(Cflow1.aspectOf());
  17. if (ctrs!=1) {
  18. throw new RuntimeException("Should be one cflow counter, but found: "+ctrs);
  19. }
  20. int stacks = ReflectionHelper.howManyCflowStackFields(Cflow1.aspectOf());
  21. if (stacks!=1) {
  22. throw new RuntimeException("Should be one cflow stacks, but found: "+stacks);
  23. }
  24. }
  25. public void sayMessage() {
  26. print("Hello ");
  27. print("World\n");
  28. }
  29. public void print(String msg) {
  30. System.out.print(msg);
  31. }
  32. }
  33. aspect Cflow1 {
  34. before(): execution(* print(..)) && cflow(execution(* main(..))) {
  35. // Managed by a CflowCounter
  36. }
  37. before(): execution(* print(..)) && cflow(execution(* main(..))) {
  38. // Managed by a CflowCounter
  39. }
  40. before(Object o): execution(* print(..)) && cflow(execution(* main(..)) && target(o)) {
  41. // Managed by a CflowStack - since state is exposed
  42. }
  43. }
  44. class ReflectionHelper {
  45. public static List getCflowfields(Object o,boolean includeCounters,boolean includeStacks) {
  46. List res = new ArrayList();
  47. Class clazz = o.getClass();
  48. Field[] fs = clazz.getDeclaredFields();
  49. for (int i = 0; i < fs.length; i++) {
  50. Field f = fs[i];
  51. if ((f.getType().getName().endsWith("CFlowCounter") && includeCounters) ||
  52. (f.getType().getName().endsWith("CFlowStack") && includeStacks)) {
  53. res.add(f.getType().getName()+":"+f.getName());
  54. }
  55. }
  56. return res;
  57. }
  58. public static int howManyCflowCounterFields(Object o) {
  59. return getCflowfields(o,true,false).size();
  60. }
  61. public static int howManyCflowStackFields(Object o) {
  62. return getCflowfields(o,false,true).size();
  63. }
  64. }