Skip to content

Commit 2df4b84

Browse files
committed
adding "--json_output" flag
1 parent 7533a90 commit 2df4b84

File tree

10 files changed

+68
-40
lines changed

10 files changed

+68
-40
lines changed

JavaExtractor/JPredict/dependency-reduced-pom.xml

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
<plugins>
1111
<plugin>
1212
<artifactId>maven-compiler-plugin</artifactId>
13-
<version>3.2</version>
13+
<version>3.8.0</version>
1414
<configuration>
15-
<source>1.8</source>
16-
<target>1.8</target>
15+
<release>11</release>
1716
<excludes>
1817
<exclude>Test.java</exclude>
1918
</excludes>

JavaExtractor/JPredict/pom.xml

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
<plugins>
1212
<plugin>
1313
<artifactId>maven-compiler-plugin</artifactId>
14-
<version>3.2</version>
14+
<version>3.8.0</version>
1515
<configuration>
16-
<source>1.8</source>
17-
<target>1.8</target>
16+
<release>11</release>
1817
<excludes>
1918
<exclude>Test.java</exclude>
2019
</excludes>
@@ -68,6 +67,11 @@
6867
<artifactId>commons-lang3</artifactId>
6968
<version>3.5</version>
7069
</dependency>
70+
<dependency>
71+
<groupId>com.google.code.gson</groupId>
72+
<artifactId>gson</artifactId>
73+
<version>2.8.5</version>
74+
</dependency>
7175
</dependencies>
7276
<properties>
7377
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

