泛型为方法返回类型 [英] Generic as method return type

查看:100
本文介绍了泛型为方法返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环视了一下StackOverflow,找到了我面临的问题的答案。我遇到了很多很好的答案,但仍然没有回答我的问题。



如何找到返回类型的参数化类型通过检查?



Java泛型:获得泛型方法返回类型的类



http://qussay.com/2013/09/28/handling-java-generic-types-with-反射/



HTTP:// gafter .blogspot.com / search?q = super + type + token

所以这就是我想要做的。
使用反射,我想获得所有方法和它的返回类型(非泛型)。
我一直在使用 Introspector.getBeanInfo 来做到这一点。然而,当我遇到返回类型未知的方法时,我遇到了限制。

  public class Foo {

公共字符串名称;

public String getName(){
return name;
}

public void setName(final String name){
this.name = name;
}
}

public class Bar< T> {

T对象;

public T getObject(){
return object;
}

public void setObject(final T object){
this.object = object;


$ b @Test
public void testFooBar()抛出NoSuchMethodException,SecurityException,IllegalAccessException,
IllegalArgumentException,InvocationTargetException {

Foo foo = new Foo();
Bar< Foo> bar = new Bar< Foo>();
bar.setObject(foo);
方法mRead = bar.getClass()。getMethod(getObject,null);

System.out.println(Foo.class); // Foo
System.out.println(foo.getClass()); // Foo
System.out。 println(Bar.class); // Bar
System.out.println(bar.getClass()); // Bar
System.out.println(mRead.getReturnType()); // java.lang.Object
System.out.println(mRead.getGenericReturnType()); // T
System.out.println(mRead.getGenericReturnType()); // T
System.out.println(mRead.invoke(bar,null).getClass()); // Foo
}

如何知道方法返回类型 T 是否为泛型?
我没有奢望在运行时拥有一个对象。
我正在试验Google TypeToken 或者使用抽象类来获取类型信息。
我想为 getObject 关联 T Foo $ c $> $ Bar object。



有些人认为java不保留泛型信息。在这种情况下,为什么第一次投射和第二次投射不会。


bar.setObject((Foo)fooObject); //这个工程
Object object = 12;
bar.setObject((Foo)object); //引发铸造错误

任何帮助都是值得赞赏的。

解决方案

  Bar< Foo> bar = new Bar< Foo>(); 
方法mRead = bar.getClass()。getMethod(getObject,null);
TypeToken< Bar< Foo>> tt = new TypeToken< Test.Bar< Foo>>(){};
Invokable< Bar< Foo>,Object> inv = tt.method(mRead);
System.out.println(inv.getReturnType()); // Test $ Foo

也许这就是您要搜索的内容。
TypeToken和Invokable来自Google Guava。



€:修复了关于@PaulBellora的注释的代码


I have looked around on StackOverflow to find the answer for the problem I am facing. I came across many good answers but still it doesn't answer my question.

Get type of a generic parameter in Java with reflection

How to find the parameterized type of the return type through inspection?

Java generics: get class of generic method's return type

http://qussay.com/2013/09/28/handling-java-generic-types-with-reflection/

http://gafter.blogspot.com/search?q=super+type+token

So here is what I want to do. Using Reflection, I want to get all methods and its return type (non-generic). I have been using Introspector.getBeanInfo to do so. However I hit the limitation when I run into a method where return type is unknown.

public class Foo {

    public String name;

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }
}

public class Bar<T> {

    T object;

    public T getObject() {
        return object;
    }

    public void setObject(final T object) {
        this.object = object;
    }
}

@Test
    public void testFooBar() throws NoSuchMethodException, SecurityException, IllegalAccessException,
            IllegalArgumentException, InvocationTargetException {

        Foo foo = new Foo();
        Bar<Foo> bar = new Bar<Foo>();
        bar.setObject(foo);
        Method mRead = bar.getClass().getMethod("getObject", null);

        System.out.println(Foo.class);// Foo
        System.out.println(foo.getClass());// Foo
        System.out.println(Bar.class);// Bar
        System.out.println(bar.getClass());// Bar
        System.out.println(mRead.getReturnType()); // java.lang.Object
        System.out.println(mRead.getGenericReturnType());// T
        System.out.println(mRead.getGenericReturnType());// T
        System.out.println(mRead.invoke(bar, null).getClass());// Foo
    }

How do I know if the method return type T is generic or not? I don't have a luxury to have an object at runtime. I am experimenting with Google TypeToken or use an abstract class to get type information. I want to associate T to Foo for the getObject method for Bar<Foo> object.

Some have argued that java doesn't preserve the generic information. In that case why first casting works and second casting doesn't.

Object fooObject = new Foo();
bar.setObject((Foo) fooObject); //This works
Object object = 12;
bar.setObject((Foo) object); //This throws casting error

Any help is appreciated.

解决方案

Bar<Foo> bar = new Bar<Foo>();
Method mRead = bar.getClass().getMethod( "getObject", null );
TypeToken<Bar<Foo>> tt = new TypeToken<Test.Bar<Foo>>() {};
Invokable<Bar<Foo>, Object> inv = tt.method( mRead );
System.out.println( inv.getReturnType() ); // Test$Foo

Maybe this is what you are searching for. TypeToken and Invokable are from Google Guava.

€: Fixed the code with respect to the comment of @PaulBellora

这篇关于泛型为方法返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