private static Map<String, PsiType> getCompatibleTypeNames( @NotNull PsiType type, @Nullable PsiType min, PsiManager manager, GlobalSearchScope scope) { if (type instanceof PsiDisjunctionType) type = ((PsiDisjunctionType) type).getLeastUpperBound(); // if initial type is not assignable to min type we don't take into consideration min type. if (min != null && !TypesUtil.isAssignable(min, type, manager, scope)) { min = null; } Map<String, PsiType> map = new LinkedHashMap<String, PsiType>(); final PsiPrimitiveType unboxed = PsiPrimitiveType.getUnboxedType(type); if (unboxed != null) type = unboxed; final Set<PsiType> set = new LinkedHashSet<PsiType>(); set.add(type); while (!set.isEmpty()) { PsiType cur = set.iterator().next(); set.remove(cur); if (!map.containsValue(cur) && (min == null || TypesUtil.isAssignable(min, cur, manager, scope))) { if (isPartiallySubstituted(cur)) { LOG.assertTrue(cur instanceof PsiClassType); PsiClassType rawType = ((PsiClassType) cur).rawType(); map.put(rawType.getPresentableText(), rawType); } else { map.put(cur.getPresentableText(), cur); } for (PsiType superType : cur.getSuperTypes()) { if (!map.containsValue(superType)) { set.add(superType); } } } } return map; }
@Override public void visitBinaryExpression(GrBinaryExpression expression) { super.visitBinaryExpression(expression); if (expression.getOperationTokenType() != GroovyTokenTypes.kIN) return; GrExpression leftOperand = expression.getLeftOperand(); GrExpression rightOperand = expression.getRightOperand(); if (leftOperand == null || rightOperand == null) return; PsiType ltype = leftOperand.getType(); PsiType rtype = rightOperand.getType(); if (ltype == null || rtype == null) return; PsiType component; if (rtype instanceof PsiArrayType) { component = ((PsiArrayType) rtype).getComponentType(); } else if (InheritanceUtil.isInheritor(rtype, CommonClassNames.JAVA_UTIL_COLLECTION)) { component = PsiUtil.substituteTypeParameter(rtype, CommonClassNames.JAVA_UTIL_COLLECTION, 0, false); } else { checkSimpleClasses(ltype, rtype, expression); return; } if (component == null) return; if (TypesUtil.isAssignable( ltype, component, expression.getManager(), expression.getResolveScope(), false)) return; registerError(expression, ltype, rtype); }
@Override public PsiType fun(GrIndexPropertyImpl index) { GrExpression selected = index.getInvokedExpression(); PsiType thisType = selected.getType(); if (thisType == null) return null; GrArgumentList argList = index.getArgumentList(); PsiType[] argTypes = PsiUtil.getArgumentTypes(argList); if (argTypes == null) return null; final PsiManager manager = index.getManager(); final GlobalSearchScope resolveScope = index.getResolveScope(); if (argTypes.length == 0) { PsiType arrType = null; if (selected instanceof GrBuiltinTypeClassExpression) { arrType = ((GrBuiltinTypeClassExpression) selected).getPrimitiveType(); } if (selected instanceof GrReferenceExpression) { final PsiElement resolved = ((GrReferenceExpression) selected).resolve(); if (resolved instanceof PsiClass) { String qname = ((PsiClass) resolved).getQualifiedName(); if (qname != null) { arrType = TypesUtil.createTypeByFQClassName(qname, index); } } } if (arrType != null) { final PsiArrayType param = arrType.createArrayType(); return TypesUtil.createJavaLangClassType(param, index.getProject(), resolveScope); } } if (PsiImplUtil.isSimpleArrayAccess( thisType, argTypes, manager, resolveScope, PsiUtil.isLValue(index))) { return TypesUtil.boxPrimitiveType( ((PsiArrayType) thisType).getComponentType(), manager, resolveScope); } final GroovyResolveResult[] candidates = index.multiResolve(false); PsiType[] args = PsiUtil.getArgumentTypes( argList.getNamedArguments(), argList.getExpressionArguments(), GrClosableBlock.EMPTY_ARRAY, true, null, false); final GroovyResolveResult candidate = PsiImplUtil.extractUniqueResult(candidates); final PsiElement element = candidate.getElement(); if (element instanceof PsiNamedElement) { final String name = ((PsiNamedElement) element).getName(); if ("putAt".equals(name) && args != null) { args = ArrayUtil.append( args, TypeInferenceHelper.getInitializerFor(index), PsiType.class); } } PsiType overloadedOperatorType = ResolveUtil.extractReturnTypeFromCandidate(candidate, index, args); PsiType componentType = extractMapValueType(thisType, args, manager, resolveScope); if (overloadedOperatorType != null && (componentType == null || !TypesUtil.isAssignable( overloadedOperatorType, componentType, manager, resolveScope))) { return TypesUtil.boxPrimitiveType(overloadedOperatorType, manager, resolveScope); } return componentType; }