Keyword Programming in Java

Keyword Programming in Java

Keyword Programming in Java Greg Little and Robert C. Miller MIT CSAIL 32 Vassar Street Cambridge, MA 02139 {glittle,rcm}@mit.edu ABSTRACT Keyword programming is a novel technique for reducing the need to remember details of programming language syntax and APIs, by translating a small number of keywords pro- vided by the user into a valid expression. Prior work has demonstrated the feasibility and merit of this approach in limited domains. This paper presents a new algorithm that scales to the much larger domain of general-purpose Java programming. We tested the algorithm by extracting key- words from method calls in open source projects, and found that it could accurately reconstruct over 90% of the origi- nal expressions. We also conducted a study using keywords generated by users, whose results suggest that users can ob- tain correct Java code using keyword queries as accurately as they can write the correct Java code themselves. Figure 1: In keyword programming, the user types some keywords, presses a completion command Categories and Subject Descriptors (such as Ctrl-Space in Eclipse), and the keywords D.2.3 [Software Engineering]: Coding Tools and Techniques{ are translated into a valid expression. program editors, object-oriented programming Our goal is to develop techniques that reduce the bur- General Terms den of remembering the details of a particular language or Experimentation, Languages API. The technique proposed in this paper, keyword pro- gramming, uses a few keywords provided by the user to Keywords search for expressions that are possible given the context of the code. The user interface takes the form of an advanced Java, Autocomplete, Code Assistants code completion interface in an IDE. For instance, Figure 1 shows a user entering add line in a Java ¯le, which the sys- 1. INTRODUCTION tem translates in-place to lines.add(in.readLine()). The generated expression contains the user's keywords add and Software development is rapidly changing and steadily in- line, but also ¯lls in many details, including the receiver creasing in complexity. Modern programmers must learn objects lines and in, the full method name readLine, and and remember the details of many programming languages the formal Java syntax for method invocation. and APIs in order to build and maintain today's systems. A In this paper, we propose an algorithm for ¯nding keyword simple web application may require the use of half a dozen query completions quickly. This work builds on keyword formal syntaxes { such as Java, Javascript, PHP, HTML, programming techniques in Chickenfoot [5] and Koala [4], CSS, XML, SQL { in addition to many di®erent APIs in but scales the algorithms to the larger domain of Java. each language. Learning, remembering, and using all these This work is similar to Prospector [6] and XSnippet [9], technologies correctly is becoming a signi¯cant burden. which suggest Java code given a return type and available types. However, our system uses keywords to guide the search, making it more expressive when type information Permission to make digital or hard copies of all or part of this work for alone is not enough to infer the desired code. personal or classroom use is granted without fee provided that copies are Our key contributions 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, to ² An algorithm for translating keyword queries into Java republish, to post on servers or to redistribute to lists, requires prior specific code e±ciently. permission and/or a fee. ² An evaluation of the algorithm on a corpus of open ASE’07,November5–9,2007,Atlanta,Georgia,USA. Copyright 2007 ACM 978-1-59593-882-4/07/0011 ...$5.00. source programs, using arti¯cial inputs generated by 84 extracting keywords from existing method call expres- We de¯ne a function as a tuple in T £ L £ T £ ::: £ T . The sions. The algorithm is able to reconstruct over 90% ¯rst T is the return type, followed by the label, and all the of the expressions correctly given only the keywords parameter types. As an example, the Java function: (in random order). This suggests that punctuation String toString(int i, int radix) is modeled as and ordering contribute relatively little information to (java.lang.String,(to, string), int, int). most Java method calls, so an automatic technique like For convenience, we also de¯ne ret(f), label(f) and keyword programming can take care of these details params(f) to be the return type, label, and parameter types, instead. respectively, of a function f. ² An evaluation of the algorithm on human-generated 2.4 Function Tree inputs. The algorithm translates the keyword queries The purpose of de¯ning types, labels and functions is to with the same accuracy as users writing correct Java model expressions that can be generated by a keyword query. code without tool support (which is roughly 50% in We model expressions as a function tree. Each node in the our study). tree is associated with a function from F , and obeys certain type constraints. In the next section, we present a model, which is followed In particular, a node is a tuple consisting of an element by a problem statement. We then present the algorithm, and from F followed by some number of child nodes. For a node the two evaluations. Then we discuss related work, future n, we de¯ne func(n) to be the function, and children(n) to work, and conclusions. be the list of child nodes. We require that the number of children in a node be equal to the number of parameter types 2. MODEL of the function, i.e., jchildren(n)j = jparams(func(n))j. We We want to model the following scenario: a user is at also require that the return types from the children ¯t into some location in their source code, and they have entered the parameters, i.e., a keyword query. The keywords are intended to produce 8iret(func(children(n)i)) 2 sub(params(func(n))i). a valid expression in a programming language, using APIs Note that in the end, the system renders the function tree and program components that are accessible at that point as a syntactically-correct and type-correct expression in the in the source code. In order to ¯nd the expression, we need underlying language. to model the context of the location in the source code. In 2.5 Java Mapping the case of Java, we need to model the available methods, We now provide the particulars for mapping various Java ¯elds and local variables, and how they ¯t together using elements to T , L and F . Most of these mappings are nat- Java's type system. The resulting model de¯nes the search ural and straightforward, and could be adapted to other space. languages. Although this paper focuses on Java, our model is more generic, and could be applied to many languages. We de¯ne 2.5.1 Classes the model M as the triple (T , L, F ), where T is a set of A class or interface c is modeled as a type in T , using types, L is a set of labels used for matching the keywords, its fully quali¯ed name (e.g. java.lang.String). Any class and F is a set of functions. that is assignment compatible with c is added to sub(c), 2.1 Type Set: T including any classes that extend or implement c. The model includes all classes that are directly referenced Each type is represented by a unique name. For Java, we in the current source ¯le, plus classes that can be obtained get this from the fully quali¯ed name for the type. Examples from those classes by method calls or ¯eld references. Since include int and java.lang.Object. the function trees generated by our algorithm are bounded We also de¯ne sub(t) to be the set of both direct and by a maximum depth, the model does not include classes indirect subtypes of t. This set includes t itself, and anything that can only be obtained by a method call sequence longer assignment-compatible with t. We also include a universal than that maximum. supertype >, such that sub(>) = T . This is used when we want to place no restriction on the resulting type of an 2.5.2 Primitive Types expression. Because of automatic boxing and unboxing in Java 1.5, we model primitive types like int, and char as being the L 2.2 Label Set: same as their object equivalents java.lang.Integer and Each label is a sequence of keywords. We use labels to java.lang.Character. represent method names, so that we can match them against the keywords in a query. 2.5.3 Methods To get the keywords from a method name, we break up Methods are modeled as functions that take their receiver the name at capitalization boundaries. For instance, the object as the ¯rst parameter. For instance, the method: method name currentTimeMillis is represented with the public Object get(int index) of Vector is modeled as: label (current, time, millis). Note that capitalization is ig- (java.lang.Object,(get), java.util.Vector, int). nored when labels are matched against the user's keywords. 2.5.4 Fields 2.3 Function Set: F Fields become functions that return the type of the ¯eld, Functions are used to model each component in an expres- and take their object as a parameter. For instance, the ¯eld sion that we want to match against the user's keyword query.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    10 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us