Method Differentiator Using Slice-based Cohesion Metrics
Akira Goto1 , Norihiro Yoshida2 , Masakazu Ioka1 , Eunjong Choi1 , Katsuro Inoue1
12

Graduate School of Information Science and Technology, Osaka University, Japan{a-gotoh, m-ioka, ejchoi, inoue}@ist.osaka-u.ac.jp
Graduate School of Information Science, Nara Institute of Science and Technology, Japanyoshida@is.naist.jp

ABSTRACTIn our tool demonstration, our plugin visualizes semantic
diﬀerences between a pair of methods involved in an open
source software (see Figure 5 and 6).

It is important to understand semantic diﬀerences between
a pair of Java methods during maintenance. However, textual or syntactic diﬀerence is insuﬃcient to give clear idea
which code fragment realizes a single functionality in Javamethods. In this paper, we present an Eclipse plugin forsemantic diﬀerentiation of a given pair of Java methods.

2.

PROPOSED TOOL

Figure 1 shows an overview of the proposed tool. The
proposed tool accepts a pair of given Java methods as input,
and then identifies sets of Semantic Diﬀerences (SDs). Given
two Java methods(ma , mb ), code fragments CF s extracted
from them are called a set of SDs when CF s satisfy all of
the following three conditions:Categories and Subject Descriptors
D.2.7 [Software Engineering]: Distribution, Maintenance,and Enhancement—Restructuring, reverse engineering, andreengineering

1. Any SD in CFs does not overlap any other SD in thecode fragments.

General Terms

2. Any SD in CFs involves at least one syntactic diﬀerence between ma and mb .

Experimentation

Keywords

3. After all SDs in CFs are removed, ma and mb aresyntactically same.

Cohesion metrics, Program slicing

After the SD identification, identified sets of SDs are ranked
based on slice-based cohesion metrics. Highly-ranked set ofSDs means that each SD realizes a single functionality. Oncea user selected one of sets of SDs, SDs in the selected set arehighlighted (see Figure 5 and 6).

1. INTRODUCTION
Developers often need to understand semantic diﬀerences
between a pair of Java methods during software maintenance(e.g., enhancement, form template method refactoring[3]).
However, textual or syntactic diﬀerence is insuﬃcient to give
clear idea which code fragment realizes a single functionality
in Java methods.
Therefore, we have developed an Eclipse plugin for sematic diﬀerentiation of a given pair of Java methods. Thisplugin firstly identifies syntactic diﬀerences a given pair ofJava methods, and then finds cohesive code fragments including at least one syntactic diﬀerence. Finally, this plugin
proposes (highlights) two sets of cohesive code fragments assematic diﬀerences between a given pair of Java methods.
The cohesiveness of code fragments is calculated by slicebased cohesion metrics [4, 2] (i.e., Tightness, Coverage, andOverlap) which represent the functional uniqueness of a codefragment or method.

2.1

Detecting Syntactic Differences

First of all, the proposed tool extracts abstract syntax
trees (ASTs) from a pair of given Java methods. The AST
extraction is performed by Eclipse JDT1 .
Then, syntactic diﬀerences are detected by comparing twoASTs. Figure 2 shows an example of a syntactic diﬀerence.
A node in an AST has a type (e.g., IFStatement, NumberLiteral). In the case of several types, a node has a value(e.g., variable, constant, literal). The detection starts fromroot nodes of the ASTs. The comparison of two nodes isperformed according to their types. Only if those nodes
have values, their values are compared after that. In thecase that those two nodes are matched, the detection moves
to the child nodes of them. Otherwise, they are detected
as the diﬀerence. Please note that the minimum level of adiﬀerence is a statement in the detection.

Permission to make digital or hard copies of all or part of this work forpersonal or classroom use is granted without fee provided that copies are
not made or distributed for profit or commercial advantage and that copies
bear this notice and the full citation on the first page. To copy otherwise, torepublish, to post on servers or to redistribute to lists, requires prior specificpermission and/or a fee.
AOSD’13 Companion, March 24-29, 2013, Fukuoka, Japan.
Copyright 2013 ACM 978-1-4503-1873-0/13/03 ...$15.00.

2.2

Identifying Semantic Differences

To identify SDs from given two Java methods, code regions detected as syntactic diﬀerences are expanded. This
expansion has two types. The first one is expansion towards
111

http://www.eclipse.org/jdt/index.php

INPUT : A pair of similar methods
IFStatement

M1

If (…){
a = b;
b = c;}

M2
Assignment

Assignment

ab

Proposed Tool=b=c

Detecting syntactic differences

Expand to sibling nodeDetecting semantic differences

IFStatement

Computing Cohesion Metrics

Assignment

Assignment

ab=b=

If (…){
a = b;
b = c;}

c

Expand to parent node

OUTPUT : Ranking of semantic differences (SD)…
IFStatement

SDI

SDII

If (...){
a = b;
b = c;}

SDm
Assignment

AssignmentabFigure 1: Overview of the proposed tool
Block=b=c

Block

Figure 3: Example of expantions
Assignment

Assignment

Assignment

Assignment

abab=b

Different node=c=b=based cohesion metrics [4] because the characteristics of theselected metrics are empirically verified[2]. A user can selectone of the slice-based metrics in the proposed tool.
We redefine Tightness, Coverage, and Overlap as follows
for small code fragments. In the following definitions, M isa method, len(M ) is the number of statements in M , V is aset of parameters and return values, Vi is a set of parametersin M , Vo is a set of return values in M , F SLx is a forward
slice in the case that the entry point is variable x, BSLx isa backward slice in the case that the entry point is variablex, SLint is an intersection of forward slices obtained for allvariables in Vi and backward slices obtained for all variables
in Vo .

