Skip to content

Commit

Permalink
SONARJAVA-4950 Fix S6204 IndexOutOfBoundsException when lombok.val is…
Browse files Browse the repository at this point in the history
… used (#4857)
  • Loading branch information
leonardo-pilastri-sonarsource authored Sep 11, 2024
1 parent 07eda8b commit 26ab5be
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> strings;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Type> typeArguments = collectMethodInvocation.symbolType().typeArguments();
return !typeArguments.isEmpty() && typeArguments.get(0).is(streamType.typeArguments().get(0).fullyQualifiedName());
}

private void reportIssue(MethodInvocationTree collector, boolean mutable) {
Expand Down

0 comments on commit 26ab5be

Please sign in to comment.