[Java] FP on rule squid:S1905 - Redundant casts should not be used

239 views
Skip to first unread message

David Racodon

unread,
Oct 23, 2016, 1:19:20 PM10/23/16
to SonarQube
Hi,

I believe that I am facing a false positive on rule squid:S1905 - Redundant casts should not be used: https://sonarqube.com/issues/search#issues=AVfyhShpdWSMUWoxk8nF

Regards,

David RACODON
Freelance QA Consultant

Brian Sperlongano

unread,
Oct 23, 2016, 6:34:25 PM10/23/16
to SonarQube
I believe this finding is correct - the cast can be safely removed, and in fact, you have a redundant set of parentheses as well.

The prototype of get annotation is:

public <A extends Annotation> A getAnnotation(Class<A> annotationClass)

Because it is typed <A extends Annotation>, the Rule.class passed in as the parameter causes the return value to be Rule at compile time.

Have you tried removing the cast?

Brian Sperlongano

unread,
Oct 23, 2016, 6:46:44 PM10/23/16
to SonarQube
Hi -

Further follow-up -

Here is the java documentation that describes type inference.  A very nifty trick!


Brian

David Racodon

unread,
Oct 24, 2016, 3:31:14 AM10/24/16
to Brian Sperlongano, SonarQube
Hi Brian,

Thanks for your feedback.

However, updating the piece of code as follows:
public static String paramsErrorMessage(Class clazz, String message) {
return "Check jproperties:" + clazz.getAnnotation(Rule.class).key()
+ " (" + ((Rule) clazz.getAnnotation(Rule.class)).name() + "): "
+ message;
}
I get the following error while compiling:

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile (default-compile) on project jproperties-checks: Compilation failure
[ERROR] /home/racodond/git/sonar-jproperties-plugin/jproperties-checks/src/main/java/org/sonar/jproperties/checks/CheckUtils.java:[33,66] cannot find symbol
[ERROR] symbol:   method key()
[ERROR] location: interface java.lang.annotation.Annotation


Regards,

David RACODON
Freelance QA Consultant

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/b554c374-52d9-47cb-a21b-c65d9d0e4562%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Nicolas Peru

unread,
Oct 24, 2016, 3:42:06 AM10/24/16
to David Racodon, Brian Sperlongano, SonarQube
Hi David, 

Are you compiling this using java 7 ? 


To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.

--
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/CACTFSAjf86_DPDvGJr2BxCgmtjL9dPScdOem0xDWNLh85tsiDQ%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.
--
Nicolas PERU | SonarSource
Senior Developer
http://sonarsource.com

David Racodon

unread,
Oct 24, 2016, 3:55:00 AM10/24/16
to Nicolas Peru, Brian Sperlongano, SonarQube
Hi Nicolas,

Java 8 I believe.

In my pom file:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

racodond@racodond-XPS-15:~/git/sonar-jproperties-plugin$ javac -version
javac 1.8.0_45

Also, I use 'stream' and compiling is working fine.

Regards,

David RACODON
Freelance QA Consultant

To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+unsubscribe@googlegroups.com.

--
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+unsubscribe@googlegroups.com.

David Racodon

unread,
Oct 31, 2016, 2:12:36 PM10/31/16
to Nicolas Peru, Brian Sperlongano, SonarQube
Hi,

Any feedback on this matter?

Thank you

Regards,

David RACODON
Freelance QA Consultant

David Racodon

unread,
Nov 17, 2016, 7:10:18 AM11/17/16
to Nicolas Peru, Brian Sperlongano, SonarQube
Hi Nicolas,

Do you have enough information to investigate?
Could I help you in any way?

Thank you

Regards,

David RACODON
Freelance QA Consultant

Nicolas Peru

unread,
Nov 22, 2016, 11:14:44 AM11/22/16
to David Racodon, Brian Sperlongano, SonarQube
Hi David, 

please, no need to bump thread when I answer, I keep track of them.
Took me a bit of time to figure out what is going on

this is indeed a false positive related to that issue  https://jira.sonarsource.com/browse/SONARJAVA-1616 which I improved with your case.

what does that mean : You are using the raw type of class and so the return type of getAnnotation is erased and so the cast is required.
If you were to use Class<?> as a parameter (for instance, or anything bounded properly) then the return type of getAnnotation would be the class you pass in parameter. 

Relevant SO question on that topic : 
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.

--
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.
--
Nicolas PERU | SonarSource
Senior Developer
http://sonarsource.com

--
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/CACTFSAjObp4Yebh-opmEOS9dv5dA2cqR5VXJOT9phvgQLO8EHw%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

David Racodon

unread,
Nov 27, 2016, 4:29:26 PM11/27/16
to Nicolas Peru, Brian Sperlongano, SonarQube
Thanks for your feedback!

David RACODON
Freelance QA Consultant

To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+unsubscribe@googlegroups.com.

--
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+unsubscribe@googlegroups.com.
--
Nicolas PERU | SonarSource
Senior Developer
http://sonarsource.com

--
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+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages