Graph-based Pattern-oriented, Context-sensitive Code Completion

Anh Tuan Nguyen, Tung Thanh Nguyen, Hoan Anh Nguyen, Ahmed Tamrawi, Hung Viet Nguyen, Jafar Al-Kofahi,
Tien N. Nguyen

Video Demo Downloads

Motivating Examples

API Usage Patterns

 

Example 1
Figure 1. SWT Usage Example 1

 

Figure 1 illustrates a portion of code that uses SWT, a Java graphical user interface (GUI) library, to create a window containing a button labeled "OK". A Display object is responsible for managing the GUI events and related resources between SWT and the underlying operating system (OS). A Shell object represents a GUI window, which is associated with the Display object and acts as a container for other GUI elements such as buttons, text boxes, etc. Thus, as seen in Figure 1 (lines 1-2), after a Display object (display) is created, a Shell object (shell) is then created to form a top-level window, which uses the associated Display object to receive and process the GUI and OS events. At the lines 9-15, object shell is activated and starts to receive and process the events via object display. This task is done via the call to shell.open and the while loop in which object display waits for and dispatches the events (using method readAndDispatch) until the window is closed, i.e. object shell is disposed. Finally, object display is disposed.

This API usage (lines 1-2 and 9-15) occurs very frequently in Java code that uses SWT library. We call this correct API usage an API usage pattern of SWT (or a pattern for short). As seen, this pattern includes two objects and multiple method calls with specific control and data dependencies among them. For example, a Shell object is created after the creation of the Display object, and uses that Display object in its initialization. The pattern also involves two program control structures: a while loop and an if branching statement. Due to possible branch(es) and loop(s), the elements in a pattern are structurally and semantically related, rather than sequentially.

This example of a usage pattern of a library illustrates a common practice in software development. That is, developers reuse existing software components via software libraries and their APIs. Libraries could be standard (e.g. Java JDKs, SWT, Elipse JDT) or project-specific.

Since code completion tools aim to improve developers' productivity in programming, they could be based on the usage patterns of the APIs and help developers reduce their burden in remembering exact API elements and their usages. With the knowledge on API usage patterns, it could recommend the appropriate usage pattern that fits with the (incomplete) code under editing. For example, if a developer finishes just the two first lines in Figure 1, a pattern-oriented code completion tool should recognize that (s)he is using two SWT variables of types Display and Shell, which are often used in the aforementioned usage pattern for creating a GUI window. Then, it could predict that (s)he intends to create a GUI window, and recommend the remaining part of the usage pattern (as in the lines 9-15). Thus, instead of manually writing the entire usage, the developer could get help from the tool to automatically complete the rest.

This type of code completion could help the developer to complete his/her code faster than the single/individual method recommendation in the traditional code completion tools In summary, API usage patterns are well-suited to serve as a guidance for code completion.