Sonar Analyzer Java 8 Lambda Bug

507 views
Skip to first unread message

smontg...@gmail.com

unread,
Dec 22, 2016, 6:16:46 AM12/22/16
to SonarQube
Hi,
FYI, Saw a few other lambda issues with Sonar analysis in the mailing list but here is another for you.

org.sonarsource.scanner.maven:sonar-maven-plugin = 3.2
SonarQube version = 6.1 and 6.2

mvn sonar:sonar

...
[INFO] 4 source files to be analyzed
[ERROR] Unable to create symbol table for : xxxx/xxxxx/xxxx.java
java.lang.IndexOutOfBoundsException: index (0) must be less than size (0)
at com.google.common.base.Preconditions.checkElementIndex(Preconditions.java:310)
at com.google.common.base.Preconditions.checkElementIndex(Preconditions.java:293)
at com.google.common.collect.RegularImmutableList.get(RegularImmutableList.java:67)
at org.sonar.java.resolve.TypeAndReferenceSolver.visitLambdaExpression(TypeAndReferenceSolver.java:520)
at org.sonar.java.model.expression.LambdaExpressionTreeImpl.accept(LambdaExpressionTreeImpl.java:91)
at org.sonar.java.resolve.TypeAndReferenceSolver.setInferedType(TypeAndReferenceSolver.java:308)
at org.sonar.java.resolve.TypeAndReferenceSolver.visitAssignmentExpression(TypeAndReferenceSolver.java:830)
at org.sonar.java.model.expression.AssignmentExpressionTreeImpl.accept(AssignmentExpressionTreeImpl.java:71)
at org.sonar.plugins.java.api.tree.BaseTreeVisitor.scan(BaseTreeVisitor.java:43)
at org.sonar.plugins.java.api.tree.BaseTreeVisitor.visitExpressionStatement(BaseTreeVisitor.java:101)
at org.sonar.java.resolve.TypeAndReferenceSolver.visitExpressionStatement(TypeAndReferenceSolver.java:762)
at org.sonar.java.model.statement.ExpressionStatementTreeImpl.accept(ExpressionStatementTreeImpl.java:65)

Sonar analyzer didn't like Java 8 lambda:

principal = () -> "blah";

Changing it to back to using anonymous class works:

principal = new Principal() {
                @Override
                public String getName() {
                    return "blah";
                }
};

Thanks for a great product.

Stephen


Nicolas Peru

unread,
Dec 22, 2016, 7:06:27 AM12/22/16
to smontg...@gmail.com, SonarQube
Hi, 

Can you specify which version of SonarJava you are using ? 
Can you try with the latest version (4.3.0 at time of writing) thanks.

Cheers, 

--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/3a51c63e-0d22-46de-88f0-2f7d9900b478%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Nicolas PERU | SonarSource
Senior Developer
http://sonarsource.com

smontg...@gmail.com

unread,
Dec 22, 2016, 7:50:10 AM12/22/16
to SonarQube, smontg...@gmail.com

Hi,
Sure - this is with latest SonarQube 6.2 install with all plugins updated. We're using the latest Java Plugin of 4.3.0.7717.

Stephen

Nicolas Peru

unread,
Dec 22, 2016, 8:41:14 AM12/22/16
to smontg...@gmail.com, SonarQube
Hi, 
Would you be able to share a bit more the context of that lambda ? Especially the type of the principal variable, and if it is a field or a local var  ? And if the code you show is an assignement or a declaration. 
All in all, I am very interested by the smallest reproducer you can come up with. 

Thanks

Le jeu. 22 déc. 2016 à 12:16, <smontg...@gmail.com> a écrit :
--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/3a51c63e-0d22-46de-88f0-2f7d9900b478%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nicolas Peru

unread,
Dec 22, 2016, 8:52:30 AM12/22/16
to smontg...@gmail.com, SonarQube
Hi, 

Ok I got it : Principal in your code is java.security.Principal 

So what's happening is that the semantic analysis picks up the wrong method as the one from the SAM and we have a difference in argument types. 
(we pick up the equals method because it is the first declared). 

Ticket created to handle the issue : https://jira.sonarsource.com/browse/SONARJAVA-2031

Thanks a lot for reporting this one ! 

Cheers, 
Reply all
Reply to author
Forward
0 new messages