├── .gitignore
├── JavaMethodExactor.jar
├── README.md
├── pom.xml
└── src
└── main
└── java
└── dreamcold
├── Main.java
├── parser
└── FileParser.java
├── task
└── Task.java
├── util
└── FileWriter.java
└── visiter
└── FunctionVisitor.java
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea/*
2 | JavaExtractor.iml
3 |
4 |
--------------------------------------------------------------------------------
/JavaMethodExactor.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kangyujian/JavaMethodExactor/b5aab600ba9b9b5111cc11d27b1f95ed5c88b1bd/JavaMethodExactor.jar
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # JavaExtractor
2 |
3 | ## About the project
4 |
5 | The project is a code extractor, whose function is to input the path of a java project and output the method information in the project.
6 | The method information includes the class name, method name, method body code, return value, parameter, etc. it is very convenient to use.
7 | I have packaged the project code into a jar package, which can be run according to the command to quickly extract the Java method information
8 |
9 | ## Environmental
10 |
11 | ```
12 | jDK 1.8
13 | Maven
14 | ```
15 | ## How to use
16 | ```
17 | # The first parameter is the project path, and the second parameter is the specific file to output to
18 | # example
19 | java -jar JavaMethodExactor.jar /home/javaproject ./ouput.txt
20 | ```
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.dreamcold.debug
8 | dreamcold
9 | 1.0-SNAPSHOT
10 |
11 |
12 |
13 |
14 | org.apache.maven.plugins
15 | maven-compiler-plugin
16 | 3.2
17 |
18 | 1.8
19 | 1.8
20 |
21 |
22 |
23 | org.apache.maven.plugins
24 | maven-assembly-plugin
25 | 2.5.5
26 |
27 |
28 |
29 | dreamcold.Main
30 |
31 |
32 |
33 | jar-with-dependencies
34 |
35 |
36 |
37 |
38 |
39 | make-assemble
40 | package
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 | com.github.javaparser
51 | javaparser-core
52 | 3.0.0-alpha.4
53 |
54 |
55 | commons-io
56 | commons-io
57 | 1.3.2
58 | compile
59 |
60 |
61 | com.fasterxml.jackson.core
62 | jackson-databind
63 | 2.9.3
64 |
65 |
66 | args4j
67 | args4j
68 | 2.33
69 |
70 |
71 | org.apache.commons
72 | commons-lang3
73 | 3.5
74 |
75 |
76 | mysql
77 | mysql-connector-java
78 | 5.1.6
79 |
80 |
81 |
82 |
83 |
--------------------------------------------------------------------------------
/src/main/java/dreamcold/Main.java:
--------------------------------------------------------------------------------
1 | package dreamcold;
2 |
3 |
4 |
5 | import dreamcold.task.Task;
6 | import dreamcold.util.FileWriter;
7 |
8 | import java.io.File;
9 | import java.io.IOException;
10 | import java.nio.file.Files;
11 | import java.nio.file.Paths;
12 | import java.util.LinkedList;
13 | import java.util.concurrent.Executors;
14 | import java.util.concurrent.ThreadPoolExecutor;
15 |
16 | public class Main {
17 |
18 | private static String dir=null;
19 |
20 | private static String outputdir=null;
21 |
22 | private static Integer numThreads=4;
23 |
24 |
25 |
26 |
27 |
28 | public static void main(String[] args){
29 | if (args==null||args.length!=2){
30 | System.out.println("参数数量不正确");
31 | System.out.println("请正常输入参数:要提取的Java的项目的路径,要输出到的文件的路径");
32 | System.exit(0);
33 | }
34 | dir=args[0];
35 | outputdir=args[1];
36 | FileWriter.outputPath=outputdir;
37 | travelDir();
38 |
39 | }
40 |
41 |
42 | private static void travelDir(){
43 | if (dir != null) {
44 | File root = new File(dir);
45 | if(root.exists() && root.isDirectory()) {
46 | File[] projs = root.listFiles();
47 | int cnt=0;
48 | int all=projs.length;
49 | for(File proj : projs) {
50 | cnt++;
51 | dir = proj.getPath();
52 |
53 | extractDir();
54 | System.out.printf("all is %d , now is the %d",all,cnt);
55 | }
56 | }
57 | }
58 | }
59 |
60 |
61 | private static void extractDir() {
62 | ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(numThreads);
63 | LinkedList tasks = new LinkedList();
64 | try {
65 | Files.walk(Paths.get(dir)).filter(Files::isRegularFile)
66 | .filter(p -> p.toString().toLowerCase().endsWith(".java")).forEach(f -> {
67 | Task task = new Task(f);
68 | tasks.add(task);
69 | System.out.println("解析 "+f.getFileName()+"...");
70 | });
71 | } catch (IOException e) {
72 | e.printStackTrace();
73 | return;
74 | }
75 | try {
76 | executor.invokeAll(tasks);
77 | } catch (InterruptedException e) {
78 | e.printStackTrace();
79 | } finally {
80 | executor.shutdown();
81 | }
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/src/main/java/dreamcold/parser/FileParser.java:
--------------------------------------------------------------------------------
1 | package dreamcold.parser;
2 |
3 |
4 | import com.github.javaparser.JavaParser;
5 | import com.github.javaparser.ParseException;
6 | import com.github.javaparser.ParseProblemException;
7 | import com.github.javaparser.ast.CompilationUnit;
8 | import com.github.javaparser.ast.body.MethodDeclaration;
9 | import com.github.javaparser.ast.body.Parameter;
10 | import dreamcold.util.FileWriter;
11 | import dreamcold.visiter.FunctionVisitor;
12 |
13 | import java.io.IOException;
14 | import java.util.ArrayList;
15 | import java.util.List;
16 |
17 | public class FileParser {
18 |
19 | private String code;
20 | private CompilationUnit m_CompilationUnit;
21 | private String className;
22 |
23 | public FileParser(String code) {
24 | this.code = code;
25 | }
26 |
27 | public FileParser(String code, String className) {
28 | this.code = code;
29 | this.className = className;
30 | }
31 |
32 | public CompilationUnit getParsedFile() {
33 | return m_CompilationUnit;
34 | }
35 |
36 | public void extractFeatures() throws ParseException, IOException {
37 | m_CompilationUnit = parseFileWithRetries(code);
38 |
39 | FunctionVisitor functionVisitor = new FunctionVisitor();
40 |
41 | functionVisitor.visit(m_CompilationUnit, null);
42 | ArrayList nodes = functionVisitor.getMethodDeclarations();
43 | for (int i=0;i parameters = methodDeclaration.getParameters();
46 | StringBuilder sb=new StringBuilder();
47 | for (Parameter parameter:parameters){
48 | sb.append(parameter.getType().toString()).append(" ");
49 | }
50 | sb.delete(sb.length() - 2, sb.length());
51 | String classNameStr=className;
52 | String parametersStr=sb.toString();
53 | String returnTypeStr=methodDeclaration.getType().toString();
54 | String bodyContentStr=methodDeclaration.getBody().toString().replaceAll("\r|\n","");
55 | String methodNameStr=methodDeclaration.getName();
56 | sb=new StringBuilder();
57 | sb.append(className);
58 | sb.append("@");
59 | sb.append(returnTypeStr);
60 | sb.append("@");
61 | sb.append(parametersStr);
62 | sb.append("@");
63 | sb.append(bodyContentStr);
64 | sb.append("@");
65 | sb.append(methodNameStr);
66 | sb.append("\n");
67 | String line=sb.toString();
68 | FileWriter.wirteContent(line);
69 | }
70 |
71 | }
72 |
73 | public CompilationUnit parseFileWithRetries(String code) throws IOException {
74 | final String classPrefix = "public class Test {";
75 | final String classSuffix = "}";
76 | final String methodPrefix = "SomeUnknownReturnType f() {";
77 | final String methodSuffix = "return noSuchReturnValue; }";
78 |
79 | String originalContent = code;
80 | String content = originalContent;
81 | CompilationUnit parsed = null;
82 | try {
83 | parsed = JavaParser.parse(content);
84 | } catch (ParseProblemException e1) {
85 | // Wrap with a class and method
86 | try {
87 | content = classPrefix + methodPrefix + originalContent + methodSuffix + classSuffix;
88 | parsed = JavaParser.parse(content);
89 | } catch (ParseProblemException e2) {
90 | // Wrap with a class only
91 | content = classPrefix + originalContent + classSuffix;
92 | parsed = JavaParser.parse(content);
93 | }
94 | }
95 |
96 | return parsed;
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/src/main/java/dreamcold/task/Task.java:
--------------------------------------------------------------------------------
1 | package dreamcold.task;
2 |
3 |
4 | import com.github.javaparser.ParseException;
5 | import dreamcold.parser.FileParser;
6 |
7 | import java.io.IOException;
8 |
9 | import java.nio.file.Files;
10 | import java.nio.file.Path;
11 | import java.util.concurrent.Callable;
12 |
13 | public class Task implements Callable {
14 | String code = null;
15 | String className=null;
16 |
17 |
18 | public Task(Path path) {
19 | try {
20 | this.code = new String(Files.readAllBytes(path));
21 | String fileName=path.getFileName().toString();
22 | fileName=fileName.substring(0,fileName.length()-5);
23 | this.className=fileName;
24 | } catch (IOException|StringIndexOutOfBoundsException e) {
25 | e.printStackTrace();
26 | this.code = "";
27 | this.className="";
28 | }
29 | }
30 |
31 | @Override
32 | public Void call() throws Exception {
33 | try {
34 | FileParser featureExtractor = new FileParser(code,className);
35 | featureExtractor.extractFeatures();
36 | } catch (ParseException | IOException e) {
37 | e.printStackTrace();
38 | }
39 | return null;
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/dreamcold/util/FileWriter.java:
--------------------------------------------------------------------------------
1 | package dreamcold.util;
2 |
3 | import java.io.File;
4 | import java.io.IOException;
5 |
6 | public class FileWriter {
7 |
8 | public static String outputPath;
9 |
10 | public static void wirteContent(String content){
11 | wirteContent(content,outputPath);
12 | }
13 |
14 |
15 | public static void wirteContent(String content,String path){
16 | java.io.FileWriter fw=null;
17 | try {
18 | File file=new File(path);
19 | fw=new java.io.FileWriter(file,true);
20 | fw.write(content);
21 | }catch (IOException e){
22 | e.printStackTrace();
23 | }finally {
24 | try {
25 | if (fw!=null){
26 | fw.close();
27 | }
28 | } catch (IOException e) {
29 | e.printStackTrace();
30 | }
31 | }
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/dreamcold/visiter/FunctionVisitor.java:
--------------------------------------------------------------------------------
1 | package dreamcold.visiter;
2 |
3 | import com.github.javaparser.ast.body.MethodDeclaration;
4 | import com.github.javaparser.ast.expr.AnnotationExpr;
5 | import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
6 |
7 | import java.util.ArrayList;
8 |
9 | public class FunctionVisitor extends VoidVisitorAdapter