Joshua
Joshua

Reputation: 2982

Method used by method reference - recognised as not used

So, I have this List of Class instances. I want to convert it into a List of Constructors which are declared by the classes.

I map the classes to their declared constructors and flatten it with collect:

services.stream()
    .map(Class::getDeclaredConstructors)
    .collect(ArrayList::new,
        this::collectConstructors,
        List::addAll)

For collecting I use a method, which needs access to the class context, which is why it is not static.

private void collectConstructors(List<Constructor> l, Constructor<?>[] arr) {
    if (arr.length == 0) {
        return;
    }

    Class<?> clz = arr[0].getDeclaringClass();
    if (arr.length > 1) {
        throw new MoreThanOneConstructor(clz);
    }

    if (!Modifier.isPublic(arr[0].getModifiers())) {
        throw new NoVisibleConstructor(clz);
    }

    l.add(arr[0]);
    findDefinition(clz).setConstructor(arr[0]);
}

The good is, everything works fine. The bad is , both IntelliJ and SonarQube tell me, that the method collectConstructors is not used. The ugly, or rather funny, is, IntelliJ knows perfectly that the method is used, when I click on it and the matching counterpart is highlighted.

How does it come, that the tools tell me, that the method is not used? Missing tool support? My shitty code? What is it?

Upvotes: 0

Views: 95

Answers (2)

Supun Wijerathne
Supun Wijerathne

Reputation: 12948

It had been identified as a known issue in SonarQube. And it says that they have fixed that with version 4.0. It's a problem with semantic analysis.

There's no problem in your code, just the tool.

Upvotes: 1

Mariusz Lotko
Mariusz Lotko

Reputation: 384

Maybe it can be statically analyzed that serivces.isEmpty() is always true, therefore stream() will be empty and no collectConstructors call will be performed?

Upvotes: 0

Related Questions