49
Interactive Extraction of Examples from Existing Code Auxiliary Material A1. Observations from Formative Study We conducted a formative study to understand the process that programmers follow when creating executable code examples from their own code, and the obstacles they encounter along the way. We observed 12 programmers as they created example code (referred to as F1–12 for “formative study participant”). Participants were recruited from our professional networks, local MeetUps, and computer science researchers from our institution. Participants were asked to create an understandable, executable code example from code they had recently written. The first five participants were asked to produce this code example as a blog post. We observed that the first five participants sometimes produced erroneous or untested code. With this observation, we realized that, without the proper tools, examples might be created that contain errors and do not run. We asked all remaining participants to only create a minimal, understandable, executable code example. Participants authored code examples in a variety of languages, including PHP, Java, Python, JavaScript, Bash, C, and C#. Code examples demonstrated a variety of tasks, including query- ing a Salesforce database, and building a Chrome extension for circumventing news website pay walls. One participant (F12) unexpectedly brought code that had been written by someone else that they wished to simplify; because they followed a similar workflow as other partici- pants, we include anecdotes from their experience as well. Theme 1: Supporting Additive Authoring of Code Examples When we cued the first five participants to produce a blog post that demonstrated a usage pattern with concise, easy-to-understand example code, all participants took an “additive” pro- cess to authoring example code, copying and pasting code from an existing project into a new

Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Interactive Extraction of Examples from Existing Code

Auxiliary Material

A1. Observations from Formative Study

We conducted a formative study to understand the process that programmers follow when

creating executable code examples from their own code, and the obstacles they encounter

along the way. We observed 12 programmers as they created example code (referred to

as F1–12 for “formative study participant”). Participants were recruited from our professional

networks, local MeetUps, and computer science researchers from our institution.

Participants were asked to create an understandable, executable code example from code

they had recently written. The first five participants were asked to produce this code example

as a blog post. We observed that the first five participants sometimes produced erroneous

or untested code. With this observation, we realized that, without the proper tools, examples

might be created that contain errors and do not run. We asked all remaining participants to

only create a minimal, understandable, executable code example.

Participants authored code examples in a variety of languages, including PHP, Java, Python,

JavaScript, Bash, C, and C#. Code examples demonstrated a variety of tasks, including query-

ing a Salesforce database, and building a Chrome extension for circumventing news website

pay walls. One participant (F12) unexpectedly brought code that had been written by someone

else that they wished to simplify; because they followed a similar workflow as other partici-

pants, we include anecdotes from their experience as well.

Theme 1: Supporting Additive Authoring of Code Examples

When we cued the first five participants to produce a blog post that demonstrated a usage

pattern with concise, easy-to-understand example code, all participants took an “additive” pro-

cess to authoring example code, copying and pasting code from an existing project into a new

Page 2: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

file. Programmers copied and pasted individual lines (F2, F4, F6) and the contents of entire

files (F1, F3) into a new buffer.

Participants who built a code example by moving code from an existing project faced two

major obstacles. First, in some cases, it could be tedious to rebuild the original code’s project

settings, dependencies, and environment for the code example. For F6, this involved gener-

ating configuration files, setting paths to libraries, copying import statements, duplicating style

files, and modifying the directory structure. All of these steps had to be completed before any

example code could be tested. The same code could encounter unexpected bugs in a new

location. For example, relative file paths resolved to the wrong location when the code was

run in a different directory (F11).

Second, programmers introduced bugs in the process of copying, pasting, and modifying

the code. Sometimes, important fragments of code were missing that caused the program

to crash, like missing variable definitions or return statements (F2, F4). Programmers in-

troduced bugs through manual edits that, while appearing innocuous, altered the program’s

behavior in unexpected ways (F4, F10).

After the first participants faced obstacles when authoring example code additively, we

asked F8–12 to author code examples “subtractively”, by removing code from a working project

until they created a minimal code example. While participants no longer needed to configure

the runtime environment for a new example, removing unrelated code and dependencies could

still take tens of minutes (F10, F12). This confirmed for us that, for both approaches, the

demonstrated authoring techniques were costly and tedious.

Theme 2: Modifying Examples to Improve Effectiveness

Participants omitted irrelevant implementation details to better highlight the focal code of an

example. To reduce irrelevant detail, participants simplified strings to “Lorum Ipsum” (F5)

and “hello world” strings (F7), and simplified the SQL queries to include only fields used by

the final example code (F8). Return types of methods were changed from complex types to

simpler types like lists (F8). Literal values were inserted to provide concise and reasonable

default parameters (F8, F12). Participants commented out the names of argument parameters

that would distract from those that the user should set, while leaving them present in the code

Page 3: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

to prevent compiler errors (F4), and annotated suggested values for arguments (F2, F4).

We observed that authors sometimes added code back in to make the example more us-

able. By adding code, authors could increase the visibility of execution, demonstrate best

practices, and provide variations on examples. To make the code example’s internal compu-

tations more visible, participant F7 replaced a return statement with a print statement to

make the code’s success obvious to someone who executed it, and F12 left in dozens of lines

dedicated to printing out updates to a machine learning model during training. F11 added ex-

plicit if statements to check whether method return values were null pointers and empty lists,

to make clear to readers that the API calls might return empty values. F4 produced three ver-

sions of one code example, each of which utilized different test data and showed progressively

more advanced usage of an audio API.

A2. System Implementation Details

Ass described in the “System Implementation” section of the main text, CodeScoop comprises

a collection of modules to flag errors, suggest resolutions, and resolve errors. This section

describes additional modules that were not described in the main text.

Detecting errors and relevant code

Missing previous uses of a variable

When a user adds a variable use and its definition, CodeScoop collects all past uses of the

variable that occur between the use and definition, and which are not yet in scoop. These

previous uses are presented to the user all at one time.

Missing exceptions for a function call

Using Soot as a parser, we locate all function calls in the source program. For each call, we

use the Java Reflections API1 to find all exceptions the method can throw. An ANTLR parser

is run on the source program to find try-catch blocks and the exceptions they catch. When a

1https://docs.oracle.com/javase/tutorial/reflect/index.html

Page 4: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

line is added to the scoop with an exception-prone function, CodeScoop checks to see whether

(a) the scoop already throws the exception or a superclass of the exception or (b) the scoop

includes a try-catch block surrounding the function call that catches that exception. If neither

of these is true, CodeScoop reports that this exception must be caught or thrown.

Suggesting fixes and code additions

Throwing exceptions for exception-prone function calls

CodeScoop searches for an import statement that defines either the exception or one of its su-

perclasses. When a relevant type is found in the import statements, CodeScoop recommends

that this exception type is thrown from the main method. If an user accepts the suggestion,

the exception type is added to the throws clause of the main method.

Suggesting stubs for undefined object variables

CodeScoop extends the idea of replacing variable names with literal values to complex data

types, by generating “stubs” with identical runtime behavior to object variables. While this

feature is currently experimental, we propose it as one possible solution. To create stubs,

an additional debugger virtual machine is launched. CodeScoop tracks every property access

and every method call on every object defined in the source program, and logs all values these

properties and methods returned. These variables and method calls can return other objects,

which get tracked as well. CodeScoop generates stubs as classes that replicate the recorded

behavior given the same order of property accesses and method calls.

Rendering Example Text from an Internal Scoop

Generating the example text from a list of selections and user choices involves instantiating a

hierarchical string template. First, the text for all text selections, stored as numerical “ranges”

that indicate the character offset of the text selections in the source program, is retrieved

from the source program. These texts are joined in a temporary buffer, in the order that they

appeared in the original code. The position of each of these selections is “marked” in the

Page 5: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

example text editor. These markers will keep track of the selection’s offset in the example text

as it is built up, and will be used later for highlighting errors and inserting literal values.

Next, the snippets are wrapped in the main function of a class called ExtractedExample.

Object stubs are generated from inner classes and added above the main function. All inner

classes and local methods that the user included are added as static members of the class

below the main function. Finally, the generated example code is automatically indented.

Once all selections and stubs have been added, literal values are inserted into the example,

in the positions tracked by the “markers” of individual text selections. If the program is in the

flag state, it then highlights errors for a user to choose from. If in the suggest state, it highlights

the chosen error and displays a menu for previewing and accepting resolutions.

A3. CodeScoop Usability Study Tasks

For each example extraction task, participants were shown one of three fabricated “Stack

Overflow questions”, describing something a programmer might want to do with Java. Partic-

ipants were asked to make an example that answered the question they were shown. Each

question was shown as a mocked-up post on the Stack Overflow site, which included a title,

body, author, and vote count. For example, this post was shown as a prompt for task 1:

The titles and texts of the posts for the three tasks were as follows:

Task 1. How do you get a row from a database in Java? I’m currently trying to use the

Page 6: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Cursor class from the org.acme.database package. I can’t find any examples

about it. What’s the recommended way of get a row from a table in the database?

Task 2. Using JSoup to extract text from elements on a web page? I’m writing a

program to fetch a web page and want to extract the text from all of a certain type

of element. I know I can use JSoup for this, but I’m stuck on how exactly to do it.

Task 3. Sending an email over SMTP with Java. Does anyone have a suggestion

for how to send an email message automatically in Java? I’m trying to use the

