Skip to content

Commit 4a499c4

Browse files
committed
Initial commit
1 parent 90605cc commit 4a499c4

17 files changed

Lines changed: 1703 additions & 2 deletions

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
bin/
2+
dist/
3+
target/
4+
.project
5+
.classpath
6+
.settings
7+
*.class
8+
.pydevproject
9+
__pycache__
10+
*.pyc

LICENSE.md

Lines changed: 415 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,57 @@
1-
# SequenceExtractor
2-
Statement Sequence Extractor for Java Source Code Snippets
1+
SequenceExtractor: Statement Sequence Extractor for Java Source Code Snippets
2+
=============================================================================
3+
SequenceExtractor is a statement sequence extractor for Java source code snippets.
4+
The tool allows exporting the sequence of statements of snippets in a list format.
5+
It can be used as a library either from Java or from Python using the Python binding.
6+
7+
Using as a library
8+
------------------
9+
Import the library in your code. Then, you can parse snippets as follows:
10+
<pre><code>ArrayList<String> sequence = SequenceExtractor.extractSequence(""
11+
+ "JFrame frame = new JFrame(\"myframe\");\n"
12+
+ "JPanel panel = new JPanel()\n;"
13+
+ "Container pane = frame.getContentPane();\n"
14+
+ "GridLayout layout = new GridLayout(2,2);\n"
15+
+ "panel.setLayout(layout);\n"
16+
+ "panel.add(upperLeft);\n"
17+
+ "panel.add(upperRight);\n"
18+
+ "panel.add(lowerLeft);\n"
19+
+ "panel.add(lowerRight);\n"
20+
+ "pane.add(panel);\n"
21+
);</code></pre>
22+
The result is a list with the sequence of calls for the snippet. For the above example the result is:
23+
<pre><code>[CI_JFrame, CI_JPanel, FC_Container, CI_GridLayout, FC_void, FC_void, FC_void, FC_void, FC_void, FC_void]</code></pre>
24+
25+
There are three types of commands:
26+
- object instantiations (<code>CI</code>)
27+
- assignments (<code>AM</code>)
28+
- function calls (<code>FC</code>)
29+
30+
There are also certain options when extracting the snippets provided asparameters of the <code>extractSequence</code> function. These are:
31+
- keepFunctionCallTypes: denotes whether to output also the objects performing the function calls (instead of only the return types), default is false.
32+
- keepLiterals denotes if commands with literals (primitive types) should be extracted, or discarded, default is false.
33+
34+
Using in Python
35+
---------------
36+
SequenceExtractor also has python bindings. Using the python wrapper is simple. At first, the library
37+
has to be imported and the SequenceExtractor object has to be initialized given the path to the jar
38+
of the library and the options to keep function call types (<code>keep_function_call_types</code>)
39+
and keep literals (<code>keep_literals</code>):
40+
<pre><code>sequence_extractor = SequenceExtractor("path/to/SequenceExtractor-0.1.jar", True, False)</code></pre>
41+
After that, you can parse snippets as follows:
42+
<pre><code>sequence = sequence_extractor.parse_snippet(
43+
"JFrame frame = new JFrame(\"myframe\");\n" +
44+
"JPanel panel = new JPanel();\n" +
45+
"Container pane = frame.getContentPane();\n" +
46+
"GridLayout layout = new GridLayout(2,2);\n" +
47+
"panel.setLayout(layout);\n" +
48+
"panel.add(upperLeft);\n" +
49+
"panel.add(upperRight);\n" +
50+
"panel.add(lowerLeft);\n" +
51+
"panel.add(lowerRight);\n" +
52+
"pane.add(panel)\n;"
53+
)</code></pre>
54+
55+
Note that after using the library, you have to close the SequenceExtractor object using function <code>close</code>, i.e.:<pre><code>sequence_extractor.close()</code></pre>
56+
57+

