How to get the content in the method declaration of the test file in the custom rule

I am using the visitMethod (Methodtree)()
to try to get the contents of the test file. I used methodTree.block().body().get(0) to get the StatementTree object, and then found the content I wanted in the blue content. These contents can be seen in the debug state, but these attributes are not available in the actual development.

Friend Dorian here suggested that I use the visitMethodInvocation(MethodInvocationTree tree) to find the method declaration and count. I actually don’t know how to use it (the address is here).

I haven’t replied for a long time. I’m anxious to solve this problem, so I opened another topic. Could you guys give me some suggestions? I want to get the data in the method body and then write the Java rule.

Kevin

Please,please,could you kindly give me a hint?
I really have a lot of confusion here.
The biggest difficulty is that I know nothing about visitMethodInvocation, and this method does not support debug, which means that I do not know which method or attribute can get the data I want.
When I use System.out.println to try to view some properties and methods (such as tree.methodSelect), I find that this content is not reflected in the console in the debug log.
Thank you for supporting this forum.

Best,
Jin

Hi @KevinJin,
What you are seeing in the debug window are the implementation types. You may not be able to use those directly in your code but they always implement some interface/abstract class that you can cast objects to (with MemberSelectExpressionTreeImpl implements MemberSelectExpressionTree).

var expression = statementTree.expression();
if (expression.is(Tree.Kind.MEMBER_SELECT)) {
  var memberSelect = (MemberSelectTree) expression;
}

Then you should be able to get whatever you want from the right types.

Hi @Dorian_Burihabwa ,
Thank you very much for your reply. I have solved this problem earlier, and the method used is consistent with what you said.
Thank you for your technical support to this community. I hope this post can help people with the same problems as me.

1 Like