Custom Sonar Rule: Require methods when class is used as a parameter to a particular method


(Benjamin Kohler) #1

I am trying to create a custom rule for sonarqube.

I want to search for times when a method is called for example:

ExClass classInstance = new ExClass();
classInstance.methodCall(a, b);
where methodCall is defined as:

public void methodCall(Object a, Object b){}

What I want is to verify that any class passed as an argument into methodCall() has overridden the methods equals() and hashCode().

I have already found the source code for the EqualsOverridenWithHashCodeCheck, so I can verify that classes have in fact overriden both equals and hashCode, however, I don’t know how to qualify that this should /only/ be done when the class if found as an argument to methodCall.

Any suggestions?

EqualsOverrideWithHashCodeCheck source:

FYI - Question duplicated on stackexchange:

(Michael Gumowski) #3


There is a dedicated template rule for that: squid:S2253 - Track uses of disallowed methods

Couldn’t you use this rule instead of writing a custom rule?