Search in sources :

Example 61 with PsiType

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

the class ArgumentInstruction method inferMixinType.

@Override
@Nullable
public PsiType inferMixinType() {
    PsiElement element = getElement();
    assert element != null;
    GrCall call = findCall(element);
    GrExpression[] arguments = call.getExpressionArguments();
    boolean hasNamed = PsiImplUtil.hasNamedArguments(call.getArgumentList());
    int index = ArrayUtil.indexOf(arguments, element) + (hasNamed ? 1 : 0);
    GroovyResolveResult[] variants = call.getCallVariants((GrReferenceExpression) element);
    PsiType result = null;
    for (GroovyResolveResult variant : variants) {
        GrClosureSignature signature = GrClosureSignatureUtil.createSignature(variant);
        if (signature == null)
            continue;
        if (GrClosureSignatureUtil.mapParametersToArguments(signature, call) != null && !haveNullParameters(call)) {
            return null;
        }
        GrClosureParameter[] parameters = signature.getParameters();
        if (index >= parameters.length)
            continue;
        result = TypesUtil.getLeastUpperBoundNullable(result, parameters[index].getType(), element.getManager());
    }
    return result;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrClosureParameter(org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType) Nullable(org.jetbrains.annotations.Nullable)

Example 62 with PsiType

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

the class TypeInferenceHelper method getInitializerTypeFor.

@Nullable
public static PsiType getInitializerTypeFor(PsiElement element) {
    final PsiElement parent = skipParentheses(element.getParent(), true);
    if (parent instanceof GrAssignmentExpression) {
        if (element instanceof GrIndexProperty) {
            final GrExpression rvalue = ((GrAssignmentExpression) parent).getRValue();
            //don't try to infer assignment type in case of index property because of infinite recursion (example: a[2]+=4)
            return rvalue != null ? rvalue.getType() : null;
        }
        return ((GrAssignmentExpression) parent).getType();
    }
    if (parent instanceof GrTupleExpression) {
        GrTupleExpression list = (GrTupleExpression) parent;
        if (list.getParent() instanceof GrAssignmentExpression) {
            // multiple assignment
            final GrExpression rValue = ((GrAssignmentExpression) list.getParent()).getRValue();
            int idx = list.indexOf(element);
            if (idx >= 0 && rValue != null) {
                PsiType rType = rValue.getType();
                if (rType instanceof GrTupleType) {
                    PsiType[] componentTypes = ((GrTupleType) rType).getComponentTypes();
                    if (idx < componentTypes.length)
                        return componentTypes[idx];
                    return null;
                }
                return PsiUtil.extractIterableTypeParameter(rType, false);
            }
        }
    }
    if (parent instanceof GrUnaryExpression && TokenSets.POSTFIX_UNARY_OP_SET.contains(((GrUnaryExpression) parent).getOperationTokenType())) {
        return ((GrUnaryExpression) parent).getType();
    }
    return null;
}
Also used : GrIndexProperty(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty) GrTupleType(org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType) Nullable(org.jetbrains.annotations.Nullable)

Example 63 with PsiType

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

the class TypeDfaState method getBindings.

Map<String, PsiType> getBindings(Instruction instruction) {
    HashMap<String, PsiType> map = ContainerUtil.newHashMap();
    for (Map.Entry<String, DFAType> entry : myVarTypes.entrySet()) {
        DFAType value = entry.getValue();
        map.put(entry.getKey(), value == null ? null : value.negate(instruction).getResultType());
    }
    return map;
}
Also used : Map(java.util.Map) HashMap(java.util.HashMap) PsiType(com.intellij.psi.PsiType) DFAType(org.jetbrains.plugins.groovy.lang.psi.dataFlow.DFAType)

Example 64 with PsiType

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

the class GroovyNamesUtil method getMethodArgumentsNames.

public static String[] getMethodArgumentsNames(Project project, PsiType[] types) {
    Set<String> uniqNames = new LinkedHashSet<>();
    Set<String> nonUniqNames = new THashSet<>();
    for (PsiType type : types) {
        final SuggestedNameInfo nameInfo = JavaCodeStyleManager.getInstance(project).suggestVariableName(VariableKind.PARAMETER, null, null, type);
        final String name = nameInfo.names[0];
        if (uniqNames.contains(name)) {
            int i = 2;
            while (uniqNames.contains(name + i)) i++;
            uniqNames.add(name + i);
            nonUniqNames.add(name);
        } else {
            uniqNames.add(name);
        }
    }
    final String[] result = new String[uniqNames.size()];
    int i = 0;
    for (String name : uniqNames) {
        result[i] = nonUniqNames.contains(name) ? name + 1 : name;
        i++;
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) THashSet(gnu.trove.THashSet) PsiType(com.intellij.psi.PsiType)

Example 65 with PsiType

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

the class GrNumericBinaryExpressionTypeCalculator method fun.

@Nullable
@Override
public PsiType fun(GrOperatorExpression e) {
    final GroovyResolveResult resolveResult = PsiImplUtil.extractUniqueResult(e.multiResolve(false));
    if (resolveResult.isApplicable() && !PsiUtil.isDGMMethod(resolveResult.getElement())) {
        return ResolveUtil.extractReturnTypeFromCandidate(resolveResult, e, new PsiType[] { e.getRightType() });
    }
    PsiType lType = e.getLeftType();
    PsiType rType = e.getRightType();
    if (TypesUtil.isNumericType(lType) && TypesUtil.isNumericType(rType)) {
        return inferNumericType(lType, rType, e);
    }
    return ResolveUtil.extractReturnTypeFromCandidate(resolveResult, e, new PsiType[] { rType });
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) PsiType(com.intellij.psi.PsiType) Nullable(org.jetbrains.annotations.Nullable)

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