I have this generic class:
public class MyClass<T> {
public void myMethod(T arg) {
// ...
}
}
How can I get an instance of this class which uses the class of an object as generic type T
?
That is if I have:
SomeClass o = new SomeClass()
How can I get an instance of MyClass<SomeClass>
by getting the runtime class of o
?
Basically what I need is something like this (I know this doesn't make sense... it's just to give an idea):
MyClass<o.getClass()> m = new MyClass<o.getClass()>()
Is it possible? If not, can I achieve my goal of having an instance of MyClass
which has a method that takes an argument of the same type of the runtime type of o
? (yes, it has to be that exact type, cannot be a superclass...)
Reflection is an option here!
Thanks!