VariablesProcessor (final PsiDeclarationStatement declaration, final PsiType type) { VariablesProcessor proc = new VariablesProcessor(false) { @Override protected boolean check(PsiVariable var, ResolveState state) { for (PsiElement element : declaration.getDeclaredElements()) { if (element == var || var instanceof LightElement) return false; } return TypeConversionUtil.isAssignable(var.getType(), type); } }; PsiElement scope = declaration; while (scope != null) { if (scope instanceof PsiFile || scope instanceof PsiMethod || scope instanceof PsiLambdaExpression || scope instanceof PsiClassInitializer) break; scope = scope.getParent(); } if (scope == null) return proc; PsiScopesUtil.treeWalkUp(proc, declaration, scope); return proc; }