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();
}
}
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());
}
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");
}
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());
}
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());
}
Aggregations