‘javax.mail’ package, but haven’t figured out how to get it to work yet.

Because participants had a limited time to review code before extracting an example, we

offered short written hints of what they should look for in the source programs, and what should

be included in the examples they extracted. We introduced these checklists to participants as

“bare minimum checklists”, to emphasize participants that participants should include more

than this minimum if they felt it was appropriate. The texts of the checklists for each task were:

Task 1. You will probably need to make a Database, get a cursor for the database, fetch a

line, and get data from that line. Beyond that, use your best judgment.

Task 2. You will probably need to create a JSoup object for a URL and extract links from

a JSoup object. Beyond that, use your best judgment.

Task 3. You will probably need to create a session, make a message with some text and

a recipient, connect to a transport and send a message. Beyond that, use your

best judgment.

Participants were also provided with a set of guidelines of verbal cues they should use to

indicate when they were finished scanning the source program or extracting the example. The

written guidelines, reviewed verbally with each participant, were:

Instructions. For each question you are given,

• Take 5 minutes to find and understand relevant parts of the code. When

you’re done, say “I understand the relevant parts of the code.”

Page 7: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

• Extract code that answers the question.

• As long as you think it answers the question, you can extract whatever code

you feel is appropriate.

• Before formatting or changing the code, say “Finished Extracting”.

Participants were allowed to ask the study facilitator any questions they wished about how

the source programs worked at any time, with the intent of helping participants focus on ex-

tracting the right example rather than comprehending unfamiliar code.

A4. CodeScoop Tutorial Task

Before performing any of the main experimental tasks, participants were asked to complete a

tutorial that taught them how to use the major features of CodeScoop (nicknamed “Rockies” in

the study). We include the guide for the tutorial as a document called “How to Use Rockies”,

starting on the next page.

A5. Source Programs and Scooped Examples

In the usability study, participants were asked to create examples from two source programs.

We list the source programs and the 19 examples that participants created with CodeScoop

beneath the CodeScoop tutorial guide.

Page 8: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

How to Use Rockies Rockies is a tool for making example code from existing code.

Suppose you’ve been working on the code in SillyDictionaryBuilder.java (see below). The program generates random words and saves them to a file.

Your friend asked you how to create a random string of alphabetic characters. You want to share your example with your friend. But you also want to make it shorter and more readable, while keeping it compilable and runnable.

In this tutorial, you will use Rockies to extract an example that is more concise than the original code, but also runnable. If you learn two things about Rockies in this tutorial, it is that:

1. Rockies helps you include necessary code to preserve program behavior 2. Rockies proposes values from the original program’s execution to give you

opportunities to eliminate irrelevant or complex code

Start an Example by Pointing to Main Example Results First, select the code that should have the final results of the example program. In this case, it’s the text on Line 28, which wraps up the creation of a random word. Select this line:

(Continued on next page)

A4. CodeScoop Tutorial Guide

Page 9: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

To extract a code example, right click on the selected text. Scroll down and click on the “Make example” command:

Immediately, you’ll notice that Rockies creates a new pane for the extracted example:

A4. CodeScoop Tutorial Guide

Page 10: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Rockies Makes Some Fixes For You Rockies takes about 10-20 seconds to analyze the code after you’ve made the initial selection. Once it’s done analyzing the code, it (1) starts fixing the code and (2) starts suggesting ways for you to complete the example.

For example, you’ll see that one line was already added that defines the variable “buffer”. There’s also a suggestion of more updates to make to the code. Ignore this suggestion for now: we’ll get to it in a few steps.

A4. CodeScoop Tutorial Guide

Page 11: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Compile and Run the Code Before interacting with Rockies, let’s check on the state of the example code in the right pane: You can compile and run the code at any time by clicking on the Run button. Do it right now to see whether the compiles and runs:

Once the code has finished running, you’ll see any output in the pane at the bottom. The example doesn’t yet print any results, so all you see is a “Finished” message:

A4. CodeScoop Tutorial Guide

Page 12: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Adding Other Uses of a Variable Rockies prompts you to consider other lines that use variables you’ve included, to make sure you don’t leave out any important logic. This is what it’s doing with the ‘Do you want any of those uses of “buffer”?’ prompt.

Rockies highlights the lines on the left side that use the variable. Click on the line number in the left text editor to include that line, by clicking on the number 26:

For future prompts about variable uses, if you don’t want to include any of the suggested lines, just click “No” to the right of the prompt.

Tip: You can include multiple lines at once by clicking and dragging over the line numbers.

A4. CodeScoop Tutorial Guide

Page 13: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Adding Missing Code Rockies highlights undefined variables. You’ll need to add in extra code or data to resolve undefined variables before the example can compile and run. Currently, character is undefined. See it highlighted?

To define a variable, move the mouse underneath that variable. See the “Define” button appear:

A4. CodeScoop Tutorial Guide

Page 14: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

This pulls up a small menu that will let you define the variable by either adding code or setting a concrete value for the variable. Hover over “Line 25” in the “Add code” menu. This highlights the line in SillyDictionaryBuilder.java that you should include to define character.

Click on “Line 25”, and the line will be automatically added.

Repeat this to define the variable asciiCode, by adding Line 24.

A4. CodeScoop Tutorial Guide

Page 15: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Reviewing Structure Suggestions When you choose code both inside and outside of a control structure (if statements, try-catch blocks, for loops, etc.), Rockies asks if you want to include that control structure:

Respond to these prompts as follows:

● Accept the first suggested “for” loop: this one lets you build a word from characters

● Reject the second suggested “for” loop: it creates multiple words, which isn’t necessary to the example. We only want to build up one word

A4. CodeScoop Tutorial Guide

Page 16: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Replacing Variables with Values Sometimes, you don’t want to add code to resolve an undefined variable: It might look better to just add a literal value in the place of that variable.

This time, when you define, MIN_ASCII_CHARACTER, hover over the “Set value” menu:

Hover the mouse over the value “65”. You can see the value directly substituted into the example program for you to preview it:

A4. CodeScoop Tutorial Guide

Page 17: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Click on “65” to replace the variable with the value.

Repeat this process to:

● Replace ALPHABET_LENGTH with 26

● Replace WORD_LENGTH with 8

Print Out a Variable While this example code can compile and run, it doesn’t show anything. Rockies can help you add a print statement for variables to display output at the end of an example.

In the example pane, select the text for the variable word in the line: “String word = new String(buffer);”, and then click the “Print” button.

Why is there a button for printing? A print button lets you display output, without the need for typing or pasting in hand-written text. Rockies only allows expansion of the code through line selections, prompts, and these print statements, to make sure it can still recommend a path to completing the example no matter what state it is in.

(Continued on the next page)

A4. CodeScoop Tutorial Guide

Page 18: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

You’ll see that a print statement was added for the variable:

Run the example again to verify that the output is now shown.

A4. CodeScoop Tutorial Guide

Page 19: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Throwing Relevant Exceptions While the example is now complete, there’s one more feature you should know about. Rockies can help you “throw” relevant exceptions when for lines of code you included. Let’s try this out. Click on Lines 33 and 39 in the left pane:

Line 33 can throw an IOException. Rockies tells you this by pointing to the line as it appears in the example editor:

A4. CodeScoop Tutorial Guide

Page 20: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Click on “Accept” to throw the exception. If you don’t do this, and the IOException is not caught by a try-catch block, the code may not compile.

Undo the Last Action With Rockies, the only way to remove lines of code is to rewind the example-making process. To do this, click the Undo button. Click it once just to try it.

Parting Words If you learn two things about Rockies in this tutorial, it is that:

1. Rockies helps you include necessary code to preserve program behavior 2. Rockies proposes values from the original program’s execution to give you

opportunities to eliminate irrelevant or complex code

To have maximum success with Rockies, keep in mind that Rockies helps the most when your initial code are the lines including the final results of the example. This helps Rockies identify other lines of code you may want to include.

A4. CodeScoop Tutorial Guide

Page 21: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Task 1, Participant 18 import org.acme.database.Database; automatic import

import org.acme.database.Cursor; automatic import

import org.acme.database.Book; automatic import

import org.acme.database.ConnectionException; automatic import

public class ExtractedExample { boilerplate

public static void main(String[] args) throws ConnectionException { boilerplate + prompted throws

int COLUMN_INDEX_ID = 0; prompted selection (def)

int COLUMN_INDEX_TITLE = 1; prompted selection (def)

int COLUMN_INDEX_YEAR = 2; prompted selection (def)

int COLUMN_INDEX_NUM_PAGES = 3; prompted selection (def)

Database database = new Database("lou", "PA$$W0RD", "https://acme-books.com/db"); initial selection

Cursor cursor = database.cursor(); selection

cursor.execute("SELECT id, title, year, num_pages FROM table WHERE title LIKE '%romance%'"); prompted selection (use)

cursor.fetchone(); selection

int id = cursor.getInt(COLUMN_INDEX_ID); prompted selection (use)

String title = cursor.getString(COLUMN_INDEX_TITLE); prompted selection (use)

int year = cursor.getInt(COLUMN_INDEX_YEAR); prompted selection (use)

int num_pages = cursor.getInt(COLUMN_INDEX_NUM_PAGES); prompted selection (def)

Book book = new Book(id, title, year, num_pages); selection

System.out.println(title); add print statement

} boilerplate

} boilerplate

