Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[platform-ui-dev] arrays and generics in JFace

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,
I'm currently adding generics support to the JFace Viewer. But run
into problems with the generic typisation of mostly object typed
arrays. I know arrays and generics do not mix very well. But I'm not
able to replace arrays by lists, because this would break the
compatibility top older versions.

But how should I type/cast the arrays correctly?

Here is an example from the
org.eclipse.jface.viewers.LabelProviderChangedEvent Classe:
- ------------------------------------------------------------

private Object[] elements;
//...
    public LabelProviderChangedEvent(IBaseLabelProvider source, Object
element) {
        super(source);
        this.elements = new Object[1];
        this.elements[0] = element;
    }

- ------------------------------------------------------------

First solution would be this:
- ------------------------------------------------------------
private E[] elements;
//...
    public LabelProviderChangedEvent(IBaseLabelProvider<E> source, E
element) {
        super(source);
	@SuppressWarnings("unchecked")
        this.elements = (E[]) new Object[1]; //Warning
        this.elements[0] = element;
    }
- ------------------------------------------------------------

Second would be this:
- ------------------------------------------------------------
public LabelProviderChangedEvent(IBaseLabelProvider<E> source, E
element) {
        super(source);
        ArrayList<E> arrayList = new ArrayList<E>();
        arrayList.add(element);
        this.elements = (E[]) arrayList.toArray();
    }
- ------------------------------------------------------------

What do you think is the better solution in the case of the JFace
Toolkit? Or do you have a better idea to solve this problem?

Regars,
Hendrik


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with undefined - http://www.enigmail.net/

iQEcBAEBAgAGBQJR8aWwAAoJECjZOs4dIxisZh0IAK+DqsNioocPTvJIRBdLfX8z
J6SCJO2rVuCFjZLO+yRxBxnELRspWCMISPwgMCRgzlCHiajWqD9SqectqNCMQhIb
3xkR7zOaInsQzpF/u6DLUEFdcSzpUDSOU2dEC+9SGhki2Ic79py5QBUFOYSCvfNm
BogYwXKkjbdN7E2XipGvYHOsgJjq+TXKXs5w4E9d/EsHR0R+rm4LyZ+eP5PtXr/X
S/U7UMC/ND8gwabIqprlX4Sp2OYfkxCrQtxiGbmPOEDoEtWzNaMlzhCpZsHut8VO
bdRL0+cynZVcI67WL6GDjCE76twNV/pp3fCrr3OCYGN4u+4n8xWDViaWkGLMi14=
=LYpj
-----END PGP SIGNATURE-----


Back to the top