简体   繁体   English

将 java.lang.reflect.Method object 传递给接口

[英]Pass a java.lang.reflect.Method object to an interface

I'm trying to pass a java.lang.reflect.Method object to an interface.我正在尝试将 java.lang.reflect.Method object 传递给接口。 The problem is I don't know what is the best way to cast and how should I do it.问题是我不知道什么是最好的投射方式以及我应该怎么做。

Look at example看例子

interface IHandler<T> {
    void handle(T param);
}

interface IContentHandler {
    void process();
}

interface IManageContentHandler {
    IManageContentHandler handler(IHandler<IContentHandler> param);
    IManageContentHandler next();
}

class Foo implements IContentHandler {
    @Override
    public void process() {

    }
}

class Bar implements IManageContentHandler {
    @Override
    public IManageContentHandler handler(IHandler<IContentHandler> param) {
        return this;
    }

    @Override
    public IManageContentHandler next() {
        System.out.println("Next");
        return this;
    }
}

class Job {

    public static void doSomething01(IContentHandler foo) {
        foo.process();
        System.out.println("process 01");
    }

    public static void doSomething02(IContentHandler foo) {
        foo.process();
        System.out.println("process 02");
    }

    public static void doSomething03(IContentHandler foo) {
        foo.process();
        System.out.println("process 03");
    }

}

Now I want to pass a method to the handler.现在我想将一个方法传递给处理程序。 this code works fine.此代码工作正常。

public class App {

    public static void main(String[] args) {

        Bar bar = new Bar();    
        
        bar.handler(Job::doSomething01);

    }
}

But the problem is I don't know how many methods will be, and I prefer to do it dynamically So I tried another method.但问题是我不知道会有多少方法,而且我更喜欢动态地做所以我尝试了另一种方法。

public class App {

    public static void main(String[] args) {
    
        Bar bar = new Bar();
        Class cls = Job.class;
        Method[] methods = cls.getDeclaredMethods();
        
        for (Method item : methods) {
            // this one is not true
            // "item" is a Method type but I must pass a IHandler<IContentHandler>
            bar.handler(item);
        }

    }
}

the method handler() accepts an instance of interface IHandler<IContentHandler> as its argument, so you can not pass an instance of Method to it ( Method is a completely different class).方法handler()接受接口IHandler<IContentHandler>的实例作为其参数,因此您不能将Method的实例传递给它( Method是一个完全不同的类)。

if you insist on passing all static methods of the class Job you can use lambda closure:如果您坚持通过 class Job的所有 static 方法,您可以使用 lambda 闭包:

    for (Method item : methods) {
        //** WARNING **: you MUST check each item to be sure that its argument and return type are what you want!
        bar.handler(param -> {
            try {
                item.invoke(null, param);
            } catch (IllegalAccessException | InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        });
    }

but this does not seem like a good design.但这似乎不是一个好的设计。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM