Towards Generating Templates of Method Body
Based on Method Name and Related Identifiers
Yuya Onizuka∗ Yasuhiro Hayase† Tetsuo Yamamoto‡
Yuki Kashiwabara∗ Takashi Ishio∗
Katsuro Inoue∗∗

Graduate School of Information Science and Technology, Osaka University {y-onizuk,k-yuki,ishio,inoue}@ist.osaka-u.ac.jp†
Faculty of Engineering, Information and Systems, University of Tsukuba hayase@cs.tsukuba.ac.jp‡
College of Engineering, Department of Computer Science, Nihon University tetsuo@cs.ce.nihon-u.ac.jp

Abstract
In modern software development, developers have to select andcombine appropriate APIs from software libraries to implement
any features. This paper proposes an approach that takes as input
a method name which a developer is attempting to create, andsuggests APIs that are likely used as a template of method body.
By using the template as a reference and/or editing the template,the developer can write the method body. Our approach generates
templates from association rules that associate APIs with identifiers
such as method names, class names, and field types/names included
in a large set of source files.
Categories and Subject Descriptors D.2.6 [Software Engineering]: Programming Environments
Keywords API, Code completion, Association rule mining

1. Introduction
In modern software development, developers have to select andcombine appropriate APIs from software libraries to implement
features. However, learning APIs is difficult and time consuming
because of numerous APIs and their combinations.
Some approaches which suggest candidates of method bodyfrom the context of the source code under editing are proposed
to develop software efficiently [2–4]. They suggest code snippets
by leveraging the context of the method body, therefore, they cansuggest better candidates than code completion of standard IDEs.
However, they cannot suggest appropriate candidates without a partof a method body.
In this paper, we propose to suggest sets of APIs as templates ofa method to developers attempting to create a new method. The
approach suggests APIs which developers probably write in themethod body after they decide the method name. The idea whichsuggests templates of a method body is implemented as one of thecode completions in Eclipse [1]. Eclipse suggests creating method
getFoo and setFoo in a class which has field foo, because developers
are likely to create a getter/setter to get the value or set a value

Permission to make digital or hard copies of all or part of this work for personal orclassroom use is granted without fee provided that copies are not made or distributedfor profit or commercial advantage and that copies bear this notice and the full citationon the first page. To copy otherwise, to republish, to post on servers or to redistributeto lists, requires prior specific permission and/or a fee.
AOAsia’13, March 25, 2013, Fukuoka, Japan.
Copyright c 2013 ACM 978-1-4503-1861-7/13/03. . . $15.00

in a class declared a field. We generalize the idea for other kindsof methods so that IDE can suggest templates of method body todevelopers. APIs which developers are likely used in method bodyare learned by association rule mining. We associate APIs in eachmethod with its method name and identifiers around the method
such as class names, field names and field types.

2. ApproachThis paper proposes an approach which suggests templates ofmethod body. Developers complete the method body by using thetemplate as a reference and editing it. Our approach focuses on
relations between a method name and the method body to generate
templates of method name.
A method usually has a short and self-describing name, whoseverb and object respectively express the operation and its target
performed in the method. In some cases, a method name comprises
only a verb, whose object is implicitly indicated by identifiers
around the method, e.g. the class name or fields.
Ideally, it is expected that generated templates of method bodyare complete source code, however, generating complete methodbodies to general methods is very difficult, since method bodies
have variable definitions, method calls, and complex control structures.
In this paper, a template of a method body is a set of methods
which are likely called in the method. Our approach helps developers to choose APIs from a lot of APIs; developers can look up
various documents that explain the usage of the APIs included in atemplate.
Templates of method body are generated from association rules
which association rule mining find from a large set of source files.
Association rule mining use method names, methods called by themethods, and identifiers around the methods such as class namesand field types/names. Method names are divided into verb andobject because the verb of a method name represents the behaviorof the method.
One example is a Data Access Object (DAO) class. Classes
using databases have connection fields and is defined a method
which executes SQL statements. The method is named updateUserInfo and so forth and processed the following steps: 1) create a
SQL statement (the method name is createStatement), 2) execute
the SQL statement (executeUpdate), 3) close the SQL statement(close). It expects that association rules found from source files using a database have a pattern which methods called by the method
are createStatement, executeUpdate, and close when the field typeis Connection and the verb of the method is update. Our approachsuggests a set of methods {createStatement, executeUpdate, close} 3, 4  5 672+  $5  $  $  $      ! "#$ %    $    

      +  % 2 $   $       

&     ' ()* &       '   +  #  ' (,,,truv w xvyrz{|} qr{~| pnqr

