private static void collectConditions(PsiExpression condition, Set<PsiExpression> conditions, IElementType tokenType) { if (condition == null) { return; } if (condition instanceof PsiParenthesizedExpression) { final PsiParenthesizedExpression parenthesizedExpression = (PsiParenthesizedExpression)condition; final PsiExpression contents = parenthesizedExpression.getExpression(); collectConditions(contents, conditions, tokenType); return; } if (condition instanceof PsiPolyadicExpression) { final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)condition; final IElementType testTokeType = polyadicExpression.getOperationTokenType(); if (testTokeType.equals(tokenType)) { final PsiExpression[] operands = polyadicExpression.getOperands(); for (PsiExpression operand : operands) { collectConditions(operand, conditions, tokenType); } return; } } conditions.add(condition); }
private void collectConditionsForExpression(PsiExpression condition, Set<PsiExpression> conditions) { if (condition == null) { return; } if (condition instanceof PsiParenthesizedExpression) { final PsiParenthesizedExpression parenthesizedExpression = (PsiParenthesizedExpression)condition; final PsiExpression contents = parenthesizedExpression.getExpression(); collectConditionsForExpression(contents, conditions); return; } if (condition instanceof PsiPolyadicExpression) { final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)condition; final IElementType tokenType = polyadicExpression.getOperationTokenType(); if (JavaTokenType.OROR.equals(tokenType)) { final PsiExpression[] operands = polyadicExpression.getOperands(); for (PsiExpression operand : operands) { collectConditionsForExpression(operand, conditions); } return; } } conditions.add(condition); }
/** * 識別子の this 式判定 * @param id 識別子ノード * @return 識別子ノードが、this式の名称か否か (true:this式の識別子、false:this式の識別子でない) */ public static boolean isThisExpression(@NonNull PsiIdentifier id) { // 名称がthis式の識別名か否かをチェック List<PsiElement> elderBrothers = ElementUtil.getElderBrother(id); boolean isThisExpr = false; for (PsiElement brother : elderBrothers) { isThisExpr = isThisExpr || (brother instanceof PsiThisExpression) || (brother instanceof PsiParenthesizedExpression); } return isThisExpr; }
@Override public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException { if (psiElement instanceof PsiParenthesizedExpression) { final PsiElement lastChild = psiElement.getLastChild(); if (lastChild != null && !")".equals(lastChild.getText())) { editor.getDocument().insertString(psiElement.getTextRange().getEndOffset(), ")"); } } }
@Override public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException { if(psiElement instanceof PsiParenthesizedExpression) { final PsiElement lastChild = psiElement.getLastChild(); if(lastChild != null && !")".equals(lastChild.getText())) { editor.getDocument().insertString(psiElement.getTextRange().getEndOffset(), ")"); } } }
private static void replaceMultiplyOrDivideWithShift( PsiBinaryExpression expression) throws IncorrectOperationException { final PsiExpression lhs = expression.getLOperand(); final PsiExpression rhs = expression.getROperand(); final IElementType tokenType = expression.getOperationTokenType(); final String operatorString; if (tokenType.equals(JavaTokenType.ASTERISK)) { operatorString = "<<"; } else { operatorString = ">>"; } final String lhsText; if (ParenthesesUtils.getPrecedence(lhs) > ParenthesesUtils.SHIFT_PRECEDENCE) { lhsText = '(' + lhs.getText() + ')'; } else { lhsText = lhs.getText(); } String expString = lhsText + operatorString + ShiftUtils.getLogBase2(rhs); final PsiElement parent = expression.getParent(); if (parent instanceof PsiExpression) { if (!(parent instanceof PsiParenthesizedExpression) && ParenthesesUtils.getPrecedence((PsiExpression)parent) < ParenthesesUtils.SHIFT_PRECEDENCE) { expString = '(' + expString + ')'; } } replaceExpression(expString, expression); }
private static void replaceShiftWithMultiplyOrDivide(PsiElement element) throws IncorrectOperationException { final PsiBinaryExpression exp = (PsiBinaryExpression)element; final PsiExpression lhs = exp.getLOperand(); final PsiExpression rhs = exp.getROperand(); final IElementType tokenType = exp.getOperationTokenType(); final String operatorString; if (tokenType.equals(JavaTokenType.LTLT)) { operatorString = "*"; } else { operatorString = "/"; } final String lhsText; if (ParenthesesUtils.getPrecedence(lhs) > ParenthesesUtils.MULTIPLICATIVE_PRECEDENCE) { lhsText = '(' + lhs.getText() + ')'; } else { lhsText = lhs.getText(); } String expString = lhsText + operatorString + ShiftUtils.getExpBase2(rhs); final PsiElement parent = exp.getParent(); if (parent instanceof PsiExpression) { if (!(parent instanceof PsiParenthesizedExpression) && ParenthesesUtils.getPrecedence((PsiExpression)parent) < ParenthesesUtils.MULTIPLICATIVE_PRECEDENCE) { expString = '(' + expString + ')'; } } replaceExpression(expString, exp); }
@Override public void visitParenthesizedExpression(PsiParenthesizedExpression expression) { mVisitor.report("PsiParenthesizedExpression", expression.getText(), expression); super.visitExpression(expression); }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { final PsiElement element = descriptor.getPsiElement(); final PsiElement parent; if(element instanceof PsiReferenceExpression) { parent = element.getParent(); } else { parent = element; } if(!(parent instanceof PsiAssignmentExpression)) { return; } final PsiExpression rExpression = ((PsiAssignmentExpression) parent).getRExpression(); final PsiElement gParent = parent.getParent(); if((gParent instanceof PsiExpression || gParent instanceof PsiExpressionList) && rExpression != null) { if(!FileModificationService.getInstance().prepareFileForWrite(gParent.getContainingFile())) { return; } if(gParent instanceof PsiParenthesizedExpression) { gParent.replace(rExpression); } else { parent.replace(rExpression); } return; } PsiElement resolve = null; if(element instanceof PsiReferenceExpression) { resolve = ((PsiReferenceExpression) element).resolve(); } else { final PsiExpression lExpr = PsiUtil.deparenthesizeExpression(((PsiAssignmentExpression) parent).getLExpression()); if(lExpr instanceof PsiReferenceExpression) { resolve = ((PsiReferenceExpression) lExpr).resolve(); } } if(!(resolve instanceof PsiVariable)) { return; } sideEffectAwareRemove(project, rExpression, parent, (PsiVariable) resolve); }
@Override public void visitPolyadicExpression(PsiPolyadicExpression expression) { super.visitPolyadicExpression(expression); final IElementType tokenType = expression.getOperationTokenType(); if (!tokenType.equals(JavaTokenType.ANDAND) && !tokenType.equals(JavaTokenType.OROR)) { return; } PsiElement parent = expression.getParent(); while (parent instanceof PsiParenthesizedExpression) { parent = parent.getParent(); } if (parent instanceof PsiBinaryExpression) { final PsiBinaryExpression parentExpression = (PsiBinaryExpression)parent; if (tokenType.equals(parentExpression.getOperationTokenType())) { return; } } final Set<PsiExpression> conditions = new HashSet<PsiExpression>(); collectConditions(expression, conditions, tokenType); final int numConditions = conditions.size(); if (numConditions < 2) { return; } final PsiExpression[] conditionArray = conditions.toArray(new PsiExpression[numConditions]); final boolean[] matched = new boolean[conditionArray.length]; Arrays.fill(matched, false); for (int i = 0; i < conditionArray.length; i++) { if (matched[i]) { continue; } final PsiExpression condition = conditionArray[i]; for (int j = i + 1; j < conditionArray.length; j++) { if (matched[j]) { continue; } final PsiExpression testCondition = conditionArray[j]; final boolean areEquivalent = EquivalenceChecker.getCanonicalPsiEquivalence().expressionsAreEquivalent(condition, testCondition); if (areEquivalent) { registerError(testCondition); if (!matched[i]) { registerError(condition); } matched[i] = true; matched[j] = true; } } } }