Refactoring of Design Patterns from Object-Oriented to Aspect-Oriented: a practical approach
DIPARTIMENTO di
MATEMATICA e INFORMATICA
Daniele Contarino System 2015 – September 28, 2015 2 of 39
Introduction
Recognition process
Conversion from OODP to AODP
Example: Singleton DP
Annotations
Executive summary
The Design Patterns are …
Introduction
… of developers!
Daniele Contarino System 2015 – September 28, 2015 4 of 39
The Aspect-Oriented Programming we allow to separate the main concerns from cross-cutting concerns
Introduction
OOP classes
Aspects
Introduction
Daniele Contarino System 2015 – September 28, 2015 6 of 39
+
Introduction
Aspect Oriented
Programming = Oriented Design Aspect
Patterns
Introduction
import java.util.WeakHashMap;
public aspect MySingletonAOP {
private WeakHashMap<String, MyST> singles = new WeakHashMap<String, MyST>();
pointcut trapCreate() : call((MyST).new(..))
&& !within(MySingletonAOP);
Object around() : trapCreate() {
Object obj = singles.get("MyST");
if (obj == null) { obj = proceed();
singles.put(className, obj);
}
return obj;
};
}
public class MyST { private int attr;
public MyST(){
this.attr = 1;
}
public void setAttr(int attr){
this.attr = attr;
}
public int getAttr(){
return attr;
} }
Daniele Contarino System 2015 – September 28, 2015 8 of 39
Introduction
Design pattern in Aspect-Oriented:
Improves the code reusabilty
Reduce project complexity
Split from code the business logic (domain concerns)
and design pattern features (non-domain concerns)
Introduction
In real world
Convert all Object- Oriented Design Pattens into
Aspect Oriented Design Pattens …
… in a (very)
BIG software
project!
Daniele Contarino System 2015 – September 28, 2015 10 of 39
Introduction
Introduction
Recognition process
Conversion from OODP to AODP
Example: Singleton DP
Annotations
Executive summary
Daniele Contarino System 2015 – September 28, 2015 12 of 39
Recognition process
For recognize a design pattern, we observe their tipical
"signature" or rather the
design pattern features.
Recognition process
We propose three research approach:
1. Research a set of implemented class or inherited from a specific class
2. Match between interface with objects that
implement them
3. Check if may exists some
attributes and/or methods
Daniele Contarino System 2015 – September 28, 2015 14 of 39
Recognition process
An example of this research is the DP Observer search, because the relevant classes implement the Observer interface and
Observable (included in the JDK standard library).
1. Research a set of implemented class or inherited
from a specific class
Recognition process
Stating from a given project, we get all interface. We try to
observe where they are implemented and the behavior of the classes that implement the same interface. In case of DP
Composite it will be usefull to get a classes’ list that implements a determinated interface. We will check if a specific class contains a list of typed attributes with the same interface (a list of leafs).
2. Match between interface with objects that
implement them
Daniele Contarino System 2015 – September 28, 2015 16 of 39
Recognition process
Recognition process
Other examples of this type of recognizer is: Proxy, Factory
Method and Abstract Factory (search a interface that have a
method whose return type is another interface)
Daniele Contarino System 2015 – September 28, 2015 18 of 39
Recognition process
Some DP are characterized by the presence of some specific
attributes or properties concerning the methods (or assimilable as the constructors). The DP Singleton for example has a private constructor, a private static attribute of the same type of the
class and a static method that return a object of the same type of the class. Other examples of this type of recognizer is: Flyweight, Proxy, Object Pool (with a single Creator, like Singleton)
3. Check if may exists some attributes and/or methods
Recognition process
Daniele Contarino System 2015 – September 28, 2015 20 of 39
Introduction
Recognition process
Conversion from OODP to AODP
Example: Singleton DP
Annotations
Executive summary
Conversion from OODP to AODP
OODP
AODP
1. Project analysis and research of the classes that implements a
particular DP
2. Creation of the object that will perform the conversion
3. Creation of the Aspects
4. Change the code of the
project
Daniele Contarino System 2015 – September 28, 2015 22 of 39
Conversion from OODP to AODP
Base toolkit
Conversion from OODP to AODP
getDesignerPattern(IJavaProject)
take in input a Java project e return a array of AspectDesignerPattern objects already setted for carry out the
refactoring
OODP
AODP
Daniele Contarino System 2015 – September 28, 2015 24 of 39
Conversion from OODP to AODP
createAspect()
to create a custom Aspect for he found class; he use AspectPrototype class who noted the various internal elements of Aspect (pointcuts, advice, attributes, etc.) and write the code in the .aj file into indicated package
OODP
AODP
Conversion from OODP to AODP
replaceCode()
to delete and/or modify all those parts of code characterized a given DP in OO paradigm.
OODP
AODP
Daniele Contarino System 2015 – September 28, 2015 26 of 39
Introduction
Recognition process
Conversion from OODP to AODP
Example: Singleton DP
Annotations
Executive summary
Example: Singleton DP
Daniele Contarino System 2015 – September 28, 2015 28 of 39
Example: Singleton DP
public static AspectDesignePattern getDesignePattern(ICompilationUnit classFile, IJavaProject project){
if(classFile == null) return null;
String className;
IMethod[] methods;
try {
for(IType classStub : classFile.getTypes()){
className = classStub.getElementName();
methods = classStub.getMethods();
IMethod privateConstructor = Singleton.getOnlyPrivateConstructor(methods);
if( privateConstructor == null) return null;
IMethod getInstanceMethod = Singleton.getInstanceMethod(methods, className);
if( getInstanceMethod == null) return null;
IField[] fields = classStub.getFields();
IField staticInstance = Singleton.getInstanceAttribute(fields, className);
if(staticInstance == null) return null;
return new Singleton(classFile, project, classStub, privateConstructor, getInstanceMethod, staticInstance);
}
} catch (JavaModelException e) {}
return null;
}
Example: Singleton DP
private static IMethod getOnlyPrivateConstructor(IMethod[] methods) throws JavaModelException{
int numOfPrivateConstructors = 0, privateConstructorsIndex = -1;
for(int i=0; i < methods.length; i++) if(methods[i].isConstructor() &&
Flags.isPrivate(methods[i].getFlags())) { numOfPrivateConstructors++;
privateConstructorsIndex = i;
}
if(numOfPrivateConstructors != 1) return null;
else return methods[privateConstructorsIndex];
}
Daniele Contarino System 2015 – September 28, 2015 30 of 39
Example: Singleton DP
private static IMethod getInstanceMethod(IMethod[] methods, String className) throws JavaModelException{
for(int i=0; i < methods.length; i++)
if(methods[i].getReturnType().compareTo("Q"+className+";") == 0 &&
Flags.isPublic(methods[i].getFlags()) &&
Flags.isStatic(methods[i].getFlags()))
return methods[i];
return null;
}
private static IField getInstanceAttribute(IField[] fields, String className) throws JavaModelException{
for(int i=0; i < fields.length; i++)
if(fields[i].getTypeSignature().compareTo("Q"+className+";") == 0 &&
Flags.isStatic(fields[i].getFlags())) return fields[i];
return null;
}
Example: Singleton DP
MyClass uniqueObject = MyClass.getInstance();
MyClass uniqueObject = new MyClass();
After conversion, all calls like
will be replaced by
Daniele Contarino System 2015 – September 28, 2015 32 of 39
Introduction
Recognition process
Convertion from OODP to AODP
Example: Singleton DP
Annotations
Executive summary
Annotation
Despite the use of the Aspect-Oriented
programming, the non- domanis concers is still related with business logic (often in Aspect code we find the name of the class)
import java.util.WeakHashMap;
public aspect MySingletonAOP {
private WeakHashMap<String, MyST> singles = new WeakHashMap<String, MyST>();
pointcut trapCreate() : call((MyST).new(..))
&& !within(MySingletonAOP);
Object around() : trapCreate() {
Object obj = singles.get("MyST");
if (obj == null) { obj = proceed();
singles.put(className, obj);
}
return obj;
};
Daniele Contarino System 2015 – September 28, 2015 34 of 39
Annotation
We propose another solution using Aspects and Java Annotation
package it.unict.dmi.aoa.singleton;
import java.util.WeakHashMap;
public aspect SingletonPattern {
private WeakHashMap<String, Object> singles = new WeakHashMap<String, Object>();
pointcut trapCreate() :
call((@Singleton *).new(..)) && !within(SingletonPattern);
Object around() : trapCreate() { String className =
thisJoinPoint.getSignature().getDeclaringTypeName();
Object obj = singles.get(className );
if (obj == null) { obj = proceed();
singles.put(className, obj);
}
return obj;
};
}
@Singleton
public class MyST { private int attr;
public MyST(){
this.attr = 1;
}
public void setAttr(int attr){
this.attr = attr;
}
public int getAttr(){
return attr;
} }
Future works
Complete the recognition of all design patterns;
Parallelize the recognition;
Create a library with all Aspect- Oriented
Design Patterns with Annotation;
Daniele Contarino System 2015 – September 28, 2015 36 of 39
References
1. G. Kiczales, J. Lamping, A. Mendhekar, C. Maeda, C. Lopes, J.M. Loingtier, and J. Irwin, “Aspect-oriented programming,”
vol. 1241, pp. 220–242.
2. R. Giunta, G. Pappalardo, and E. Tramontana, “Aodp:
Refactoring code to provide advanced aspect-oriented
modularization of design patterns”, Proceedings of the 27th
Annual ACM Symposium on Applied Computing, 2012.
References
3. R. Giunta, G. Pappalardo, and E. Tramontana, “Using aspects and annotations to separate application code from design patterns”, Proceedings of the 2010 ACM Symposium on Applied Computing, 2010.
4. R. Giunta, G. Pappalardo, and E. Tramontana, “Aspects and
annotations for controlling the roles application classes
play for design patterns”, Proceedings of the 2011 18th
Asia-Pacific Software Engineering Conference, 2011.
Daniele Contarino System 2015 – September 28, 2015 38 of 39
References
5. D. Sardina and E. Tramontana, “Realizzazione di un plug-in di eclipse per la generazione di codice ad aspetti che
implementa alcuni design pattern”, Università di Catania,
2012.
DIPARTIMENTO di
MATEMATICA e INFORMATICA