Get parent of method invocation - Java

Hi,

I’m currently writting a custom rule for Java project.

For exemple I hava this code to analyze :

 class MyObject {
   List<String> properties = Arrays.asList("test");

  void mythirdMethod(){
	  this.properties.clear(); // Compliant

  }
}

I can get “clear()” with Kind.METHOD_INVOCATION.
But I don’t know how to get “properties” from the method invocation.

I tried call method “parent()”, but it returns an expression statement object. So I can’t have access to the variable properties.

Do you know how to access variable from method invocation ?

Thanks

Hey,

In order to get properties, you need to look at the expression used to select the clear() method, not at the parent.

From the MethodInvocationTree, get the expression used as methodSelect(). Then look at its kind, it should be a MEMBER_SELECT (containing clear as identifier, and this.properties as expression). From there, you should be able to get back to the properties field.

Cheers,
Michael

Thank you very much !

It works now and I can have access to this.properties :grinning: