java:S1612 is suggesting replacing the lambda e -> (E) e by E.class::cast.
However it’s not possible because E is a generic type and E.class cannot be compiled.
package fpreproducer;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import lombok.AccessLevel;
import lombok.Getter;
public class EntityCollector {
private static final Map<Class<?>, EntityCollector> entityCollectors = new LinkedHashMap<>();
@Getter(AccessLevel.PRIVATE)
private final List<Entity<?>> collection = new ArrayList<>();
@SuppressWarnings("unchecked")
public static <E extends Entity<E>> void someMethod( final Class<E> clazz ) {
entityCollectors.get(clazz).getCollection()
.stream()
.map( e -> (E) e ) // java:S1612 Lambdas should be replaced with method references
;
}
class Entity<T> {
@SuppressWarnings("unused")
private T field;
}
}
Hi, I created the following SONARJAVA-4203 ticket to remove the false-positive. Unfortunately, if you have a false-positive, it means the java analyzer has an unknown symbol E and from what you report I can not understand.