dSubtree corresponding to syntactic difference

Figure 2: An example of a syntactic diﬀerence

sibling nodes (ETS). The other one is expansion towards
parent nodes (ETP). The expansion starts from a node recognized as a diﬀerence, and then apply ETS to the code
region of the node. After ETS is applied to all of siblings ofthe node, ETP is applied to the siblings. After each expansion, each code region is identified as a SD. The expansion
is ended when it moves to a root node of each AST.
Figure 3 represents examples of ETS and ETP. In thisfigure, this expantion starts from a = b corresponding tothe syntactic diﬀerence, and then the perform ETS. Finally,
ETP is perfromed for moving to the if statement.
Given two Java methods often has one more than syntactic
diﬀerences. In this case, this expantion is applied to eachof the diﬀerences, and then all of resultant sets of SDs arecombined.

F T ightness(M )=|SLint |len(M )F Coverage(M )=X |BSLx |1 X |F SLx |+)(|V | x∈V len(M ) x∈V len(M )iF Overlap(M )=oX |SLint |1 X |SLint |(+)
|V | x∈V |F SLx | x∈V |BSLx |ioWe suppose that each SD is extracted as a method M , eachslice-based metric is calculated according to the above definitions. The slice-based cohesion metric of a set of SDs isan average of metrics of SDs.

2.3 Sorting by Slice-based Cohesion Metrics
Identified sets of SDs are ranked according to the cohesiveness of each SD. Cohesiveness of each SD is calculated basedon slice-based cohesion metrics. To calculate those metrics,
program slice and dependence graph is derived using Antlr2We selected Tightness, Coverage, and Overlap from slice-

As a demonstration, we applied the proposed tool to apair of two Java methods in Apache Ant 1.7.03 . One of thosemethods is executeDrawOperation method in Arc class. The233.

http://www.antlr.org

12DEMONSTRATION

http://ant.apache.org/

Figure 4: Visualization of syntactic diﬀerences
other one is executeDrawOperation method in Ellipse class.
Those methods are similar but syntactically diﬀerent.
At first, the proposed tool simply highlights syntactic differences between the given methods (Figure 4). The corresponding diﬀerences are painted by the same color.
Once a user pushes the Detect button at the bottom of
the window, the expantion, the metric calculation, and thesorting are performed. Figure 5 shows the screenshot after
the sorting. In this figure, a tab corresponds each set of SD,
and indicates selected slice-based cohesion metric. In thiscase, 14 sets of SDs are derived, and the tab 2 is selected.
FTightness metric in the tab is 0.88.
In this figure, the red SDs correspond buﬀer allocation, thegreen SDs correspond rendering of outline of a graphic, andthe yellow SDs correspond rendering of the body a graphic.
According to this result, the proposed tool successfully presented SDs using FTightness metric, each of which realizes
a single functionality.
Figure 6 is a screenshot that illustrates another set of SDs.
Although the FTightness metric 0.62 is lower than before,
the green SDs correspond whole rendering of a graphic. Appropriate set of SDs depends on the context of development(e.g., bug fixing, refactoring). We believe that suggesting
multiple sets of SD by the proposed tool is useful for practical software development.

Java methods. CloneDiﬀerentiator characterizes identified
diﬀerences using program dependence graph. On the otherhand, we focus on the finding of cohesive code fragment including at least one syntactic diﬀerence for giving clear ideaof semantic diﬀerences in a given pair of Java methods.

5.

SUMMARY

We presented an Eclipse plugin for sematic diﬀerentiationof a given pair of Java methods. As a demonstration, weshowed the visualization of the semantic diﬀerences between
a pair of methods involved in Apache Ant.
One of future works is conducting empirical studies toshow the usefulness of the proposed tool. We plan to confirm
that the proposed tool is able to reduce the eﬀort for bug
fixing and refactoring. Also, the development of refactoring
tool based on the proposed tool is interesting challenge.

6.

REFERENCES[1] M. Ioka, N. Yoshida, T. Masai, Y. Higo, and K. Inoue.
A tool support to merge similar methods with acohesion metric COB. In Proc of IWESEP 2011, pages
23–24, 2011.
[2] T. M. Meyers and D. Binkley. An empirical study ofslice-based cohesion and coupling metrics. ACM Trans.
Softw. Eng. Methodol., 17(2):1–27, 2007.
[3] M.Fowler. Refactoring: Improving the Design ofExisting Code. Addison Wesley, 1999.
[4] M. Weiser. Program slicing. In Proc of ICSE 1981,
pages 439–449, 1981.
[5] Z. Xing, Y. Xue, and S. Jarzabek. Clonediﬀerentiator:
Analyzing clones by diﬀerentiation. In Proc of ASE
2011, pages 576–579, 2011.

4. RELATED WORK
In our earlier work, we have developed a method diﬀerentiator using Cohesion of Blocks (COB)[1]. It is easier tocompute COB compared to slice-base cohesion metrics. We
believe that slice-based metrics used by this study are more
appropriate to capture the semantic of a program.
Xing, et al. presented CloneDiﬀerentiator[5]. It is a differentiator for duplicated code. We have developed a diﬀerentiator plugin for a pair of Java methods. Our plugin canbe applied to a pair of partly similar but mostly diﬀerent13

Figure 5: Visualization of semantic diﬀerences (FTightness is 0.88)

Figure 6: Visualization of semantic diﬀerences (FTightness is 0.62)

14