Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

CounterTest02.java 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 create a pointcut p1() which uses cflow and then we reference
  8. * it in two other anonymous pointcuts attached to advice. The cflow() should be managed
  9. * by a counter (as no state is maintained) and the reused pointcut should not mean two
  10. * counters are created. One counter should be created and shared.
  11. */
  12. public class CounterTest02 {
  13. public static void main(String []argv) {
  14. new CounterTest02().sayMessage();
  15. int ctrs = ReflectionHelper.howManyCflowCounterFields(Cflow1.aspectOf());
  16. int stacks = ReflectionHelper.howManyCflowStackFields(Cflow1.aspectOf());
  17. if (ctrs!=1)
  18. throw new RuntimeException("Should be one cflow counter, but found: "+ctrs);
  19. if (stacks!=1)
  20. throw new RuntimeException("Should be one cflow stacks, but found: "+stacks);
  21. if (Cflow1.stackAdvice!=2)
  22. throw new RuntimeException("Expected two piece of stack advice to run: "+Cflow1.stackAdvice);
  23. if (Cflow1.counterAdvice!=4)
  24. throw new RuntimeException("Expected four pieces of counter advice to run: "+Cflow1.counterAdvice);
  25. }
  26. public void sayMessage() {
  27. printmsg("Hello "); printmsg("World\n");
  28. }
  29. public void printmsg(String msg) {
  30. System.out.print(msg);
  31. }
  32. }
  33. aspect Cflow1 {
  34. public static int stackAdvice = 0;
  35. public static int counterAdvice = 0;
  36. // CflowCounter created for this pointcut should be shared below!
  37. pointcut p1(): cflow(execution(* main(..)));
  38. before(): call(* print(..)) && p1() {
  39. // Managed by a CflowCounter
  40. Cflow1.counterAdvice++;
  41. }
  42. before(): call(* print(..)) && p1() {
  43. // Managed by a CflowCounter
  44. Cflow1.counterAdvice++;
  45. }
  46. before(Object o): call(* print(..)) && cflow(execution(* main(..)) && args(o)) {
  47. // Managed by a CflowStack - since state is exposed
  48. Cflow1.stackAdvice++;
  49. }
  50. }
  51. class ReflectionHelper {
  52. public static List getCflowfields(Object o,boolean includeCounters,boolean includeStacks) {
  53. List res = new ArrayList();
  54. Class clazz = o.getClass();
  55. Field[] fs = clazz.getDeclaredFields();
  56. for (int i = 0; i < fs.length; i++) {
  57. Field f = fs[i];
  58. if ((f.getType().getName().endsWith("CFlowCounter") && includeCounters) ||
  59. (f.getType().getName().endsWith("CFlowStack") && includeStacks)) {
  60. res.add(f.getType().getName()+":"+f.getName());
  61. }
  62. }
  63. return res;
  64. }
  65. public static int howManyCflowCounterFields(Object o) {
  66. return getCflowfields(o,true,false).size();
  67. }
  68. public static int howManyCflowStackFields(Object o) {
  69. return getCflowfields(o,false,true).size();
  70. }
  71. }