diff --git a/helper.py b/helper.py
index fbb5666b..3fcd7311 100644
--- a/helper.py
+++ b/helper.py
@@ -6,14 +6,14 @@
###############################################################
-g_test_file = "data/selected_file/mn_c2x/c2x_jl_test_correct_prediction_samefile.txt"
+g_deltas_types = ["token", "char"]
g_simp_file = "data/tmp/sm_test.java"
+JAR_LOAD_JAVA_METHOD = "others/LoadJavaMethod/target/jar/LoadJavaMethod.jar"
-g_deltas_types = ["token", "char"]
+# TODO - update file_path and delta_type
+g_test_file = "data/selected_file/mn_c2x/c2x_jl_test_correct_prediction_samefile.txt"
g_deltas_type = g_deltas_types[0]
-JAR_LOAD_JAVA_METHOD = "others/LoadJavaMethod/target/jar/LoadJavaMethod.jar"
-
###############################################################
diff --git a/others/GetAstNodes/.gitignore b/others/GetAstNodes/.gitignore
new file mode 100644
index 00000000..4bba832f
--- /dev/null
+++ b/others/GetAstNodes/.gitignore
@@ -0,0 +1,32 @@
+
+# OS file
+.DS_Store
+
+# Generated files
+/target/
+.idea/
+*.iml
+
+# Compiled class file
+*.class
+
+# Log file
+*.log
+
+# BlueJ files
+*.ctxt
+
+# Mobile Tools for Java (J2ME)
+.mtj.tmp/
+
+# Package Files #
+*.jar
+*.war
+*.nar
+*.ear
+*.zip
+*.tar.gz
+*.rar
+
+# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
+hs_err_pid*
diff --git a/others/GetAstNodes/pom.xml b/others/GetAstNodes/pom.xml
new file mode 100644
index 00000000..5d591e0a
--- /dev/null
+++ b/others/GetAstNodes/pom.xml
@@ -0,0 +1,49 @@
+
+
+ 4.0.0
+
+ com.uhserg.dd.getastnodes
+ GetAstNodes
+ 1.0
+
+
+
+
+ org.apache.maven.plugins
+ maven-assembly-plugin
+
+ jar/GetAstNodes
+
+ false
+
+
+ Main
+
+
+
+ jar-with-dependencies
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+
+
+ 1.8
+
+
+
+
+
+
+
+ com.github.javaparser
+ javaparser-core
+ 3.14.5
+
+
+
+
\ No newline at end of file
diff --git a/others/GetAstNodes/src/main/java/Main.java b/others/GetAstNodes/src/main/java/Main.java
new file mode 100644
index 00000000..0d5c323f
--- /dev/null
+++ b/others/GetAstNodes/src/main/java/Main.java
@@ -0,0 +1,72 @@
+import com.github.javaparser.StaticJavaParser;
+import com.github.javaparser.ast.CompilationUnit;
+import com.github.javaparser.ast.Node;
+import com.github.javaparser.ast.body.MethodDeclaration;
+import com.github.javaparser.ast.visitor.TreeVisitor;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Input: string_program
+ * Output: ast_nodes
+ */
+public class Main {
+ static final String INNER_DELIMITER = " ___INNER___ ";
+ static final String OUTER_DELIMITER = " ___OUTER___ ";
+
+ public static void main(String[] args) {
+ String strCode = args[0];
+ CompilationUnit cu = getParseUnit(strCode);
+ List allTerminals = getTerminalNodes(cu);
+ List allClasses = getNodeClasses(cu);
+ String astData = String.join(INNER_DELIMITER, allTerminals)
+ + OUTER_DELIMITER
+ + String.join(INNER_DELIMITER, allClasses);
+ System.out.print(astData);
+ }
+
+ public static List getTerminalNodes (CompilationUnit cu) {
+ List allTerminals = new ArrayList<>();
+ MethodDeclaration md = cu.findAll(MethodDeclaration.class).get(0);
+ md.setName("METHOD_NAME"); // skip actual method name
+ new TreeVisitor() {
+ @Override
+ public void process(Node node) {
+ if (node.getChildNodes().size() == 0) {
+ allTerminals.add(node.toString());
+ }
+ }
+ }.visitPreOrder(md);
+ return allTerminals;
+ }
+
+ public static List getNodeClasses(CompilationUnit cu) {
+ List allClasses = new ArrayList<>();
+ MethodDeclaration md = cu.findAll(MethodDeclaration.class).get(0);
+ new TreeVisitor() {
+ @Override
+ public void process(Node node) {
+ String clsStr = node.getClass().toString();
+ // clsStr = clsStr.replace("class com.github.javaparser.ast.", "");
+ String lastName = clsStr.substring(clsStr.lastIndexOf('.') + 1);
+ allClasses.add(lastName);
+ }
+ }.visitPreOrder(md);
+ return allClasses;
+ }
+
+ public static CompilationUnit getParseUnit(String txtCode) {
+ CompilationUnit cu = null;
+ try {
+ // add tmp class to parse
+ if(!txtCode.startsWith("class")) txtCode = "class T { \n" + txtCode + "\n}";
+ // remove comments
+ StaticJavaParser.getConfiguration().setAttributeComments(false);
+ // parse code
+ cu = StaticJavaParser.parse(txtCode);
+ } catch (Exception ignore) {}
+ return cu;
+ }
+
+}
diff --git a/others/GetAstNodes/target/jar/GetAstNodes.jar b/others/GetAstNodes/target/jar/GetAstNodes.jar
new file mode 100644
index 00000000..9b996fea
Binary files /dev/null and b/others/GetAstNodes/target/jar/GetAstNodes.jar differ
diff --git a/others/LoadJavaMethod/src/main/java/Main.java b/others/LoadJavaMethod/src/main/java/Main.java
index 1848eeaa..d28d6786 100644
--- a/others/LoadJavaMethod/src/main/java/Main.java
+++ b/others/LoadJavaMethod/src/main/java/Main.java
@@ -8,31 +8,29 @@
import java.util.List;
/**
- * Input: .../file.java
- * Output: "method_name" "method_body"
+ * Input: .java
+ * Output: "method_name" "single_line"
*/
public class Main {
public static void main(String[] args) {
File inputFile = new File(args[0]);
- //System.out.println(inputFile);
CompilationUnit root = getParseUnit(inputFile);
List contents = loadJavaMethod(root);
System.out.print(String.join(" ", contents));
}
static List loadJavaMethod(CompilationUnit root) {
- List contents = new ArrayList();
+ List contents = new ArrayList<>();
if (root != null) {
try {
MethodDeclaration methodDec = root.findAll(MethodDeclaration.class).get(0);
String method_name = methodDec.getName().toString();
contents.add(method_name);
-// methodDec.setName("f"); // keep original name
- String method_body = methodDec.toString()
+ String single_line = methodDec.toString()
.replaceAll("\\n", " ")
.replaceAll("\\r", " ");
- contents.add(method_body);
+ contents.add(single_line);
} catch (Exception ignore){}
}
return contents;
@@ -42,8 +40,11 @@ static CompilationUnit getParseUnit(File javaFile) {
CompilationUnit root = null;
try {
String txtCode = new String(Files.readAllBytes(javaFile.toPath()));
- if(!txtCode.startsWith("class")) txtCode = "class T { \n" + txtCode + "\n}"; // add tmp class to parse
- StaticJavaParser.getConfiguration().setAttributeComments(false); // remove comments
+ // add tmp class to parse
+ if(!txtCode.startsWith("class")) txtCode = "class T { \n" + txtCode + "\n}";
+ // remove comments
+ StaticJavaParser.getConfiguration().setAttributeComments(false);
+ // parse code
root = StaticJavaParser.parse(txtCode);
} catch (Exception ignore) {}
return root;
diff --git a/others/LoadJavaMethod/target/jar/LoadJavaMethod.jar b/others/LoadJavaMethod/target/jar/LoadJavaMethod.jar
index 9138efdd..e459a84a 100644
Binary files a/others/LoadJavaMethod/target/jar/LoadJavaMethod.jar and b/others/LoadJavaMethod/target/jar/LoadJavaMethod.jar differ
diff --git a/others/get_ast_nodes.py b/others/get_ast_nodes.py
new file mode 100644
index 00000000..dcb3f61c
--- /dev/null
+++ b/others/get_ast_nodes.py
@@ -0,0 +1,25 @@
+import subprocess
+from pathlib import Path
+
+JAR_JAVA_AST_DATA = "GetAstNodes/target/jar/GetAstNodes.jar"
+INNER_DELIMITER = " ___INNER___ "
+OUTER_DELIMITER = " ___OUTER___ "
+
+
+def get_ast_data(str_code):
+ cmd = ['java', '-jar', JAR_JAVA_AST_DATA, str_code]
+ content = subprocess.check_output(cmd, encoding="utf-8", close_fds=True)
+ [all_terminals, all_classes] = content.strip().split(OUTER_DELIMITER)
+ all_terminals = all_terminals.split(INNER_DELIMITER)
+ all_classes = all_classes.split(INNER_DELIMITER)
+ return all_terminals, all_classes
+
+
+if __name__ == '__main__':
+ program = Path('sample_input.java').read_text()
+ print("program:\n{}".format(program))
+ ast_terminals, ast_classes = get_ast_data(program)
+ print("ast_terminals = {}".format(ast_terminals))
+ print("ast_classes = {}".format(ast_classes))
+ ast_nodes = list(set(ast_terminals + ast_classes))
+ print("all_nodes = {}".format(ast_nodes))
diff --git a/others/get_tokens_java.py b/others/get_tokens_java.py
new file mode 100644
index 00000000..06080e2d
--- /dev/null
+++ b/others/get_tokens_java.py
@@ -0,0 +1,14 @@
+import javalang
+from pathlib import Path
+
+
+def get_tokens(str_code):
+ tokens = list(javalang.tokenizer.tokenize(str_code))
+ tokens = [token.value for token in tokens]
+ return tokens
+
+
+if __name__ == '__main__':
+ program = Path('sample_input.java').read_text()
+ print("program:\n{}".format(program))
+ print("tokens = {}".format(get_tokens(program)))
diff --git a/others/load_java_method.py b/others/load_java_method.py
new file mode 100644
index 00000000..bd350413
--- /dev/null
+++ b/others/load_java_method.py
@@ -0,0 +1,21 @@
+import subprocess
+from pathlib import Path
+
+JAR_LOAD_JAVA_METHOD = "LoadJavaMethod/target/jar/LoadJavaMethod.jar"
+
+
+def load_method(file_path):
+ cmd = ['java', '-jar', JAR_LOAD_JAVA_METHOD, file_path]
+ contents = subprocess.check_output(cmd, encoding="utf-8", close_fds=True)
+ contents = contents.split()
+ name, body = contents[0], " ".join(contents[1:])
+ return name, body
+
+
+if __name__ == '__main__':
+ input_path = 'sample_input.java'
+ program = Path(input_path).read_text()
+ print("program:\n{}".format(program))
+ method_name, single_line = load_method(input_path)
+ print("method_name = {}".format(method_name))
+ print("single_line = {}".format(single_line))
diff --git a/others/sample_input.java b/others/sample_input.java
new file mode 100644
index 00000000..d1bb277a
--- /dev/null
+++ b/others/sample_input.java
@@ -0,0 +1,3 @@
+void print() {
+ System.out.println("Hello World!");
+}