[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [platform-ui-dev] arrays and generics in JFace
|
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
This is a nice approach, I'll see how I'll use this in my changes. Thanks
Regards,
Hendrik
Am 29.07.2013 14:44, schrieb Nigel Westbury:
> 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 :
>>
> 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
>
>
>> _______________________________________________ platform-ui-dev
>> mailing list platform-ui-dev@xxxxxxxxxxx <javascript:;>
>> 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
>
>
>
>
> _______________________________________________ platform-ui-dev
> mailing list platform-ui-dev@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/platform-ui-dev
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with undefined - http://www.enigmail.net/
iQEcBAEBAgAGBQJR9oM+AAoJECjZOs4dIxisglkH/RGPIVvrhPjBeNRLnGTsfeuJ
K2IplCnsM1+Krvw1wXzmLocA1WnG1W9luJfJ+QehFTyhlJyWZCvNXAGWo3ve/wRI
tiOZ8OOsI3nzHxz9TCsL4OkG579+SgM5gQX7mACpbbm8kCSmt3789uJDVQM2m+Kh
KJmDGGTw/KFh4NPGm/3ojwuaN8Fj3w/oMYI1cxFgwaRoiUxG9/dlGE7Jh09S/jrO
VQBUU5nXk6tgfueEb30AeF2V1glYf2KPvdtMzpIDwmOabc3h8Z2eTe7RAT8Xx//L
V5OMx/LiPI8JkJP/7+ohlPmJhmgrAoME0r5GAKOwg3lb7k/eVPi5LKA1iu4Nmds=
=XUKv
-----END PGP SIGNATURE-----