Annotated Examples from CodeScoop Study

The color of a line indicates the source of the line:

For each example that a participant produced with CodeScoop, we display the example with annotations of how it was constructed.

The marks in the left gutter indicate author interactions. There are three types of interactions:

Task 2 Example (Participant 4)import java.util.List; automatic import

import java.util.ArrayList; automatic import

import java.net.URLEncoder; automatic import

import java.net.URL; automatic import

import org.jsoup.nodes.Document; automatic import

import org.jsoup.Jsoup; automatic import

import org.jsoup.select.Elements; automatic import

import java.util.Iterator; automatic import

import org.jsoup.nodes.Element; automatic import

import java.io.IOException; automatic import

public class ExtractedExample { boilerplate

public static void main(String[] args) throws IOException, java.lang.NumberFormatException { boilerplate

int priceInt; automatic declaration

String arg0 = "electric bike"; prompted selection (def)

List prices = new ArrayList(); automatic definition

String query = URLEncoder.encode(arg0, "utf-8"); prompted selection (def)

URL url = new URL("https://sfbay.craigslist.org/search/bia?query=" + query); automatic definition

Document doc = Jsoup.parse(url, 3*1000); automatic definition

Elements rows = doc.select("li.result-row"); automatic definition

for (Iterator rowsIterator = rows.iterator(); rowsIterator.hasNext();) { prompted selection (control)

Element row = (Element) rowsIterator.next(); automatic definition

String price = row.select("span.result-meta span.result-price").text(); selection

try { prompted selection (control)

priceInt = Integer.parseInt(price.replaceFirst("\\$", "")); prompted selection (def)

} catch (NumberFormatException exception) {

priceInt = -1; selection

}

prices.add(new Integer(priceInt)); initial selection

}

System.out.println(prices); add print statement

} boilerplate

} boilerplate

An author's first code selections when starting CodeScoop.

Task 2 Example (Participant 4)import java.util.List; automatic import

import java.util.ArrayList; automatic import

import java.net.URLEncoder; automatic import

import java.net.URL; automatic import

import org.jsoup.nodes.Document; automatic import

import org.jsoup.Jsoup; automatic import

import org.jsoup.select.Elements; automatic import

import java.util.Iterator; automatic import

import org.jsoup.nodes.Element; automatic import

import java.io.IOException; automatic import

public class ExtractedExample { boilerplate

public static void main(String[] args) throws IOException, java.lang.NumberFormatException { boilerplate

int priceInt; automatic declaration

String arg0 = "electric bike"; prompted selection (def)

List prices = new ArrayList(); automatic definition

String query = URLEncoder.encode(arg0, "utf-8"); prompted selection (def)

URL url = new URL("https://sfbay.craigslist.org/search/bia?query=" + query); automatic definition

Document doc = Jsoup.parse(url, 3*1000); automatic definition

Elements rows = doc.select("li.result-row"); automatic definition

for (Iterator rowsIterator = rows.iterator(); rowsIterator.hasNext();) { prompted selection (control)

Element row = (Element) rowsIterator.next(); automatic definition

String price = row.select("span.result-meta span.result-price").text(); selection

try { prompted selection (control)

priceInt = Integer.parseInt(price.replaceFirst("\\$", "")); prompted selection (def)

} catch (NumberFormatException exception) {

priceInt = -1; selection

}

prices.add(new Integer(priceInt)); initial selection

}

System.out.println(prices); add print statement

} boilerplate

} boilerplate

Additional lines added to the example, without any prompting from CodeScoop.

Task 2 Example (Participant 4)import java.util.List; automatic import

import java.util.ArrayList; automatic import

import java.net.URLEncoder; automatic import

import java.net.URL; automatic import

import org.jsoup.nodes.Document; automatic import

import org.jsoup.Jsoup; automatic import

import org.jsoup.select.Elements; automatic import

import java.util.Iterator; automatic import

import org.jsoup.nodes.Element; automatic import

import java.io.IOException; automatic import

public class ExtractedExample { boilerplate

public static void main(String[] args) throws IOException, java.lang.NumberFormatException { boilerplate

int priceInt; automatic declaration

String arg0 = "electric bike"; prompted selection (def)

List prices = new ArrayList(); automatic definition

String query = URLEncoder.encode(arg0, "utf-8"); prompted selection (def)

URL url = new URL("https://sfbay.craigslist.org/search/bia?query=" + query); automatic definition

Document doc = Jsoup.parse(url, 3*1000); automatic definition

Elements rows = doc.select("li.result-row"); automatic definition

for (Iterator rowsIterator = rows.iterator(); rowsIterator.hasNext();) { prompted selection (control)

Element row = (Element) rowsIterator.next(); automatic definition

String price = row.select("span.result-meta span.result-price").text(); selection

try { prompted selection (control)

priceInt = Integer.parseInt(price.replaceFirst("\\$", "")); prompted selection (def)

} catch (NumberFormatException exception) {

priceInt = -1; selection

}

prices.add(new Integer(priceInt)); initial selection

}

System.out.println(prices); add print statement

} boilerplate

} boilerplate

Lines an author included in response to a suggestion from CodeScoop.

Boilerplate: this code shows up in every example so the example can compile.

Automatic: CodeScoop inferred that this line was required, and added it automatically.

Manual: The participant included these lines without any prompting.

Prompted: The participant included these lines when reviewing a prompt from CodeScoop .

The labels in the right gutter are fine-grained descriptions each line's source. There are three variants of prompted selections:

• def: adding code to define a variable

• use: adding a previous use of a variable

• control: adding a control structure that surrounds a statement

• throws: throwing an exception from the main method signature

A line is generated with the "add print statement" label when an author clicks the "Print" with a variable name selected.

Where an author replaced an undefined variable with a literal, the literal is highlighted in green, underlined, and bolded.

Page 22: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Task 1 Source Program1 import org.acme.database.Book;

2 import org.acme.database.Booklist;

3 import org.acme.database.Cursor;

4 import org.acme.database.Database;

5 import org.acme.database.ConnectionException;

6

7 import java.util.ArrayList;

8 import java.util.List;

9

10

11 public class BookListing {

12

13 private boolean DEBUG = true;

14

15 public Booklist getBookListing(String genre, int maxBooks) {

16

17 String QUERY = "SELECT id, title, year, num_pages FROM table WHERE title LIKE '%" + genre + "%'";

18 int COLUMN_INDEX_ID = 0;

19 int COLUMN_INDEX_TITLE = 1;

20 int COLUMN_INDEX_YEAR = 2;

21 int COLUMN_INDEX_NUM_PAGES = 3;

22

23 Database database = new Database("lou", "PA$$W0RD", "https://acme-books.com/db");

24 Cursor cursor = database.cursor();

25 Booklist booklist = new Booklist();

26 List titles = new ArrayList();

27

28 try {

29

30 cursor.execute(QUERY);

31 boolean finished = false;

32

33 if (cursor.rowCount() > 0) {

34

35 int rowNumber = 0;

36 while (!finished) {

37

38 int rowCount = cursor.rowCount();

39

40 for (int i = 0; i < Math.min(rowCount, maxBooks); ++i) {

41

42 cursor.fetchone();

43 int id = cursor.getInt(COLUMN_INDEX_ID);

44 String title = cursor.getString(COLUMN_INDEX_TITLE);

45 int year = cursor.getInt(COLUMN_INDEX_YEAR);

46 int num_pages = cursor.getInt(COLUMN_INDEX_NUM_PAGES);

47 Book book = new Book(id, title, year, num_pages);

48

49 if (title != null) {

50 titles.add(title);

51 }

52 if (id != -1) {

53 boolean bestseller = isBestseller(book.getId());

54 if (bestseller) {

55 booklist.hasBestseller = bestseller;

56 }

57 }

58

59 if (DEBUG) {

60 System.out.println("Fetched book: " + title + " (" + genre + ")");

61 }

A5. Source Programs and Examples

Page 23: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

62

63 rowNumber++;

64

65 if (cursor.end() || rowNumber >= maxBooks) {

66 finished = true;

67 } else if (i == rowCount - 1){

68 cursor.next(id);

69 }

70

71 }

72 }

73 } else {

74 System.out.println("No results found in the database");

75 }

76

77 } catch (ConnectionException exception) {

78 exception.printStackTrace();

79 }

80

81 booklist.titles = titles;

82 return booklist;

83

84 }

85

86 public boolean isBestseller(int id) {

87 return id == 2;

88 }

89

90 public static void main(String[] args) {

91 new BookListing().getBookListing("romance", 3);

92 }

93

94 }

A5. Source Programs and Examples

Page 24: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Task 1, Participant 5 import org.acme.database.Database; automatic import

import org.acme.database.Cursor; automatic import

import org.acme.database.Book; automatic import

import org.acme.database.ConnectionException; automatic import

