15 October 2009

Little confusion about Generics

Lets consider some class used for collecting References. It requires little work but there is way to do not determine Reference type to early. So lets observe consequences of this action.

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

 * Collects {@link Reference}-s
public class ReferenceCollector<Ref extends Reference</* ? extends */ Type>, Type>
    implements Iterable<Reference<? extends Type>> { ... }

This class will allow given code to compile:

ReferenceCollector<WeakReference<String>, ?> rc1 = null;
ReferenceCollector<WeakReference<String>, ? extends Object> rc2 = null;

On the other hand reversing Type arguments will be not allowed:

ReferenceCollector<WeakReference<? extends Object>, String> rc2 = null;

This mean that nesting is count more then arguments order, what more it's all about compatibility not as I would expect equality of types. Probably I just need read rest of JLS when I'll have free time to get idea why '?' is allowed as second generic argument.

4 October 2009

I just killed Eclipse

I like enum-s more than most people do.

They are really nice to use and synchronization-safe. But as I found, they have their limits as well.

After generating 1,8Mb Java code for one enum Eclipse dying after most try of refactorization ;), Java was not happy at all about it as well with error:

The code for the static initializer is exceeding the 65535 bytes limit