Okay the question title may not have made sense... mostly because I don't know how to explain it.
Here is what I have:
class SomeClass
{
private Class paramType;
private SomeInterface<?> someObject;
}
public interface SomeInterface<T>
{
public void foo(T in);
}
The variable someObject
might be assigned to like this:
someObject = new SomeInterface<Integer> { ... }
I want to be able to invoke the generic method on someObject
like this:
someObject.foo(paramType.cast(someVariable));
The issue is that someObject is of type SomeInterface. Now, is there some way I could get someObjects
's REAL type so that I could invoke the method correctly?
I know I could use reflection and do something like .getMethod("foo", paramType);
But, I don't like the fact that "foo" has to be hardcoded by the client. If you can find a more robust way of calling that method, I'd be open to this as well.
Any ideas? Sorry if this isn't quite making sense... feel free to edit if you think you can explain it better :).
EDIT: Some functional designs, as it might seem insane that I'm going this route :).
Basically, I'm making a framework that will allow a client to hook into some event. An example of this would be:
SomeClass.addHandler(Integer.class, new SomeInterface<Integer> {
public void foo(Integer in)
{
// do something
}});
So, I'm basically trying to write the event trigger function. The advantage of going this route is that the client doesn't have to worry about casting anything, and has type safety (I can restrict the addHandler parameter to be SomeInterface<bar>
for example).
The "invoker" just has to get the data into the appropriate object (easy) and call SomeInterface's method (hard) with that object.
I've almost given up and may just resort to making the client have to cast, but I'm so close!