Skip to main content

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

Hi Hendrik,

I have recently completed the adding of generics to the databinding bundles.  The way I approached this problem was to add a new method that uses Collections and deprecated the method that takes arrays.  We use @SuppressWarning only on deprecated code, so unless the users are using deprecated code we can be sure that we are not breaking the types.

There is of course the issue of performance.  In my opinion cleaner code generally trumps questionable performance gains, but that would be a case-by-case decision.

It is really good to see generics being added to JFace viewer.  JFace viewer was a large source of problems when adding generics to databinding.  I initially parameterized JFace collections with <Object>.  However that did not make it easy for the consumers who would often have collections parameterized by something else, but I was able to loosen the parameterization a little by making all the immutable collections be parameterized by a super class <? super E> to the element type in the underlying collection <E>.

Another thing I ran against is that there are three methods that return values with parameterization that is too loose.  These are Class.cast, Class.getComponentType, and Array.newInstance.   If you look in the e4 databinding repository at org.eclipse.jface.databinding.util.Util, you will see the following:

    @SuppressWarnings("unchecked")
    public static <E> Class<E> getComponentType(E[] a) {
        return (Class<E>) a.getClass().getComponentType();
    }

    @SuppressWarnings("unchecked")
    public static <E> E[] createArrayInstance(Class<E> componentType, int size) {
        return (E[]) Array.newInstance(componentType, size);
    }

So at least the warnings are isolated.

Let me know if there is anything I can do to help.

Nigel Westbury

On 26/07/2013 19:22, Lars Vogel wrote:
Based on an offline discussion with John, I think @SuppressWarning is the better approach as this avoid the unnecessary object creation.

Best regards, Lars

Am Freitag, 26. Juli 2013 schrieb Hendrik Still :
-----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-----
_______________________________________________
platform-ui-dev mailing list
platform-ui-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/platform-ui-dev


_______________________________________________
platform-ui-dev mailing list
platform-ui-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/platform-ui-dev


Back to the top