public class ExtractedExample { boilerplate

public static void main(String[] args) { boilerplate

String QUERY = "SELECT id, title, year, num_pages FROM table WHERE title LIKE '%" + "romance" + "%'"; prompted selection (def)

Database database = new Database("lou", "PA$$W0RD", "https://acme-books.com/db"); automatic definition

Cursor cursor = database.cursor(); automatic definition

try { prompted selection (control)

cursor.execute(QUERY); prompted selection (use)

if (cursor.rowCount() > 0) { prompted selection (use)

int rowCount = cursor.rowCount(); prompted selection (use)

cursor.fetchone(); prompted selection (use)

int id = cursor.getInt(0); prompted selection (use)

String title = cursor.getString(1); prompted selection (def)

int year = cursor.getInt(2); prompted selection (def)

int num_pages = cursor.getInt(3); prompted selection (def)

Book book = new Book(id, title, year, num_pages); prompted selection (use)

System.out.println("Fetched book: " + title + " (" + "romance" + ")"); initial selection

}

} catch (ConnectionException exception) {

}

} boilerplate

} boilerplate

A5. Source Programs and Examples

Page 25: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Task 1, Participant 6 import org.acme.database.Database; automatic import

import org.acme.database.Cursor; automatic import

import org.acme.database.ConnectionException; automatic import

public class ExtractedExample { boilerplate

public static void main(String[] args) throws ConnectionException { boilerplate + prompted throws

Database database = new Database("lou", "PA$$W0RD", "https://acme-books.com/db"); initial selection

Cursor cursor = database.cursor(); selection

try { prompted selection (control)

cursor.execute("SELECT id, title, year, num_pages FROM table WHERE title LIKE '%romance%'"); selection

if (cursor.rowCount() > 0) { prompted selection (control)

int rowCount = cursor.rowCount(); prompted selection (use)

for (int i = 0; i < Math.min(rowCount, 3); ++i) { selection

cursor.fetchone(); selection

int id = cursor.getInt(0); selection

String title = cursor.getString(1); selection

}

}

} catch (ConnectionException exception) {

}

} boilerplate

} boilerplate

A5. Source Programs and Examples

Page 26: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Task 1, Participant 12 import org.acme.database.Database; automatic import

import org.acme.database.Cursor; automatic import

import org.acme.database.Book; automatic import

import org.acme.database.ConnectionException; automatic import

public class ExtractedExample { boilerplate

public static void main(String[] args) throws ConnectionException { boilerplate + prompted throws

String QUERY = "SELECT id, title, year, num_pages FROM table WHERE title LIKE '%" + "romance" + "%'"; selection

Database database = new Database("lou", "PA$$W0RD", "https://acme-books.com/db"); selection

Cursor cursor = database.cursor(); selection

cursor.execute(QUERY); selection

cursor.fetchone(); initial selection

String title = cursor.getString(1); selection

int year = cursor.getInt(2); selection

int num_pages = cursor.getInt(3); selection

Book book = new Book(1, title, year, num_pages); selection

if (true == true) { prompted selection (control)

System.out.println("Fetched book: " + title + " (" + "romance" + ")"); selection

}

} boilerplate

} boilerplate

A5. Source Programs and Examples

Page 27: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Task 1, Participant 15Buggy: While the participant finished extracting the example, it does not compile and run: the participant's first selection was a

return statement that conflicts with the main function's signature, and which could only be removed if they started over.

import org.acme.database.Booklist; automatic import

import org.acme.database.Database; automatic import

import org.acme.database.Cursor; automatic import

import org.acme.database.ConnectionException; automatic import

import org.acme.database.Book; automatic import

public class ExtractedExample { boilerplate

public static void main(String[] args) throws ConnectionException { boilerplate + prompted throws

String QUERY = "SELECT id, title, year, num_pages FROM table WHERE title LIKE '%" + "romance" + "%'"; selection

Database database = new Database("lou", "PA$$W0RD", "https://acme-books.com/db"); selection

Cursor cursor = database.cursor(); selection

Booklist booklist = new Booklist(); automatic definition

cursor.execute(QUERY); prompted selection (use)

boolean finished = false; prompted selection (def)

if (cursor.rowCount() > 0) { prompted selection (use)

while (finished == false) { prompted selection (control)

int rowCount = cursor.rowCount(); prompted selection (use)

cursor.fetchone(); selection

int id = cursor.getInt(0); selection

String title = cursor.getString(1); selection

int year = cursor.getInt(2); prompted selection (use)

int num_pages = cursor.getInt(3); selection

Book book = new Book(id, title, year, num_pages); selection

}

}

return book; initial selection

} boilerplate

} boilerplate

A5. Source Programs and Examples

Page 28: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Task 1, Participant 17 import org.acme.database.Database; automatic import

import org.acme.database.Cursor; automatic import

import org.acme.database.ConnectionException; automatic import

public class ExtractedExample { boilerplate

public static void main(String[] args) throws ConnectionException { boilerplate + prompted throws

String QUERY = "SELECT id, title, year, num_pages FROM table WHERE title LIKE '%" + "romance" + "%'"; prompted selection (def)

Database database = new Database("lou", "PA$$W0RD", "https://acme-books.com/db"); automatic definition

Cursor cursor = database.cursor(); automatic definition

try { prompted selection (control)

cursor.execute(QUERY); prompted selection (use)

if (cursor.rowCount() > 0) { prompted selection (control)

int rowCount = cursor.rowCount(); prompted selection (use)

cursor.fetchone(); initial selection

}

} catch (ConnectionException exception) {

}

} boilerplate

} boilerplate

A5. Source Programs and Examples

Page 29: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Task 1, Participant 18 import org.acme.database.Database; automatic import

import org.acme.database.Cursor; automatic import

import org.acme.database.Book; automatic import

import org.acme.database.ConnectionException; automatic import

public class ExtractedExample { boilerplate

public static void main(String[] args) throws ConnectionException { boilerplate + prompted throws

int COLUMN_INDEX_ID = 0; prompted selection (def)

int COLUMN_INDEX_TITLE = 1; prompted selection (def)

int COLUMN_INDEX_YEAR = 2; prompted selection (def)

int COLUMN_INDEX_NUM_PAGES = 3; prompted selection (def)

Database database = new Database("lou", "PA$$W0RD", "https://acme-books.com/db"); initial selection

Cursor cursor = database.cursor(); selection

cursor.execute("SELECT id, title, year, num_pages FROM table WHERE title LIKE '%romance%'"); prompted selection (use)

cursor.fetchone(); selection

int id = cursor.getInt(COLUMN_INDEX_ID); prompted selection (use)

String title = cursor.getString(COLUMN_INDEX_TITLE); prompted selection (use)

int year = cursor.getInt(COLUMN_INDEX_YEAR); prompted selection (use)

int num_pages = cursor.getInt(COLUMN_INDEX_NUM_PAGES); prompted selection (def)

Book book = new Book(id, title, year, num_pages); selection

System.out.println(title); add print statement

} boilerplate

} boilerplate

A5. Source Programs and Examples

Page 30: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Task 2 Source Program1 import com.sun.mail.smtp.SMTPTransport;

2

3 import org.apache.commons.lang.Validate;

4 import org.apache.commons.lang.StringUtils;

5 import org.jsoup.nodes.Document;

6 import org.jsoup.nodes.Element;

7 import org.jsoup.Jsoup;

8 import org.jsoup.select.Elements;

9

10 import java.io.BufferedReader;

11 import java.io.FileReader;

12 import java.io.IOException;

13 import java.net.URL;

14 import java.net.URLEncoder;

15 import java.security.Security;

16 import java.util.ArrayList;

17 import java.util.Date;

18 import java.util.Iterator;

19 import java.util.List;

20 import java.util.Properties;

21

22 import javax.mail.Message;

23 import javax.mail.MessagingException;

24 import javax.mail.Session;

25 import javax.mail.internet.AddressException;

26 import javax.mail.internet.InternetAddress;

27 import javax.mail.internet.MimeMessage;

28

29 /**

30 * Example program to list links from a URL.

31 */

