Custom rule: check invocation order

Hi everyone,

I’m writing a custom rule to check one method called before another on the same variable. Understand that it can depend on runtime information, but it’s not my case.

Here is a test code for this example:

class A
{ 	
    void method()  {
	    ClassB x1 = getInstanceWithMandatory();	  
	    x1.get(); // Compliant

	    ClassB x2 = getInstance();	  
	    x2.get(); // Noncompliant: mandatoryMethod() not called before get()
     }

   void getInstanceWithMandatory() {
       ClassB x = new ClassB();
       x.mandatoryMethod(); 
       return x;
   }

    void getInstance() {
       return new ClassB();
    }

}

I found a similar rule was implemented OptionalGetBeforeIsPresentCheck using ProgramState / Constraints approach, but not sure is it an option for my case and it’s not covered in the documentation.

What would you recommend to implement such a rule, please?

Hi,

To be clear, you’re writing a rule for Java?

 
Ann

Hi,

You are right, a rule for Java.

Hi,

I’ve added the java tag to draw the attention of the people who can actually help you. (as opposed to me :smile:)

 
Ann