我选择了一段代码,我想用 AST 进行解析,目的是识别所选代码中的方法。
public void run(IAction action) {
SelectedText selectedText;
IEditorPart editor = getActiveEditor();
if (editor instanceof AbstractTextEditor) {
selectedText = getSelectedText(editor);
creteAST(selectedText);
}
}
private void creteAST(SelectedText selectedText) {
CompilationUnit parse = parse(selectedText);
MethodVisitor visitor = new MethodVisitor();
parse.accept(visitor);
System.out.println("Printing methods from the selected code");
for (MethodDeclaration method : visitor.getMethods()) {
System.out.println("Method name: " + method.getName()+ ". Return type: " + method.getReturnType2());
System.out.println(method);
}
}
private static CompilationUnit parse(SelectedText selectedText) {
String s_text = selectedText.getSelectedText();
char[] c_text = s_text.toCharArray();
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(c_text);
parser.setResolveBindings(true);
return (CompilationUnit) parser.createAST(null);
}
如您所见,我必须在解析之前将类型从SelectedText
更改为。char[]
我做错了什么,因为解析器没有找到任何方法。
我做错了什么?