如何确定 Eclipse JDT 中方法或字段的修饰符?

发布于 2024-12-28 11:43:39 字数 1449 浏览 3 评论 0原文

我正在为 Eclipse JDT 编写一些简单的 AST 访问者。我有一个 MethodVisitorFieldVisitor 类,它们各自扩展了 ASTVisitor。以MethodVisitor 为例。在该类的 Visit 方法(这是一个重写)中,我能够找到每个 MethodDeclaration 节点。当我有这些节点之一时,我想查看它的修饰符,看看它是公共的还是私有的(也许还有其他修饰符,例如出色地)。有一个名为 getModifiers() 的方法,但我不清楚如何使用它来确定应用于特定 MethodDeclaration 的修饰符类型。我的代码发布在下面,如果您有任何想法,请告诉我如何继续。

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.MethodDeclaration;

public class MethodVisitor extends ASTVisitor {

    private List<MethodDeclaration> methods;

    // Constructor(s)
    public MethodVisitor() {
        this.methods = new ArrayList<MethodDeclaration>();
    }

    /**
     * visit - this overrides the ASTVisitor's visit and allows this
     * class to visit MethodDeclaration nodes in the AST.
     */
    @Override
    public boolean visit(MethodDeclaration node) {
        this.methods.add(node);

            //*** Not sure what to do at this point ***
            int mods = node.getModifiers();

        return super.visit(node);
    }

    /**
     * getMethods - this is an accessor methods to get the methods
     * visited by this class.
     * @return List<MethodDeclaration>
     */
    public List<MethodDeclaration> getMethods() {
        return this.methods;
    }
}

I am writing some simple AST visitors for the Eclipse JDT. I have a MethodVisitor and FieldVisitor class which each extend the ASTVisitor. Take the MethodVisitor for instance. In that class' Visit method (which is an override), I am able to find each of the MethodDeclaration nodes. When I have one of those nodes, I want to look at its Modifiers to see whether it is public or private (and perhaps other modifiers as well). There is a method called getModifiers(), but it is unclear to me how to use this to determine the type of modifier applied to the particular MethodDeclaration. My code is posted below, please let me know if you have any ideas how to proceed.

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.MethodDeclaration;

public class MethodVisitor extends ASTVisitor {

    private List<MethodDeclaration> methods;

    // Constructor(s)
    public MethodVisitor() {
        this.methods = new ArrayList<MethodDeclaration>();
    }

    /**
     * visit - this overrides the ASTVisitor's visit and allows this
     * class to visit MethodDeclaration nodes in the AST.
     */
    @Override
    public boolean visit(MethodDeclaration node) {
        this.methods.add(node);

            //*** Not sure what to do at this point ***
            int mods = node.getModifiers();

        return super.visit(node);
    }

    /**
     * getMethods - this is an accessor methods to get the methods
     * visited by this class.
     * @return List<MethodDeclaration>
     */
    public List<MethodDeclaration> getMethods() {
        return this.methods;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖风昔人 2025-01-04 11:43:39

正如文档所述,调用 getModifiers() 的结果是相关 修饰符 常量。例如,如果您想查明该方法是否为 final,您可以使用:

int modifiers = node.getModifiers();
if (modifiers & Modifier.FINAL != 0) {
    // It's final
}

或者您可以使用 Modifier 类中的便捷方法:

int modifiers = node.getModifiers();
if (Modifier.isFinal(modifiers)) {
    // It's final
}

As the documentation states, the result of calling getModifiers() is a bit-wise "or" of the relevant Modifier constants. So for example, if you want to find out whether or not the method is final you'd use:

int modifiers = node.getModifiers();
if (modifiers & Modifier.FINAL != 0) {
    // It's final
}

Or you can use the convenience methods in the Modifier class:

int modifiers = node.getModifiers();
if (Modifier.isFinal(modifiers)) {
    // It's final
}
泅人 2025-01-04 11:43:39

还有一个辅助方法 modifiers() 可以为您提供您的方法所具有的修饰符列表。要检查它是否是final,您可以直接检查该列表。

for(Object o : methodDeclarationNode.modifiers()) {  
   if(o.toString().equals("final")) {
      return true;
   }
}

There is one more helper method modifiers() which gives you the list of modifiers your method has. To check whether it is final or not, you can directly check in that list.

for(Object o : methodDeclarationNode.modifiers()) {  
   if(o.toString().equals("final")) {
      return true;
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文