Search in sources :

Example 11 with PsiExpression

use of com.intellij.psi.PsiExpression in project intellij-community by JetBrains.

the class ConstantConditionalExpressionInspection method calculateReplacementExpression.

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();
    }
}
Also used : PsiExpression(com.intellij.psi.PsiExpression)

Example 12 with PsiExpression

use of com.intellij.psi.PsiExpression in project intellij-community by JetBrains.

the class AssertionCanBeIfInspection method doFixImpl.

private static void doFixImpl(@NotNull PsiElement element) {
    final PsiAssertStatement assertStatement = (PsiAssertStatement) element;
    @NonNls final StringBuilder newStatement = new StringBuilder();
    final PsiExpression condition = assertStatement.getAssertCondition();
    newStatement.append("if(").append(BoolUtils.getNegatedExpressionText(condition)).append(") throw new java.lang.AssertionError(");
    final PsiExpression description = assertStatement.getAssertDescription();
    if (description != null) {
        newStatement.append(description.getText());
    }
    newStatement.append(");");
    PsiReplacementUtil.replaceStatement(assertStatement, newStatement.toString());
}
Also used : NonNls(org.jetbrains.annotations.NonNls) PsiExpression(com.intellij.psi.PsiExpression) PsiAssertStatement(com.intellij.psi.PsiAssertStatement)

Example 13 with PsiExpression

use of com.intellij.psi.PsiExpression in project intellij-community by JetBrains.

the class HibernateResourceInspection method isResourceCreation.

protected boolean isResourceCreation(PsiExpression expression) {
    if (!(expression instanceof PsiMethodCallExpression)) {
        return false;
    }
    final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) expression;
    final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
    final String methodName = methodExpression.getReferenceName();
    if (!HardcodedMethodConstants.OPEN_SESSION.equals(methodName)) {
        return false;
    }
    final PsiExpression qualifier = methodExpression.getQualifierExpression();
    return qualifier != null && TypeUtils.expressionHasTypeOrSubtype(qualifier, "org.hibernate.SessionFactory");
}
Also used : PsiExpression(com.intellij.psi.PsiExpression) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) PsiMethodCallExpression(com.intellij.psi.PsiMethodCallExpression)

Example 14 with PsiExpression

use of com.intellij.psi.PsiExpression in project intellij-community by JetBrains.

the class ConstantSubexpressionIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element) throws IncorrectOperationException {
    final PsiJavaToken token;
    if (element instanceof PsiJavaToken) {
        token = (PsiJavaToken) element;
    } else {
        final PsiElement prevSibling = element.getPrevSibling();
        if (prevSibling instanceof PsiJavaToken) {
            token = (PsiJavaToken) prevSibling;
        } else {
            throw new AssertionError();
        }
    }
    final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression) element.getParent();
    final PsiPolyadicExpression subexpression = ConstantSubexpressionPredicate.getSubexpression(polyadicExpression, token);
    if (subexpression == null) {
        return;
    }
    final Object value = ExpressionUtils.computeConstantExpression(subexpression);
    @NonNls final StringBuilder newExpressionText = new StringBuilder();
    final PsiExpression[] operands = polyadicExpression.getOperands();
    PsiExpression prevOperand = null;
    PsiJavaToken prevToken = null;
    for (PsiExpression operand : operands) {
        final PsiJavaToken currentToken = polyadicExpression.getTokenBeforeOperand(operand);
        if (token == currentToken) {
            if (prevToken != null) {
                newExpressionText.append(prevToken.getText());
            }
            if (newExpressionText.length() > 0) {
                newExpressionText.append(' ');
            }
            if (value instanceof Long) {
                newExpressionText.append(value).append('L');
            } else if (value instanceof Double) {
                final double v = ((Double) value).doubleValue();
                if (Double.isNaN(v)) {
                    newExpressionText.append("java.lang.Double.NaN");
                } else if (Double.isInfinite(v)) {
                    if (v > 0.0) {
                        newExpressionText.append("java.lang.Double.POSITIVE_INFINITY");
                    } else {
                        newExpressionText.append("java.lang.Double.NEGATIVE_INFINITY");
                    }
                } else {
                    newExpressionText.append(Double.toString(v));
                }
            } else if (value instanceof Float) {
                final float v = ((Float) value).floatValue();
                if (Float.isNaN(v)) {
                    newExpressionText.append("java.lang.Float.NaN");
                } else if (Float.isInfinite(v)) {
                    if (v > 0.0F) {
                        newExpressionText.append("java.lang.Float.POSITIVE_INFINITY");
                    } else {
                        newExpressionText.append("java.lang.Float.NEGATIVE_INFINITY");
                    }
                } else {
                    newExpressionText.append(Float.toString(v)).append('f');
                }
            } else {
                newExpressionText.append(value);
            }
            prevOperand = null;
            prevToken = null;
        } else {
            if (prevToken != null) {
                newExpressionText.append(prevToken.getText());
            }
            if (prevOperand != null) {
                newExpressionText.append(prevOperand.getText());
            }
            prevOperand = operand;
            prevToken = currentToken;
        }
    }
    if (prevToken != null) {
        newExpressionText.append(prevToken.getText());
    }
    if (prevOperand != null) {
        newExpressionText.append(prevOperand.getText());
    }
    PsiReplacementUtil.replaceExpression(polyadicExpression, newExpressionText.toString());
}
Also used : NonNls(org.jetbrains.annotations.NonNls) PsiJavaToken(com.intellij.psi.PsiJavaToken) PsiExpression(com.intellij.psi.PsiExpression) PsiPolyadicExpression(com.intellij.psi.PsiPolyadicExpression) PsiElement(com.intellij.psi.PsiElement)

