void (@NotNull PsiElement context, @NotNull Processor<? super @NotNull PsiElement> processor) { class Visitor extends JavaRecursiveElementWalkingVisitor { boolean stop = false; @Override public void visitElement(@NotNull PsiElement psiElement) { if (psiElement != context) { PsiElement parent = psiElement.getParent(); // do not process any anonymous class children except its getArgumentList() //if the visitor was not called from inside anonymous class if (parent instanceof PsiAnonymousClass && !(psiElement instanceof PsiExpressionList)) { return; } } stop = !processor.process(psiElement); if (stop) { return; } super.visitElement(psiElement); } @Override public void visitAnonymousClass(@NotNull PsiAnonymousClass aClass) { // process anonymous getArgumentList() visitElement(aClass); } @Override public void visitClass(@NotNull PsiClass aClass) { } @Override public void visitLambdaExpression(@NotNull PsiLambdaExpression expression) { } } Visitor visitor = new Visitor(); context.accept(visitor); }