java


SonarQube java.lang.ClassCastException: ParametrizedTypeJavaType cannot be cast to ParametrizedTypeJavaType


I am trying to make my own SonarQube rule, the objective of the rule is to check that I am not using the contains method on a collection of a specific object. Example with Integer Object :
List<Integer> a = new ArrayList<>();
a.contains(1); // Noncompliant
To do that I am trying to get the ParametrizedTypeJavaType. Then I will be able to test if it is an Integer or not ...
#Override
public void visitNode(Tree tree) {
MethodInvocationTree methodInvocationTree = (MethodInvocationTree) tree;
ExpressionTree expressionTree = ((MethodInvocationTree) tree).methodSelect();
if(expressionTree.is(Kind.MEMBER_SELECT)){
MemberSelectExpressionTree memberSelectExpressionTree = (MemberSelectExpressionTree) expressionTree;
Type type = memberSelectExpressionTree.expression().symbolType();
ParametrizedTypeJavaType parametrizedTypeJavaType = (ParametrizedTypeJavaType) type;
// SOME CODE Test if it is an integer or not ...
reportIssue(tree,"REPORT !");
}
}
#Override
public List<Kind> nodesToVisit() {
List<Kind> kinds = new ArrayList<>();
kinds.add(Kind.METHOD_INVOCATION);
return kinds;
}
}
It seems to work well during jUnit test but when i launched sonnar-scanner on my test project I get the following error :
Caused by: java.lang.ClassCastException: org.sonar.java.resolve.ParametrizedTypeJavaType cannot be cast to org.sonar.java.resolve.ParametrizedTypeJavaType
at org.sonar.samples.java.checks.CollectionCheck.visitNode(CollectionCheck.java:38)
I have done some research and I came across this question, that looks to be my problem :
Sonarqube error java.lang.ClassCastException: org.sonar.java.resolve.SemanticModel cannot be cast to org.sonar.java.resolve.SemanticModel
I came also across this rule which looks very similar to my rule and uses ParametrizedTypeJavaType.
https://github.com/SonarSource/sonar-java/blob/master/java-checks/src/main/java/org/sonar/java/checks/CollectionInappropriateCallsCheck.java
So I am totally confused. What is the good approach to deal with my problem ?
I made a git for more informations :
https://github.com/Eeezzzddd/java-custom-rules
SonarQube version : 6.3.1
Thanks for your help.

Related Links

Java JSONObject#has throwing exception
Android WiFi Direct - client/server - ECONNREFUSED (Connection refused) UPDATE
In Vim, how can I compile a .java file in /src/ and run its class file from /bin/?
Creating a Frame with MenuBar - Size not recognized by pack(), menubar is being cutoff
Bitmap.createBitmap(src, x, y, width, height) not working as expected
Convert several JComboBoxes, JButtons to a Set<String> in an efficient way
Mockito Matchers.any(…) on one argument only
Does spring FORM_LOGIN_FILTER support ConcurrentSessionControl?
XML check for duplicates
TestNG - Cannot find class in classpath exception though class is present at mentioned location
I got java.util.ArrayList.throwIndexOutOfBoundsException and a stack trace that doesn't have my code in it
properties include properties Struts2
How to send onError if result is invalid in RxJava
Java could not delete file because it is opened in Java SE BINARY
Spring Data - Page Request - order by function
Why do I get an unexpected RuntimeException warn in the logs when using RemoteFileTemplate?

Categories

HOME
openlayers
zeromq
angular-material
fme
grep
at-command
analysis
amortized-analysis
windows-server
echarts
portia
dacpac
gorm
remote-access
hapi
libtiff
graphlab
reactcsstransitiongroup
css-animations
excel-2007
php-openssl
java-7
fish
facet
semantic-analysis
fgetcsv
applozic
elasticsearch-ruby
windows-server-2000
procdump
delicious-api
jspresso
accelerate-framework
environment-modules
overriding
stacked
vapor
ssjs
email-templates
avro4s
ncalc
spring-security-kerberos
galleria
babel-core
errordocument
tizen-native-app
csound
directory-structure
parallel-data-warehouse
xcb
google-feed-api
httplistener
savon
thrust
xml-attribute
contact-list
nsviewcontroller
testng-dataprovider
make-install
pyke
freedesktop.org
separator
python-3.2
spidermonkey
angular-leaflet-directive
drawbitmap
twirl
sailfish-os
operation
mmc
unity5.2.3
camanjs
qpainter
gadt
relocation
phalanger
client-side-templating
gulp-less
ember-charts
access-rights
cdc
aqtime
sitemesh
listings
ora-00911
plasma
orchardcms-1.7
twrequest
nsmanagedobject
removeclass
assembly-loading
invite
actionview
jmock
wise
dentrix
gin
vc90
user-friendly
avatar
spec#

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile