ExpressionUtils { @CheckForNull public static MethodTree getEnclosingMethod(ExpressionTree expr) { Tree result = expr.parent(); while (result != null && !result.is(Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR)) { result = result.parent(); } return (MethodTree) result; } private  ExpressionUtils(); static boolean isSimpleAssignment(AssignmentExpressionTree tree); static boolean isSelectOnThisOrSuper(AssignmentExpressionTree tree); static boolean isSelectOnThisOrSuper(MemberSelectExpressionTree tree); static IdentifierTree extractIdentifier(AssignmentExpressionTree tree); static Optional<Symbol> extractIdentifierSymbol(ExpressionTree tree); static boolean isInvocationOnVariable(MethodInvocationTree mit, @Nullable Symbol variable, boolean defaultReturn); static ExpressionTree skipParentheses(ExpressionTree tree); static boolean isNullLiteral(ExpressionTree tree); static boolean isSecuringByte(ExpressionTree expression); static IdentifierTree methodName(MethodInvocationTree mit); @CheckForNull static MethodTree getEnclosingMethod(ExpressionTree expr); static Optional<Symbol> getAssignedSymbol(ExpressionTree exp); static boolean isThis(ExpressionTree expression); @CheckForNull static Object resolveAsConstant(ExpressionTree tree);  }
@Test void enclosing_method_test() { File file = new File("src/test/files/model/ExpressionEnclosingMethodTest.java"); CompilationUnitTree tree = JParserTestUtils.parse(file); FindAssignment findAssignment = new FindAssignment(); tree.accept(findAssignment); findAssignment.assignments.forEach(a -> { String expectedName = a.firstToken().trivias().get(0).comment().substring(3); MethodTree enclosingMethod = ExpressionUtils.getEnclosingMethod(a); if ("null".equals(expectedName)) { assertThat(enclosingMethod).isNull(); } else { assertThat(enclosingMethod.simpleName().name()).isEqualTo(expectedName); } }); }