I have selected a piece of code and I want to parse with AST with the purpose of identify methods in the selected code.
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);
}
As you can see, I have to change the type from SelectedText to char[] before parsing.
I am doing something wrong because the parser does not find any method.
What I am doing wrong?
javadoc for ASTParser.setResolveBindings() says: