Skip to content

Commit 6cd58d3

Browse files
committed
initial commit
0 parents  commit 6cd58d3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2645
-0
lines changed

Diff for: Input.java

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
int f(int n) {
2+
if (n == 0) {
3+
return 1;
4+
} else {
5+
return n * f(n-1);
6+
}
7+
}

Diff for: JavaExtractor/JPredict/.classpath

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry excluding="Test.java" kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
15+
<attributes>
16+
<attribute name="maven.pomderived" value="true"/>
17+
</attributes>
18+
</classpathentry>
19+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
20+
<classpathentry kind="output" path="target/classes"/>
21+
</classpath>

Diff for: JavaExtractor/JPredict/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/

Diff for: JavaExtractor/JPredict/.project

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>JavaExtractor</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
eclipse.preferences.version=1
2+
encoding//src/main/java=UTF-8
3+
encoding/<project>=UTF-8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
12+
org.eclipse.jdt.core.compiler.source=1.8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1

Diff for: JavaExtractor/JPredict/pom.xml

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>JavaExtractor</groupId>
5+
<artifactId>JavaExtractor</artifactId>
6+
<name>JPredict</name>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<url>http://maven.apache.org</url>
9+
<build>
10+
<plugins>
11+
<plugin>
12+
<artifactId>maven-compiler-plugin</artifactId>
13+
<version>3.2</version>
14+
<configuration>
15+
<source>1.8</source>
16+
<target>1.8</target>
17+
<excludes>
18+
<exclude>Test.java</exclude>
19+
</excludes>
20+
</configuration>
21+
</plugin>
22+
<plugin>
23+
<artifactId>maven-shade-plugin</artifactId>
24+
<version>2.1</version>
25+
<executions>
26+
<execution>
27+
<phase>package</phase>
28+
<goals>
29+
<goal>shade</goal>
30+
</goals>
31+
<configuration>
32+
<transformers>
33+
<transformer
34+
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
35+
</transformer>
36+
</transformers>
37+
</configuration>
38+
</execution>
39+
</executions>
40+
</plugin>
41+
</plugins>
42+
</build>
43+
<dependencies>
44+
<dependency>
45+
<groupId>com.github.javaparser</groupId>
46+
<artifactId>javaparser-core</artifactId>
47+
<version>3.0.0-alpha.4</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>commons-io</groupId>
51+
<artifactId>commons-io</artifactId>
52+
<version>1.3.2</version>
53+
<scope>compile</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>com.fasterxml.jackson.core</groupId>
57+
<artifactId>jackson-databind</artifactId>
58+
<version>2.9.3</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>args4j</groupId>
62+
<artifactId>args4j</artifactId>
63+
<version>2.33</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.apache.commons</groupId>
67+
<artifactId>commons-lang3</artifactId>
68+
<version>3.5</version>
69+
</dependency>
70+
</dependencies>
71+
<properties>
72+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
73+
</properties>
74+
</project>
75+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package JavaExtractor;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Paths;
6+
import java.util.LinkedList;
7+
import java.util.concurrent.Executors;
8+
import java.util.concurrent.ThreadPoolExecutor;
9+
10+
import org.kohsuke.args4j.CmdLineException;
11+
12+
import JavaExtractor.Common.CommandLineValues;
13+
import JavaExtractor.FeaturesEntities.ProgramRelation;
14+
15+
public class App {
16+
private static CommandLineValues s_CommandLineValues;
17+
18+
public static void main(String[] args) {
19+
try {
20+
s_CommandLineValues = new CommandLineValues(args);
21+
} catch (CmdLineException e) {
22+
e.printStackTrace();
23+
return;
24+
}
25+
26+
if (s_CommandLineValues.NoHash) {
27+
ProgramRelation.setNoHash();
28+
}
29+
30+
if (s_CommandLineValues.File != null) {
31+
ExtractFeaturesTask extractFeaturesTask = new ExtractFeaturesTask(s_CommandLineValues,
32+
s_CommandLineValues.File.toPath());
33+
extractFeaturesTask.processFile();
34+
} else if (s_CommandLineValues.Dir != null) {
35+
extractDir();
36+
}
37+
}
38+
39+
private static void extractDir() {
40+
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(s_CommandLineValues.NumThreads);
41+
LinkedList<ExtractFeaturesTask> tasks = new LinkedList<>();
42+
try {
43+
Files.walk(Paths.get(s_CommandLineValues.Dir)).filter(Files::isRegularFile)
44+
.filter(p -> p.toString().toLowerCase().endsWith(".java")).forEach(f -> {
45+
ExtractFeaturesTask task = new ExtractFeaturesTask(s_CommandLineValues, f);
46+
tasks.add(task);
47+
});
48+
} catch (IOException e) {
49+
e.printStackTrace();
50+
return;
51+
}
52+
try {
53+
executor.invokeAll(tasks);
54+
} catch (InterruptedException e) {
55+
e.printStackTrace();
56+
} finally {
57+
executor.shutdown();
58+
}
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package JavaExtractor.Common;
2+
3+
import java.io.File;
4+
import org.kohsuke.args4j.CmdLineException;
5+
import org.kohsuke.args4j.CmdLineParser;
6+
import org.kohsuke.args4j.Option;
7+
8+
/**
9+
* This class handles the programs arguments.
10+
*/
11+
public class CommandLineValues {
12+
@Option(name = "--file", required = false)
13+
public File File = null;
14+
15+
@Option(name = "--dir", required = false, forbids = "--file")
16+
public String Dir = null;
17+
18+
@Option(name = "--max_path_length", required = true)
19+
public int MaxPathLength;
20+
21+
@Option(name = "--max_path_width", required = true)
22+
public int MaxPathWidth;
23+
24+
@Option(name = "--no_hash", required = false)
25+
public boolean NoHash = false;
26+
27+
@Option(name = "--num_threads", required = false)
28+
public int NumThreads = 32;
29+
30+
@Option(name = "--min_code_len", required = false)
31+
public int MinCodeLength = 1;
32+
33+
@Option(name = "--max_code_len", required = false)
34+
public int MaxCodeLength = 10000;
35+
36+
@Option(name = "--pretty_print", required = false)
37+
public boolean PrettyPrint = false;
38+
39+
@Option(name = "--max_child_id", required = false)
40+
public int MaxChildId = Integer.MAX_VALUE;
41+
42+
public CommandLineValues(String... args) throws CmdLineException {
43+
CmdLineParser parser = new CmdLineParser(this);
44+
try {
45+
parser.parseArgument(args);
46+
} catch (CmdLineException e) {
47+
System.err.println(e.getMessage());
48+
parser.printUsage(System.err);
49+
throw e;
50+
}
51+
}
52+
53+
public CommandLineValues() {
54+
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package JavaExtractor.Common;
2+
3+
import java.util.ArrayList;
4+
import java.util.stream.Collectors;
5+
import java.util.stream.Stream;
6+
7+
import com.github.javaparser.ast.Node;
8+
import com.github.javaparser.ast.UserDataKey;
9+
10+
import JavaExtractor.FeaturesEntities.ProgramNode;
11+
import JavaExtractor.FeaturesEntities.Property;
12+
13+
public final class Common {
14+
public static final UserDataKey<Property> PropertyKey = new UserDataKey<Property>() {
15+
};
16+
public static final UserDataKey<ProgramNode> ProgramNodeKey = new UserDataKey<ProgramNode>() {
17+
};
18+
public static final UserDataKey<Integer> ChildId = new UserDataKey<Integer>() {
19+
};
20+
public static final String EmptyString = "";
21+
public static final String UTF8 = "UTF-8";
22+
public static final String EvaluateTempDir = "EvalTemp";
23+
24+
public static final String FieldAccessExpr = "FieldAccessExpr";
25+
public static final String ClassOrInterfaceType = "ClassOrInterfaceType";
26+
public static final String MethodDeclaration = "MethodDeclaration";
27+
public static final String NameExpr = "NameExpr";
28+
public static final String MethodCallExpr = "MethodCallExpr";
29+
public static final String DummyNode = "DummyNode";
30+
public static final String BlankWord = "BLANK";
31+
32+
public static final int c_MaxLabelLength = 50;
33+
public static final String methodName = "METHOD_NAME";
34+
public static final String internalSeparator = "|";
35+
36+
public static String normalizeName(String original, String defaultString) {
37+
original = original.toLowerCase().replaceAll("\\\\n", "") // escaped new
38+
// lines
39+
.replaceAll("//s+", "") // whitespaces
40+
.replaceAll("[\"',]", "") // quotes, apostrophies, commas
41+
.replaceAll("\\P{Print}", ""); // unicode weird characters
42+
String stripped = original.replaceAll("[^A-Za-z]", "");
43+
if (stripped.length() == 0) {
44+
String carefulStripped = original.replaceAll(" ", "_");
45+
if (carefulStripped.length() == 0) {
46+
return defaultString;
47+
} else {
48+
return carefulStripped;
49+
}
50+
} else {
51+
return stripped;
52+
}
53+
}
54+
55+
public static boolean isMethod(Node node) {
56+
String type = node.getUserData(Common.PropertyKey).getType();
57+
58+
return isMethod(node, type);
59+
}
60+
61+
public static boolean isMethod(Node node, String type) {
62+
Property parentProperty = node.getParentNode().getUserData(Common.PropertyKey);
63+
if (parentProperty == null) {
64+
return false;
65+
}
66+
67+
String parentType = parentProperty.getType();
68+
return Common.NameExpr.equals(type) && Common.MethodDeclaration.equals(parentType);
69+
}
70+
71+
public static ArrayList<String> splitToSubtokens(String str1) {
72+
String str2 = str1.trim();
73+
return Stream.of(str2.split("(?<=[a-z])(?=[A-Z])|_|[0-9]|(?<=[A-Z])(?=[A-Z][a-z])|\\s+"))
74+
.filter(s -> s.length() > 0).map(s -> Common.normalizeName(s, Common.EmptyString))
75+
.filter(s -> s.length() > 0).collect(Collectors.toCollection(ArrayList::new));
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package JavaExtractor.Common;
2+
3+
import java.util.ArrayList;
4+
import com.github.javaparser.ast.Node;
5+
6+
public class MethodContent {
7+
private ArrayList<Node> leaves;
8+
private String name;
9+
private long length;
10+
11+
public MethodContent(ArrayList<Node> leaves, String name, long length) {
12+
this.leaves = leaves;
13+
this.name = name;
14+
this.length = length;
15+
}
16+
17+
public ArrayList<Node> getLeaves() {
18+
return leaves;
19+
}
20+
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public long getLength() {
26+
return length;
27+
}
28+
29+
}

0 commit comments

Comments
 (0)