简体   繁体   English

反思和延伸的阶级

[英]Reflection and class that extends

I have a problem with using Reflection in Java. 我在Java中使用Reflection时遇到问题。 This is my SubCommandExecutor class, a class that handles all the commands sent: 这是我的SubCommandExecutor类,该类处理所有发送的命令:

public class SubCommandExecutor implements CommandExecutor{

    @Override
    public final boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

        if (args.length > 0){       

            Method[] meth = this.getClass().getMethods();
            for (int i = 0; i < meth.length; i++){
                 SubCommand sub = meth[i].getAnnotation(SubCommand.class);
                 if (sub != null){

                     // some reflaction staff

                 }    
            }

        } else {
             // ...
        }
    }
}

Everytime that a command is executed, the onCommand method is called. 每次执行命令时,都会调用onCommand方法。 In the onCommand method I would like to loop through all the class methods to find if there is any method with the SubCommand annotation. 在onCommand方法中,我想遍历所有类方法以查找是否有带有SubCommand批注的方法。

I even create a TestCommand class that extends SubCommandExecutor: 我什至创建一个扩展SubCommandExecutor的TestCommand类:

public class TestCommand extends SubCommandExecutor {

    @SubCommand(command="a")
    private boolean cmdA(CommandSender sender){
        // ...
    }

    @SubCommand(command="b")
    private boolean cmdB(CommandSender sender, String[] args){
        // ...
    }

    @SubCommand(command="c")
    private boolean cmdC(CommandSender sender, String[] args){
        // ...
    }

}

The problem is that where I call the onCommand method of the TestCommand class (inherited by the SubCommandExecutor), it loops only through the methods of the SubCommandExecutor and not throught the methods of TextCommand. 问题是,在我调用TestCommand类的onCommand方法(由SubCommandExecutor继承)的地方,它仅通过SubCommandExecutor的方法循环,​​而没有通过TextCommand的方法循环。

There is any way to fix this problem? 有什么办法可以解决这个问题? Thank you very much. 非常感谢你。

Methods in TestCommand class are private but in TestCommand类中的方法是private但在

Method[] meth = this.getClass().getMethods();

getMethods() can return only public ones (including inherited ones). getMethods()只能返回public的(包括继承的)。

If you want to use methods declared in TestCommand use getDeclaredMethods() . 如果要使用在TestCommand声明的方法, TestCommand使用getDeclaredMethods()
Other option is to change your annotated methods to public . 另一种选择是将带注释的方法更改为public

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Java反射检查ParameterizedType参数是否扩展了特定的类 - How to use Java reflection to check if ParameterizedType argument extends a specific class 如何使用Java反射从字段中获取扩展类的匿名实例? - How to get an anonymous instance that extends a class from a field with java reflection? 反思java? 扩展 - reflection java ? extends 如何动态创建一个使用反射扩展另一个动态类的类? - How can I create a class dynamically that extends another dynamic class using reflection? 如何使用Java反射来检查给定的类是实现Iterable <? extends T> ,对于任何给定的T - How to use Java reflection to check a given class is implements Iterable<? extends T>, for any given T 可以使用反射来获得具体的实现类型(例如Class <? extends MyInterface> )? - Can reflection be used to get a concrete implementing type (e.g. Class<? extends MyInterface>)? 如何使用Java中的反射查找在类中使用其他接口扩展其他类的所有接口 - How to find all interfaces in class that extends other classes with other interfaces using reflection in java 将扩展JFrame的类更改为扩展JPanel的类 - Change class that extends JFrame to class that extends JPanel 创建一个扩展keyListener的类 - Creating a class that extends keyListener 如何扩展类两次? - How to extends class twice?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM