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ů.

IndeterminateArgType.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import org.aspectj.testing.Tester;
  2. import org.aspectj.lang.*;
  3. import org.aspectj.lang.reflect.*;
  4. import java.util.Arrays;
  5. /** @testcase PR#764 check arg types with indeterminate prefix and suffix */
  6. public class IndeterminateArgType {
  7. public static void main (String[] args) {
  8. OTarget o;
  9. STarget c;
  10. // both pointcuts pick out args 1..3; neither picks out args 0
  11. // both actual Object and String match
  12. c = new STarget();
  13. c = new STarget("s1");
  14. c = new STarget("s1", "s2");
  15. c = new STarget("s1", "s2", "s3");
  16. c.f();
  17. c.f("s1");
  18. c.f("s1", "s2");
  19. c.f("s1", "s2", "s3");
  20. // String pointcut should match even though declared type is Object
  21. o = new OTarget();
  22. o = new OTarget("o1");
  23. o = new OTarget("o1", "o2");
  24. o = new OTarget("o1", "o2", "o3");
  25. o.f();
  26. o.f("o1");
  27. o.f("o1", "o2");
  28. o.f("o1", "o2", "o3");
  29. // only actual Object types match these
  30. Object input = new Object();
  31. o = new OTarget();
  32. o = new OTarget(input);
  33. o = new OTarget(input, input);
  34. o = new OTarget(input, input, input);
  35. o.f();
  36. o.f(input);
  37. o.f(input, input);
  38. o.f(input, input, input);
  39. Tester.checkEventsFromFile("IndeterminateArgType.events");
  40. }
  41. }
  42. interface Safe {}
  43. class OTarget implements Safe {
  44. OTarget() { }
  45. OTarget(Object s1) { }
  46. OTarget(Object s1, Object s2) { }
  47. OTarget(Object s1, Object s2, Object s3) { }
  48. void f() { }
  49. void f(Object s1) { }
  50. void f(Object s1, Object s2) { }
  51. void f(Object s1, Object s2, Object s3) { }
  52. }
  53. class STarget implements Safe {
  54. STarget() { }
  55. STarget(String s1) { }
  56. STarget(String s1, String s2) { }
  57. STarget(String s1, String s2, String s3) { }
  58. void f() { }
  59. void f(String s1) { }
  60. void f(String s1, String s2) { }
  61. void f(String s1, String s2, String s3) { }
  62. }
  63. class T {
  64. static void e(String s) {
  65. Tester.event(s);
  66. }
  67. }
  68. aspect A {
  69. String actualTypes(JoinPoint jp) { // XXX gather as utility
  70. Object[] types = jp.getArgs();
  71. StringBuffer sb = new StringBuffer();
  72. sb.append("[");
  73. for (int i = 0; i < types.length; i++) {
  74. sb.append(types[i].getClass().getName());
  75. if ((1+i) < types.length) {
  76. sb.append(", ");
  77. }
  78. }
  79. sb.append("]");
  80. return sb.toString();
  81. }
  82. void check(String pc, JoinPoint jp) {
  83. Class[] types = ((CodeSignature) jp.getSignature()).getParameterTypes();
  84. String name = jp.toLongString() + " " + actualTypes(jp) + ": " + pc;
  85. T.e(name);
  86. }
  87. pointcut safe() : (call(Safe+.new(..))) ||
  88. (call(* *.*(..)) && target(Safe));
  89. pointcut o1End() : args(.., Object);
  90. pointcut s1End() : args(.., String);
  91. pointcut o1Start() : args(Object, ..);
  92. pointcut s1Start() : args(String, ..);
  93. before() : safe() && o1Start() { check ("o1Start()", thisJoinPoint); }
  94. before() : safe() && o1End() { check ("o1End()", thisJoinPoint); }
  95. before() : safe() && s1Start() { check ("s1Start()", thisJoinPoint); }
  96. before() : safe() && s1End() { check ("s1End()", thisJoinPoint); }
  97. }