java - Usage of Enum<> generic type for variables -
we have code similar below, wherein have enum , check whether given variable of enum type present in list of enum type.
import java.util.arraylist; import java.util.list; public class test { public static enum color {red, blue, green}; public static void main(string[] args) { enum<color> red = color.red; list<color> colorlist = new arraylist<>(); colorlist.add(color.green); // ** find bugs reports warning - gc_unrelated_types system.out.println(colorlist.contains(red)); } }
our qa team has run findbugs against code, , have flagged warning - gc_unrelated_types, states that
gc: no relationship between generic parameter , method argument (gc_unrelated_types)
this call generic collection method contains argument incompatible class of collection's parameter (i.e., type of argument neither supertype nor subtype of corresponding generic type argument). therefore, unlikely collection contains objects equal method argument used here. likely, wrong value being passed method.
my question use of variables types enum<enumclass>
, , should findbug warning fixed. have planning resolve using type casting.
colorlist.contains((color) red)
would correct way of fixing warning if assuming not @ liberty change enum<color>
color
variable red
.
update: reason not @ liberty change variables - in real code, have gui reusable control - enumlistbox
- , seems designed work enum - , hence, when inherit enumlistbox
create specific uses - have override method accepts parameter of type, let says, enum<color>
.
enum
class
, not entities of enum color
, type of it, enum<color>
similar construct class<color>
...
your enum<color> red = color.red;
line makes not sense.
should color red = color.red;
...
also see comment below joop eggen...
wiki
Comments
Post a Comment