Eclipse plugin 从Eclipse插件向方法添加注释

Eclipse plugin 从Eclipse插件向方法添加注释,eclipse-plugin,refactoring,eclipse-jdt,Eclipse Plugin,Refactoring,Eclipse Jdt,我正在尝试从Eclipse插件向方法添加注释。我可以访问表示特定方法的IJavaElement 使用EclipseJDT解决这个问题的最佳方法是什么?是的,JDT是解决这个问题的好方法 下面的代码将告诉您如何实现相同的功能。 (注:代码未经测试或编译) 谢谢你。我使用了EclipseLuna提供的简单API。 if (javaElement instanceof IMethod) { // Get the compilation unit for traversing AST

我正在尝试从Eclipse插件向方法添加注释。我可以访问表示特定方法的
IJavaElement


使用EclipseJDT解决这个问题的最佳方法是什么?

是的,JDT是解决这个问题的好方法

下面的代码将告诉您如何实现相同的功能。
(注:代码未经测试或编译)

谢谢你。我使用了EclipseLuna提供的简单API。
if (javaElement instanceof IMethod) {

    // Get the compilation unit for traversing AST
    final ASTParser parser = ASTParser.newParser(AST.JLS4);
    parser.setSource(javaElement.getCompilationUnit());
    parser.setResolveBindings(true);

    final CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null);

    // Record modification - to be later written with ASTRewrite
    compilationUnit.recordModifications();

    // Get AST node for IMethod
    int methodIndex = javaElement.getCompilationUnit().getSource().indexOf(javaElement.getSource());

    ASTNode methodASTNode = NodeFinder.perform(compilationUnit.getRoot(), methodIndex, javaElement.getSource().length());

    // Create the annotation
    final NormalAnnotation newNormalAnnotation = methodASTNode.getAST().newNormalAnnotation();
    newNormalAnnotation.setTypeName(methodASTNode.getAST().newName("AnnotationTest"));

   // Add logic for writing the AST here.

}