On 10/06/2006 12:27 AM, Stefan Ram wrote:
> z-man <> writes:
>> Let me say: generics type erasure is just a misfeature that's
>> embarrassingly hiped as a "feature".
>
> "hyped"
sorry for my typo.
> Please be aware that some people in this newsgroup love Java,
> so when you use such wording you might hurt their feelings.
I sincerely didn't want to hurt anybody. Anyway, I think not to be the
only one to complain about such a crippling implementation choice.
> You could have just asked your question without the
> introducing rant.
That was due to my frustration
>> Is there any wizard that can solve this annoying problem (dynamic
>
> "wizard who can ..."
>
>> protected <T,TSuper extends MySuperType<T>> void setEntry(
>> String key,
>> T value
>> )
>> {
>> TSuper entry = myMap.get(key));
>> if(entry == null)
>> {
>> // Owing to that crappy type erasure, neither this works...
>> myMap.put(key,entry = new TSuper());
>> // ...nor this one (no 'class' member available).
>> myMap.put(key,entry = TSuper.class.newInstance());
>> }
>>
>> entry.setValue(value);
>> }
>
> This sounds like an FAQ. My own, untested approach in this
> case would be:
>
> public class EntrySetter <T, TSuper extends MySuperType< T >>
> { final Factory<TSuper> factory;
>
> public EntrySetter( final Factory<TSuper> factory )
> { this.factor = factory; }
>
> public void setEntry
> ( final java.lang.String key,
> final T value )
> { final TSuper old_entry = myMap.get( key );
> final TSuper entry;
> if( old_entry == null )
> { entry = factory.newInstance();
> myMap.put( key, entry ); }
> else entry = old_entry;
> entry.setValue( value ); }}
I thank you Stefan.
Don't you think, anyway, that being forced by the language to use the
awkward factory pattern is much more a workaround than an elegant solution?
Best Regards