Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

GeneratedStaticAsTransient.java 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import org.aspectj.testing.*;
  2. import java.lang.reflect.*;
  3. // XXX incomplete - find all cases of generated static fields
  4. /** @testcase PR#704 mark generated static fields transient */
  5. public class GeneratedStaticAsTransient {
  6. public static void main (String[] args) {
  7. new GeneratedStaticAsTransient().callRun();
  8. checkStatic(GeneratedStaticAsTransient.class, false);
  9. Tester.checkAllEvents();
  10. }
  11. public void callRun() { run(); }
  12. public void run() {
  13. Tester.event("run");
  14. Tester.check(null != A.aspectOf(this),"null != A.hasAspect(this)");
  15. Tester.check(null != C.aspectOf(this),"null != C.hasAspect(this)");
  16. Tester.check(null != B.aspectOf(),"null != B.hasAspect()");
  17. }
  18. static {
  19. Tester.expectEvent("after returning - target");
  20. Tester.expectEvent("after returning - this");
  21. Tester.expectEvent("after returning - cflow");
  22. Tester.expectEvent("run");
  23. }
  24. public static void checkStatic(Class c, boolean requireStatic) {
  25. boolean gotStatic = false;
  26. Field[] fields = c.getFields();
  27. for (int i = 0; i < fields.length; i++) {
  28. int mods = fields[i].getModifiers();
  29. //System.err.println("checking " + fields[i]);
  30. if (Modifier.isStatic(mods)) {
  31. //System.err.println(" static " + fields[i]);
  32. if (!gotStatic) gotStatic = true;
  33. if (!Modifier.isTransient(mods)) {
  34. String m = "field " + i + " "
  35. + c.getName() + "." + fields[i].getName()
  36. + " is static but not transient. mods=" + mods;
  37. //System.err.println(m);
  38. Tester.check(false, m);
  39. }
  40. }
  41. }
  42. if (requireStatic) {
  43. Tester.check(gotStatic, c + "no static field");
  44. }
  45. }
  46. }
  47. aspect A pertarget(callRun()) {
  48. pointcut callRun() : call(void GeneratedStaticAsTransient.run());
  49. after () returning : callRun() {
  50. Tester.event("after returning - target");
  51. GeneratedStaticAsTransient.checkStatic(A.class, false);
  52. }
  53. }
  54. aspect B percflow(A.callRun()) {
  55. after () returning : A.callRun() {
  56. Tester.event("after returning - cflow");
  57. GeneratedStaticAsTransient.checkStatic(B.class, true);
  58. }
  59. }
  60. aspect C perthis(A.callRun()) {
  61. after () returning : A.callRun() {
  62. Tester.event("after returning - this");
  63. GeneratedStaticAsTransient.checkStatic(C.class, false);
  64. }
  65. }