Search in sources :

Example 6 with PsiExpression

use of com.intellij.psi.PsiExpression in project timber by JakeWharton.

the class WrongTimberUsageDetector method checkTagLength.

private static void checkTagLength(JavaContext context, PsiMethodCallExpression call) {
    PsiExpression argument = call.getArgumentList().getExpressions()[0];
    String tag = findLiteralValue(argument);
    if (tag != null && tag.length() > 23) {
        String message = String.format("The logging tag can be at most 23 characters, was %1$d (%2$s)", tag.length(), tag);
        context.report(ISSUE_TAG_LENGTH, argument, context.getLocation(argument), message);
    }
}
Also used : PsiExpression(com.intellij.psi.PsiExpression)

Example 7 with PsiExpression

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

the class InputValidator method isOK.

public boolean isOK(IntroduceVariableSettings settings) {
    String name = settings.getEnteredName();
    final PsiElement anchor;
    final boolean replaceAllOccurrences = settings.isReplaceAllOccurrences();
    if (replaceAllOccurrences) {
        anchor = myAnchorStatementIfAll;
    } else {
        anchor = myAnchorStatement;
    }
    final PsiElement scope = anchor.getParent();
    if (scope == null)
        return true;
    final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    final HashSet<PsiVariable> reportedVariables = new HashSet<>();
    JavaUnresolvableLocalCollisionDetector.CollidingVariableVisitor visitor = new JavaUnresolvableLocalCollisionDetector.CollidingVariableVisitor() {

        public void visitCollidingElement(PsiVariable collidingVariable) {
            if (!reportedVariables.contains(collidingVariable)) {
                reportedVariables.add(collidingVariable);
                String message = RefactoringBundle.message("introduced.variable.will.conflict.with.0", RefactoringUIUtil.getDescription(collidingVariable, true));
                conflicts.putValue(collidingVariable, message);
            }
        }
    };
    JavaUnresolvableLocalCollisionDetector.visitLocalsCollisions(anchor, name, scope, anchor, visitor);
    if (replaceAllOccurrences) {
        final PsiExpression[] occurences = myOccurenceManager.getOccurrences();
        for (PsiExpression occurence : occurences) {
            IntroduceVariableBase.checkInLoopCondition(occurence, conflicts);
        }
    } else {
        IntroduceVariableBase.checkInLoopCondition(myOccurenceManager.getMainOccurence(), conflicts);
    }
    if (conflicts.size() > 0) {
        return myIntroduceVariableBase.reportConflicts(conflicts, myProject, settings);
    } else {
        return true;
    }
}
Also used : JavaUnresolvableLocalCollisionDetector(com.intellij.refactoring.rename.JavaUnresolvableLocalCollisionDetector) PsiVariable(com.intellij.psi.PsiVariable) PsiExpression(com.intellij.psi.PsiExpression) MultiMap(com.intellij.util.containers.MultiMap) PsiElement(com.intellij.psi.PsiElement) HashSet(com.intellij.util.containers.HashSet)

Example 8 with PsiExpression

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

the class ReplaceParameterAssignmentWithCall method fixUsage.

public void fixUsage() throws IncorrectOperationException {
    final PsiAssignmentExpression assignment = PsiTreeUtil.getParentOfType(expression, PsiAssignmentExpression.class);
    assert assignment != null;
    final PsiExpression rhs = assignment.getRExpression();
    if (rhs == null) {
        return;
    }
    final String rhsText = rhs.getText();
    final String operator = assignment.getOperationSign().getText();
    final String newExpression;
    if ("=".equals(operator)) {
        newExpression = newParameterName + '.' + setterName + '(' + rhsText + ')';
    } else {
        final String strippedOperator = operator.substring(0, operator.length() - 1);
        newExpression = newParameterName + '.' + setterName + '(' + newParameterName + '.' + getterName + "()" + strippedOperator + rhsText + ')';
    }
    MutationUtils.replaceExpression(newExpression, assignment);
}
Also used : PsiExpression(com.intellij.psi.PsiExpression) PsiAssignmentExpression(com.intellij.psi.PsiAssignmentExpression)

Example 9 with PsiExpression

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

the class SimplifiableConditionalExpressionInspection method calculateReplacementExpression.

@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.getCanonicalPsiEquivalence().expressionsAreEquivalent(BoolUtils.getNegated(thenExpression), elseExpression)) {
        return ParenthesesUtils.getText(condition, ParenthesesUtils.EQUALITY_PRECEDENCE) + " != " + BoolUtils.getNegatedExpressionText(thenExpression, ParenthesesUtils.EQUALITY_PRECEDENCE);
    } else if (EquivalenceChecker.getCanonicalPsiEquivalence().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;
    }
}
Also used : NonNls(org.jetbrains.annotations.NonNls) PsiExpression(com.intellij.psi.PsiExpression) NonNls(org.jetbrains.annotations.NonNls)

Example 10 with PsiExpression

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

the class AddThisQualifierFix method doFix.

@Override
public void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
    final PsiReferenceExpression expression = (PsiReferenceExpression) descriptor.getPsiElement();
    if (expression.getQualifierExpression() != null) {
        return;
    }
    final PsiExpression thisQualifier = ExpressionUtils.getQualifierOrThis(expression);
    @NonNls final String newExpression = thisQualifier.getText() + "." + expression.getText();
    PsiReplacementUtil.replaceExpressionAndShorten(expression, newExpression);
}
Also used : NonNls(org.jetbrains.annotations.NonNls) PsiExpression(com.intellij.psi.PsiExpression) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression)

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