Example 15 with PsiExpression

use of com.intellij.psi.PsiExpression in project intellij-community by JetBrains.

the class FlipExpressionIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element) {
    final PsiJavaToken token = (PsiJavaToken) element;
    final PsiElement parent = token.getParent();
    if (!(parent instanceof PsiPolyadicExpression)) {
        return;
    }
    final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression) parent;
    final PsiExpression[] operands = polyadicExpression.getOperands();
    final StringBuilder newExpression = new StringBuilder();
    String prevOperand = null;
    // 2- -1 without the space is not legal
    final String tokenText = token.getText() + ' ';
    for (PsiExpression operand : operands) {
        final PsiJavaToken token1 = polyadicExpression.getTokenBeforeOperand(operand);
        if (token == token1) {
            newExpression.append(operand.getText()).append(tokenText);
            continue;
        }
        if (prevOperand != null) {
            newExpression.append(prevOperand).append(tokenText);
        }
        prevOperand = operand.getText();
    }
    newExpression.append(prevOperand);
    PsiReplacementUtil.replaceExpression(polyadicExpression, newExpression.toString());
}
Also used : PsiJavaToken(com.intellij.psi.PsiJavaToken) PsiExpression(com.intellij.psi.PsiExpression) PsiPolyadicExpression(com.intellij.psi.PsiPolyadicExpression) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiExpression (com.intellij.psi.PsiExpression)72 PsiElement (com.intellij.psi.PsiElement)28 PsiReferenceExpression (com.intellij.psi.PsiReferenceExpression)13 PsiMethodCallExpression (com.intellij.psi.PsiMethodCallExpression)11 Nullable (org.jetbrains.annotations.Nullable)9 PsiReference (com.intellij.psi.PsiReference)8 PsiType (com.intellij.psi.PsiType)8 NonNls (org.jetbrains.annotations.NonNls)8 PsiClass (com.intellij.psi.PsiClass)7 PsiAssignmentExpression (com.intellij.psi.PsiAssignmentExpression)6 PsiExpressionStatement (com.intellij.psi.PsiExpressionStatement)6 PsiLocalVariable (com.intellij.psi.PsiLocalVariable)6 PsiMethod (com.intellij.psi.PsiMethod)6 PsiNewExpression (com.intellij.psi.PsiNewExpression)6 PsiPolyadicExpression (com.intellij.psi.PsiPolyadicExpression)6 PsiStatement (com.intellij.psi.PsiStatement)6 Project (com.intellij.openapi.project.Project)5 PsiBinaryExpression (com.intellij.psi.PsiBinaryExpression)5 PsiDeclarationStatement (com.intellij.psi.PsiDeclarationStatement)5 PsiElementFactory (com.intellij.psi.PsiElementFactory)5