# Get parent of method invocation - Java

**URL:** <https://community.sonarsource.com/t/get-parent-of-method-invocation-java/10073>\
**Category:** Writing rules\
**Tags:** custom\_rules, java\
**Created:** [May 23, 2019, 12:30pm UTC](https://community.sonarsource.com/t/get-parent-of-method-invocation-java/10073 "2019-05-23T12:30:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![per.flo](https://avatars.discourse-cdn.com/v4/letter/p/7cd45c/32.png) [@per.flo](https://community.sonarsource.com/u/per.flo)\
**Post date:** [May 23, 2019, 12:30pm UTC](https://community.sonarsource.com/t/get-parent-of-method-invocation-java/10073/1 "2019-05-23T12:30:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/michael/32/43_2.png) [@Michael](https://community.sonarsource.com/u/Michael)\
**Post date:** [May 24, 2019, 3:07pm UTC](https://community.sonarsource.com/t/get-parent-of-method-invocation-java/10073/2 "2019-05-24T15:07:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![per.flo](https://avatars.discourse-cdn.com/v4/letter/p/7cd45c/32.png) [@per.flo](https://community.sonarsource.com/u/per.flo)\
**Post date:** [May 28, 2019, 7:07am UTC](https://community.sonarsource.com/t/get-parent-of-method-invocation-java/10073/3 "2019-05-28T07:07:10Z")

</div>

Thank you very much !

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