32 public class CraigslistMonitor {

33

34 public static void main(String[] args) throws IOException, MessagingException, AddressException {

35

36 String arg0 = "electric bike";

37 String arg1 = "[email protected]";

38

39 List titles = new ArrayList();

40 List prices = new ArrayList();

41 List links = new ArrayList();

42

43 String query = URLEncoder.encode(arg0, "utf-8");

44 String destination = arg1;

45 URL url = new URL("https://sfbay.craigslist.org/search/bia?query=" + query);

46 System.out.println("Fetching " + url.toExternalForm() + "...");

47

48 Document doc = Jsoup.parse(url, 3*1000);

49 Elements rows = doc.select("li.result-row");

50 System.out.println(url);

51

52 for (Iterator rowsIterator = rows.iterator(); rowsIterator.hasNext();) {

53

54 Element row = (Element) rowsIterator.next();

55

56 String price = row.select("span.result-meta span.result-price").text();

57 int priceInt;

58 try {

59 priceInt = Integer.parseInt(price.replaceFirst("\\$", ""));

60 } catch (NumberFormatException exception) {

61 priceInt = -1;

A5. Source Programs and Examples

Page 31: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

62 }

63 prices.add(new Integer(priceInt));

64

65 Element image = row.select("a.result-image.gallery").get(0);

66 String link = image.attr("abs:href");

67 links.add(link);

68

69 String title = row.select("a.result-title.hdrlnk").text();

70 titles.add(title);

71

72 if (titles.size() >= 5) break;

73 }

74

75 int maxTitleLength = 0;

76 int maxPriceLength = 0;

77 int maxLinkLength = 0;

78 for (int i = 0; i < titles.size(); i++) {

79 String titleLine = (String) titles.get(i);

80 if (titleLine.length() > maxTitleLength) {

81 maxTitleLength = titleLine.length();

82 }

83 int priceLine = ((Integer) prices.get(i)).intValue();

84 if (Integer.toString(priceLine).length() > maxPriceLength) {

85 maxPriceLength = Integer.toString(priceLine).length();

86 }

87 String linkLine = (String) links.get(i);

88 if (linkLine.length() > maxLinkLength) {

89 maxLinkLength = linkLine.length();

90 }

91 }

92

93 String messageHtml = "<code>";

94 for (int j = 0; j < titles.size(); j++) {

95 String titleText = (String) titles.get(j);

96 String priceText = ((Integer) prices.get(j)).toString();

97 String linkText = (String) links.get(j);

98 String titlePadded = titleText + StringUtils.repeat(" ", maxTitleLength - titleText.length());

99 String pricePadded = priceText + StringUtils.repeat(" ", maxPriceLength - priceText.length());

100 String linkPadded = linkText + StringUtils.repeat(" ", maxLinkLength - linkText.length());

101 messageHtml += ("( $" + pricePadded + " ) " + titlePadded + "<br/>" + linkPadded + "<br/><br/>");

102 }

103 messageHtml += "</code>";

104

105 BufferedReader confReader = new BufferedReader(new FileReader("/etc/smtp.conf"));

106 String username = confReader.readLine();

107 String password = confReader.readLine();

108 System.out.println("Logging in with " + username + ", " + password);

109

110 Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

111 String sslFactoryClass = "javax.net.ssl.SSLSocketFactory";

112

113 Properties properties = System.getProperties();

114 properties.setProperty("mail.smtps.host", "smtp.gmail.com");

115 properties.setProperty("mail.smtp.socketFactory.class", sslFactoryClass);

116 properties.setProperty("mail.smtp.port", "587");

117 properties.setProperty("mail.smtp.socketFactory.port", "587");

118 properties.setProperty("auth", "true");

119 properties.put("mail.smtps.quitwait", "false");

120

121 Session session = Session.getInstance(properties, null);

122

123 MimeMessage message = new MimeMessage(session);

124 message.setFrom(new InternetAddress(username));

A5. Source Programs and Examples

Page 32: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

125 message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(destination, false));

126 message.setSubject("Update of Craigslist posts");

127 message.setText(messageHtml, "utf-8", "html");

128 message.setSentDate(new Date());

129

130 SMTPTransport transport = (SMTPTransport) session.getTransport("smtps");

131 transport.connect("smtp.gmail.com", username, password);

132 transport.sendMessage(message, message.getAllRecipients());

133 transport.close();

134 }

135 }

A5. Source Programs and Examples

Page 33: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Task 2, Participant 3Exemplar: Creating an example primarily through manual selections, with a little bit of assistance from CodeScoop.

import java.net.URLEncoder; automatic import

import java.net.URL; automatic import

import org.jsoup.nodes.Document; automatic import

import org.jsoup.Jsoup; automatic import

import org.jsoup.select.Elements; automatic import

import java.util.Iterator; automatic import

import org.jsoup.nodes.Element; automatic import

import java.io.IOException; automatic import

import java.util.List; automatic import

import java.util.ArrayList; automatic import

public class ExtractedExample { boilerplate

public static void main(String[] args) throws IOException, java.lang.NumberFormatException { boilerplate + prompted throws

int priceInt; automatic declaration

String arg0 = "electric bike"; prompted selection (def)

List prices = new ArrayList(); automatic definition

String query = URLEncoder.encode(arg0, "utf-8"); prompted selection (def)

URL url = new URL("https://sfbay.craigslist.org/search/bia?query=" + query); initial selection

Document doc = Jsoup.parse(url, 3*1000); selection

Elements rows = doc.select("li.result-row"); selection

for (Iterator rowsIterator = rows.iterator(); rowsIterator.hasNext();) { selection

Element row = (Element) rowsIterator.next(); automatic definition

String price = row.select("span.result-meta span.result-price").text(); selection

priceInt = Integer.parseInt(price.replaceFirst("\\$", "")); prompted selection (def)

prices.add(new Integer(priceInt)); selection

}

System.out.println(prices); add print statement

} boilerplate

} boilerplate

A5. Source Programs and Examples

Page 34: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Task 2, Participant 4import java.util.List; automatic import

import java.util.ArrayList; automatic import

import java.net.URLEncoder; automatic import

import java.net.URL; automatic import

import org.jsoup.nodes.Document; automatic import

import org.jsoup.Jsoup; automatic import

import org.jsoup.select.Elements; automatic import

import java.util.Iterator; automatic import

import org.jsoup.nodes.Element; automatic import

import java.io.IOException; automatic import

public class ExtractedExample { boilerplate

public static void main(String[] args) throws IOException, java.lang.NumberFormatException { boilerplate + prompted throws

int priceInt; automatic declaration

String arg0 = "electric bike"; prompted selection (def)

List prices = new ArrayList(); automatic definition

String query = URLEncoder.encode(arg0, "utf-8"); prompted selection (def)

URL url = new URL("https://sfbay.craigslist.org/search/bia?query=" + query); automatic definition

Document doc = Jsoup.parse(url, 3*1000); automatic definition

Elements rows = doc.select("li.result-row"); automatic definition

for (Iterator rowsIterator = rows.iterator(); rowsIterator.hasNext();) { prompted selection (control)

Element row = (Element) rowsIterator.next(); automatic definition

String price = row.select("span.result-meta span.result-price").text(); selection

try { prompted selection (control)

priceInt = Integer.parseInt(price.replaceFirst("\\$", "")); prompted selection (def)

} catch (NumberFormatException exception) {

priceInt = -1; selection

}

prices.add(new Integer(priceInt)); initial selection

}

System.out.println(prices); add print statement

} boilerplate

} boilerplate

A5. Source Programs and Examples

Page 35: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Task 2, Participant 7Exemplar: The author makes an initial selection, and the rest of the example is completed automatically by CodeScoop.

import java.util.List; automatic import

import java.util.ArrayList; automatic import

import java.net.URL; automatic import

import org.jsoup.nodes.Document; automatic import

import org.jsoup.Jsoup; automatic import

import org.jsoup.select.Elements; automatic import

import java.util.Iterator; automatic import

import org.jsoup.nodes.Element; automatic import

import java.io.IOException; automatic import

public class ExtractedExample { boilerplate

public static void main(String[] args) throws IOException { boilerplate + prompted throws

List titles = new ArrayList(); automatic definition

URL url = new URL("https://sfbay.craigslist.org/search/bia?query=" + "electric+bike"); automatic definition

Document doc = Jsoup.parse(url, 3*1000); automatic definition

Elements rows = doc.select("li.result-row"); automatic definition

for (Iterator rowsIterator = rows.iterator(); rowsIterator.hasNext();) { automatic definition

Element row = (Element) rowsIterator.next(); automatic definition

String title = row.select("a.result-title.hdrlnk").text(); initial selection

titles.add(title);

}

} boilerplate

} boilerplate

A5. Source Programs and Examples

Page 36: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Task 2, Participant 8Buggy: After seeing their code execute, Participant 8's last step was to add the erroneous print statement on line 32 (buggy because price is out of scope). While their code did not compile with the addition of the print statement, we count this example as "finished"

because the program had been verified to run correctly just before, and the participant acknowledged the error.

import java.net.URLEncoder; automatic import

import java.net.URL; automatic import

import org.jsoup.nodes.Document; automatic import

import org.jsoup.Jsoup; automatic import

import org.jsoup.select.Elements; automatic import

import java.util.Iterator; automatic import

import org.jsoup.nodes.Element; automatic import

import java.io.IOException; automatic import

public class ExtractedExample { boilerplate

public static void main(String[] args) throws IOException { boilerplate + prompted throws

String arg0 = "electric bike"; prompted selection (def)

String query = URLEncoder.encode(arg0, "utf-8"); prompted selection (def)

URL url = new URL("https://sfbay.craigslist.org/search/bia?query=" + query); prompted selection (use)

System.out.println("Fetching " + url.toExternalForm() + "..."); automatic definition

Document doc = Jsoup.parse(url, 3*1000); automatic definition

Elements rows = doc.select("li.result-row"); automatic definition

for (Iterator rowsIterator = rows.iterator(); rowsIterator.hasNext();) { automatic definition

Element row = (Element) rowsIterator.next(); automatic definition

String price = row.select("span.result-meta span.result-price").text(); initial selection

}

System.out.println(price); add print statement

} boilerplate

} boilerplate

A5. Source Programs and Examples

Page 37: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Task 2, Participant 9 import java.net.URL; automatic import

import org.jsoup.nodes.Document; automatic import

import org.jsoup.Jsoup; automatic import

import org.jsoup.select.Elements; automatic import

import java.util.Iterator; automatic import

import org.jsoup.nodes.Element; automatic import

import java.io.IOException; automatic import

public class ExtractedExample { boilerplate

public static void main(String[] args) throws IOException { boilerplate + prompted throws

URL url = new URL("https://sfbay.craigslist.org/search/bia?query=" + "electric+bike"); automatic definition

Document doc = Jsoup.parse(url, 3*1000); automatic definition

Elements rows = doc.select("li.result-row"); automatic definition

for (Iterator rowsIterator = rows.iterator(); rowsIterator.hasNext();) { automatic definition

Element row = (Element) rowsIterator.next(); automatic definition

String price = row.select("span.result-meta span.result-price").text(); initial selection

}

} boilerplate

} boilerplate

A5. Source Programs and Examples

Page 38: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Task 2, Participant 16Incomplete: This participant did not finish the example because they ran out of time.

import java.util.List; automatic import

import java.util.ArrayList; automatic import

import java.net.URLEncoder; automatic import

import java.net.URL; automatic import

import org.jsoup.nodes.Document; automatic import

import org.jsoup.Jsoup; automatic import

import org.jsoup.select.Elements; automatic import

import java.util.Iterator; automatic import

import org.jsoup.nodes.Element; automatic import

import java.io.IOException; automatic import

public class ExtractedExample { boilerplate

public static void main(String[] args) throws IOException { boilerplate + prompted throws

int priceInt; automatic declaration

List titles = new ArrayList(); automatic definition

List prices = new ArrayList(); automatic definition

List links = new ArrayList(); automatic definition

String query = URLEncoder.encode("electric bike", "utf-8"); prompted selection (def)

URL url = new URL("https://sfbay.craigslist.org/search/bia?query=" + query); automatic definition

Document doc = Jsoup.parse(url, 3*1000); automatic definition

Elements rows = doc.select("li.result-row"); selection

for (Iterator rowsIterator = rows.iterator(); rowsIterator.hasNext();) { selection

Element row = (Element) rowsIterator.next(); selection

String price = row.select("span.result-meta span.result-price").text(); prompted selection (use)

try { selection

} catch (NumberFormatException exception) {

priceInt = -1; selection

}

prices.add(new Integer(priceInt)); selection

Element image = row.select("a.result-image.gallery").get(0); selection

String link = image.attr("abs:href"); selection

links.add(link); selection

String title = row.select("a.result-title.hdrlnk").text(); selection

titles.add(title); selection

if (titles.size() >= 5) break; selection

}

for (int j = 0; j < titles.size(); j++) { selection

String titleText = (String) titles.get(j); selection

String priceText = ((Integer) prices.get(j)).toString(); selection

String linkText = (String) links.get(j); selection

String titlePadded = titleText + StringUtils.repeat(" ", maxTitleLength - titleText.length()); selection

String pricePadded = priceText + StringUtils.repeat(" ", maxPriceLength - priceText.length()); selection

String linkPadded = linkText + StringUtils.repeat(" ", maxLinkLength - linkText.length()); selection

messageHtml += ("( $" + pricePadded + " ) " + titlePadded + "<br/>" + linkPadded + "<br/><br/>"); selection

}

messageHtml += "</code>" initial selection

} boilerplate

} boilerplate

A5. Source Programs and Examples

Page 39: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Task 2, Participant 19 import java.net.URLEncoder; automatic import

import java.net.URL; automatic import

import org.jsoup.nodes.Document; automatic import

import org.jsoup.Jsoup; automatic import

import org.jsoup.select.Elements; automatic import

import java.util.Iterator; automatic import

import org.jsoup.nodes.Element; automatic import

import java.io.IOException; automatic import

public class ExtractedExample { boilerplate

public static void main(String[] args) throws IOException { boilerplate + prompted throws

String arg0 = "electric bike"; prompted selection (def)

String query = URLEncoder.encode(arg0, "utf-8"); prompted selection (def)

URL url = new URL("https://sfbay.craigslist.org/search/bia?query=" + query); automatic definition

System.out.println("Fetching " + url.toExternalForm() + "..."); prompted selection (use)

Document doc = Jsoup.parse(url, 3*1000); automatic definition

Elements rows = doc.select("li.result-row"); automatic definition

for (Iterator rowsIterator = rows.iterator(); rowsIterator.hasNext();) { selection

Element row = (Element) rowsIterator.next(); automatic definition

String price = row.select("span.result-meta span.result-price").text(); initial selection

}

} boilerplate

} boilerplate

A5. Source Programs and Examples

Page 40: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Task 3 Source Program (same as Task 2)1 import com.sun.mail.smtp.SMTPTransport;

2

3 import org.apache.commons.lang.Validate;

4 import org.apache.commons.lang.StringUtils;

5 import org.jsoup.nodes.Document;

6 import org.jsoup.nodes.Element;

7 import org.jsoup.Jsoup;

8 import org.jsoup.select.Elements;

9

10 import java.io.BufferedReader;

11 import java.io.FileReader;

12 import java.io.IOException;

13 import java.net.URL;

14 import java.net.URLEncoder;

15 import java.security.Security;

16 import java.util.ArrayList;

17 import java.util.Date;

18 import java.util.Iterator;

19 import java.util.List;

20 import java.util.Properties;

21

22 import javax.mail.Message;

23 import javax.mail.MessagingException;

24 import javax.mail.Session;

25 import javax.mail.internet.AddressException;

26 import javax.mail.internet.InternetAddress;

27 import javax.mail.internet.MimeMessage;

28

29 /**

30 * Example program to list links from a URL.

31 */

32 public class CraigslistMonitor {

33

34 public static void main(String[] args) throws IOException, MessagingException, AddressException {

35

36 String arg0 = "electric bike";

37 String arg1 = "[email protected]";

38

39 List titles = new ArrayList();

40 List prices = new ArrayList();

41 List links = new ArrayList();

42

43 String query = URLEncoder.encode(arg0, "utf-8");

44 String destination = arg1;

45 URL url = new URL("https://sfbay.craigslist.org/search/bia?query=" + query);

46 System.out.println("Fetching " + url.toExternalForm() + "...");

47

48 Document doc = Jsoup.parse(url, 3*1000);

49 Elements rows = doc.select("li.result-row");

50 System.out.println(url);

51

52 for (Iterator rowsIterator = rows.iterator(); rowsIterator.hasNext();) {

53

54 Element row = (Element) rowsIterator.next();

55

56 String price = row.select("span.result-meta span.result-price").text();

57 int priceInt;

58 try {

59 priceInt = Integer.parseInt(price.replaceFirst("\\$", ""));

60 } catch (NumberFormatException exception) {

61 priceInt = -1;

A5. Source Programs and Examples

Page 41: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

62 }

63 prices.add(new Integer(priceInt));

64

65 Element image = row.select("a.result-image.gallery").get(0);

66 String link = image.attr("abs:href");

67 links.add(link);

68

69 String title = row.select("a.result-title.hdrlnk").text();

70 titles.add(title);

71

72 if (titles.size() >= 5) break;

73 }

74

75 int maxTitleLength = 0;

76 int maxPriceLength = 0;

77 int maxLinkLength = 0;

78 for (int i = 0; i < titles.size(); i++) {

79 String titleLine = (String) titles.get(i);

80 if (titleLine.length() > maxTitleLength) {

81 maxTitleLength = titleLine.length();

82 }

83 int priceLine = ((Integer) prices.get(i)).intValue();

84 if (Integer.toString(priceLine).length() > maxPriceLength) {

85 maxPriceLength = Integer.toString(priceLine).length();

86 }

87 String linkLine = (String) links.get(i);

88 if (linkLine.length() > maxLinkLength) {

89 maxLinkLength = linkLine.length();

90 }

91 }

92

93 String messageHtml = "<code>";

94 for (int j = 0; j < titles.size(); j++) {

95 String titleText = (String) titles.get(j);

96 String priceText = ((Integer) prices.get(j)).toString();

97 String linkText = (String) links.get(j);

98 String titlePadded = titleText + StringUtils.repeat(" ", maxTitleLength - titleText.length());

99 String pricePadded = priceText + StringUtils.repeat(" ", maxPriceLength - priceText.length());

100 String linkPadded = linkText + StringUtils.repeat(" ", maxLinkLength - linkText.length());

101 messageHtml += ("( $" + pricePadded + " ) " + titlePadded + "<br/>" + linkPadded + "<br/><br/>");

102 }

103 messageHtml += "</code>";

104

105 BufferedReader confReader = new BufferedReader(new FileReader("/etc/smtp.conf"));

106 String username = confReader.readLine();

107 String password = confReader.readLine();

108 System.out.println("Logging in with " + username + ", " + password);

109

110 Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

111 String sslFactoryClass = "javax.net.ssl.SSLSocketFactory";

112

113 Properties properties = System.getProperties();

114 properties.setProperty("mail.smtps.host", "smtp.gmail.com");

115 properties.setProperty("mail.smtp.socketFactory.class", sslFactoryClass);

116 properties.setProperty("mail.smtp.port", "587");

117 properties.setProperty("mail.smtp.socketFactory.port", "587");

118 properties.setProperty("auth", "true");

119 properties.put("mail.smtps.quitwait", "false");

120

121 Session session = Session.getInstance(properties, null);

122

123 MimeMessage message = new MimeMessage(session);

124 message.setFrom(new InternetAddress(username));

A5. Source Programs and Examples

Page 42: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

125 message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(destination, false));

126 message.setSubject("Update of Craigslist posts");

127 message.setText(messageHtml, "utf-8", "html");

128 message.setSentDate(new Date());

129

130 SMTPTransport transport = (SMTPTransport) session.getTransport("smtps");

131 transport.connect("smtp.gmail.com", username, password);

132 transport.sendMessage(message, message.getAllRecipients());

133 transport.close();

134 }

