river-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Niclas Hedhman <nic...@hedhman.org>
Subject Re: Fw: Re: Space/outrigger suggestions
Date Sun, 19 Dec 2010 05:55:21 GMT
On Sat, Dec 18, 2010 at 4:11 PM, Peter <jini@zeus.net.au> wrote:
> if you have a field in an Entry declared:
>
> public Collection<String> names;
>
> In bytecode, because of erasure, its type is Collection, this means I can
> take from the javaspace and set the field to contain a Collection that
> doesn't contain String's and the next time your code accesses it, a
> ClassCastException will occur.

That is not correct. For fields, method return values and parameters,
the Type is preserved (ParameterizedType it is called). So for the
above example, do

    Type[] types = names.getClass().getGenericInterfaces();

or something like this for the declaration itself

    Field field = Main.class.getDeclaredField( "names" );
    field.setAccessible( true );
    Type type = field.getGenericType();


In fact, quite a lot of the things that people think are 'erased' are
actually not.

For instance, in a declaration like;

public interface Abc<K,V>

you can even retrieve the variable names of 'K' and 'V' respectively
(those Type are called TypeVariable).


Say what you want about Sun's compromises in the generics system, but
it is far from "just compiler checks".


In my project Qi4j (http://www.qi4j.org), we use this extensively to
do sweet things like;

    Property<String> fullname();  // in an interface

to declare a property instead of getter-setter-mania...


Now, I am not saying that any of this should be deployed into River at
all, just that more things are possible than you might think.


Also Note; There are some small bugs in Sun's JDK1.5 generics
compiler, where some corner cases are handled incorrectly. The ones
that we have found are fixed in 1.6.


Cheers
-- 
Niclas Hedhman, Software Developer
http://www.qi4j.org - New Energy for Java

I live here; http://tinyurl.com/3xugrbk
I work here; http://tinyurl.com/24svnvk
I relax here; http://tinyurl.com/2cgsug

Mime
View raw message