void (PsiLambdaExpression lambdaExpression, final Set<PsiVariable> variables, Set<? super String> namesOfVariablesInTheBlock) { PsiElement block = PsiUtil.getTopLevelEnclosingCodeBlock(lambdaExpression, null); if (block == null) { block = lambdaExpression; } block.accept(new JavaRecursiveElementWalkingVisitor() { @Override public void visitVariable(@NotNull PsiVariable variable) { super.visitVariable(variable); if (!(variable instanceof PsiField)) { variables.add(variable); } } }); final PsiResolveHelper helper = PsiResolveHelper.getInstance(lambdaExpression.getProject()); for (Iterator<PsiVariable> iterator = variables.iterator(); iterator.hasNext(); ) { PsiVariable local = iterator.next(); final String localName = local.getName(); if (localName == null || shadowingResolve(localName, lambdaExpression, helper) || !PsiTreeUtil.isAncestor(lambdaExpression, local, false)) { iterator.remove(); namesOfVariablesInTheBlock.add(localName); } } }