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.

idioms.adoc 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. = Idioms
  2. [[idioms-intro]]
  3. == Introduction
  4. This chapter consists of very short snippets of AspectJ code, typically
  5. pointcuts, that are particularly evocative or useful. This section is a
  6. work in progress.
  7. Here's an example of how to enfore a rule that code in the `java.sql`
  8. package can only be used from one particular package in your system.
  9. This doesn't require any access to code in the `java.sql` package.
  10. [source, java]
  11. ....
  12. /* Any call to methods or constructors in java.sql */
  13. pointcut restrictedCall():
  14. call(* java.sql.*.*(..)) || call(java.sql.*.new(..));
  15. /* Any code in my system not in the sqlAccess package */
  16. pointcut illegalSource():
  17. within(com.foo..*) && !within(com.foo.sqlAccess.*);
  18. declare error: restrictedCall() && illegalSource():
  19. "java.sql package can only be accessed from com.foo.sqlAccess";
  20. ....
  21. Any call to an instance of a subtype of `AbstractFacade` whose class is
  22. not exactly equal to `AbstractFacade`:
  23. [source, java]
  24. ....
  25. pointcut nonAbstract(AbstractFacade af):
  26. call(* *(..))
  27. && target(af)
  28. && !if(af.getClass() == AbstractFacade.class);
  29. ....
  30. If `AbstractFacade` is an abstract class or an interface, then every
  31. instance must be of a subtype and you can replace this with:
  32. [source, java]
  33. ....
  34. pointcut nonAbstract(AbstractFacade af):
  35. call(* *(..))
  36. && target(af);
  37. ....
  38. Any call to a method which is defined by a subtype of `AbstractFacade`,
  39. but which isn't defined by the type `AbstractFacade` itself:
  40. [source, java]
  41. ....
  42. pointcut callToUndefinedMethod():
  43. call(* AbstractFacade+.*(..))
  44. && !call(* AbstractFacade.*(..));
  45. ....
  46. The execution of a method that is defined in the source code for a type
  47. that is a subtype of `AbstractFacade` but not in `AbstractFacade` itself:
  48. [source, java]
  49. ....
  50. pointcut executionOfUndefinedMethod():
  51. execution(* *(..))
  52. && within(AbstractFacade+)
  53. && !within(AbstractFacade)
  54. ....