boolean (final PsiExpression addAllExpression, PsiNewExpression newExpression) { final boolean[] isReplaceable = new boolean[]{true}; final PsiFile newExpressionContainingFile = newExpression.getContainingFile(); final TextRange newExpressionTextRange = newExpression.getTextRange(); addAllExpression.accept(new JavaRecursiveElementVisitor() { @Override public void visitReferenceExpression(@NotNull PsiReferenceExpression expression) { final PsiElement resolved = expression.resolve(); if (PsiUtil.isJvmLocalVariable(resolved)) { PsiVariable variable = (PsiVariable) resolved; final LocalSearchScope useScope = (LocalSearchScope)variable.getUseScope(); if (!useScope.containsRange(newExpressionContainingFile, newExpressionTextRange)) { isReplaceable[0] = false; } } } }); return isReplaceable[0]; }