Search in sources :

Example 46 with PsiExpression

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

the class JNDIResourceInspection method isResourceCreation.

protected boolean isResourceCreation(PsiExpression expression) {
    if (expression instanceof PsiMethodCallExpression) {
        final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) expression;
        final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
        @NonNls final String methodName = methodExpression.getReferenceName();
        if ("list".equals(methodName) || "listBindings".equals(methodName)) {
            final PsiExpression qualifier = methodExpression.getQualifierExpression();
            if (qualifier == null) {
                return false;
            }
            return TypeUtils.expressionHasTypeOrSubtype(qualifier, "javax.naming.Context");
        } else if ("getAll".equals(methodName)) {
            final PsiExpression qualifier = methodExpression.getQualifierExpression();
            if (qualifier == null) {
                return false;
            }
            return TypeUtils.expressionHasTypeOrSubtype(qualifier, "javax.naming.directory.Attribute", "javax.naming.directory.Attributes") != null;
        } else {
            return false;
        }
    } else if (expression instanceof PsiNewExpression) {
        return TypeUtils.expressionHasTypeOrSubtype(expression, "javax.naming.InitialContext");
    }
    return false;
}
Also used : NonNls(org.jetbrains.annotations.NonNls) PsiExpression(com.intellij.psi.PsiExpression) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) PsiNewExpression(com.intellij.psi.PsiNewExpression) PsiMethodCallExpression(com.intellij.psi.PsiMethodCallExpression)

Example 47 with PsiExpression

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

the class SocketResourceInspection method isResourceCreation.

@Override
protected boolean isResourceCreation(PsiExpression expression) {
    if (expression instanceof PsiMethodCallExpression) {
        final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) expression;
        final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
        @NonNls final String methodName = methodExpression.getReferenceName();
        if (!"accept".equals(methodName)) {
            return false;
        }
        final PsiExpression qualifier = methodExpression.getQualifierExpression();
        if (qualifier == null || !TypeUtils.expressionHasTypeOrSubtype(qualifier, "java.net.ServerSocket")) {
            return false;
        }
        return TypeUtils.expressionHasTypeOrSubtype(methodCallExpression, "java.net.Socket");
    } else if (expression instanceof PsiNewExpression) {
        return TypeUtils.expressionHasTypeOrSubtype(expression, "java.net.Socket", "java.net.DatagramSocket", "java.net.ServerSocket") != null;
    }
    return false;
}
Also used : NonNls(org.jetbrains.annotations.NonNls) PsiExpression(com.intellij.psi.PsiExpression) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) PsiNewExpression(com.intellij.psi.PsiNewExpression) PsiMethodCallExpression(com.intellij.psi.PsiMethodCallExpression)

Example 48 with PsiExpression

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

the class ConvertToGrayQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement element = descriptor.getPsiElement();
    final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
    final PsiExpression expression = factory.createExpressionFromText("com.intellij.ui.Gray._" + myNum, element.getContext());
    final PsiElement newElement = element.replace(expression);
    JavaCodeStyleManager.getInstance(project).shortenClassReferences(newElement);
}
Also used : PsiExpression(com.intellij.psi.PsiExpression) PsiElementFactory(com.intellij.psi.PsiElementFactory) PsiElement(com.intellij.psi.PsiElement)

Example 49 with PsiExpression

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

the class ConvertNumberIntentionBase method processIntention.

@Override
protected void processIntention(@NotNull final PsiElement element) throws IncorrectOperationException {
    final PsiExpression expression = (PsiExpression) element;
    final Number value = (Number) ExpressionUtils.computeConstantExpression(expression);
    if (value == null)
        return;
    final PsiType type = expression.getType();
    final boolean negated = ExpressionUtils.isNegative(expression);
    final String resultString = convertValue(value, type, negated);
    if (resultString == null)
        return;
    if (negated) {
        PsiReplacementUtil.replaceExpression((PsiExpression) expression.getParent(), resultString);
    } else {
        PsiReplacementUtil.replaceExpression(expression, resultString);
    }
}
Also used : PsiExpression(com.intellij.psi.PsiExpression) PsiType(com.intellij.psi.PsiType)

Example 50 with PsiExpression

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

the class FlipConditionalIntention method processIntention.

@Override
public void processIntention(PsiElement element) {
    final PsiConditionalExpression exp = (PsiConditionalExpression) element;
    final PsiExpression condition = exp.getCondition();
    final PsiExpression elseExpression = exp.getElseExpression();
    final PsiExpression thenExpression = exp.getThenExpression();
    assert elseExpression != null;
    assert thenExpression != null;
    final String newExpression = BoolUtils.getNegatedExpressionText(condition) + '?' + elseExpression.getText() + ':' + thenExpression.getText();
    PsiReplacementUtil.replaceExpression(exp, newExpression);
}
Also used : PsiExpression(com.intellij.psi.PsiExpression) PsiConditionalExpression(com.intellij.psi.PsiConditionalExpression)

Aggregations

PsiExpression (com.intellij.psi.PsiExpression)67 PsiElement (com.intellij.psi.PsiElement)24 PsiReferenceExpression (com.intellij.psi.PsiReferenceExpression)11 PsiMethodCallExpression (com.intellij.psi.PsiMethodCallExpression)8 NonNls (org.jetbrains.annotations.NonNls)8 PsiLocalVariable (com.intellij.psi.PsiLocalVariable)7 PsiType (com.intellij.psi.PsiType)7 PsiAssignmentExpression (com.intellij.psi.PsiAssignmentExpression)6 PsiExpressionStatement (com.intellij.psi.PsiExpressionStatement)6 PsiPolyadicExpression (com.intellij.psi.PsiPolyadicExpression)6 PsiStatement (com.intellij.psi.PsiStatement)6 Nullable (org.jetbrains.annotations.Nullable)6 PsiBinaryExpression (com.intellij.psi.PsiBinaryExpression)5 PsiClass (com.intellij.psi.PsiClass)5 PsiDeclarationStatement (com.intellij.psi.PsiDeclarationStatement)5 PsiElementFactory (com.intellij.psi.PsiElementFactory)5 PsiField (com.intellij.psi.PsiField)5 PsiReference (com.intellij.psi.PsiReference)5 Nullable (com.android.annotations.Nullable)4 Project (com.intellij.openapi.project.Project)4