@Override protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException { PsiConditionalExpression cond = (PsiConditionalExpression)element.getParent(); PsiElement savedBlock; if (cond.getElseExpression() == element) { savedBlock = element; } else { savedBlock = cond.getThenExpression(); } context.extractElement(savedBlock, cond); if (cond.getParent() instanceof PsiExpressionList) { context.delete(cond); } else { context.deleteExactly(cond); } }
@Override public void visitConditionalExpression( PsiConditionalExpression expression) { super.visitConditionalExpression(expression); final PsiExpression condition = expression.getCondition(); final PsiExpression thenExpression = expression.getThenExpression(); if (thenExpression == null) { return; } final PsiExpression elseExpression = expression.getElseExpression(); if (elseExpression == null) { return; } if (BoolUtils.isFalse(condition) || BoolUtils.isTrue(condition)) { registerError(expression, expression); } }
@Override public void visitConditionalExpression( PsiConditionalExpression expression) { super.visitConditionalExpression(expression); final PsiExpression thenExpression = expression.getThenExpression(); if (thenExpression == null) { return; } final PsiExpression elseExpression = expression.getElseExpression(); if (elseExpression == null) { return; } if (BoolUtils.isFalse(thenExpression) && BoolUtils.isTrue(elseExpression) || BoolUtils.isTrue(thenExpression) && BoolUtils.isFalse(elseExpression)) { registerError(expression, expression); } }
@Override public void visitConditionalExpression(PsiConditionalExpression expression) { super.visitConditionalExpression(expression); final PsiExpression thenBranch = expression.getThenExpression(); if (thenBranch == null) { return; } final PsiExpression elseBranch = expression.getElseExpression(); if (elseBranch == null) { return; } final PsiExpression condition = expression.getCondition(); if (!ExpressionUtils.isNegation(condition, m_ignoreNegatedNullComparison, false)) { return; } registerError(condition); }
public boolean satisfiedBy(PsiElement element) { if (!(element instanceof PsiConditionalExpression)) { return false; } final PsiConditionalExpression condition = (PsiConditionalExpression)element; PsiExpression thenExpression = condition.getThenExpression(); PsiExpression elseExpression = condition.getElseExpression(); if (thenExpression == null || elseExpression == null) { return false; } thenExpression = ParenthesesUtils.stripParentheses(thenExpression); elseExpression = ParenthesesUtils.stripParentheses(elseExpression); if (thenExpression == null || elseExpression == null) { return false; } @NonNls final String thenText = thenExpression.getText(); @NonNls final String elseText = elseExpression.getText(); if ("true".equals(elseText) && "false".equals(thenText)) { return !ErrorUtil.containsError(element); } else if ("true".equals(thenText) && "false".equals(elseText)) { return !ErrorUtil.containsError(element); } return false; }
@Override @NotNull public String buildErrorString(Object... infos) { final PsiConditionalExpression expression = (PsiConditionalExpression)infos[0]; return InspectionGadgetsBundle.message("simplifiable.conditional.expression.problem.descriptor", calculateReplacementExpression(expression)); }
@NonNls static String calculateReplacementExpression(PsiConditionalExpression expression) { final PsiExpression thenExpression = expression.getThenExpression(); final PsiExpression elseExpression = expression.getElseExpression(); final PsiExpression condition = expression.getCondition(); assert thenExpression != null; assert elseExpression != null; if (EquivalenceChecker.expressionsAreEquivalent(BoolUtils.getNegated(thenExpression), elseExpression)) { return ParenthesesUtils.getText(condition, ParenthesesUtils.EQUALITY_PRECEDENCE) + " != " + BoolUtils.getNegatedExpressionText(thenExpression, ParenthesesUtils.EQUALITY_PRECEDENCE); } else if (EquivalenceChecker.expressionsAreEquivalent(thenExpression, BoolUtils.getNegated(elseExpression))) { return ParenthesesUtils.getText(condition, ParenthesesUtils.EQUALITY_PRECEDENCE) + " == " + ParenthesesUtils.getText(thenExpression, ParenthesesUtils.EQUALITY_PRECEDENCE); } if (BoolUtils.isTrue(thenExpression)) { final String elseExpressionText = ParenthesesUtils.getText(elseExpression, ParenthesesUtils.OR_PRECEDENCE); return ParenthesesUtils.getText(condition, ParenthesesUtils.OR_PRECEDENCE) + " || " + elseExpressionText; } else if (BoolUtils.isFalse(thenExpression)) { @NonNls final String elseExpressionText = ParenthesesUtils.getText(elseExpression, ParenthesesUtils.AND_PRECEDENCE); return BoolUtils.getNegatedExpressionText(condition, ParenthesesUtils.AND_PRECEDENCE) + " && " + elseExpressionText; } if (BoolUtils.isFalse(elseExpression)) { @NonNls final String thenExpressionText = ParenthesesUtils.getText(thenExpression, ParenthesesUtils.AND_PRECEDENCE); return ParenthesesUtils.getText(condition, ParenthesesUtils.AND_PRECEDENCE) + " && " + thenExpressionText; } else { @NonNls final String thenExpressionText = ParenthesesUtils.getText(thenExpression, ParenthesesUtils.OR_PRECEDENCE); return BoolUtils.getNegatedExpressionText(condition, ParenthesesUtils.OR_PRECEDENCE) + " || " + thenExpressionText; } }
@Override public void visitConditionalExpression(PsiConditionalExpression expression) { super.visitConditionalExpression(expression); final PsiExpression thenExpression = expression.getThenExpression(); if (thenExpression == null) { return; } final PsiExpression elseExpression = expression.getElseExpression(); if (elseExpression == null) { return; } final PsiType thenType = thenExpression.getType(); if (!PsiType.BOOLEAN.equals(thenType)) { return; } final PsiType elseType = elseExpression.getType(); if (!PsiType.BOOLEAN.equals(elseType)) { return; } final boolean thenConstant = BoolUtils.isFalse(thenExpression) || BoolUtils.isTrue(thenExpression); final boolean elseConstant = BoolUtils.isFalse(elseExpression) || BoolUtils.isTrue(elseExpression); if (thenConstant == elseConstant) { if (EquivalenceChecker.expressionsAreEquivalent(BoolUtils.getNegated(thenExpression), elseExpression) || EquivalenceChecker.expressionsAreEquivalent(thenExpression, BoolUtils.getNegated(elseExpression))) { registerError(expression, expression); } return; } registerError(expression, expression); }
@Override @NotNull public String buildErrorString(Object... infos) { final PsiConditionalExpression expression = (PsiConditionalExpression)infos[0]; return InspectionGadgetsBundle.message( "constant.conditional.expression.problem.descriptor", calculateReplacementExpression(expression)); }
static String calculateReplacementExpression( PsiConditionalExpression exp) { final PsiExpression thenExpression = exp.getThenExpression(); final PsiExpression elseExpression = exp.getElseExpression(); final PsiExpression condition = exp.getCondition(); assert thenExpression != null; assert elseExpression != null; if (BoolUtils.isTrue(condition)) { return thenExpression.getText(); } else { return elseExpression.getText(); } }
@Override public void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException { final PsiConditionalExpression expression = (PsiConditionalExpression)descriptor.getPsiElement(); final String newExpression = calculateReplacementExpression(expression); PsiReplacementUtil.replaceExpression(expression, newExpression); }
@Override @NotNull public String buildErrorString(Object... infos) { final PsiConditionalExpression expression = (PsiConditionalExpression)infos[0]; return InspectionGadgetsBundle.message( "simplifiable.conditional.expression.problem.descriptor", calculateReplacementExpression(expression)); }
static String calculateReplacementExpression( PsiConditionalExpression exp) { final PsiExpression thenExpression = exp.getThenExpression(); final PsiExpression elseExpression = exp.getElseExpression(); final PsiExpression condition = exp.getCondition(); if (BoolUtils.isFalse(thenExpression) && BoolUtils.isTrue(elseExpression)) { return BoolUtils.getNegatedExpressionText(condition); } else { return condition.getText(); } }
@Override public void visitConditionalExpression( PsiConditionalExpression expression) { super.visitConditionalExpression(expression); if (PsiTreeUtil.getParentOfType(expression, PsiConditionalExpression.class) == null) { return; } registerError(expression); }
@Override public void visitConditionalExpression(PsiConditionalExpression expression) { super.visitConditionalExpression(expression); final PsiExpression thenExpression = expression.getThenExpression(); final PsiExpression elseExpression = expression.getElseExpression(); if (thenExpression != null && EquivalenceChecker.expressionsAreEquivalent(thenExpression, elseExpression)) { registerError(expression); } }
@Override protected void processIntention(@NotNull PsiElement element) { final PsiConditionalExpression conditionalExpression = (PsiConditionalExpression)element; final String newExpression = conditionalExpression.getCondition().getText() + '?' + BoolUtils.getNegatedExpressionText(conditionalExpression.getThenExpression()) + ':' + BoolUtils.getNegatedExpressionText(conditionalExpression.getElseExpression()); replaceExpressionWithNegatedExpressionString(newExpression, conditionalExpression); }
@Override public boolean satisfiedBy(PsiElement element) { if (!(element instanceof PsiConditionalExpression)) { return false; } final PsiConditionalExpression conditionalExpression = (PsiConditionalExpression)element; final PsiType type = conditionalExpression.getType(); return PsiType.BOOLEAN.equals(type); }
public boolean satisfiedBy(PsiElement element) { if (!(element instanceof PsiConditionalExpression)) { return false; } final PsiConditionalExpression condition = (PsiConditionalExpression)element; if (condition.getThenExpression() == null || condition.getElseExpression() == null) { return false; } return !ErrorUtil.containsError(element); }
@Override public void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException { final PsiConditionalExpression expression = (PsiConditionalExpression)descriptor.getPsiElement(); final String newExpression = calculateReplacementExpression(expression); replaceExpression(expression, newExpression); }
@Override public void visitConditionalExpression( PsiConditionalExpression expression) { super.visitConditionalExpression(expression); final PsiExpression thenExpression = expression.getThenExpression(); if (thenExpression == null) { return; } final PsiType thenType = thenExpression.getType(); if (thenType != PsiType.BOOLEAN) { return; } final PsiExpression elseExpression = expression.getElseExpression(); if (elseExpression == null) { return; } final PsiType elseType = elseExpression.getType(); if (elseType != PsiType.BOOLEAN) { return; } final boolean thenConstant = BoolUtils.isFalse(thenExpression) || BoolUtils.isTrue(thenExpression); final boolean elseConstant = BoolUtils.isFalse(elseExpression) || BoolUtils.isTrue(elseExpression); if (thenConstant == elseConstant) { return; } registerError(expression, expression); }
@Override public void visitConditionalExpression( PsiConditionalExpression expression) { super.visitConditionalExpression(expression); final PsiExpression thenExpression = expression.getThenExpression(); final PsiExpression elseExpression = expression.getElseExpression(); if (EquivalenceChecker.expressionsAreEquivalent(thenExpression, elseExpression)) { registerError(expression); } }