PsiType (@NotNull PsiType type, @NotNull PsiElement context) { if (type instanceof PsiClassType) { PsiClassType.ClassResolveResult result = ((PsiClassType)type).resolveGenerics(); PsiClass aClass = result.getElement(); if (aClass != null) { PsiSubstitutor substitutor = result.getSubstitutor(); PsiSubstitutor captureSubstitutor = substitutor; for (PsiTypeParameter typeParameter : typeParametersIterable(aClass)) { PsiType substituted = substitutor.substitute(typeParameter); if (substituted instanceof PsiWildcardType) { captureSubstitutor = captureSubstitutor.put(typeParameter, PsiCapturedWildcardType.create((PsiWildcardType)substituted, context, typeParameter)); } } if (captureSubstitutor != substitutor) { Map<PsiTypeParameter, PsiType> substitutionMap = null; for (PsiTypeParameter typeParameter : typeParametersIterable(aClass)) { PsiType substituted = substitutor.substitute(typeParameter); if (substituted instanceof PsiWildcardType) { if (substitutionMap == null) substitutionMap = new HashMap<>(substitutor.getSubstitutionMap()); PsiCapturedWildcardType capturedWildcard = (PsiCapturedWildcardType)captureSubstitutor.substitute(typeParameter); LOG.assertTrue(capturedWildcard != null); PsiType upperBound = PsiCapturedWildcardType.captureUpperBound(typeParameter, (PsiWildcardType)substituted, captureSubstitutor); if (upperBound != null) { capturedWildcard.setUpperBound(upperBound); } substitutionMap.put(typeParameter, capturedWildcard); } } if (substitutionMap != null) { PsiElementFactory factory = JavaPsiFacade.getElementFactory(aClass.getProject()); PsiSubstitutor newSubstitutor = factory.createSubstitutor(substitutionMap); return factory.createType(aClass, newSubstitutor); } } } } return type; }