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

The right solution for array creations with a variable type is to use @SuppressWarnings("unchecked") on the smallest element possible.

Introduce a local variable to avoid marking the whole method as unchecked. If you have to use the same pattern in many places, then extract it into a method (but only if you can guarantee that this won't hide actual ClassCastExceptions at run time).

The second proposed solution with the additional ArrayList only suppressed the warning by accident. I just fixed https://bugs.eclipse.org/338350 so that casting to (E[]) will always show the warning, even when the "Ignore unavoidable generic type problems" compiler option is enabled. And BTW: creating an additional object just to suppress a warning is always wrong.

Markus



From:        Daniel Megert/Zurich/IBM@IBMCH
To:        "Eclipse Platform UI component developers list." <platform-ui-dev@xxxxxxxxxxx>
Date:        2013-07-29 10:46
Subject:        Re: [platform-ui-dev] arrays and generics in JFace
Sent by:        platform-ui-dev-bounces@xxxxxxxxxxx




> Based on an offline discussion with John, I think @SuppressWarning is the better approach as this avoid the unnecessary object creation.

If the method is not called extensively, then that object creation is negligible in my opinion and "clean" code preferred. Also note that the proposed code does not compiled as is: you would either have to add a local variable or move the @SuppressWarnings to the method.


Dani



From:        
Lars Vogel <lars.vogel@xxxxxxxxx>
To:        
"Eclipse Platform UI component developers list." <platform-ui-dev@xxxxxxxxxxx>
Date:        
26.07.2013 20:23
Subject:        
Re: [platform-ui-dev] arrays and generics in JFace
Sent by:        
platform-ui-dev-bounces@xxxxxxxxxxx




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
_______________________________________________
platform-ui-dev mailing list
platform-ui-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/platform-ui-dev


Back to the top