Professional Documents
Culture Documents
Measuring Coupling in Aspect-Oriented Systems
Measuring Coupling in Aspect-Oriented Systems
Jianjun Zhao
Department of Computer Science and Engineering
Fukuoka Institute of Technology
3-30-1 Wajiro-Higashi, Higashi-ku, Fukuoka 811-0295, Japan
zhao@cs.fit.ac.jp
1
AO systems, the coupling is mainly about the degree of in- tems. We believe that our ideas are independent of AspectJ
terdependence among aspects and classes. To test this hy- and are generally applicable to the class of AOP languages.
pothesis, appropriate coupling measures for AO systems are AspectJ [13] is a seamless aspect-oriented extension to
needed. Moreover, in order to measure the coupling of an Java by adding some new concepts and associated con-
aspect-oriented system, we should consider different types structs. These concepts and associated constructs are called
of interactions between aspects and classes in the system. join points, pointcut, advice, inter-type declaration, and as-
However, although coupling has been widely studied for pect.
object-oriented systems [5, 4, 7, 10], it has not been stud-
ied for AO systems yet. Moreover, existing approaches to The aspect is the modular unit of crosscutting implemen-
measuring the coupling of object-oriented systems can not tation. Each aspect encapsulates functionality that crosscuts
be directly applied to aspect-oriented systems since an AO other classes in a program. An aspect can be instantiated,
system contains some new types of interactions between as- can contain states and methods, and also may be specialized
pects and/or classes that may have great impact on the cou- with subaspects. An aspect is combined with the classes it
pling of the system. It is therefore of interest to develop crosscuts according to specifications given within the as-
new coupling framework and new coupling measures based pect. Moreover, an aspect can use an inter-type declara-
on this framework for AO systems. tion construct to declare fields, methods, and interface im-
In this paper, we propose a measure suite for assess- plementation declarations for classes. Declared members
ing the coupling in AO systems. We first present a cou- may be made visible to all classes and aspects (public inter-
pling framework for AO systems which specially designed type declaration) or only within the aspect (private inter-
to count the dependencies between aspects and classes in type declaration), allowing one to avoid name conflicts with
the system. Based on this framework, we formally define pre-existing elements.
various coupling measures in terms of different types of de- A central concept in the composition of an aspect with
pendencies between aspects and classes. We also discuss other classes is called a join point. A join point is a well-
the mathematical properties of these measures. defined point in the execution of a program, such as a call to
Because aspect-oriented paradigm significantly different a method, an access to an attribute, an object initialization,
from object-oriented paradigms, we really need to develop a an exception handler, etc. Sets of join points may be rep-
notion of coupling for AO systems, which is an indicator of resented by pointcuts, implying that such sets may crosscut
the degree to which the components in the system interact the system. Pointcuts can be composed and new pointcut
each other. We hope that by examining the ideas of the designators can be defined according to these combinations.
coupling in aspect-oriented systems from several different AspectJ provides various pointcut designators that may be
viewpoints and through independently developed measures, combined through logical operators to build up complete
we can have a better understanding of what the coupling is descriptions of pointcuts of interest [2].
meant in AO systems and the role that coupling plays in An aspect can specify advice to define code that executes
the development of quality aspect-oriented software. As the when a pointcut is reached. Advice is a method-like mech-
first step to study the coupling in aspect-oriented systems, in anism which consists of instructions that execute before, af-
this paper we would like to provide a sound and formal basis ter, or around a pointcut. Around advice executes in place
for coupling measurement in AO systems before applying it of the indicated pointcut, allowing a method to be replaced.
to real aspect-oriented system design.
The rest of the paper is organized as follows. Section 2 An AspectJ program can be divided into two parts: base
briefly introduces AspectJ, a general aspect-oriented pro- code which includes classes, interfaces, and other stan-
gramming language based on Java. Section 3 defines a dard Java constructs and aspect code which implements the
terminogy for an AO system. Section 4 presents a cou- crosscutting concerns in the program. Any AspectJ imple-
pling framework for defining coupling measures for AO mentation must ensure that the aspect and base code run
systems. Section 5 defines a coupling measure suite based together in a properly coordinated fashion. The key com-
on the framework presented in Section 4. Section 6 studies ponent is the aspect weaver, which ensures that applicable
the mathematical properties of the proposed coupling mea- advice runs at the appropriate join points.
sures. Section 7 discusses some related work. Concluding
remarks are given in Section 8. Example. Figure 1 shows an AspectJ program taken
from [2] that associates shadow points with ev-
2 Aspect-Oriented Programming and As- ery Point object. The program contains one as-
pectJ pect PointShadowProtocol and two classes
Point and Shadow. The aspect has three methods
In this paper we use AspectJ [2] as our target language to getShadowCount, associate and getShadow,
show the basic ideas of coupling measurement in AO sys- and three pieces of advice related to pointcuts setting,
2
public class Point { aspect PS_Protocol {
protected int x, y; private int shadowCount = 0;
public Point(int _x, int _y) { public static int getCount() {
x = _x; return PS_Protocol.aspectOf().shadowCount;
y = _y; }
} private Shadow Point.shadow;
public int getX() { public static void associate(Point p, Shadow s){
return x; p.shadow = s;
} }
public int getY() { public static Shadow getShadow(Point p) {
return y; return p.shadow;
} }
public void setX(int _x) {
x = _x; pointcut setting(int x, int y, Point p):
} args(x,y) && call(Point.new(int,int));
public void setY(int _y) { pointcut settingX(Point p):
y = _y; target(p) && call(void Point.setX(int));
} pointcut settingY(Point p):
public void printPosition() { target(p) && call(void Point.setY(int));
System.out.println("Point
at("+x+","+y+")"); after(int x, int y, Point p) returning :
} setting(x, y, p) {
public static void main(String[] args) { Shadow s = new Shadow(x,y);
Point p = new Point(1,1); associate(p,s);
p.setX(2); shadowCount++;
p.setY(2); }
} after(Point p): settingX(p) {
} Shadow s = new getShadow(p);
class Shadow { s.x = p.getX() + Shadow.offset;
public static final int offset = 10; p.printPosition();
public int x, y; s.printPosition();
}
Shadow(int x, int y) { after(Point p): settingY(p) {
this.x = x; Shadow s = new getShadow(p);
this.y = y; s.y = p.getY() + Shadow.offset;
public void printPosition() { p.printPosition();
System.outprintln("Shadow at s.printPosition();
("+x+","+y+")"); }
} }
}
3
system. For each aspect a 2 A(S ), let An
estors(a) For each method m 2 A(a), the set of invoked methods
C (S ) be the set of ancestor classes of a. of m is denoted as SIM (m) such that if 9m0 2 M(
)
and the body of m has a method invocation where m0
3.2 Modules is invoked for an object of
, then m0 2 SIM (m).
In an AO system, an aspect contains several types of Definition 6 (The Number of Method Invocations) Let
module, i.e., advice, intertype declaration, pointcut, and S be an AO system, a 2 A(S ) be an aspect of S , and
method, and a class contains only one type of module called
2 C (S ) be a class of S .
method.
For each piece of advice 2 A(a), NSI (; m) is
Definition 3 (Modules of an Aspect or a Class) Let S be the number of method invocations of m by such that
an AO system. For each a 2 A(S ), let A(a) be the set of m 2 SIM () and m is invoked for an object of
.
advice of a, I (a) be the set of intertype declarations of a,
P (a) be the set of pointcuts of a, and M(a) be the set of For each intertype declaration i 2 I (a), NSI (i; m) is
methods of a, and Mall (a) be the set of all modules of a. the number of method invocations of m by i such that
For each
2 C (S ), let M(
) be the set of methods of
. m 2 SIM (i) and m is invoked for an object of
.
In an AO system, advice, intertype declaration, pointcut, For each pointcut p 2 A(a), NSI (p; m) is the num-
or method may have a set of parameters that may also in- ber of method invocations of m by p such that m 2
fluence coupling measurement. So we define this issue as SIM (p) and m is invoked for an object of
.
follows. For each method m0 2 A(a), NSI (m0 ; m) is the
number of method invocations of m by m0 such that
Definition 4 (Parameters) Let S be an AO system. For
m 2 SIM (m0 ) and m is invoked for an object of
.
each 2 A(S ), i 2 I (S ), p 2 P (S ), or m 2 M(S ),
let P ar() be the parameters of advice , P ar(i) be the 3.4 Attributes
parameters of intertype declaration i, P ar(p) be the pa-
In AO systems, aspects and classes contain attributes that
rameters of pointcut p, and P ar(m) be the parameters of
are either inherited or newly defined. Attributes are for-
method m.
mally defined as follows:
3.3 Module Invo
ations Definition 7 (Attributes of Aspects and Classes) Let S
In AO systems, modules such as advice, intertype dec- be an AO system. For each a 2 A(S ), let Aa (a) be the set
larations, and methods in an aspect may invoke other mod- of attributes of aspect a. For each
2 C (S ), let Aa (
) be
ules of some classes. To measure coupling of an aspect, it is the set of attributes of class
.
necessary to define the set of modules that a piece of advice, 3.5 Types
an intertype declaration, or a method of the aspect invokes.
Also we should define the frequency of these invocations. Attributes and parameters contains types that all can con-
tribute to coupling measurement of AO systems. The AO
Definition 5 (The Set of Invoked Methods) Let S be an programming languages provide built-in types and support
AO system, a 2 A(S ) be an aspect of S , and
2 C (S ) users to define new aspect and class types as well as tradi-
be a class of S . tional types. In AspectJ, the type of an attribute or param-
eter either is an aspect, a class, a built-in type or a user-
For each piece of advice 2 A(a), the set of in-
defined type. Therefore, the set T of available types in an
voked methods of is denoted as SIM () such that
AO system is defined as follows:
if 9m 2 M(
) and the body of has a method in-
vokation where m is invoked for an object of
, then Definition 8 (Available Types) Let S be an AO system.
m 2 SIM () . The set T of available types in S is T = Tbi [ Tud [ T
where Tbi is the set of built-in types provided by the pro-
For each intertype declaration i 2 I (a), the set of in- gramming language, Tud is the set of user-defined types,
voked methods of i is denoted as SIM (i) such that and T
is the set of class types.
if 9m 2 M(
) and the body of i has a method in-
vokation where m is invoked for an object of
, then We next denote the type of attributes and parameters.
m 2 SIM (i) . Definition 9 (Types of Attributes and Parameters) Let
For each pointcut p 2 P (a), the set of invoked methods S be an AO system, x 2 Aa (a) be an attribute of aspect
of p is denoted as SIM (p) such that if 9m 2 M(
) a, and y 2 Aa (
) be an attribute of class
. The type of
and the body of p has a method invokation where m is x is denoted by T (x) 2 T and the type of y is denoted by
invoked for an object of
, then m 2 SIM (p) . p T (y) 2 T .
4
4 Coupling Framework for Aspect-Oriented
is the return type of m. The number of method-class
Systems dependencies from a to
can formally be represented
as
We next present our coupling framework for AO systems X
which is a basis for defining coupling measures for AO sys- MC (a;
) = f x j x 2 P ar(m) ^ T (x) =
g
tem. Our framework focuses on coupling caused by de- m2M(a)
pendencies that occur between aspect and class in an AO Pointcut-class dependence:
system, and therefore can handle issues of coupling mea- Let p be a pointcut of aspect a. There is a pointcut-
surement that are specific to AO systems. class dependence between a and
, if
is the type of a
Dependence between aspects and classes is a key point parameter of a pointcut p of a. The number of pointcut-
to determine the mechanism by which an aspect and a class class dependencies from a to
can formally be repre-
are coupled. Here, we identify different types of dependen- sented as
cies that can be used to determine if a particular type more X
accurately indicates fault likelihood. In the following, we P C (a;
) = f x j x 2 P ar(p) ^ T (x) =
g
describe these types of dependencies between aspects and p2P (a)
classes, which we call aspect-class dependencies. To define
Definition 12 (Module-method Dependence) Let S be an
coupling measures in AO systems in Section 5, we also give
AO system, a 2 A(S ) be an aspect of S , and
2 C (S ) be
the formal expressions for these dependencies.
a class of S . There are four types of module-method depen-
Definition 10 (Attribute-class dependence) There is an dencies between a and
that can be defined as follows:
attribute-class dependence between aspect a and class
, Advice-method dependence:
if
is the type of an attribute of a. The number of attribute- There is an advice-method dependence between a and
class dependencies from a to
can formally be represented
, if a piece of advice of a directly invokes a method
as m of
. The number of advice-method dependencies
AtC (a;
) =f x j x 2 Aa (a) ^ T (x) =
g from a to
can formally be represented as
X X
Definition 11 (Module-class Dependence) Let S be an AM (a;
) = ( NSI (; m)
AO system, a 2 A(S ) be an aspect of S , and
2 C (S ) 2A(a) m2M (
)
be a class of S . There are four types of module-class de-
Intertype-method dependence:
pendencies that can be defined as follows:
There is an intertype-method dependence between a
Advice-class dependence: and
, if an intertype i of a directly invokes a method
There is an advice-class dependence between a and m of
. The number of intertype-method dependencies
, if
is the type of a parameter of a piece of advice from a to
can formally be represented as
of a, or
is the return type of . The number of IM (a;
) =
X X
NSI (i; m)
(
m2M(a) m0 2M (
)
ration i of a, or
is the return type of i. The number of
intertype-class dependencies from a to
can formally Pointcut-method dependence:
be represented as There is a pointcut-method dependence between a and
X
, if a pointcut p of a contains at least one join point
IC (a;
) = f x j x 2 P ar(i) ^ T (x) =
g that is related to a method m of
. The number of
i2I (a) pointcut-method dependencies from a to
can formally
be represented as
Method-class dependence: X X
There is a method-class dependence between a and
, P M (a;
) = ( NSI (p; m)
if
is the type of a parameter of a method m of a, or p2P (a) m2M (
)
5
Definition 13 (Aspect-inheritance dependence) There is Method-class dependence measure can be defined as
an aspect-inheritance dependence between aspect a and follows:
class
, if
is an ancestor of a. The number of aspect- X
inheritance dependencies from a to c can formally be rep- ÆMC (a) = MC (a;
)
resented as
2C (S )
AI (a;
) =f x j x 2 An
estors(a)g counts all method-class dependencies between aspect
a and some classes in S .
Example 1 For the example program showed in Figure 1,
Let a be apsect PS Protocol and
be class Point, we Pointcut-class dependence measure can be defined as
can obtain the following dependencies between a and
. follows: X
6
Definition 17 (Aspect-Inheritance dependence Measure) The coupling of a is null if Dep(a) is empty, i.e.,
Let S be an AO system, a 2 A(S ) be an aspect of S , and Dep(a) = ) Coupling(a) = 0.
2 C (S ) be a class of S . Aspect-inheritance dependence
measure can be defined as follows: Property 3 (Monotonicity) Let S be an AO system and
X
a 2 A(S ) be an aspect of S . We modify a to form a
ÆAI (a) = AI (a;
) new aspect a0 which is identical to a except that Dep(a) 2
2C (S ) Dep(a0 ), i.e., we add some relationships to a. Let Sa be
the AO system which is identical to S except that aspect a
counts all AI-dependencies between aspect a and all ances- is replaced by a0 . Then
tor classes of a.
Coupling(a) Coupling(a ), and
0
7
Table 1: Properties of Coupling Measures for AO Systems
Measures Type of coupling Strength of coupling inheritance P1 P2 P3 P4 P5
ÆAtC (a) type of attributes #attributes non-inh.-based Yes Yes Yes Yes Yes
ÆAC (a) type of parameters #parameters non-inh.-based Yes Yes Yes Yes Yes
ÆIC (a) type of parameters #parameters non-inh.-based Yes Yes Yes Yes Yes
ÆMC (a) type of parameters #parameters non-inh.-based Yes Yes Yes Yes Yes
ÆP C (a) type of parameters #parameters non-inh.-based Yes Yes Yes Yes Yes
ÆAM (a) method call #method calls non-inh.-based Yes Yes Yes Yes Yes
ÆIM (a) method call #method calls non-inh.-based Yes Yes Yes Yes Yes
ÆMM (a) method call #method calls non-inh.-based Yes Yes Yes Yes Yes
ÆP M (a) method call #method calls non-inh.-based Yes Yes Yes Yes Yes
ÆAI (a) inheritance #ancesters inh.-based Yes Yes Yes Yes Yes
properties 4 and 5. However, since it is really meaningful surement for object-oriented systems has also been studied
if we merge two aspects in an AO system, we can refine by Li and Harry [11] who propose some coupling measure
properties 4 and 5 as we showed in this section. in terms of massage passing and data abstraction for object-
Table 1 summarizes the discussion of our measures. For oriented systems and Martin [15] who proposes efferent and
each measure, we show the type of coupling it uses, the afferent coupling for object-oriented programs. Moreover,
factors that determines the strength of coupling, how in- Hitz and Montazeri [10] present a framework for measur-
heritance is dealt with (inheritance-based coupling, non- ing class-level and object-level coupling for object-oriented
inheritance-based coupling, or both). The columns P1, P2, systems.
P3, P4, and P5 show whether or not a measure satisfies these
five properties. Although these coupling measures can be used to as-
sess the coupling for object-oriented systems from differ-
7 Related work ent viewpoints, they can not be directly applied to AO sys-
tems since these approaches only consider the interactions
We discuss some related work in the area of coupling between classes in object-oriented systems. Since an AO
measurement for object-oriented systems and also measur- system contains aspects that are significantly different from
ing the complexity of AO systems. To the best of our knowl- classes in object-oriented systems, new abstraction models
edge, our work is the first attempt to study the coupling are needed for representing various types of interactions be-
measurement for AO systems. tween aspects and classes in order to derive new coupling
7.1 Coupling Measurement in Obje
t- measures for AO systems.
Oriented Systems
Chidamber and Kemerer [7] propose a coupling measure 7.2 Measures for Aspe
t-Oriented Sys-
called the Coupling Between Object (CBO) classes to assess tems
class coupling in object-oriented systems. With the CBO
measures, class C1 is coupled to class C2 if C1 uses C2 ’s
member functions and/or instance variables. CBO counts Zhao [18] proposes a metrics suite for aspect-oriented
the number of classes to which a given class is coupled. software, which are specifically designed to quantify the in-
Briand et al. [5] investigate the coupling measures for C++ formation flows in an aspect-oriented program. To this end,
programs. They propose a comprehensive suite of measures He presents a dependence model for aspect-oriented soft-
to quantify the level of class coupling during the design of ware which is composed of several dependence graphs to
object-oriented systems. Their coupling measures are de- explicitly represent dependence relationships in a module, a
fined based on various types of interactions between classes class, or the whole program. Based on the model, he defines
in an object-oriented system. Briand et al [4] also present some metrics that can be used to measure the complexity
a unified framework for comparing coupling measures for of an aspect-oriented program from various different view-
object-oriented systems from various viewpoints. Roughly points. Recently, cohesion measurement for AO systems
speaking, our work can be regarded as an extension of the has also been studied by Zhao and Xu [19]. However, these
work by Briand et.al [5, 4] to handle the unique problems studies do not address the issue on coupling measurement
of coupling measurement for AO systems. Coupling mea- for AO systems.
8
8 Concluding Remarks [9] B. Henderson-Sellers. Software Metrics. Prentice Hall,
Hemel Hempstaed, U.K., 1996.
In this paper, we proposed a measure suite for assess-
ing the coupling in AO systems. We first presented a cou- [10] M. Hitz and B. Montazeri. Measuring Coupling and
pling framework for AO systems which specially designed Cohesion in Object-Oriented Systems. Proceedings of
to count the dependencies between aspects and classes in International Symposium on Applied Corporate Com-
the system. Based on this framework, we formally defined puting, pp.25-27, Monterrey, Mexico, October 1995.
various coupling measures in terms of different types of de-
pendencies between aspects and classes. We also discussed [11] W. Li and S. Henry. Object-Oriented Metrics that Pre-
the mathematical properties of these measures in which we dict Maintainability. Journal of Systems and Software,
showed that our measures satisfy the properties that a good Vol.23, No.2, pp.111-222, 1993.
coupling measure should have. [12] G. Kiczales, J. Lamping, A. Mendhekar, C. Maeda, C.
The coupling measures proposed in this paper focused Lopes, J. M. Loingtier, and J. Irwin, “Aspect-Oriented
only on the dependencies between aspects and classes, Programming, “ Proceedings of the 11th European
and did not take dependencies between aspects or between Conference on Object-Oriented Programming, pp.220-
classes into account. 242, LNCS, Vol.1241, Springer-Verlag, June 1997.
In our future work, we will study the influence of depen-
dencies between aspects or classes, aspect and class inheri- [13] G. Kiczales, J. Lamping, A. Mendhekar, C. Maeda, C.
tances, and other aspect-oriented features on coupling mea- Lopes, J. M. Loingtier, and J. Irwin, “An Overview of
surement of AO systems, and apply our coupling measures AspectJ,” Proc. 13th European Conference on Object-
to real aspect-oriented system design. Oriented Programming, pp.220-242, LNCS, Vol.1241,
Springer-Verlag, June 2000.
References
[14] K. Lieberher, D. Orleans, and J. Ovlinger, “Aspect-
[1] F. Abreu, M. Goulao, and R. Esteves, “Toward the De- Oriented Programming with Adaptive Methods,” Com-
sign Quality Evaluation of Object-Oriented Software munications of the ACM, Vol.44, No.10, pp.39-41, Oc-
Systems,” Proc. 5th International Conference on Soft- tober 2001.
ware Quality, Austin, Texas, October 1995.
[15] R. Martin, “OO Design Quality Metrics - An Anal-
[2] The AspectJ Team. The AspectJ Programming Guide. ysis of Dependencies,“ position paper, Proc. Work-
2003. shop Pragmatic and Theoretical Directions in Object-
[3] L. Bergmans and M. Aksits. Composing crosscutting Oriented Software Metrics, OOPSLA’94, October 94.
Concerns Using Composition Filters. Communications
[16] L. M. Ott, J. M. Bieman, B. K. Kang, and B. Mehra.
of the ACM, Vol.44, No.10, pp.51-57, October 2001.
”Developing Measures of Class Cohesion for Object-
[4] L. C. Briand, J. Daly and J. Wuest. A Unified Frame- Oriented Software,” Proc. 7th Annual Oregon Work-
work for Coupling Measurement in Object-Oriented shop on Software Metrics, June 1995.
Systems. IEEE Transactions on Software Engineering,
Vol.25, No.1, pp.91-121, January/February 1999. [17] P. Tarr, H. Ossher, W. Harrison, and S. M. Sutton, “N
Degrees of Separation: Multi-Dimensional Separation
[5] L. C. Briand, P. Devanbu, and W. Melo, “An Investiga- of Concerns,” Proceedings of the International Confer-
tion into Coupling Measures for C++,” Proc. 19th Inter- ence on Software Engineering, pp.107-119, 1999.
national Conference on Software Engineering, pp.412-
421, May 1997. [18] J. Zhao, “Toward A Metrics Suite for Aspect-Oriented
Software,” Technical Report SE-136-5, Information
[6] L. C. Briand, S. Morasca and V. Basili. Property-Based Processing Society of Japan (IPSJ), March 2002.
Software Engineering Measurement. IEEE Transac-
tions on Software Engineering, Vol.22, No.1, pp.68-86, [19] J. Zhao and B. Xu, “Measuring Aspect Cohesion,”
1996. Proc. Fundamental Approaches to Software Engineer-
ing (FASE’2004), LNCS 2984, pp.54-68, Springer-
[7] S. R. Chidamber and C. F. Kemerer. A Metrics Suite for
Verlag, Barcelona, Spain, March 2004.
Object-Oriented Design. IEEE Transactions on Soft-
ware Engineering, pp.476-493, Vol.20, No.6, 1994.
[8] J. Eder, G. Kappel, and M. Schrefl. Coupling and Co-
hesion in Object-Oriented Systems. Technical Report,
University of Klagenfurt, 1994.