【问题标题】:How can I test if a Method will accept a parameter type?如何测试方法是否接受参数类型?
【发布时间】:2015-02-08 03:47:33
【问题描述】:

假设我有以下代码...

@FunctionalInterface
static interface MessageFunction<T> {
    void send(T obj);
}

static @interface Message {
    Class<?> value();
}

static class Foo {

    @Message(String.class)
    MessageFunction<String> bass = (string) -> { 
        // Do Stuff
    };
}

static class MessageManager {

    Map<Class<?>, MessageFunction<?>> messages = new HashMap<>();

    public void register(Object obj) {

        for (Field field : obj.getClass().getDeclaredFields()) {
            Message message = field.getAnnotation(Message.class);
            if (message != null) {
                MessageFunction<?> function;

                try {
                    function = (MessageFunction<?>) field.get(obj);
                } catch (IllegalArgumentException | IllegalAccessException e) {
                    e.printStackTrace();
                    return;
                }

                Method sendMethod;

                try {
                    // Will this work?
                    sendMethod = function.getClass().getDeclaredMethod("send", Object.class);
                } catch (NoSuchMethodException | SecurityException e) {
                    e.printStackTrace();
                    return;
                }

                // How do I do something like this?
                /*if (sendMethod.testParamaters(message.value())) {
                    this.messages.put(message.value(), function);
                }*/
            }
        }
    }
}

public static void main(String[] args) {
    MessageManager manager = new MessageManager();
    manager.register(new Foo());
}

我正在反映一个引用泛型类型@FunctionalInterface 的字段。因为方法参数也是通用的,所以我无法知道它接受什么参数,因此我必须通过其他方式(注解)传递它。

问题是有注解值和泛型类型不必匹配,似乎没有办法检查。如果注释中列出的类型不被 send 方法接受,我不希望它注册失败。

如果不实际调用该方法,我将如何处理这件事。有办法吗?更好的是,虽然我知道这很可能是不可能的,但有没有办法在没有注释的情况下知道参数类型是什么?

【问题讨论】:

  • Get generic type of class at runtime 的可能重复项。对于 lambda,基本上答案是“无法完成”。
  • 我知道有关在运行时获取泛型类型的其他问题的答案。然而,在处理通用功能接口的 lambda 时,这些解决方案似乎不起作用
  • 我发现TypeTools 提到的是关于堆栈溢出的另一个问题。虽然它没有直接解决我提出的问题,但它解决了我的特定问题。

标签: java generics reflection lambda


【解决方案1】:

试试TypeTools。首先,您需要获得对 bass 的声明类的引用,这是您在 register 方法中获得的:

Class<?> bassClass = field.getDeclaringClass();

然后您可以使用 TypeTools 来解析它为 MessageFunction 提供的类型参数 T

Class<?> msgType = TypeResolver.resolveRawArgument(MessageFunction.class, bassClass);

【讨论】:

  • 是的,这解决了我的问题。我确实在您发帖前不久找到了该库的链接,但是感谢您提供了有关如何使用它与我的示例代码相关的示例。
【解决方案2】:

以下只是一个建议,我在我的项目中使用过。但这不是该问题的完美解决方案。也许您可以下载 GenericHibernateDao 框架的源代码并查看方法“getTypeArguments”的源代码。我觉得太酷了!。

// get a class object for your entity
Class clazz = ...
Type type = clazz.getGenericSuperclass();
        if (type instanceof ParameterizedType) {
            Type trueType = ((ParameterizedType)type).getActualTypeArguments()[0];
            Class modelClass = (Class) trueType;
            // Now you can creat an Instance in you generic parameterType
            Object entity  = modelClass.forInstance();
        } 

【讨论】:

  • 我在堆栈溢出时发现了类似于这个 else ware 的东西,但是它不起作用。我认为这是因为所讨论的类不是声明的类,而是 lambda。因此,它不能转换为 ParameterizedType。这只是根据我的观察的猜测,我其实不知道。
【解决方案3】:

我在我的一些代码中做了类似的事情这是一个 sn-p。

Method[] meths = actionClass.getMethods();
                for (Method meth : meths) {
                    Class<?>[] pTypes = meth.getParameterTypes();

                    /*
                     * Filter out all methods that do not meet correct
                     * signature. The correct signature for an action method
                     * is: String actionName(HttpServletRequest request)
                     */
                    //...check for the correct number of params and the correct param type
                    if (pTypes.length != 1 || !HttpServletRequest.class.toString().equals(pTypes[0].toString())) {
                        continue;
                    } else {
                        //...check for return type
                        if (!String.class.toString().equals(meth.getReturnType().toString())) {
                            continue;
                        }
                    }

                    //If you make it here than that means the method 
                    //meets the requirements to be a full fledged action.
                    //...
                }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多