reflection2001 - Northeastern University

Download Report

Transcript reflection2001 - Northeastern University

DJ: Dynamic Adaptive
Programming in Java
Doug Orleans
Karl Lieberherr
Northeastern University, Boston
Goal of DJ
• Focus on crosscutting behavioral concerns:
involve a group of collaborating objects
from which information is gathered to
implement a behavior.
• Provide a Java library to cleanly
encapsulate crosscutting behavioral
concerns whose ad hoc implementation
would be scattered across many classes.
Solves Open Problem in AOP
for Behavioral Aspects in Java
• The ClassGraph-Aspect-Freezing problem
– When we have n behavioral aspects and the class
graph changes, we potentially need to update all n
aspects.
– DJ allows us to loosely couple behavioral aspects
to the class graph.
– And this is all done in Java using REFLECTION.
Example Java Program:
Analyzing XML Schemas
static final ClassGraph cg = new ClassGraph();
public Set getDefinedTypeNames() {
final Set def = new HashSet();
cg.traverse(this,
”from Schema via ->TypeDef,attrs,* to Attribute”,
new Visitor() {
TRAVERSAL
void before(Attribute host){
STRATEGY
if (host.name.equals(“name”))
def.add(host.value);}});
return def;
}
UML class diagram for XML Schemas
* Attribute
*
*
attrs
Schema
1..*
0..1
TypeDef
has-a
*
is-a
*
Classes and edges involved in finding defined type names
* Attribute
Schema
1..*
*
attrs
*
0..1
TypeDef
*
*
”from Schema via ->TypeDef,attrs,* to Attribute”
Use of Reflection in DJ
• ClassGraph default constructor builds a
graph object from all the classes in the
default package
• inspecting and invoking visitor methods
• interpret traversal strategies at runtime
Additional ClassGraph
Construction with Reflection
• A string containing a package name can be
provided as a constructor argument to build
a class graph from another package.
• addPackage(String pkgname).
• addClass(Class cl).
Reflection for Visitors
• At each step in a traversal, the fields and
methods of the current object, as well as
methods on the visitor object, are inspected
and invoked by reflection.
Limited Intercession
• Java's reflection system,
unlike other meta-object
protocols, has no mechanism
for intercession.
• However, DJ's Visitor class
does allow a limited form of
intercession.
Limited Intercession
• method before(Object obj, Class
cl) (and corresponding after) is invoked by
ClassGraph.traverse method at each
traversal step. Looks for a method named
before with a single parameter whose type is
the class represented by cl, and invokes it with
obj as argument.
Traversal Strategy Interpretation
at Run-time using Reflection
• interpret traversal strategy with respect to
class graph constructed by reflection.
• uses intersection of non-deterministic
automata.
• the traversal is done using an algorithm
similar to simulating a non-deterministic
automaton.
• Polyomial algorithm; before: exponential.
Three new additions to
Adaptive Programming
• Traverse classes for which programmer
does not have source code.
• asList method: List view of a traversal.
Lists that cut across many objects.
• Apply traversal strategy to a class graph to
create a subgraph. Compose traversal
strategies by intersection.
Conclusions
• DJ is a useful Java tool for the Java
programmer who wants to encapsulate
crosscutting behavioral concerns.
• Because of the heavy use of reflection the
price to be paid for controlling the
scattering and tangling is efficiency.
On Reserve
Why Traversal Strategies?
• Law of Demeter: a method should talk only to its
friends:
arguments and part objects (computed or stored)
and newly created objects
• Dilemma:
•Small method problem of OO (if followed) or
•Unmaintainable code (if not followed)
•Traversal strategies are the solution to this dilemma
Styles of Adaptive Programming
• Traditional: parameterize by class graph;
strategy is part of adaptive program
• Traditional optimized: parameterize by class
graph and strategy graph; reuse pair of class
graph and strategy graph.