An Exploratory Study On API Method Parameters

A number of techniques have been developed that support method call completion, however, there has been little focus on the problem of method parameter completion. The task of method parameter completion is non trivial and we know little about how developers complete them. We conducted a study that helps us to understand how developers complete method parameters. Based on our observation, we developed a recommendation technique, called Parc, that uses a source code localness property to collect parameter usage context. Parc uses previous code examples together with contextual and static type analysis to recommend method parameters. Evaluation with the only available state-of-the-art tool using a number of subject systems and different Java libraries shows that our proposed technique has strong potential. Furthermore, Parc also improves the parameter recommendation support of Eclipse JDT (Java Development Tool)

You can download the source code of Parc from the following link: Parc Source Code

The subject systems we used for our study can be download from the following link: Subject Systems used in our study

You can find more detail about how developers complete method parameters in the following section:

How Developers Complete Method Parameters

Recommending method parameters using previous code examples can be supported by identifying patterns of parameter usage. Towards this goal, we first conduct a manual investigation to understand how developers complete method parameters in practice. This helps us to identify language constructs that need to be considered to capture the context of a parameter usage. Due to the large number of API methods and their pa-rameters it is not feasible to analyze all of them. Thus, we follow the following procedure. We analyzed three different subject systems (JEdit, ArgoUML, JHotDraw). The actual parameters use in calling methods of Java Swing and AWT libraries were collected and then categorized by their expression types. More about these expression types can be found in the Eclipse JDT documentation . 100 examples from each category were randomly selected and manually analyzed with their corresponding source code to identify usage patterns. Although there can be different kinds of expressions, we ignore some of them because they are too complex or we do not find any example of them. In the following subsections we describe the usage patterns we observed for these parameter expression types.

A. Simple Name
Simple names are identifiers excluding Java keywords. Simple names can appear in three different ways: local variables, method parameters or as Java class fields. We found the following three patterns while completing simple names. In the first case a local variable that is being used as a loop counter is used to complete a method call. For example, variable pos is used both as a loop counter and as a parameter of a method.
for (int pos=size – 1; pos >= 0; pos–) {
UndoableEdit u;
u =(UndoableEdit)edits.elementAt(pos);

In the second case, developers create an instance of an object or a local variable, initialize it and then use the variable as a method parameter. For example, the object or variable in this case can be a class variable that is being initialized within a constructor. See following for an example of this:

Dimension dim= new Dimension(100, 100);
frame.setSize ( dim ) ;
In the final case, developers use the parameter of the enclosing method to complete a method parameter, as in the following code fragment:
public void addComponent(TextArea ta){
ta.setLineWrap( true ) ;
JScrollPane scroll=new JScrollPane(ta);
B. Qualified Name
A qualified name is defined recursively as a simple name preceded by another name. In general this simple names are static fields of a Class. The following is an example of a method invocation that uses the qualified name BorderLayout.CENTRE as a method parameter:
JFrame frame =newJFrame();
Container c= frame.getContentpane();
c.add(new JLabel(“Place”, BorderLayout.CENTRE) ;
Their use can depend on other factors or can be arbitrary. For example, while adding a component to a JPanel, developers typically use a qualified name to specify the target position. The target qualified name in this case (whether it should be a BorderLayout.Centre or FlowLayout.LEFT) depends on the LayoutManager specified using the setLayout method of the JPanel class.

C. Method Invocation
Here, parameter expression of type method invocation is used to complete an API method call. The receiver of the method invocation can be a local variable, enclosing method parameter, field variable or another method call. For example:
Container menubar = createMenuBar();
//additional code goes here

D. Class Instance Creation
There are two general usage patterns for the class instance creation type. In the first case developers create an instance of an object. In the other case an anonymous class has been created as a method parameter and additional functionality has been added through overriding methods of that class. We found various examples of the later case while adding various Listeners to an instance of JComponent. For example:
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setMinimumSize(new Dimension(120,0))

Another example of this is shown below:
JButton closeButton = new JButton(“Close”);
new ActionListener()
{ public void actionPerformed(ActionEvent event)
//additional code goes here

E. Field Access
In the case of a parameter of field access expression type, the receiver is typically a method call that returns an object of a particular type. We found only a few cases of field access parameters to complete method calls. For example: public void paintComponent(Graphics gfx){
Graphics2D g2d =(Graphics2D)_gfx;
g2d.setRenderingHints( textArea.getPainter( ).renderingHints);
//additional code goes here

F. Array Access
We also observe cases where developers create an array of objects and access individual elements using a loop counter. For example, in the following example an array of type Action object is accessed to add individual element to another object.
Actions actions[]=ta.getActions[];
for (int i=0; i <mid; i++) {
firstHalf.add( actions [ i ] );

G. Number literal
Here, numbers are used to complete method parameters. Although, it may be difficult to accurately predict the correct parameter value in this case, but previous code examples can guide developers to learn about typical values others used for the same method and can help to decide to use the correct one.

H. String literal
Here, string constants are used to complete method parameters. See below for an example of using string literal as a method parameter:
JLabel label =newJlabel();

I. Boolean literal
There are only two possible values used while using boolean literals (true or false). For a group of method calls developers only use a certain boolean value as a method parameter. For instance, in all examples of the setVisible method calls of the JFrame object contain true as the method parameter. This indicates that the boolean value false is highly unlikely for this method call. During manual investigation we also found that in many cases the target boolean value of a method parameter depends on the condition of the If expression that encloses that method call. In those cases, considering the condition of if expression can help to better predict method parameter.

J. Null literal
Here, a null value is used to complete a method parameter. Developers use the value to ignore a method parameter. For example, consider the showInputDialog method that takes seven parameters. The two parameters before the last one are of type Icon and array of Objects that gives the default selection values. Developers often use the dialog box without using any icon or default selection values by using null. An example of this is shown below:
public void run(){
dispose( ) ;
JOptionPane.showMessageDialog( null, message, “Installation aborted”, JOptionPane.ERROR_MESSAGE);
//additional code goes here

K. Casting
Casting expressions contain the following form to complete method parameters: (Type name).Expression. The Expression here can be any valid expression type. Such expressions are not very frequent and those that exists frequently contain infix expressions that are complicated to predict correctly. An example can be: progress.setMaximum((int)max))

L. This expression
Here, the keyword this is used for self reference. We found that while adding or removing various action listeners developers use the this keyword to complete parameters and the enclosing class in such cases implement the corresponding listener interface. The receiver can also contain a class name followed by a dot(.), however such cases are very rare. The following is an example of using this as a method parameter:
public Transferable getTransferable() {
return clipboard.getContents(this);

M. Type Literal
The type literal parameter takes the form of Type-Name.class. The TypeName can come from the type name of enclosing method parameter, left hand side expression of an assignment operator or can be the root in the class hierarchy (that is Object). The following code fragment shows an example of this:
public static EditPane get(TextArea ta)
if (ta == null) return null;
SwingUtilities.getAncestorOfClass( EditPane.class,ta);
//additional code goes here

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s