开发者

How can I get the "real" type of a generic class?

开发者 https://www.devze.com 2023-01-16 11:31 出处:网络
Okay the question title may not have made sense... mostly because I don\'t know how to exp开发者_如何转开发lain it.

Okay the question title may not have made sense... mostly because I don't know how to exp开发者_如何转开发lain 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!


You need to parameterize the addHandler() method as well. Then the cast will work.

public <T> void addHandler(Class<T> paramType, SomeInterface<T> someObject) {
    someObject.foo(paramType.cast(someVariable));
}


Generally it is impossible, it's like calling add() on a List<?>. If you provide more detail and background of what you really want to achieve, people may suggest better solutions.

Suppose the question is this, given

SomeInterface<?> x;
Object y;

and programmer is sure that they are of compatible types, how to call x.foo(y). This works:

((SomeInterface)x).foo(y);

EDIT

You'll be better off to use 'raw type' only in your implementation, forget <?>

Generics give programmers more ways to express type relations, that can be checked by a compiler. It is however limited, it cannot express the type relation in this question for example, even though the relation looks simple and straightforward.

That's not the end of the world. We didn't have Generics before, and we survived. Use raw types, and static type check your program with your eyes - you know more about your object types than a compiler.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号