Get parent of method invocation - Java


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(){; // 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 ?



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 as expression). From there, you should be able to get back to the properties field.


Thank you very much !

It works now and I can have access to :grinning: