java 如何获取泛型方法参数的类型参数类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14953359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:00:08  来源:igfitidea点击:

How to get type arguments class of generic method parameter?

javagenerics

提问by Adelin

How to get the type argument of an argument passed to a method ? For example I have

如何获取传递给方法的参数的类型参数?例如我有

List<Person> list = new ArrayList<Person>(); 

public class Datastore {

  public <T> void insert(List<T> tList) {
     // when I pass the previous list to this method I want to get Person.class ; 
  }
} 

回答by Denis Rosca

Due to type erasure, the only way you can do it is if you pass the type as an argument to the method.

由于类型擦除,唯一的方法是将类型作为参数传递给方法。

If you have access to the Datastore code and can modify you can try to do this:

如果您有权访问 Datastore 代码并且可以修改,则可以尝试执行以下操作:

public class Datastore {
    public T void insert(List<T> tList, Class<T> objectClass) {
    }
}

and then call it by doing

然后通过做来调用它

List<Person> pList = new ArrayList<Person>();
...
dataStore.insert(pList, Person.class);

Every response I've seen to this type of question was to send the class as a parameter to the method.

我看到的对此类问题的每个响应都是将类作为参数发送给方法。

回答by Ajay George

Due to Type Erasure I doubt whether we can get it, barring some reflection magic which might be able to do it.

由于类型擦除,我怀疑我们是否可以得到它,除非一些反射魔法可能能够做到。

But if there are elements inside the list, we can reach out to them and invoke getClass on them.

但是如果列表中有元素,我们可以联系它们并调用它们的 getClass。

回答by Apurv

You can try this ...

你可以试试这个...

if(tList != null && tList.size() > 0){
    Class c = tList.get(0).getClass();
}

回答by Adam Arold

I think you can't do that. Check out this class:

我认为你不能那样做。看看这个类:

public class TestClass
{
    private List<Integer> list = new ArrayList<Integer>();

    /**
     * @param args
     * @throws NoSuchFieldException
     * @throws SecurityException
     */
    public static void main(String[] args)
    {
        try
        {
            new TestClass().getClass().getDeclaredField("list").getGenericType(); // this is the type parameter passed to List
        }
        catch (SecurityException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (NoSuchFieldException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        new TestClass().<Integer> insert(new ArrayList<Integer>());
    }

    public <T> void insert(List<T> tList)
    {
        ParameterizedType paramType;
        paramType = (ParameterizedType) tList.getClass().getGenericInterfaces()[0];
        paramType.getActualTypeArguments()[0].getClass();
    }
}

You can get the type parameters from a class or a field but it is not working for generic methods. Try using a non-generic method!

您可以从类或字段获取类型参数,但它不适用于泛型方法。尝试使用非通用方法!

OR

或者

another solution might be passing the actual Classobject to the method.

另一种解决方案可能是将实际Class对象传递给方法。