CS 541 Meeting -*- Outline -*- * Other parts of AspectJ (static crosscutting) reference: AspectJ Programmer's guide ------------------------------------------ OTHER USES OF ASPECTJ Provide: - glue code (joining existing programs) e.g., smooth over interface mismatches between two DBMS systems in a data warehouse program - enforce policies (laws) for software development e.g., never call service layer code directly, always go through facade ------------------------------------------ ** introductions *** augmenting methods, and constructors, fields (static introductions) ------------------------------------------ AUGMENTING METHODS AND FIELDS (STATIC INTRODUCTIONS) AspectMemberDecl ::= ... | IntroductionDecl IntroductionDecl ::= | Modifiers Type Type.Id(Formals) ThrowsOpt { Body } | Modifiers Type TypePattern.Id(Formals) ThrowsOpt ; | Modifiers Type.new(Formals) ThrowsOpt { Body } | Modifiers Type Type.Id ; | Modifiers Type Type.Id = Expression ; E.g., import java.awt.Point; aspect AddLocation { private Point M.location = new Point(); public Point M.getLocation() { return location; } private M.setLocation(Point p) { location = p; } } ------------------------------------------ These define methods, abstract methods, constructors, and fields. Q: How would these be implemented? Q: Why would you need these? Q: What should "this" mean in a method used in one of these? the aspect or the class... it's the class, why? Q: How would these map to Smalltalk? Q: If you can add all the methods needed for an interface, does that make a type implement that interface in Java? no, so need... Q: Can you add a private member to a type? What does that mean? it's private to the aspect Q: How about a protected member? not supported, why? Q: What happens if a private method is added to an interface, through some type pattern? It gets added! The aspect can call the private method on an object that implements the interface. Q: Can you introduce constructors to an interface? no Q: Can you introduce a field in an interface? yes! even if it's not public, static, or final. Q: If you can introduce fields in an interface, do we get multiple inheritance of fields and all its problems? yes, they declare it an error to have an ambiguous reference multiple inheritance of methods is also possible, only allow one to be concrete. Q: What happens if nothing matches the type pattern? nothing *** declare parents ------------------------------------------ DECLARE PARENTS AspectMemberDecl ::= ... | DeclareParentsDecl DeclareParentsDecl ::= | declare parents : TypePattern extends Type ; | declare parents : TypePattern implements TypeList ; E.g., aspect MakeCloneable { declare parents : mypack.* implements Cloneable; } aspect MakeRunnable { declare parents : MyType implements Runnable; public void MyType.run() { this.execute(); } } ------------------------------------------ Q: What could this be used for? make all types implement some interface... Q: What happens if nothing matches the type pattern? nothing Q: What if a class matching the type pattern already has a superclass? an error? Q: What happens if the new interface demands methods that aren't implemented? Order of interface inheritance now matters, because of the initializers, which may have side effects. The AspectJ programmer's guide (1.0.6) says: "A supertype is initialized before a subtype, that initialized code runs only once, and initializers for supertypes run in left-to-right order." It gives this example: Object M O \ / \ / C N Q \ / / D P \ / E "when a new E is instantiated, the initializers run in this order: Object M C O N D Q P E" *** example cd examples/introduction See, for example, CloneablePoint, HashablePoint *** declare soft ------------------------------------------ DECLARE SOFT AspectMemberDecl ::= ... | DeclareSoftDecl DeclareSoftDecl ::= declare soft : Type : Pointcut ; e.g., from Kiselev's book: import java.io.*; import javax.servlet.jsp.tagext.*; public aspect Exceptions { declare soft : SQLException : call(* db.StoriesDb.*(..)); declare soft : IOException : call(* BodyContent.writeOut(..)); } ------------------------------------------ At the given joinpoint, this catches the given exception and rethrows a org.aspectj.lang.SoftException (which is a runtime exception) This makes it an unchecked exception. Q: How would you simulate this using around advice? aspect A { void around() execution(void main(String[] args)) { try { proceed(); } catch (Exception e) { throw new org.aspectj.lang.SoftException(e); } } } Q: Would it make sense to use cflow in a pointcut for declare soft? No, and thus "such pointcuts may not include, directly or indirectly (through user-defined pointcut declarations) pointcuts that discriminate based on dynamic (runtime) context. Therefore, such pointcuts may not be defined in terms of cflow cflowbelow this target args if all of which can discriminate on runtime information." ** static errors, declare error and warning ------------------------------------------ STATIC ERRORS AND WARNINGS AspectMemberDecl ::= ... | DeclareErrorDecl | DeclareWarningDecl DeclareErrorDecl ::= declare error : PointCut : String ; DeclareWarningDecl ::= declare warning : PointCut : String ; E.g., (from Kiselev's book): import java.sql.*; public aspect CodeSegregation { pointcut dbCode() : call(Connection DriverManager .getConnection(..)); pointcut badDbCode() : dbCode() && !within(db.*); pointcut reallyBadDbCode() : badDbCode() && !within(security.*) && !within(servlets.*); declare warning : badDbCode() : "Database code outside 'db' package."; declare error : reallyBadDbCode() : : "Database code is not permitted here."; } ------------------------------------------ This "Declares that if any of the join points in Pointcut possibly exist in the program, the compiler should emit an error" (or warning) containing the given String. Q: What can these be used for? Q: What restrictions on the pointcuts are necessary for this? ** declare precedence ------------------------------------------ DECLARE PRECEDENCE AspectMemberDecl ::= ... | DeclarePrecedenceDecl DeclarePrecedenceDecl ::= declare precedence : TypePatList ; e.g., declare precedence : Security, Logging, *; ------------------------------------------ says that advice from Security has higher precedence than Logging advice, which is higher precedence than all other advice. precedence ordering (from Laddad p. 114) higher before higher lower after | around | | | | lower | v v lower before higher higher after around Advice lexically earlier in a single aspect has higher precedence than that later. Declared precedence ordering applies. Can't predict advice execution ordering otherwise