dep.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<assembly
2+
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
5+
<id>jar-with-dependencies</id>
6+
<formats>
7+
<format>jar</format>
8+
</formats>
9+
<includeBaseDirectory>false</includeBaseDirectory>
10+
<dependencySets>
11+
<dependencySet>
12+
<outputDirectory>/</outputDirectory>
13+
<useProjectArtifact>true</useProjectArtifact>
14+
<unpack>true</unpack>
15+
<scope>runtime</scope>
16+
</dependencySet>
17+
<!-- Include dependencies from system jars -->
18+
<dependencySet>
19+
<outputDirectory>/</outputDirectory>
20+
<unpack>true</unpack>
21+
<scope>system</scope>
22+
</dependencySet>
23+
</dependencySets>
24+
</assembly>

pom.xml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>SequenceExtractor</groupId>
6+
<artifactId>SequenceExtractor</artifactId>
7+
<version>0.1</version>
8+
9+
<properties>
10+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
12+
</properties>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.eclipse.tycho</groupId>
17+
<artifactId>org.eclipse.jdt.core</artifactId>
18+
<version>3.12.0.v20160516-2131</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.eclipse.core</groupId>
22+
<artifactId>runtime</artifactId>
23+
<version>3.10.0-v20140318-2214</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.eclipse.birt.runtime</groupId>
27+
<artifactId>org.eclipse.core.resources</artifactId>
28+
<version>3.10.0.v20150423-0755</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.json</groupId>
32+
<artifactId>json</artifactId>
33+
<version>20160810</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>ASTExtractor</groupId>
37+
<artifactId>ASTExtractor</artifactId>
38+
<version>0.4</version>
39+
<scope>system</scope>
40+
<!-- Change this to the directory of the ASTExtractor jar file -->
41+
<systemPath>${project.basedir}/../ASTExtractor/target/ASTExtractor-0.4.jar</systemPath>
42+
</dependency>
43+
</dependencies>
44+
45+
<build>
46+
<sourceDirectory>src</sourceDirectory>
47+
<plugins>
48+
<plugin>
49+
<groupId>org.apache.maven.plugins</groupId>
50+
<artifactId>maven-compiler-plugin</artifactId>
51+
<version>3.5.1</version>
52+
<configuration>
53+
<source>1.8</source>
54+
<target>1.8</target>
55+
</configuration>
56+
</plugin>
57+
58+
<plugin>
59+
<groupId>org.apache.maven.plugins</groupId>
60+
<artifactId>maven-javadoc-plugin</artifactId>
61+
<version>2.10.4</version>
62+
<executions>
63+
<execution>
64+
<id>attach-javadocs</id>
65+
<phase>prepare-package</phase>
66+
<goals>
67+
<goal>javadoc</goal>
68+
</goals>
69+
</execution>
70+
</executions>
71+
</plugin>
72+
73+
<plugin>
74+
<groupId>org.apache.maven.plugins</groupId>
75+
<artifactId>maven-assembly-plugin</artifactId>
76+
<version>2.6</version>
77+
<configuration>
78+
<descriptor>dep.xml</descriptor>
79+
</configuration>
80+
<executions>
81+
<execution>
82+
<id>make-assembly</id>
83+
<phase>package</phase>
84+
<goals>
85+
<goal>single</goal>
86+
</goals>
87+
<configuration>
88+
<appendAssemblyId>false</appendAssemblyId>
89+
</configuration>
90+
</execution>
91+
</executions>
92+
</plugin>
93+
94+
</plugins>
95+
</build>
96+
97+
</project>

src/parsehelpers/Block.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package parsehelpers;
2+
3+
import java.util.ArrayList;
4+
5+
/**
6+
* Class that represents a series of statements.
7+
*
8+
* @author themis
9+
*/
10+
@SuppressWarnings("serial")
11+
public class Block extends ArrayList<Statement> {
12+
13+
}