kuqrp{okzzroo } rmokr{~|o znmmr
v n qr{~|

h8$$    9:-./0. 1

55 $sk<=>?@A A?BCC DEF GHIJJKLMNIJ LIJJO          $ 5 $$    $2+

lmnoo pnqr

   %   $      8$$      5 ;, jk&       '    +  #  ' (    <=>?@A @PQ RSTUMKVWKXYJZI[\MXNJ] W^_` G\MUMKaKJM WMaM b JR__O
NJM LIRJM b cO
MXd GWMaM b MeNWfLIJJfLXKUMK\MUMKaKJM[`O
LIRJM b WMaMfKgKLRMKVSTUMK[W^_`Oh ZNJU__d G
NZ [WMaM ib JR__`WMaMfL_IWK[`Oh
XKMRXJ LIRJMOhFigure 2. VERB AND OBJECT OF METHOD NAME AND
IDENTIFIERS RELATED TO METHOD BODY

Figure 1. AN OVERVIEW OF THE IMPLEMENTATION OFOUR APPROACH

by using this pattern when a developer creates a new method theverb of which is update in a class defined a Connection field.

3. Implementation
An overview of the implementation of our approach is shown inFigure 1. The implementation consists of two steps: 1) learningknowledge by source files, and 2) code completion using association rule DB. Step 1 is performed in advance of Step 2.
Step 1 creates a database that contains support, confidence,and association rules that each antecedent is a set of identifiers ofsource files and each consequent is a set of methods called by amethod. In terms of association rule mining, a transaction is a setof identifiers related to a method. For each method in source files (atraining data set), we extract the verb of the method name, the object of the method name and related identifiers including the class
name, arguments of the method, fields accessed by the methods
and method names called in the method as shown in Figure 2. Eachname is associated with its origin; for example, the class name
“DAO” is represented as an item “class:DAO” in a transaction.
After all methods are translated into transactions, we execute association rule mining to extract association rules whose consequentis a set of methods called in method bodies. For example, a rule{verb:update, fieldType:Connection} ⇒ {method:createStatement,
method:executeUpdate, method:close} is extracted from source
files. This rule indicates that a method likely calls a set of methods createStatement, executeUpdate and close if the verb of themethod is update and the method can access a field whose type isConnection.
Step 2 suggests templates of method body as sets of method
calls to a developer just after input a method name. We extract
verbs, objects, and related identifiers from a source file edited bya developer, and then look for association rules whose antecedents
are included in the source file. Finally, consequents of searched
rules are suggested as templates of method body to the developer.
The templates are sorted or narrowed down by support, confidence,and so on. The developer chooses a template and inserts it in thesource file. The developer writes the method body by using thetemplate as a reference and/or editing the template.

4. Current Status and Future Work
Currently, association rules are obtained from a small set of source
files, and we are evaluating the rules. We are going to analyze theresults and create association rule DB from a larger set of source
files. In parallel, we are implementing our approach as an eclipse
plugin.
We are planning two evaluation experiments. One experimentevaluates the usefulness of suggested templates. In the experiment,
we compare templates of a method body generated from methods
in source files by our approach with the original method body to
see that they match. The other experiment asks subjective opinions.
Some developers worked on the tasks and answers questionnaire
about generated templates and usability of the tool.
Even though a method can have two or more functions, its
name tends to expresses only one of these functions. Those multifunction methods could obstruct the knowledge extraction fromsource file set, since method calls implementing a function arebound for a method name expressing a different function. AspectOriented programming techniques can improve the effectinvenessof our approach by improving cohesion of multi-function methods.
We are also planning to provide not a set of method calls but
different type of data to a developer. For example, developer canobtain the appropriate order of API usage if a sequence of method
calls is used as a method template.

Acknowledgments
This research was supported by KAKENHI No.21700031,No.21240002, and No.23680001.

References[1] Eclipse. http://www.eclipse.org/.
[2] M. Bruch, M. Monperrus, and M. Mezini. Learning from examples
to improve code completion systems. In Proceedings of the 7th joint
meeting of the European Software Engineering Conference and theACM Symposium on the Foundations of Software Engineering, 2009.
[3] A. T. Nguyen, T. T. Nguyen, H. A. Nguyen, A. Tamrawi, H. V. Nguyen,J. Al-Kofahi, and T. N. Nguyen. Graph-based pattern-oriented, contextsensitive source code completion. In Proceedings of the 2012 International Conference on Software Engineering, pages 69–79, 2012.
[4] Y. Tetsuo, Y. Norihiro, and H. Yoshiki. Seamless souce code reuseusing source code corpus (in japanese). IPSJ Journal, 53(2):644–652,
feb 2012.