javavisual-studio-codeintellij-ideawarningsunbounded-wildcard

Intellij - Unexpected wildcard mark as problem


We moved from VS Code to use Intellij Ultimate latest version, and we have existing code using Java 8:

private Map<Integer, Queue<Condition<?>>> loadConditions(Map<Integer, Condition<?>> conditions) {

Map<Integer, Queue<Condition<?>>> conditions = new HashMap<>();
...
Queue<Condition<?>> conditionsSet;
...
            while (rs.next()) {
                int id = rs.getInt("ID");
                int conditionId = rs.getInt("CONDITION_ID");
                conditionsSet = conditions.getOrDefault(id, new PriorityQueue<Condition<?>>(Comparator.comparingInt(Condition<?>::getPriority).reversed()));
                conditionsSet.add(conditions.get(conditionId));
                conditions.put(id, conditionsSet);
            }

Intellij in Problems tab (and underline in code) mark Condition<?>::getPriority with red as Unexpected wildcard

The code compiles (Java 8) and works, How can I reduce its severity to warning? I don't find Unexpected wildcard in Inspections window

EDIT

If I copy line (below) to a method it is marked as compilation error also in VS code, but still compiles

private void name() {
    new PriorityQueue<Condition<?>>(Comparator.comparingInt(Condition<?>::getPriority));
}

Problem:

The type Condition<capture#14-of ?> does not define getPriority(Condition<capture#1-of ?>) that is applicable hereJava(603979903)

Condition Class

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Condition<T> {
    // fields
    private int priority; 
}

Solution

  • Please check this:

    conditionsSet = conditions.getOrDefault(id, new PriorityQueue<>(Comparator.comparingInt((Condition<?> condition) -> condition.getPriority()).reversed()));
    

    If you want use method reference you should use this:

    conditionsSet = conditions.getOrDefault(id, new PriorityQueue<>(Comparator.comparingInt((ToIntFunction<Condition<?>>) Condition::getPriority).reversed()));