False positiv: Replace this type parametrization by the 'final' type `String`

i have this class in my code and Sonarqube reports here an issue for the clear. The issue is “Replace this type parametrization by the ‘final’ type String.”. When i follow the suggestion the code does no longer compile.

package foo.bar;

import java.util.List;

import javax.cache.event.CacheEntryEvent;
import javax.cache.event.CacheEntryExpiredListener;
import javax.cache.event.CacheEntryListenerException;
import javax.cache.event.CacheEntryRemovedListener;

class MyCacheEntryListener implements CacheEntryExpiredListener<String, List<String>>, CacheEntryRemovedListener<String, List<String>> {
    private final String myCacheKey;

    MyCacheEntryListener(final String myCacheKey) {
        this.myCacheKey = myCacheKey;
    }

    @Override
    public void onExpired(final Iterable<CacheEntryEvent<? extends String, ? extends List<String>>> cacheEntryEvents) throws CacheEntryListenerException {
        cacheEntryEvents.forEach(this::clear);
    }

    @Override
    public void onRemoved(final Iterable<CacheEntryEvent<? extends String, ? extends List<String>>> cacheEntryEvents) throws CacheEntryListenerException {
        cacheEntryEvents.forEach(this::clear);
    }

    private void clear(final CacheEntryEvent<? extends String, ? extends List<String>> event) {
        if (event.getKey()
                .equals(myCacheKey)) {
            System.out.println("event=" + event);
        }
    }
}

Hey there.

What version of SonarQube are you using?

we are using version 9.8.0.63668

Hi @BigMichi1,

Thank you for reporting.
This is a known FP, and we already have a ticket for fixing it.
Here, SONARJAVA-4398, you can track progress.

All the best,

Irina

I just came across this one for the first time. I have to say that the provided example in the linked ticket is quite dubious or “sus” ^^
Reason: the author wants to prevent people adding to the list at compile time, fair enough. In my opinion, “abusing” the generics systems for that purpose is a hack, however, and not readily visible for fellow devs. Instead, use a read-only view and/or “immutable” collection like Guava’s ImmutableList. Code such as

List<Class<?>> unmodifiableList = List.of();

does not rely as much on the type system (you can still call add() resulting in an exception). I would argue though that it is at least as safe as the generics hack because the intent of the programmer is very clear.
Hence, the described use case is not an FP from my perspective.

Food for thought: Why does List.of() in Java not return a typed immutable list? - Stack Overflow