src/parsehelpers/LookUpTable.java

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package parsehelpers;
2+
3+
import java.util.HashMap;
4+
5+
/**
6+
* Class that implements a look up table for variables in all levels.
7+
*
8+
* @author themis
9+
*/
10+
public class LookUpTable {
11+
12+
/**
13+
* Keeps the super class of the class for which the look up table is created.
14+
*/
15+
String superClass;
16+
17+
/**
18+
* Holds all class level variables.
19+
*/
20+
HashMap<String, String> classLevelTable;
21+
22+
/**
23+
* Holds all method level variables.
24+
*/
25+
HashMap<String, String> methodLevelTable;
26+
27+
/**
28+
* Initializes this table.
29+
*/
30+
public LookUpTable() {
31+
32+
}
33+
34+
/**
35+
* Method that is called when entering a new class.
36+
*/
37+
public void enterClass() {
38+
classLevelTable = new HashMap<String, String>();
39+
superClass = "";
40+
}
41+
42+
/**
43+
* Method that is called when entering a new class.
44+
*
45+
* @param superClass the super class of the class that is entered.
46+
*/
47+
public void enterClass(String superClass) {
48+
if (superClass != null) {
49+
classLevelTable = new HashMap<String, String>();
50+
this.superClass = superClass;
51+
} else
52+
enterClass();
53+
}
54+
55+
/**
56+
* Method that is called when entering a new method.
57+
*/
58+
public void enterMethod() {
59+
methodLevelTable = new HashMap<String, String>();
60+
}
61+
62+
/**
63+
* Adds a new class variable in this look up table.
64+
*
65+
* @param variableName the name of the variable to be added.
66+
* @param variableType the type of the variable that is added.
67+
*/
68+
public void addClassVariable(String variableName, String variableType) {
69+
classLevelTable.put(variableName, variableType);
70+
}
71+
72+
/**
73+
* Adds a new method variable in this look up table.
74+
*
75+
* @param variableName the name of the variable to be added.
76+
* @param variableType the type of the variable that is added.
77+
*/
78+
public void addMethodVariable(String variableName, String variableType) {
79+
methodLevelTable.put(variableName, variableType);
80+
}
81+
82+
/**
83+
* Returns the type of a variable given its name. The method table is looked up first and then if the variable is
84+
* not declared there, the variable is searched in class scope.
85+
*
86+
* @param variableName the name of the variable.
87+
* @return the type of the variable given or {@code "___"} if the variable does not exist.
88+
*/
89+
public String getTypeOfVariable(String variableName) {
90+
if (methodLevelTable != null && methodLevelTable.containsKey(variableName))
91+
return methodLevelTable.get(variableName);
92+
else if (classLevelTable.containsKey(variableName))
93+
return classLevelTable.get(variableName);
94+
else
95+
return "___";
96+
}
97+
98+
/**
99+
* Returns the super class of the class for which the look up table is created.
100+
*
101+
* @return the super class of the class for which the look up table is created.
102+
*/
103+
public String getSuperClass() {
104+
return superClass;
105+
}
106+
107+
/**
108+
* Returns a string representation of this look up table.
109+
*
110+
* @return a string representation of this look up table.
111+
*/
112+
@Override
113+
public String toString() {
114+
return "superClass: " + superClass + "\nclassLevelTable: " + classLevelTable + "\nmethodLevelTable: "
115+
+ methodLevelTable;
116+
}
117+
}

src/parsehelpers/Snippet.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package parsehelpers;
2+
3+
import java.util.ArrayList;
4+
5+
/**
6+
* Class that represents a snippet as a sequence of blocks.
7+
*
8+
* @author themis
9+
*/
10+
public class Snippet {
11+
12+
/**
13+
* The blocks that are contained in this statement.
14+
*/
15+
public ArrayList<Block> blocks;
16+
17+
/**
18+
* Initializes this snippet.
19+
*/
20+
public Snippet() {
21+
blocks = new ArrayList<Block>();
22+
}
23+
24+
/**
25+
* Adds a new block to this snippet.
26+
*/
27+
public void addBlock() {
28+
blocks.add(new Block());
29+
}
30+
31+
/**
32+
* Adds a statement to the last added block.
33+
*
34+
* @param statement the statement to be added.
35+
*/
36+
public void addStatement(Statement statement) {
37+
blocks.get(blocks.size() - 1).add(statement);
38+
}
39+
40+
/**
41+
* Returns a string representation of this snippet.
42+
*
43+
* @return a string representation of this snippet.
44+
*/
45+
@Override
46+
public String toString() {
47+
String ret = "";
48+
for (Block block : blocks) {
49+
for (Statement statement : block) {
50+
if (statement.size() > 1)
51+
ret += statement.toString() + "\n";
52+
}
53+
}
54+
return ret;
55+
}
56+
57+
}

0 commit comments

Comments
 (0)