DJ: Dynamic Adaptive Programming in Java Doug Orleans Karl Lieberherr

Download Report

Transcript DJ: Dynamic Adaptive Programming in Java Doug Orleans Karl Lieberherr

DJ: Dynamic Adaptive
Programming in Java
Doug Orleans
Karl Lieberherr
Northeastern University, Boston
5/13/2016
1
Four Goals of DJ
• Focus on traversal-related behavioral
concerns involving a graph of collaborating
objects that you traverse to implement a
behavior. Provide a Java library to cleanly
encapsulate traversal-related behavioral
concerns whose ad hoc implementation
would be scattered across many classes.
5/13/2016
2
Four Goals of DJ
• Loose coupling between implementations of
traversal-related concerns and the UML
class diagram.
• Easy to use by Java programmers.
• Fast implementation.
We have achieved
all but the Fast implementation goal.
5/13/2016
3
UML class diagram for XML Schemas
* Attribute
*
*
attrs
Schema
1..*
0..1
TypeDef
has-a
*
is-a
*
5/13/2016
4
”from Schema via ->TypeDef,attrs,* to Attribute”
Classes and edges involved in finding defined type names
* Attribute
Schema
1..*
*
0..1
attrs
*
7 classes
7 edges
TypeDef
but
*
mention
only 4.
*
5/13/2016
5
”from Schema via ->TypeDef,attrs,* to Attribute”
Example Java Program:
Analyzing XML Schemas
static final ClassGraph cg = new ClassGraph();
// in class Schema:
public Set getDefinedTypeNames() {
TRAVERSAL
final Set def = new HashSet();
STRATEGY
cg.traverse(this,
”from Schema via ->TypeDef,attrs,* to Attribute”,
new Visitor() {
void before(Attribute host){
if (host.name.equals(“name”))
def.add(host.value);}});
return def;
class-graph.traverse(what, where-to-go, what-to-do)
}
5/13/2016
6
Semantics of Traversal Strategies
class-graph.traverse(what, where-to-go, ...)
• Traverse object graph what guided by
where-to-go and the information about
what in the class-graph, but without
knowledge of what, except the root of
what. Go down an edge e if it satisfies
where-to-go in the context of classgraph.
5/13/2016
7
Finding the first step for the search
Schema
what:Schema
Attribute
o1:Attribute
o2:Attribute
Which arrows might lead
to an object of class Attribute
satisfying ...?
5/13/2016
Traversal Strategy where-to-go:
from Schema … to Attribute
8
Use of Java Reflection in DJ
• ClassGraph default constructor building a graph
object from all the classes in the default package, e.g.:
Class.forName(), Class.getDeclaredFields(),
Field.getType()
• inspecting and invoking visitor methods, e.g.:
Class.getDeclaredMethods(),
Method.getParameterTypes(), Method.invoke()
• traversing objects at run-time guided by a traversal
strategy, e.g.,: Object.getClass(), Field.get()
5/13/2016
9
Additional ClassGraph
Customization
• For additional customizations use the
following constructor or methods:
– ClassGraph(String pkgname)
– addPackage(String pkgname)
– addClass(Class cl)
5/13/2016
10
Reflection during Traversal
• 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.
5/13/2016
11
Traversal Strategy Interpretation
at Run-time
• interpret traversal strategy with respect to
class graph constructed by reflection.
• similar to the intersection of nondeterministic finite automata.
• the traversal is done using an algorithm
similar to simulating a non-deterministic
automaton.
• Polyomial algorithm; before: exponential.
5/13/2016
12
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.
5/13/2016
13
Performance
• Current implementation of DJ is 25-30
times slower than statically generated code.
• Conjecture: can be improved to 10 times
slower.
– If the reflection capabilities of JVM get faster,
DJ gets faster.
– We could generate byte code at run-time to
avoid some of the reflection.
5/13/2016
14
aspect-oriented programming for traversal-related concerns
Conclusions
• DJ is a useful Java tool for the Java programmer
who wants to encapsulate traversal-related
behavioral concerns.
• DJ allows us to loosely couple traversal-related
behavioral concerns to the class graph.
• DJ does not replace DemeterJ but complements
DemeterJ. DJ has a very small learning curve.
5/13/2016
15
For questions
• [email protected][email protected]
• www.ccs.neu.edu/research/demeter/DJ
5/13/2016
16
The END
• The END
5/13/2016
17
On Reserve
5/13/2016
18
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
5/13/2016
19
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.
5/13/2016
20
Solves Open Problem in AOP
for Traversal-related 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.
5/13/2016
21
Limited Intercession
• Java's reflection system, unlike other metaobject protocols, has no mechanism for
intercession.
• However, DJ's Visitor class does allow a
limited form of intercession.
5/13/2016
22
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.
5/13/2016
23