diff --git a/java-checks-test-sources/default/src/main/java/checks/CollectorsToListCheckSample.java b/java-checks-test-sources/default/src/main/java/checks/CollectorsToListCheckSample.java index 35855b746c2..7f017ef33c4 100644 --- a/java-checks-test-sources/default/src/main/java/checks/CollectorsToListCheckSample.java +++ b/java-checks-test-sources/default/src/main/java/checks/CollectorsToListCheckSample.java @@ -8,8 +8,15 @@ import java.util.stream.Collector; import java.util.stream.Collectors; import java.util.stream.Stream; +import lombok.val; public class CollectorsToListCheckSample { + + void lombok_val() { + // Lombok val is not supported, in this case the symbolType of the collect method invocation is unknown + val unknownSymbolStream = Stream.of("A", "B", "C").collect(Collectors.toList()); // Compliant + } + static class ListWrapper { List strings; } diff --git a/java-checks/src/main/java/org/sonar/java/checks/CollectorsToListCheck.java b/java-checks/src/main/java/org/sonar/java/checks/CollectorsToListCheck.java index 39acd6284d3..729f9318b14 100644 --- a/java-checks/src/main/java/org/sonar/java/checks/CollectorsToListCheck.java +++ b/java-checks/src/main/java/org/sonar/java/checks/CollectorsToListCheck.java @@ -19,6 +19,7 @@ */ package org.sonar.java.checks; +import java.util.List; import javax.annotation.CheckForNull; import org.sonar.check.Rule; import org.sonar.java.checks.methods.AbstractMethodDetection; @@ -107,9 +108,8 @@ private static boolean isInvariantTypeArgument(MethodInvocationTree collectMetho if (streamType.isRawType()) { return true; } - Type collectArgType = collectMethodInvocation.symbolType().typeArguments().get(0); - Type streamArgType = streamType.typeArguments().get(0); - return collectArgType.is(streamArgType.fullyQualifiedName()); + List typeArguments = collectMethodInvocation.symbolType().typeArguments(); + return !typeArguments.isEmpty() && typeArguments.get(0).is(streamType.typeArguments().get(0).fullyQualifiedName()); } private void reportIssue(MethodInvocationTree collector, boolean mutable) {