which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
sonarqube:9.8.0-community
what are you trying to achieve
analyzing java code from maven plugin
what have you tried so far to achieve this
On a multi-module maven project, if an enum is on a module and a switch expression is on a different module, sonar-maven-plugin throws an error:
mvn -q sonar:sonar
[ERROR] Unable to parse source file : 'module2/src/main/java/org/example/module2/Foo1.java'
[ERROR] Parse error at line 8 column 17: A switch expression should have a default case
Hey @froque,
Thank you for submitting such a detailed reproducer, can you confirm if you use the sonar.jdkHome property pointing to a JDK 17 in your analysis?
I don’t use jdkHome but it does not seem affect the outcome
/opt/maven/apache-maven-3.8.7/bin/mvn -q sonar:sonar \
-Dsonar.java.jdkHome=/usr/lib/jvm/java-17-openjdk-amd64/ \
-Dsonar.url=http://localhost:9000 \
-Dsonar.login=admin \
-Dsonar.password=admin
[ERROR] Unable to parse source file : 'module2/src/main/java/org/example/module2/Foo1.java'
[ERROR] Parse error at line 8 column 17: A switch expression should have a default case
Hey @froque,
I think I managed to reproduce the issue you brought up.
It looks like in both cases the file cannot be parsed however but, for some reason, the error is no longer logged when we build and analyze in the same command.
Can you confirm that the parsing fails in both cases by checking whether the file displayed in SonarQube has syntax highlighting or not?
We decided to migrate to maven 4 (alpha) that has better support for multi-modules projects. It still requires the mvn sonar:sonar to be done after an mvn package for the dependencies jar files to be present on disk.
public class DummyClass {
public static DummyEnum test(final DummyEnum dummy) {
return switch (dummy) {
case VAL1 -> DummyEnum.VAL1;
case VAL2 -> DummyEnum.VAL2;
};
}
}
This class leads to the following log:
error sonarExecuteScan - ERROR: Unable to parse source file : 'DummyClass.java'
error sonarExecuteScan - ERROR: Parse error at line 11 column 17: A switch expression should have a default case
Interestingly,
public enum DummyEnum {
VAL1, VAL2;
public static String test(final DummyEnum dummy) {
return switch (dummy) {
case VAL1 -> "val1";
case VAL2 -> "val2";
};
}
}
does not lead to the error.
I am using
Java 17, Gradle 8
SonarQube 9.9.4
Scanner CLI 5
Could you share your build script using the scanner cli?