-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodParser.java
More file actions
37 lines (25 loc) · 1.04 KB
/
Copy pathMethodParser.java
File metadata and controls
37 lines (25 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package paToolkit4Java.parser;
import org.eclipse.jdt.core.dom.*;
import java.util.ArrayList;
import java.util.List;
public class MethodParser {
public static MethodDeclaration parseMethodStr(String methodStr) {
String sourceStart = "public class A {";
//add a fake class A as a shell, to meet the requirement of ASTParser
String sourceEnd = "}";
String source = sourceStart + methodStr + sourceEnd;
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(source.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
List<MethodDeclaration> md = new ArrayList<MethodDeclaration>();
cu.accept(new ASTVisitor() {
//by add more visit method like the following below, then all king of statement can be visited.
public boolean visit(MethodDeclaration node) {
md.add(node);
return false;
}
});
return md.get(0);
}
}