通过java中的反射调用通用方法

发布于 2025-01-22 07:25:10 字数 157 浏览 3 评论 0原文

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Saygoodbye 2025-01-29 07:25:10

仿制药在编译时被删除,它们仅向编译器提供额外的信息以确定错误。他们实际上并未在.class文件中更改该方法的签名。

这意味着您以Java的反射方式调用通用方法,就像您在Java中称为非传播方法的方式完全相同,除了指定类型的t,而是指定类型对象

关于如何通过反射调用常规方法的教程太多,我不愿添加另一个。但是,如果您确实需要有关如何通过反射调用方法的指示,请在下面添加评论,我将添加必要的代码。

如果您发现事情无法正常工作,则可以在编译类文件上运行javap,以验证您在参数列表中使用正确的对象。如果您指定&lt; t扩展列表&gt;键入通用签名,则结果参数对象实际上可能是list对象。

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 .class file.

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.

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 javap on 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 List object.

西瓜 2025-01-29 07:25:10

这对我有用。

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());

当然,打印

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

在此理论中的理论在@Edwin的答案中都很好地解释了。

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.

孤君无依 2025-01-29 07:25:10

要突出显示Edwin答案中给出的点,我们在其中使用的延伸方式以通用类型:如果您有类似的类

GenericHibernateDao<T extends Serializable>

,以及

public T save( T entity ) {};

使用反射的方法调用方法的方法,则必须使用可序列化的类,即,您需要使用:

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

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

<T extends Serializable>

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

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:

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

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

<T extends Serializable>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文