5

We have code somewhat similar to below, wherein we have enum and we check whether a given variable of that enum type is present in a list of that 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 this code, and they have flagged a warning - GC_UNRELATED_TYPES, which states that

GC: No relationship between generic parameter and method argument (GC_UNRELATED_TYPES)

This call to a generic collection method contains an argument with an incompatible class from that of the collection's parameter (i.e., the type of the argument is neither a supertype nor a subtype of the corresponding generic type argument). Therefore, it is unlikely that the collection contains any objects that are equal to the method argument used here. Most likely, the wrong value is being passed to the method.

My question is what is the use of variables whose types are Enum<EnumClass>, and should the FindBug warning be fixed. We have currently planning to resolve it by using type casting.

 colorList.contains((Color) red)

Would that be correct way of fixing this warning if we assuming that we are not at liberty to change Enum<Color> to Color for variable red.

Update: Reason we are not at liberty to change variables is - in real code, we have a GUI reusable control - EnumListBox - and it seems to be designed to work with any Enum - and hence, when we inherit from the EnumListBox to create specific uses - we have to override a method which accepts parameter of type, let says, Enum<Color>.

11
  • Change colorList to List<Enum<Color>>. Or just disable the warning here, because it's erroneous. Aug 22, 2017 at 14:06
  • 3
    I'd question why it has to be Enum<Color> red and check whether it might maybe be possible to change that. Why aren't you at liberty to do that?
    – Thomas
    Aug 22, 2017 at 14:08
  • 3
    @BackSlash but AJNeufeld (and Thomas) are questioning why that is. Aug 22, 2017 at 14:16
  • 4
    Hmm, it seems like EnumListBox is some custom or 3rd party code but if I'd design it I'd use EnumListBox<T extends Enum<T>> and thus EnumListBox<Color> would be possible. If it isn't would you care to elaborate a bit, e.g. post some code snippet or a link to that class' javadoc/sources (e.g. on grepcode) if possible?
    – Thomas
    Aug 22, 2017 at 14:38
  • 1
    I'd definitely check whether you could use selectItem(T value) instead. Depending on how that method is called that refactoring shouldn't require too many changes. However, if you or your management deem the refactoring not worth the effort I'd also consider a simpler approach (e.g. that cast) that while not clean might cut costs. It's up to you (and I'd prefer the cleaner approach if possible).
    – Thomas
    Aug 23, 2017 at 7:46

2 Answers 2

5

Enum is like Class, it is not the entities of the enum Color, but the type of it, thus Enum<Color> is a similar construct to Class<Color>...

Your Enum<Color> red = Color.RED; line makes not much sense.
It should be Color red = Color.RED;...

Also see the comment below by Joop Eggen...

1
  • 4
    More explanation: Enum<Color> is the synthetic base class of enum Color { ... }. Hence the assignment worked. In contains, a child class element of Color is required, not a parent class Enum which hypothetically is no Color.
    – Joop Eggen
    Aug 22, 2017 at 14:38
1

As you can read from here, Enum<?> is the father of every enum.

The problem here is that FindBugs now only that:

  • the list has elements of type Color
  • You are trying to is of type Enum

This is definitely a case where FindBugs is issuing a warning on something which you know is not really dangerous, probably because it is not able to infer the relation between Color and Enum<Color> (the warning description clearly states that.)

The cleanest way is changing the type of red, but if you cannot, casting should be good enough. On the other hand, using Enum<Color> is more verbose and does not add clarity to the code, so I would investigate on the need to declare it that way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.