【问题标题】:Use Reflection on Method with Generic Type [duplicate]对泛型类型的方法使用反射[重复]
【发布时间】:2022-11-30 05:22:53
【问题描述】:

给定一个私有方法

public class SomethingService {

    private <T extends Something> void doSomething(final T thing,
                                                   final Service service) {
        // ... do something ...
    }
}

如果第一个参数是通用的,我将如何通过反射访问这个私有方法?这可能吗?

Method method = SomethingService.class.getDeclaredMethod("doSomething", ???, Service,class);

【问题讨论】:

    标签: java generics reflection


    【解决方案1】:

    看来我应该只使用扩展类。

    Method method = SomethingService.class.getDeclaredMethod("doSomething", Something.class, Service,class);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多