Search in sources :

Example 91 with GrExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.

the class SimpleBinaryTransformation method apply.

@Override
public void apply(@NotNull GrBinaryExpression expression) {
    GrExpression lhsParenthesized = addParenthesesIfNeeded(getLhs(expression));
    replaceExpression(expression, format("%s.%s(%s)", lhsParenthesized.getText(), myMethod, getRhs(expression).getText()));
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)

Example 92 with GrExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.

the class GroovyPointlessBooleanInspection method calculateSimplifiedBinaryExpression.

@Nullable
private static String calculateSimplifiedBinaryExpression(GrBinaryExpression expression) {
    final IElementType sign = expression.getOperationTokenType();
    final GrExpression lhs = expression.getLeftOperand();
    final GrExpression rhs = expression.getRightOperand();
    if (rhs == null) {
        return null;
    }
    final String rhsText = rhs.getText();
    final String lhsText = lhs.getText();
    assert sign != null;
    if (sign.equals(GroovyTokenTypes.mLAND)) {
        if (isTrue(lhs)) {
            return rhsText;
        } else {
            return lhsText;
        }
    } else if (sign.equals(GroovyTokenTypes.mLOR)) {
        if (isFalse(lhs)) {
            return rhsText;
        } else {
            return lhsText;
        }
    } else if (sign.equals(GroovyTokenTypes.mBXOR) || sign.equals(GroovyTokenTypes.mNOT_EQUAL)) {
        if (isFalse(lhs)) {
            return rhsText;
        } else if (isFalse(rhs)) {
            return lhsText;
        } else if (isTrue(lhs)) {
            return createStringForNegatedExpression(rhs);
        } else {
            return createStringForNegatedExpression(lhs);
        }
    } else if (sign.equals(GroovyTokenTypes.mEQUAL)) {
        if (isTrue(lhs)) {
            return rhsText;
        } else if (isTrue(rhs)) {
            return lhsText;
        } else if (isFalse(lhs)) {
            return createStringForNegatedExpression(rhs);
        } else {
            return createStringForNegatedExpression(lhs);
        }
    } else {
        return "";
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 93 with GrExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.

the class ConditionalUtils method isReturn.

public static boolean isReturn(GrStatement statement, String value) {
    if (statement == null) {
        return false;
    }
    if (!(statement instanceof GrReturnStatement)) {
        return false;
    }
    final GrReturnStatement returnStatement = (GrReturnStatement) statement;
    final GrExpression returnValue = returnStatement.getReturnValue();
    if (returnValue == null) {
        return false;
    }
    final String returnValueText = returnValue.getText();
    return value.equals(returnValueText);
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)

Example 94 with GrExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.

the class GroovyTrivialConditionalInspection method calculateReplacementExpression.

private static String calculateReplacementExpression(GrConditionalExpression exp) {
    final GrExpression thenExpression = exp.getThenBranch();
    final GrExpression elseExpression = exp.getElseBranch();
    final GrExpression condition = exp.getCondition();
    if (isFalse(thenExpression) && isTrue(elseExpression)) {
        return BoolUtils.getNegatedExpressionText(condition);
    } else {
        return condition.getText();
    }
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)

Example 95 with GrExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.

the class UnaryTransformation method apply.

@Override
public void apply(@NotNull GrUnaryExpression expression) {
    GrExpression operand = addParenthesesIfNeeded(requireNonNull(getOperand(expression)));
    GrInspectionUtil.replaceExpression(expression, format("%s.%s()", operand.getText(), myMethod));
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)

Aggregations

GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)312 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)93 Nullable (org.jetbrains.annotations.Nullable)68 PsiElement (com.intellij.psi.PsiElement)62 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)43 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)43 NotNull (org.jetbrains.annotations.NotNull)37 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)35 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)33 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)29 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)27 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)26 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)26 PsiType (com.intellij.psi.PsiType)24 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)23 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)23 IElementType (com.intellij.psi.tree.IElementType)22 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)18 ArrayList (java.util.ArrayList)16 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)16