0

When I compile my java code with -Xlint:unchecked, in this line:

            List<HierarchyNode> list = hier.getHierarchyNodesParentFirst(0);

I receive this:

... unchecked conversion
required: java.util.List<ir.ac.ut.iis.person.hierarchy.HierarchyNode>
found:    java.util.List

getHierarchyNodesParentFirst is defined as:

public class Hierarchy<U extends User> {
    ...
    public List<HierarchyNode> getHierarchyNodesParentFirst(int owner) {
        List<HierarchyNode> hierarchyNodesChildFirst = getHierarchyNodesChildFirst(owner);
        ...
        return hierarchyNodesChildFirst;
    }
}

It is not overrided anywhere and it does not override anything. What can the problem be?

Shayan
  • 2,758
  • 7
  • 36
  • 55

1 Answers1

1

In the comments you stated that you have defined variable hier as with Hierachy hier = new Hierarchy<>();. This is causing the unchecked warning because you have left out the type variable in the variable declaration. The warning was not caused by the invalid conversion between List and List<HierarchyNode> as one could have initially thought by reading the compiler warnings.

The following for example should do the trick:

Hierarchy<? extends User> hier = new Hierarchy<>();
List<HierarchyNode> list = hier.getHierarchyNodesParentFirst(0);

or alternatively (which probably is not intended)

Hierarchy hier = new Hierarchy<>();
List list = hier.getHierachyNodesParentFirst(0);

Edit: you can also remove the generic parameter <U extends User> from class Hierarchy. Obviously this is not probably what you want, but it emphasises that since there's no type, type erasure cannot occur and the code will compile without warnings.

This is caused by type-erasure. There's some discussion in StackOverflow.

am9417
  • 994
  • 8
  • 18
  • This solved the problem. But two points: 1- I don't think that It is the instantiation that is important. Probably only changing the type of hier to Hierarchy extends User> is sufficient (It may be passed as a parameter). 2- Why does this matter? The return type of the method (List) does not relate to the any type parameter of Hierarchy. – Shayan Jul 05 '18 at 09:54
  • If you instantiate it with `Hierarchy hier = new Hierarchy<>()`, what is the generic type of `U` in `hier`? Nothing, it cannot be determined. And then casting it to a class having the generic type will cause an unchecked conversion warning. Is this what you meant? The #2. is, as I have interpreted, caused by the type erasure for the backward compability with the versions before generics were introduced. The type erasure in the class / instance also affects its generic methods. – am9417 Jul 05 '18 at 14:34
  • 1
    But the method is not generic. Why should this happen on a non-generic method? – Shayan Jul 06 '18 at 14:39
  • Sorry. It is not generic, you're right. But the List is typed and the type disappears. I don't know the correct name for this. Why it happens is discussed in that thread I linked. – am9417 Jul 08 '18 at 05:49