Java 21 - unamed pattern error

Context:

  • Github actions
  • mvn -Pcoverage -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
  • Java 21 and activate --enable-preview for compilation and test
    <sonar.host.url>https://sonarcloud.io</sonar.host.url>
    <sonar.language>java</sonar.language>
    <sonar.java.source>21</sonar.java.source>
  • Error observed: Error: [ERROR] Parse error at line 130 column 32: '_' is a keyword from source level 9 onwards, cannot be used as identifier

Unamed pattern is a new feature since Java 21 and is recommended for differentiating unused variables. This feature was previewed in Java 21 and has been integrated into the language since Java 22. See https://www.baeldung.com/java-unnamed-patterns-variables

Hey there!

Can you try setting sonar.java.enablePreview=true?

Thanks for the answer, but with this property there are always errors.

The same errors, or different ones?

The same errors:

Error:  Unable to parse source file : 'weasis-dicom/weasis-dicom-codec/src/main/java/org/weasis/dicom/codec/HiddenSeriesManager.java'
Error: [ERROR] Parse error at line 54 column 44: '_' is a keyword from source level 9 onwards, cannot be used as identifier
Error:  Unable to parse source file : 'weasis-dicom/weasis-dicom-codec/src/main/java/org/weasis/dicom/codec/SegSpecialElement.java'
Error: [ERROR] Parse error at line 115 column 57: '_' is a keyword from source level 9 onwards, cannot be used as identifier

Thanks!

After doing some research, this seems to ultimately be a problem with the underlying Eclipse compiler that makes our Java analysis possible. The preview feature didn’t make its way in until after Java 21 support was declared.

All that to say – we’ll have to wait on the next release of the Eclipse compiler for Java in order to support this preview feature.