我从 Eclipse wiki (http://wiki.eclipse.org/JDT/FAQ#From_an_IJavaElement_to_its_declaring_ASTNode) 阅读了这篇文章,但我仍然无法从 IMethod 转换为其相应的 MethodDeclaration。
我有一个扩展点,它向 IMethod 对象添加了一个弹出菜单。拥有这个 IMethod 对象,我想用 ASTVisitor 访问它。
这是我尝试从 IMethod 转换为 MethodDeclaration 的方式
public static MethodDeclaration convertToAstNode(final IMethod method) throws JavaModelException
{
final ICompilationUnit compilationUnit = method.getCompilationUnit();
final ASTParser astParser = ASTParser.newParser( AST.JLS4 );
astParser.setSource( compilationUnit );
astParser.setKind( ASTParser.K_COMPILATION_UNIT );
astParser.setResolveBindings( true );
astParser.setBindingsRecovery( true );
final ASTNode rootNode = astParser.createAST( null );
final CompilationUnit compilationUnitNode = (CompilationUnit) rootNode;
final String key = method.getKey();
final ASTNode javaElement = compilationUnitNode.findDeclaringNode( key );
final MethodDeclaration methodDeclarationNode = (MethodDeclaration) javaElement;
return methodDeclarationNode;
}
我错过了什么?