135 }

A5. Source Programs and Examples

Page 43: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Task 3, Participant 1Exemplar: An author on "auto-pilot": they accepted all code suggestions that CodeScoop made.

The resulting example is essentially the same as a conservative program slice.

Incomplete: Participant 1 did not finish the example, because they encountered a bug with the tool. AfterParticipant 1, the Task 3 source program was updated so other participants would not encounter the

same bug. Lines 15 and 30 in this example come from a previous version of the task 3 program.

public class ExtractedExample { boilerplate

public static void main(String[] args) throws MessagingException, IOException, java.lang.NumberFormatException {boilerplate + prompted throws

if (args.length < 2) { prompted selection (control)

args = new String[]{"electric bike", "[email protected]"}; automatic definition

}

List titles = new ArrayList(); automatic definition

List prices = new ArrayList(); automatic definition

List links = new ArrayList(); automatic definition

String query = URLEncoder.encode(args[0], "utf-8"); prompted selection (def)

String destination = args[1]; prompted selection (def)

URL url = new URL("https://sfbay.craigslist.org/search/bia?query=" + query); automatic definition

System.out.println("Fetching " + url.toExternalForm() + "..."); prompted selection (use)

Document doc = Jsoup.parse(url, 3*1000); automatic definition

Elements rows = doc.select("li.result-row"); automatic definition

for (Iterator rowsIterator = rows.iterator(); rowsIterator.hasNext();) { automatic definition

Element row = (Element) rowsIterator.next(); automatic definition

String price = row.select("span.result-meta span.result-price").text(); prompted selection (use)

if (!price.equals("")) { prompted selection (use)

priceInt = Integer.parseInt(price.replaceFirst("\\$", "")); prompted selection (def)

}

prices.add(new Integer(priceInt)); prompted selection (use)

Element image = row.select("a.result-image.gallery").get(0); prompted selection (use)

String link = image.attr("abs:href"); prompted selection (use)

links.add(link); prompted selection (use)

String title = row.select("a.result-title.hdrlnk").text(); prompted selection (use)

titles.add(title); prompted selection (use)

if (titles.size() >= 5) break; prompted selection (use)

}

int maxTitleLength = 0; prompted selection (def)

int maxPriceLength = 0; prompted selection (def)

int maxLinkLength = 0; prompted selection (def)

for (int i = 0; i < titles.size(); i++) { prompted selection (use)

String titleLine = (String) titles.get(i); prompted selection (use)

if (titleLine.length() > maxTitleLength) { prompted selection (use)

maxTitleLength = titleLine.length(); prompted selection (def)

}

int priceLine = ((Integer) prices.get(i)).intValue(); prompted selection (use)

if (Integer.toString(priceLine).length() > maxPriceLength) { prompted selection (use)

maxPriceLength = Integer.toString(priceLine).length(); prompted selection (def)

}

String linkLine = (String) links.get(i); prompted selection (use)

if (linkLine.length() > maxLinkLength) { prompted selection (use)

maxLinkLength = linkLine.length(); prompted selection (def)

}

}

String messageHtml = "<code>"; prompted selection (def)

for (int j = 0; j < titles.size(); j++) { prompted selection (control)

String titleText = (String) titles.get(j); prompted selection (use)

String priceText = ((Integer) prices.get(j)).toString(); prompted selection (def)

String linkText = (String) links.get(j); prompted selection (def)

String titlePadded = titleText + StringUtils.repeat(" ", maxTitleLength - titleText.length()); prompted selection (def)

String pricePadded = priceText + StringUtils.repeat(" ", maxPriceLength - priceText.length()); prompted selection (def)

String linkPadded = linkText + StringUtils.repeat(" ", maxLinkLength - linkText.length()); prompted selection (def)

A5. Source Programs and Examples

Page 44: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

messageHtml += ("( $" + pricePadded + " ) " + titlePadded + "<br/>" + linkPadded + "<br/><br/>"); prompted selection (def)

}

messageHtml += "</code>"; prompted selection (def)

BufferedReader confReader = new BufferedReader(new FileReader("/etc/smtp.conf")); automatic definition

String username = confReader.readLine(); prompted selection (def)

String password = confReader.readLine(); prompted selection (def)

System.out.println("Logging in with " + username + ", " + password); prompted selection (use)

String sslFactoryClass = "javax.net.ssl.SSLSocketFactory"; prompted selection (def)

Properties properties = System.getProperties(); automatic definition

properties.setProperty("mail.smtps.host", "smtp.gmail.com"); prompted selection (use)

properties.setProperty("mail.smtp.socketFactory.class", sslFactoryClass); prompted selection (use)

properties.setProperty("mail.smtp.port", "587"); prompted selection (use)

properties.setProperty("mail.smtp.socketFactory.port", "587"); prompted selection (use)

properties.setProperty("auth", "true"); prompted selection (use)

properties.put("mail.smtps.quitwait", "false"); prompted selection (use)

Session session = Session.getInstance(properties, null); automatic definition

MimeMessage message = new MimeMessage(session); automatic definition

message.setFrom(new InternetAddress(username)); prompted selection (use)

message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(destination, false)); prompted selection (use)

message.setSubject("Update of Craigslist posts"); prompted selection (use)

message.setText(messageHtml, "utf-8", "html"); prompted selection (use)

message.setSentDate(new Date()); prompted selection (use)

SMTPTransport transport = (SMTPTransport) session.getTransport("smtps"); automatic definition

transport.connect("smtp.gmail.com", username, password); initial selection

transport.sendMessage(message, message.getAllRecipients()); selection

} boilerplate

} boilerplate

A5. Source Programs and Examples

Page 45: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Task 3, Participant 2import java.io.BufferedReader; automatic import

import java.io.FileReader; automatic import

import java.util.Properties; automatic import

import javax.mail.Session; automatic import

import javax.mail.internet.MimeMessage; automatic import

import javax.mail.internet.InternetAddress; automatic import

import javax.mail.Message; automatic import

import java.util.Date; automatic import

import com.sun.mail.smtp.SMTPTransport; automatic import

import javax.mail.MessagingException; automatic import

import java.io.IOException; automatic import

public class ExtractedExample { boilerplate

public static void main(String[] args) throws MessagingException, IOException { boilerplate + prompted throws

String arg1 = "[email protected]"; prompted selection (def)

String destination = arg1; prompted selection (def)

String messageHtml = "<code>"; prompted selection (def)

BufferedReader confReader = new BufferedReader(new FileReader("/etc/smtp.conf")); automatic definition

String username = confReader.readLine(); prompted selection (def)

String password = confReader.readLine(); prompted selection (def)

String sslFactoryClass = "javax.net.ssl.SSLSocketFactory"; prompted selection (def)

Properties properties = System.getProperties(); automatic definition

properties.setProperty("mail.smtps.host", "smtp.gmail.com"); prompted selection (use)

properties.setProperty("mail.smtp.socketFactory.class", sslFactoryClass); prompted selection (use)

properties.setProperty("mail.smtp.port", "587"); prompted selection (use)

properties.setProperty("mail.smtp.socketFactory.port", "587"); prompted selection (use)

properties.setProperty("auth", "true"); prompted selection (use)

properties.put("mail.smtps.quitwait", "false"); prompted selection (use)

Session session = Session.getInstance(properties, null); automatic definition

MimeMessage message = new MimeMessage(session); automatic definition

message.setFrom(new InternetAddress(username)); prompted selection (use)

message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(destination, false)); prompted selection (use)

message.setSubject("Update of Craigslist posts"); prompted selection (use)

message.setText(messageHtml, "utf-8", "html"); prompted selection (use)

message.setSentDate(new Date()); prompted selection (use)

SMTPTransport transport = (SMTPTransport) session.getTransport("smtps"); automatic definition

transport.connect("smtp.gmail.com", username, password); prompted selection (use)

transport.sendMessage(message, message.getAllRecipients()); initial selection

} boilerplate

} boilerplate

A5. Source Programs and Examples

Page 46: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Task 3, Participant 10 import java.util.Properties; automatic import

import javax.mail.Session; automatic import

import javax.mail.internet.MimeMessage; automatic import

import javax.mail.Message; automatic import

import javax.mail.internet.InternetAddress; automatic import

import com.sun.mail.smtp.SMTPTransport; automatic import

import javax.mail.MessagingException; automatic import

import java.io.BufferedReader; automatic import

import java.io.FileReader; automatic import

import java.io.IOException; automatic import

public class ExtractedExample { boilerplate

public static void main(String[] args) throws MessagingException, IOException { boilerplate + prompted throws

String arg1 = "[email protected]"; prompted selection (def)

String destination = arg1; prompted selection (def)

String messageHtml = "<code>"; prompted selection (def)

BufferedReader confReader = new BufferedReader(new FileReader("/etc/smtp.conf")); selection

String username = confReader.readLine(); selection

String password = confReader.readLine(); selection

String sslFactoryClass = "javax.net.ssl.SSLSocketFactory"; prompted selection (def)

Properties properties = System.getProperties(); automatic definition

properties.setProperty("mail.smtps.host", "smtp.gmail.com"); prompted selection (use)

properties.setProperty("mail.smtp.socketFactory.class", sslFactoryClass); prompted selection (use)

properties.setProperty("mail.smtp.port", "587"); selection

properties.setProperty("mail.smtp.socketFactory.port", "587"); prompted selection (use)

properties.setProperty("auth", "true"); prompted selection (use)

properties.put("mail.smtps.quitwait", "false"); prompted selection (use)

Session session = Session.getInstance(properties, null); automatic definition

MimeMessage message = new MimeMessage(session); initial selection

message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(destination, false)); selection

message.setText(messageHtml, "utf-8", "html"); selection

SMTPTransport transport = (SMTPTransport) session.getTransport("smtps"); selection

transport.connect("smtp.gmail.com", username, password); selection

transport.sendMessage(message, message.getAllRecipients()); selection

transport.close(); selection

} boilerplate

} boilerplate

A5. Source Programs and Examples

Page 47: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Task 3, Participant 11 import java.util.Properties; automatic import

import javax.mail.Session; automatic import

import javax.mail.internet.MimeMessage; automatic import

import javax.mail.internet.InternetAddress; automatic import

import javax.mail.Message; automatic import

import java.util.Date; automatic import

import com.sun.mail.smtp.SMTPTransport; automatic import

import javax.mail.MessagingException; automatic import

public class ExtractedExample { boilerplate

public static void main(String[] args) throws MessagingException { boilerplate + prompted throws

String sslFactoryClass = "javax.net.ssl.SSLSocketFactory"; prompted selection (def)

Properties properties = System.getProperties(); automatic definition

properties.setProperty("mail.smtps.host", "smtp.gmail.com"); prompted selection (use)

properties.setProperty("mail.smtp.socketFactory.class", sslFactoryClass); prompted selection (use)

properties.setProperty("mail.smtp.port", "587"); prompted selection (use)

properties.setProperty("mail.smtp.socketFactory.port", "587"); prompted selection (use)

properties.setProperty("auth", "true"); prompted selection (use)

properties.put("mail.smtps.quitwait", "false"); prompted selection (use)

Session session = Session.getInstance(properties, null); automatic definition

MimeMessage message = new MimeMessage(session); automatic definition

message.setFrom(new InternetAddress("[email protected]")); prompted selection (use)

message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]", false)); prompted selection (use)

message.setSubject("Update of Craigslist posts"); prompted selection (use)

message.setText("<code>( $900 ) Prodeco Tech Storm 500 - Used Electric Bike (Low Charge Cycles)<br/>https://sfbay.craigslist.org/sby/bik/d/prodeco-tech-storm-500-used/6277477031.html <br/><br/>( $2500 ) Electric Bike for sale <br/>https://sfbay.craigslist.org/eby/bik/d/electric-bike-for-sale/6296746272.html <br/><br/>( $1700 ) Evelo electric bike <br/>https://sfbay.craigslist.org/pen/bik/d/evelo-electric-bike/6288568717.html <br/><br/>( $1750 ) Electric juiced bike <br/>https://sfbay.craigslist.org/nby/bik/d/electric-juiced-bike/6296607828.html <br/><br/>( $575 ) Mint 2011 Trek Valencia Plus Electric bike New <br/>https://sfbay.craigslist.org/sfc/bik/d/mint-2011-trek-valencia-plus/6296558109.html<br/><br/></code>", "utf-8", "html");prompted selection (use)

message.setSentDate(new Date()); prompted selection (use)

SMTPTransport transport = (SMTPTransport) session.getTransport("smtps"); initial selection

transport.connect("smtp.gmail.com", "[email protected]", "<password>");

transport.sendMessage(message, message.getAllRecipients());

transport.close();

} boilerplate

} boilerplate

A5. Source Programs and Examples

Page 48: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Task 3, Participant 13 import java.io.BufferedReader; automatic import

import java.io.FileReader; automatic import

import java.util.Properties; automatic import

import javax.mail.Session; automatic import

import javax.mail.internet.MimeMessage; automatic import

import javax.mail.internet.InternetAddress; automatic import

import javax.mail.Message; automatic import

import java.util.Date; automatic import

import com.sun.mail.smtp.SMTPTransport; automatic import

import javax.mail.MessagingException; automatic import

import java.io.IOException; automatic import

import java.security.Security; automatic import

public class ExtractedExample { boilerplate

public static void main(String[] args) throws MessagingException, IOException { boilerplate + prompted throws

String arg1 = "[email protected]"; prompted selection (def)

String destination = arg1; prompted selection (def)

BufferedReader confReader = new BufferedReader(new FileReader("/etc/smtp.conf")); automatic definition

String username = confReader.readLine(); prompted selection (def)

String password = confReader.readLine(); prompted selection (def)

System.out.println("Logging in with " + username + ", " + password); prompted selection (use)

Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); selection

String sslFactoryClass = "javax.net.ssl.SSLSocketFactory"; prompted selection (def)

Properties properties = System.getProperties(); automatic definition

properties.setProperty("mail.smtps.host", "smtp.gmail.com"); prompted selection (use)

properties.setProperty("mail.smtp.socketFactory.class", sslFactoryClass); prompted selection (use)

properties.setProperty("mail.smtp.port", "587"); prompted selection (use)

properties.setProperty("mail.smtp.socketFactory.port", "587"); prompted selection (use)

properties.setProperty("auth", "true"); prompted selection (use)

properties.put("mail.smtps.quitwait", "false"); prompted selection (use)

Session session = Session.getInstance(properties, null); automatic definition

MimeMessage message = new MimeMessage(session); prompted selection (use)

message.setFrom(new InternetAddress(username)); selection

message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(destination, false)); selection

message.setSubject("Update of Craigslist posts"); selection

message.setText("<code>( $2800 ) Hanebrink Fat, Fat electric bike! X2 <br/>https://sfbay.craigslist.org/sby/bik/d/hanebrink-fat-fat-electric/6297054006.html <br/><br/>( $629 ) SF Folding Electric Bike - YJW-F6 (35km) <br/>https://sfbay.craigslist.org/sfc/bid/d/sf-folding-electric-bike-yjw/6284224510.html<br/><br/>( $2500 ) 2017 Trek Powerfly 7 pedal assist electric mountain bike emtb <br/>https://sfbay.craigslist.org/pen/bik/d/2017-trek-powerfly-7-pedal/6288012417.html <br/><br/>( $900 ) Prodeco Tech Storm 500 - Used Electric Bike (Low Charge Cycles)<br/>https://sfbay.craigslist.org/sby/bik/d/prodeco-tech-storm-500-used/6277477031.html <br/><br/>( $2500 ) Electric Bike for sale <br/>https://sfbay.craigslist.org/eby/bik/d/electric-bike-for-sale/6296746272.html <br/><br/></code>", "utf-8", "html");selection

message.setSentDate(new Date()); selection

SMTPTransport transport = (SMTPTransport) session.getTransport("smtps"); initial selection

transport.connect("smtp.gmail.com", username, password); selection

transport.sendMessage(message, message.getAllRecipients()); selection

transport.close(); selection

} boilerplate

} boilerplate

A5. Source Programs and Examples

Page 49: Interactive Extraction of Examples from Existing Code · 2020-03-07 · Using JSoup to extract text from elements on a web page? I’m writing a program to fetch a web page and want

Task 3, Participant 14 import java.util.Properties; automatic import

import javax.mail.Session; automatic import

import javax.mail.internet.MimeMessage; automatic import

import javax.mail.internet.InternetAddress; automatic import

import javax.mail.Message; automatic import

import java.util.Date; automatic import

import com.sun.mail.smtp.SMTPTransport; automatic import

import javax.mail.MessagingException; automatic import

public class ExtractedExample { boilerplate

public static void main(String[] args) throws MessagingException { boilerplate + prompted throws

String destination = "[email protected]"; prompted selection (def)

String messageHtml = "<code>"; prompted selection (def)

Properties properties = System.getProperties(); automatic definition

properties.setProperty("mail.smtps.host", "smtp.gmail.com"); prompted selection (use)

properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); prompted selection (use)

properties.setProperty("mail.smtp.port", "587"); prompted selection (use)

properties.setProperty("mail.smtp.socketFactory.port", "587"); prompted selection (use)

properties.setProperty("auth", "true"); prompted selection (use)

properties.put("mail.smtps.quitwait", "false"); prompted selection (use)

Session session = Session.getInstance(properties, null); automatic definition

MimeMessage message = new MimeMessage(session); automatic definition

message.setFrom(new InternetAddress("[email protected]")); prompted selection (use)

message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(destination, false)); prompted selection (use)

message.setSubject("Update of Craigslist posts"); prompted selection (use)

message.setText(messageHtml, "utf-8", "html"); prompted selection (use)

message.setSentDate(new Date()); prompted selection (use)

SMTPTransport transport = (SMTPTransport) session.getTransport("smtps"); automatic definition

transport.connect("smtp.gmail.com", "[email protected]", "<password>"); prompted selection (use)

transport.sendMessage(message, message.getAllRecipients()); initial selection

} boilerplate

} boilerplate

A5. Source Programs and Examples