JavaExtractor/JPredict/src/main/java/JavaExtractor/Common/CommandLineValues.java

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public class CommandLineValues {
4040
@Option(name = "--max_child_id", required = false)
4141
public int MaxChildId = 3;
4242

43+
@Option(name = "--json_output", required = false)
44+
public boolean JsonOutput = false;
45+
4346
public CommandLineValues(String... args) throws CmdLineException {
4447
CmdLineParser parser = new CmdLineParser(this);
4548
try {

JavaExtractor/JPredict/src/main/java/JavaExtractor/Common/MethodContent.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ public class MethodContent {
88
private final ArrayList<Node> leaves;
99
private final String name;
1010

11-
public MethodContent(ArrayList<Node> leaves, String name) {
11+
private final String content;
12+
13+
public MethodContent(ArrayList<Node> leaves, String name, String content) {
1214
this.leaves = leaves;
1315
this.name = name;
16+
this.content = content;
1417
}
1518

1619
public ArrayList<Node> getLeaves() {
@@ -20,4 +23,8 @@ public ArrayList<Node> getLeaves() {
2023
public String getName() {
2124
return name;
2225
}
26+
27+
public String getContent() {
28+
return content;
29+
}
2330
}

JavaExtractor/JPredict/src/main/java/JavaExtractor/ExtractFeaturesTask.java

+14-7
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
import java.util.ArrayList;
1313
import java.util.List;
1414
import java.util.concurrent.Callable;
15+
import com.google.gson.Gson;
1516

1617
class ExtractFeaturesTask implements Callable<Void> {
17-
private final CommandLineValues m_CommandLineValues;
18+
private final CommandLineValues commandLineValues;
1819
private final Path filePath;
1920

2021
public ExtractFeaturesTask(CommandLineValues commandLineValues, Path path) {
21-
m_CommandLineValues = commandLineValues;
22+
this.commandLineValues = commandLineValues;
2223
this.filePath = path;
2324
}
2425

@@ -49,8 +50,8 @@ public void processFile() {
4950
private ArrayList<ProgramFeatures> extractSingleFile() throws IOException {
5051
String code;
5152

52-
if (m_CommandLineValues.MaxFileLength > 0 &&
53-
Files.lines(filePath, Charset.defaultCharset()).count() > m_CommandLineValues.MaxFileLength) {
53+
if (commandLineValues.MaxFileLength > 0 &&
54+
Files.lines(filePath, Charset.defaultCharset()).count() > commandLineValues.MaxFileLength) {
5455
return new ArrayList<>();
5556
}
5657
try {
@@ -59,7 +60,7 @@ private ArrayList<ProgramFeatures> extractSingleFile() throws IOException {
5960
e.printStackTrace();
6061
code = Common.EmptyString;
6162
}
62-
FeatureExtractor featureExtractor = new FeatureExtractor(m_CommandLineValues);
63+
FeatureExtractor featureExtractor = new FeatureExtractor(commandLineValues, this.filePath);
6364

6465
return featureExtractor.extractFeatures(code);
6566
}
@@ -74,8 +75,14 @@ public String featuresToString(ArrayList<ProgramFeatures> features) {
7475
for (ProgramFeatures singleMethodFeatures : features) {
7576
StringBuilder builder = new StringBuilder();
7677

77-
String toPrint = singleMethodFeatures.toString();
78-
if (m_CommandLineValues.PrettyPrint) {
78+
String toPrint;
79+
if (commandLineValues.JsonOutput) {
80+
toPrint = new Gson().toJson(singleMethodFeatures);
81+
}
82+
else {
83+
toPrint = singleMethodFeatures.toString();
84+
}
85+
if (commandLineValues.PrettyPrint) {
7986
toPrint = toPrint.replace(" ", "\n\t");
8087
}
8188
builder.append(toPrint);

JavaExtractor/JPredict/src/main/java/JavaExtractor/FeatureExtractor.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import com.github.javaparser.ast.CompilationUnit;
1212
import com.github.javaparser.ast.Node;
1313

14+
import java.io.File;
15+
import java.nio.file.Path;
1416
import java.util.ArrayList;
1517
import java.util.HashSet;
1618
import java.util.Set;
@@ -26,9 +28,11 @@ class FeatureExtractor {
2628
.of("AssignExpr", "ArrayAccessExpr", "FieldAccessExpr", "MethodCallExpr")
2729
.collect(Collectors.toCollection(HashSet::new));
2830
private final CommandLineValues m_CommandLineValues;
31+
private final Path filePath;
2932

30-
public FeatureExtractor(CommandLineValues commandLineValues) {
33+
public FeatureExtractor(CommandLineValues commandLineValues, Path filePath) {
3134
this.m_CommandLineValues = commandLineValues;
35+
this.filePath = filePath;
3236
}
3337

3438
private static ArrayList<Node> getTreeStack(Node node) {
@@ -90,7 +94,8 @@ private ArrayList<ProgramFeatures> generatePathFeatures(ArrayList<MethodContent>
9094

9195
private ProgramFeatures generatePathFeaturesForFunction(MethodContent methodContent) {
9296
ArrayList<Node> functionLeaves = methodContent.getLeaves();
93-
ProgramFeatures programFeatures = new ProgramFeatures(methodContent.getName());
97+
ProgramFeatures programFeatures = new ProgramFeatures(
98+
methodContent.getName(), this.filePath, methodContent.getContent());
9499

95100
for (int i = 0; i < functionLeaves.size(); i++) {
96101
for (int j = i + 1; j < functionLeaves.size(); j++) {

JavaExtractor/JPredict/src/main/java/JavaExtractor/FeaturesEntities/ProgramFeatures.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
package JavaExtractor.FeaturesEntities;
22

3-
import com.fasterxml.jackson.annotation.JsonIgnore;
4-
3+
import java.nio.file.Path;
54
import java.util.ArrayList;
65
import java.util.stream.Collectors;
76

87
public class ProgramFeatures {
9-
private final String name;
8+
String name;
9+
10+
transient ArrayList<ProgramRelation> features = new ArrayList<>();
11+
String textContent;
12+
13+
String filePath;
1014

11-
private final ArrayList<ProgramRelation> features = new ArrayList<>();
15+
public ProgramFeatures(String name, Path filePath, String textContent) {
1216

13-
public ProgramFeatures(String name) {
1417
this.name = name;
18+
this.filePath = filePath.toAbsolutePath().toString();
19+
this.textContent = textContent;
1520
}
1621

1722
@SuppressWarnings("StringBufferReplaceableByString")
@@ -29,7 +34,6 @@ public void addFeature(Property source, String path, Property target) {
2934
features.add(newRelation);
3035
}
3136

32-
@JsonIgnore
3337
public boolean isEmpty() {
3438
return features.isEmpty();
3539
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package JavaExtractor.FeaturesEntities;
22

33
public class ProgramRelation {
4-
private final Property m_Source;
5-
private final Property m_Target;
6-
private final String m_Path;
4+
Property source;
5+
Property target;
6+
String path;
77

88
public ProgramRelation(Property sourceName, Property targetName, String path) {
9-
m_Source = sourceName;
10-
m_Target = targetName;
11-
m_Path = path;
9+
source = sourceName;
10+
target = targetName;
11+
this.path = path;
1212
}
1313

1414
public String toString() {
15-
return String.format("%s,%s,%s", m_Source.getName(), m_Path,
16-
m_Target.getName());
15+
return String.format("%s,%s,%s", source.getName(), path,
16+
target.getName());
1717
}
1818
}

JavaExtractor/JPredict/src/main/java/JavaExtractor/Visitors/FunctionVisitor.java

+9-10
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212

1313
@SuppressWarnings("StringEquality")
1414
public class FunctionVisitor extends VoidVisitorAdapter<Object> {
15-
private final ArrayList<MethodContent> m_Methods = new ArrayList<>();
16-
private final CommandLineValues m_CommandLineValues;
15+
private final ArrayList<MethodContent> methods = new ArrayList<>();
16+
private final CommandLineValues commandLineValues;
1717

1818
public FunctionVisitor(CommandLineValues commandLineValues) {
19-
this.m_CommandLineValues = commandLineValues;
19+
this.commandLineValues = commandLineValues;
2020
}
2121

2222
@Override
@@ -38,14 +38,13 @@ private void visitMethod(MethodDeclaration node) {
3838
splitName = String.join(Common.internalSeparator, splitNameParts);
3939
}
4040

41+
node.setName(Common.methodName);
42+
4143
if (node.getBody() != null) {
4244
long methodLength = getMethodLength(node.getBody().toString());
43-
if (m_CommandLineValues.MaxCodeLength > 0) {
44-
if (methodLength >= m_CommandLineValues.MinCodeLength && methodLength <= m_CommandLineValues.MaxCodeLength) {
45-
m_Methods.add(new MethodContent(leaves, splitName));
46-
}
47-
} else {
48-
m_Methods.add(new MethodContent(leaves, splitName));
45+
if (commandLineValues.MaxCodeLength <= 0 ||
46+
(methodLength >= commandLineValues.MinCodeLength && methodLength <= commandLineValues.MaxCodeLength)) {
47+
methods.add(new MethodContent(leaves, splitName, node.toString()));
4948
}
5049
}
5150
}
@@ -65,6 +64,6 @@ private long getMethodLength(String code) {
6564
}
6665

6766
public ArrayList<MethodContent> getMethodContents() {
68-
return m_Methods;
67+
return methods;
6968
}
7069
}
Binary file not shown.

0 commit comments

Comments
 (0)