简体   繁体   English

如何检查方法是否返回Collection <Foo> 在Java?

[英]How to check that a method returns Collection<Foo> in Java?

I wish to a check if a method exists in an interface based on its signatures. 我希望检查接口中是否存在基于其签名的方法。

The signature that the method should have is: 该方法应具有的签名是:

Collection<Foo> methodName(Spam arg0, Eggs arg1, ...)

I can find the methods via Class.getMethods() then find the name, parameters and return type respectively with method.getName() , method.getParameterTypes() and method.getReturnType() . 我可以通过Class.getMethods()找到方法,然后分别使用method.getName()method.getParameterTypes()method.getReturnType()查找名称,参数和返回类型。

But what do I compare the return type to in order to ensure that only methods that return Collection<Foo> are chosen, and not other collections? 但是,为了确保只选择返回Collection<Foo>方法而不选择其他集合,我应该如何比较返回类型?

method.getReturnType().equals(Collection.class) 

Since the above will be true for all methods that return a collection, not just for those that return a Foo Collection. 由于上述内容对于返回集合的所有方法都是如此,而不仅仅是那些返回Foo集合的方法。

There is a method named public Type getGenericReturnType() which can return (if it's the case) a ParameterizedType . 有一个名为public Type getGenericReturnType() ,它可以返回一个ParameterizedType (如果是这种情况)。

A ParameterizedType can give you more informations on a generic type such as Collection<Foo> . ParameterizedType可以为您提供有关泛型类型(如Collection<Foo>更多信息。

In particular with the getActualTypeArguments() method you can get the actual type for each parameter. 特别是使用getActualTypeArguments()方法,您可以获得每个参数的实际类型。

Here, ParameterizedType represents Collection and getActualTypeArguments() represents an array containing Foo 这里, ParameterizedType表示Collection, getActualTypeArguments()表示包含Foo的数组

You can try this to list the parameters of your generic type : 您可以尝试这样列出泛型类型的参数:

Type returnType = method.getGenericReturnType();
if (returnType instanceof ParameterizedType) {
    ParameterizedType type = (ParameterizedType) returnType;
    Type[] typeArguments = type.getActualTypeArguments();
    for (Type typeArgument : typeArguments) {
        Class typeArgClass = (Class) typeArgument;
        System.out.println("typeArgClass = " + typeArgClass);
    }
}

Sources : http://tutorials.jenkov.com/ 资料来源: http //tutorials.jenkov.com/

通用类型参数在Java中不会保留(即,它只是一个编译时功能),因此您无法执行此操作。

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

相关问题 如何检查java.lang.reflect.Method返回类型是Collection? - How to check that java.lang.reflect.Method return type is Collection? 实现 Java 方法,该方法返回 Kotlin 中的集合 - Implement Java method that returns Collection in Kotlin 如何在Java的IF语句中检查方法是返回true还是false? - How do I check if a method returns true or false in an IF statement in Java? 如何检查 java 方法是否返回 null 而无需运行两次 - How to check if a java method returns null without having to run it twice 在Java中,我想以“ foo”的名称调用对象上的方法,但是“ foo”是字符串。 我该如何解决? - In Java, I want to call a method on an object by the name of 'foo', but 'foo' is a string. How do I get around this? Java中的HashMap.values()方法返回的集合是什么? - What is the collection that HashMap.values() method in Java returns? 如何检查Object是否是Java中的集合类型? - How to check if an Object is a Collection Type in Java? 如何使用java Stream检查Collection是否为空 - How to check if Collection is not empty using java Stream Java库方法,用于在集合中查找项目并检查是否完全匹配 - Java library method to find an item in a collection AND check if there was exactly one match 如何检查方法是否返回空值? - How to check whether a method returns a null?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM