• 検索結果がありません。

aspect

: ’aspect’ IDENTIFIER aspectBody aspectBody

: ’{’ element* ’}’

element

: intertypeDeclaration

| pointcutDeclaration

| adviceDeclaration languageDependentBlock

: languageDeclaration

’{’ CONTENTS ’}end’

languageDeclaration : ’@’ languageType languageType

: ’Java’ | ’JavaScript’ | ’C’ | ’VB’

| ’CSharp’ | ’Ruby’ | ’Python’

intertypeDeclaration

: IDENTIFIER ’:’ languageDependentBlock pointcutDeclaration

: ’pointcut’ IDENTIFIER ’()’

’:’ pointcutDeclarator ’;’

pointcutDeclarator : pointcutType ’(’ TYPE

IDENTIFIER ’.’ IDENTIFIER ’()’ ’)’

pointcutType

: ’execution’ | ’call’

| ’get’ | ’set’

adviceDeclaration : adviceType ’:’

IDENTIFIER ’()’ adviceBody adviceType

: ’before’ | ’after adviceBody

: ’{’ languageDependentBlock+ ’}’

Figure 6-9: BNF for aspect in UniAspect

be extracted and sent to the UCO generator. On the other hand, the remainder of the aspect file will be checked for correct syntax in this module, and when there is a syntax error, it will be reported. The extracted information about weaving will only be sent to the next modules when the aspect analyzer can successfully parse the file.

6.6. IMPLEMENTATION 139

6.6.2 UCO Generator

The UCO generator translates the input source code and code fragments extracted from the previous module into a UCO using UNICOEN.

Languages that UniAspect supports are dependent on that of UNICOEN. As described in section 4, currently UNICOEN supports seven languages, and there-fore, UniAspect supports same seven languages for AOP. Moreover, since developers are able to implement new languages into UNICOEN by writing mapping rules for the UCO, UniAspect can deal with more languages. Since the weaving process is performed upon the UCO, additional implementation of the weaver is almost not necessary if new language have the joinpoint that have been adopted in UniAspect.

When there is syntax error in the extracted code fragments, it will be reported by this module. The generated UCO is then sent to the weaver.

6.6.3 Weaver

This module weaves a UCO of advice into a UCO of the input source code based on weaving information received from the aspect analyzer. This module performs three steps: identification of the joinpoints, the insertion of advice and the replacement of particular variables. As an example, Figure6-10shows the algorithm for weaving into an execution joinpoint. The details of each step are as follows, using the examples in Figures 6-10 and 6-11.

(1) Identification of joinpoints

The weaver identifies joinpoints in a UCO based on received information. Since a UCO forms a tree structure, as shown in Figure 6-11, it is easy to identify specific elements in a UCO of the input source code. For example, since the execution pointcut selects function declarations, this module obtains the list of objects of function declarations (Figure 6-11-1). Line 3 of Figure 6-10 shows how to obtain all function declaration objects, which can be traced from the root object. Other pointcuts behave similarly:

a call pointcut gathers all call objects, and a set pointcut gathers all assignment

1 ExecutionWeaveing(pointcut, advice){

2 //(1)Identification of joinpoint

3 var functions = root.getAllElements<Function>();

4 foreach(func : functions) {

5 if(func.name == pointcut.name &&

6 func.type == pointcut.type &&

7 func.Parent.name == pointcut.className) {

8

9 //(2)Insertion of advice

10 func.block.insertFirst(advice);

11

12 //(3)Replacement of particular variables

13 var variables = func.getAllElements<Variable>();

14 foreach(variable : variables) {

15 if(variable.name == "JOINPOINT_NAME") {

16 variable.name = func.name;

17 }

18 }

19 } } }

Figure 6-10: Algorithm of weaving into execution joinpoint

Function

Name Type ・・・

Function

Name Type ・・・

Class

Statement Call expression expression

Block Block

Block

①Obtain the list of functions

②Check conditions of pointcut

③Insert advice into appropriate location

Figure 6-11: Process of execution weaving expression objects.

Next, this module narrows the list of objects in accordance with the given pa-rameters of a pointcut. In the case of an execution pointcut, this module obtains the name, return type and parent name from the linked object, and checks whether they match the given conditions (Figure 6-11-2). Line 5 of Figure 6-10 shows the comparison of the function name and specified name, and lines 6 and 7 of Figure6-10 show the comparison of the type and belonging class. Finally, the objects that satisfy all given conditions will proceed to the next step.

(2) Insertion of advice

6.6. IMPLEMENTATION 141

In this step, the weaver inserts the UCO of advice into the joinpoint specified in the previous step. The basic operation is to insert the advice into a block so that it becomes a previous sibling in the case of before (Figure6-11-3). Line 10 of Figure6-10 shows how to add an appropriate object of advice into the head of a function block.

In the UCO generator, the advice is translated into a UCO as a block, and because the unified code model allows a nesting structure, a block can include another block.

UNICOEN can correctly translate this nesting structure into source code. In fact, most programming languages have adopted this syntax in which blocks can include other blocks. For example, Figures 6-12 and 6-13show examples of nesting Java and JavaScript, respectively. Thus, UniAspect guarantees safe weaving in terms of syntax by inserting advice as a block.

However, in the case of weaving after the execution joinpoint, it is necessary to insert advice in a specific way. For example, if an object is inserted into the end of a block, as shown in Figure 6-14, it will become dead code and an incorrect result will be obtained. Therefore, in this case, a list of all return statements in function block is obtained, and objects are inserted before each return statement. As a result, the advice will always be performed even if a function is ended by any return statement.

Alternatively, when a return type of function is void, the advice is inserted into the end of the block depending on the number of return statements in the function.

block

: ’{’ blockStatement* ’}’

blockStatement

: localVariableDeclarationStatement

| classOrInterfaceDeclaration

| statement statement

: block

| ifStatement

| forStatement

| (omitted)

Figure 6-12: BNF for Java

(3) Replacement of particular variables

statementBlock

: ’{’ statementList? ’}’

statementList

: statement (statement)*

statement

: statementBlock

| ifStatement

| iterationStatement

| (omitted)

Figure 6-13: BNF for JavaScript

1 int abs(int a) {

2 if(a > 0)

3 return a;

4 else

5 return -a;

6 }

Figure 6-14: Function contains return statements

Finally, this module replaces particular variables with joinpoint information as de-scribed in section 5.3. Line 13 of Figure 6-10 obtains a list of variables from a UCO and line 15 checks whether their values match ”JOINPOINT NAME”. Line 16 re-places each variable with the appropriate joinpoint name, concluding this step. The weaver repeats these three steps for all pointcuts specified in aspect file. Finally, it regenerates source code from the UCO as the final process.