String (PsiExpression expr) { final Boolean needParenthesis = expr.getCopyableUserData(NEED_PARENTHESIS); if (needParenthesis != null && needParenthesis.booleanValue()) { return JavaBundle.message("introduce.variable.change.semantics.warning"); } if (expr instanceof PsiClassObjectAccessExpression && PsiUtilCore.hasErrorElementChild(expr)) { return JavaRefactoringBundle.message("selected.block.should.represent.an.expression"); } if (!CodeBlockSurrounder.canSurround(expr)) { PsiExpression topLevelExpression = ExpressionUtils.getTopLevelExpression(expr); if (topLevelExpression != expr) { for (PsiVariable variable : VariableAccessUtils.collectUsedVariables(expr)) { if (variable instanceof PsiPatternVariable && PsiTreeUtil.isAncestor(topLevelExpression, variable, true) && !PsiTreeUtil.isAncestor(expr, variable, true)) { return JavaRefactoringBundle.message("introduce.variable.message.expression.refers.to.pattern.variable.declared.outside", variable.getName()); } } } if (topLevelExpression.getParent() instanceof PsiField f && f.getParent() instanceof PsiUnnamedClass) { return JavaRefactoringBundle.message("introduce.variable.message.cannot.extract.in.unnamed.class"); } } return null; }