Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Help > Re: Can this ca...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 14 of 24 Topic 16045 of 16078
Post > Topic >>

Re: Can this callback mechanism be generified?

by Daniel Pitts <newsgroup.spamfilter@[EMAIL PROTECTED] > May 4, 2008 at 08:24 PM

Couple of notes, nothing terribly important...

Casper Bang wrote:
> Mark Space wrote:
>> I agree this part is easier to read, I just don't see the rest of the 
>> code much improved (pulling the type by name from the interface name 
>> was a pretty good trick.  With luck, we'll get reifiable types in Java 
>> 7 and then nonsense like that will be unneeded.)
> 
> I did some reading (Java Generics and Collections) and it turns out 
> there are ways to get to the types, in spite of erasure. So now I do it 
> a bit cleaner, no pulling apart a string and no reflection required:
> 
You expect this to return Class<T>, don't you? So why don't you declare 
it that way.
> private <T> Class extractGenericType(Callback<T> callback)
> {
>     Type[] interfaces = callback.getClass().getGenericInterfaces();
> 
>     if(interfaces.length != 1 || !(interfaces[0] instanceof 
> ParameterizedType))
Don't assume it should be exactly one, it would probably be better to go 
through a loop.
>         throw new RuntimeException("You must supply a generified 
> Callback with a single parameterized type");
It would be more appropriate to throw an IllegalArgumentException I 
think.  It would probably be even more appropriate to throw a 
CallbackException of your own creation.
>     Type[] parmTypes = 
> ((ParameterizedType)interfaces[0]).getActualTypeArguments();
> 
>     return (Class)parmTypes[0];
> }
>

Hope this is useful.


-- 
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>




 24 Posts in Topic:
Can this callback mechanism be generified?
Casper Bang <casper@[E  2008-05-02 04:51:44 
Re: Can this callback mechanism be generified?
Mark Space <markspace@  2008-05-02 04:21:23 
Re: Can this callback mechanism be generified?
Casper Bang <casper@[E  2008-05-02 13:14:16 
Re: Can this callback mechanism be generified?
Daniel Pitts <newsgrou  2008-05-02 08:34:23 
Re: Can this callback mechanism be generified?
Casper Bang <casper@[E  2008-05-02 19:58:28 
Re: Can this callback mechanism be generified?
Casper Bang <casper@[E  2008-05-02 20:58:55 
Re: Can this callback mechanism be generified?
Mark Space <markspace@  2008-05-02 13:33:23 
Re: Can this callback mechanism be generified?
Joshua Cranmer <Pidgeo  2008-05-02 20:59:06 
Re: Can this callback mechanism be generified?
Mark Space <markspace@  2008-05-03 23:33:40 
Re: Can this callback mechanism be generified?
Daniel Pitts <newsgrou  2008-05-02 14:14:03 
Re: Can this callback mechanism be generified?
Casper Bang <casper@[E  2008-05-03 03:45:00 
Re: Can this callback mechanism be generified?
Mark Space <markspace@  2008-05-03 23:27:15 
Re: Can this callback mechanism be generified?
Casper Bang <casper@[E  2008-05-05 04:01:44 
Re: Can this callback mechanism be generified?
Daniel Pitts <newsgrou  2008-05-04 20:24:40 
Re: Can this callback mechanism be generified?
Mark Space <markspace@  2008-05-05 04:06:24 
Re: Can this callback mechanism be generified?
Casper Bang <casper@[E  2008-05-05 20:54:51 
Re: Can this callback mechanism be generified?
Lew <lew@[EMAIL PROTEC  2008-05-05 21:09:20 
Re: Can this callback mechanism be generified?
Mark Space <markspace@  2008-05-05 21:23:44 
Re: Can this callback mechanism be generified?
Casper Bang <casper@[E  2008-05-05 20:36:28 
Re: Can this callback mechanism be generified?
Lew <lew@[EMAIL PROTEC  2008-05-05 21:16:23 
Re: Can this callback mechanism be generified?
Daniel Pitts <newsgrou  2008-05-06 10:25:33 
Re: Can this callback mechanism be generified?
Lew <lew@[EMAIL PROTEC  2008-05-06 18:56:19 
Re: Can this callback mechanism be generified?
Daniel Pitts <newsgrou  2008-05-06 15:58:21 
Re: Can this callback mechanism be generified?
thufir <hawat.thufir@[  2008-05-02 21:05:56 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri May 16 6:40:58 CDT 2008.