Search in sources :

Example 11 with PsiType

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

the class SimplifyTernaryOperatorIntention method getElementPredicate.

@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
    return new PsiElementPredicate() {

        @Override
        public boolean satisfiedBy(PsiElement element) {
            if (!(element instanceof GrConditionalExpression)) {
                return false;
            }
            GrConditionalExpression condExp = (GrConditionalExpression) element;
            PsiType condType = condExp.getType();
            if (condType == null || !PsiType.BOOLEAN.isConvertibleFrom(condType)) {
                return false;
            }
            GrExpression thenBranch = condExp.getThenBranch();
            GrExpression elseBranch = condExp.getElseBranch();
            Object thenVal = GroovyConstantExpressionEvaluator.evaluate(thenBranch);
            if (Boolean.TRUE.equals(thenVal) && elseBranch != null) {
                return true;
            }
            Object elseVal = GroovyConstantExpressionEvaluator.evaluate(elseBranch);
            if (thenBranch != null && Boolean.FALSE.equals(elseVal)) {
                return true;
            }
            return false;
        }
    };
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) GrConditionalExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrConditionalExpression) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with PsiType

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

the class ConvertIntegerToDecimalPredicate method satisfiedBy.

@Override
public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof GrLiteral)) {
        return false;
    }
    final GrLiteral expression = (GrLiteral) element;
    final PsiType type = expression.getType();
    if (type == null) {
        return false;
    }
    if (!PsiType.INT.equals(type) && !PsiType.LONG.equals(type) && !type.equalsToText("java.lang.Integer") && !type.equalsToText("java.lang.Long")) {
        return false;
    }
    @NonNls final String text = expression.getText().replaceAll("_", "");
    if (text == null || text.length() < 2) {
        return false;
    }
    if ("0".equals(text) || "0L".equals(text) || "0l".equals(text)) {
        return false;
    }
    return text.charAt(0) == '0';
}
Also used : NonNls(org.jetbrains.annotations.NonNls) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) PsiType(com.intellij.psi.PsiType)

Example 13 with PsiType

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

the class ConvertIntegerToOctalPredicate method satisfiedBy.

@Override
public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof GrLiteral))
        return false;
    final GrLiteral expression = (GrLiteral) element;
    final PsiType type = expression.getType();
    if (type == null)
        return false;
    if (!PsiType.INT.equals(type) && !PsiType.LONG.equals(type) && !type.equalsToText("java.lang.Integer") && !type.equalsToText("java.lang.Long")) {
        return false;
    }
    @NonNls final String text = expression.getText();
    if (text == null || text.isEmpty()) {
        return false;
    }
    if (text.startsWith("0x") || text.startsWith("0X")) {
        return true;
    }
    if (text.startsWith("0b") || text.startsWith("0B")) {
        return true;
    }
    if ("0".equals(text) || "0L".equals(text)) {
        return false;
    }
    return text.charAt(0) != '0';
}
Also used : NonNls(org.jetbrains.annotations.NonNls) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) PsiType(com.intellij.psi.PsiType)

Example 14 with PsiType

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

the class IndexingMethodConversionPredicate method satisfiedBy.

@Override
public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof GrMethodCallExpression)) {
        return false;
    }
    if (ErrorUtil.containsError(element)) {
        return false;
    }
    final GrMethodCallExpression callExpression = (GrMethodCallExpression) element;
    final GrArgumentList argList = callExpression.getArgumentList();
    final GrExpression[] arguments = argList.getExpressionArguments();
    final GrExpression invokedExpression = callExpression.getInvokedExpression();
    if (!(invokedExpression instanceof GrReferenceExpression)) {
        return false;
    }
    final GrReferenceExpression referenceExpression = (GrReferenceExpression) invokedExpression;
    final GrExpression qualifier = referenceExpression.getQualifierExpression();
    if (qualifier == null) {
        return false;
    }
    final IElementType referenceType = referenceExpression.getDotTokenType();
    if (!GroovyTokenTypes.mDOT.equals(referenceType)) {
        return false;
    }
    final String methodName = referenceExpression.getReferenceName();
    if ("getAt".equals(methodName)) {
        return arguments.length == 1;
    }
    if ("get".equals(methodName)) {
        final PsiType qualifierType = qualifier.getType();
        if (!isMap(qualifierType)) {
            return false;
        }
        return arguments.length == 1;
    } else if ("setAt".equals(methodName)) {
        return arguments.length == 2;
    } else if ("put".equals(methodName)) {
        final PsiType qualifierType = qualifier.getType();
        if (!isMap(qualifierType)) {
            return false;
        }
        return arguments.length == 2;
    }
    return false;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) PsiType(com.intellij.psi.PsiType)

Example 15 with PsiType

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

the class ConvertIntegerToBinaryPredicate method satisfiedBy.

@Override
public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof GrLiteral))
        return false;
    final GrLiteral expression = (GrLiteral) element;
    final PsiType type = expression.getType();
    if (type == null)
        return false;
    if (!PsiType.INT.equals(type) && !PsiType.LONG.equals(type) && !type.equalsToText("java.lang.Integer") && !type.equalsToText("java.lang.Long")) {
        return false;
    }
    @NonNls final String text = expression.getText();
    if (text == null || text.isEmpty()) {
        return false;
    }
    if (text.startsWith("0x") || text.startsWith("0X")) {
        return true;
    }
    if (text.startsWith("0b") || text.startsWith("0B")) {
        return false;
    }
    if ("0".equals(text) || "0L".equals(text)) {
        return true;
    }
    return text.charAt(0) != '0';
}
Also used : NonNls(org.jetbrains.annotations.NonNls) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) PsiType(com.intellij.psi.PsiType)

Aggregations

PsiType (com.intellij.psi.PsiType)157 PsiElement (com.intellij.psi.PsiElement)34 Nullable (org.jetbrains.annotations.Nullable)24 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)24 NotNull (org.jetbrains.annotations.NotNull)21 PsiClassType (com.intellij.psi.PsiClassType)20 PsiClass (com.intellij.psi.PsiClass)14 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)13 PsiPrimitiveType (com.intellij.psi.PsiPrimitiveType)12 ArrayList (java.util.ArrayList)9 GrTypeElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)9 PsiArrayType (com.intellij.psi.PsiArrayType)7 PsiExpression (com.intellij.psi.PsiExpression)7 PsiField (com.intellij.psi.PsiField)7 NonNls (org.jetbrains.annotations.NonNls)7 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)7 PsiLiteralExpression (com.intellij.psi.PsiLiteralExpression)6 PsiMethod (com.intellij.psi.PsiMethod)6 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)6 GrLiteral (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)6