0

I would like to invoke this method using Reflection but am a bit confused with getting the method with getDeclaredMethod(), how would this be done?

 private static <T> void registerServiceClass(final Class<T> service, final T instance) {
        Collection<Class<?>> serviceClasses = SERVICE_MAP.get(service);
        if (null == serviceClasses) {
            serviceClasses = new LinkedHashSet<>();
        }
        serviceClasses.add(instance.getClass());
        SERVICE_MAP.put(service, serviceClasses);
    }
}
Teddy Tsai
  • 414
  • 1
  • 13
  • Does [this post](https://stackoverflow.com/questions/1901164/get-type-of-a-generic-parameter-in-java-with-reflection) answer your question? – hayden.mumm Oct 27 '22 at 13:55

1 Answers1

1

Try this: .getDeclaredMethod("registerServiceClass", Class.class, Object.class) ;

It is impossible to infer generic types within method scope at runtime, as this information is erased by the compiler

https://docs.oracle.com/javase/tutorial/java/generics/erasure.html

mrmirg
  • 53
  • 6