java 在java中通过反射调用泛型方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10739248/
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 02:19:27  来源:igfitidea点击:

Call a Generic method by reflection in java

javagenericsreflection

提问by Hussein Zawawi

How to call a Custom Generic method by reflection in java?

如何通过java中的反射调用自定义泛型方法?

class Person
{
  public <T> void print(T t)
   {
      System.out.println(t.toString());
   }
}

采纳答案by Edwin Buck

Generics are erased at compile time, they only provide extra information to the compiler to determine errors. They do not actually change the signature of the method in the .classfile.

泛型在编译时被擦除,它们只向编译器提供额外的信息来确定错误。它们实际上不会更改.class文件中方法的签名。

This means that you call a generic method by reflection in Java exactly the same way as you would call a non-generic method in Java, except that instead of specifying a type of T, you would specify a type of Object.

这意味着您在 Java 中通过反射调用泛型方法的方式与在 Java 中调用非泛型方法的方式完全相同,不同之处在于不是指定 的类型T,而是指定类型的Object

There are so many tutorials on how to call a regular method by reflection that I hesitate to add yet another; however, if you really need direction on how to call a method by reflection, please add a comment below and I'll add the necessary code.

关于如何通过反射调用常规方法的教程太多了,我犹豫要不要再添加一个;但是,如果您真的需要有关如何通过反射调用方法的指导,请在下面添加评论,我将添加必要的代码。

If you find that things are not working as expected, you can always run javapon the compiled class file to verify that you are using the right objects in the argument list. It might be possible that if you specify a <T extends List>type generic signature, the resulting parameter object might actually be a Listobject.

如果您发现事情没有按预期工作,您始终可以javap在编译后的类文件上运行以验证您使用的是参数列表中的正确对象。如果您指定<T extends List>类型泛型签名,则结果参数对象可能实际上是一个List对象,这可能是可能的。

回答by adarshr

This works for me.

这对我有用。

Method method = Person.class.getMethod("print", Object.class);

method.invoke(new Person(), "this is a string");
method.invoke(new Person(), 273);
method.invoke(new Person(), new Object());

printing

印刷

this is a string
273
java.lang.Object@addbf1

Of course the theory behind this is explained beautifully in @Edwin's answer.

当然,@Edwin 的回答很好地解释了这背后的理论

回答by Deepak

To highlight the point given in Edwin's answer, where we are using extends in a generic type: if you have a class like

为了突出 Edwin 的回答中给出的观点,我们在泛型类型中使用扩展:如果您有一个类

GenericHibernateDao<T extends Serializable>

, and a method

,和一个方法

public T save( T entity ) {};

to invoke the method save using reflection you have to use the Serializable class, i.e., you need to use:

要使用反射调用保存方法,您必须使用 Serializable 类,即,您需要使用:

Method method = GenericHibernateDao.class.getMethod(methodName, Serializable.class);

and not the Object.class as the parameter, since we are using

而不是 Object.class 作为参数,因为我们正在使用

<T extends Serializable>