void (PsiElement scope, final PsiSubstitutor substitutor, final PsiElementFactory factory) { final Map<PsiElement, PsiElement> replacement = new HashMap<>(); scope.accept(new JavaRecursiveElementVisitor() { @Override public void visitTypeElement(@NotNull PsiTypeElement typeElement) { super.visitTypeElement(typeElement); PsiType type = typeElement.getType(); if (type instanceof PsiClassType) { JavaResolveResult resolveResult = ((PsiClassType)type).resolveGenerics(); PsiElement resolved = resolveResult.getElement(); if (resolved instanceof PsiTypeParameter) { PsiType newType = resolveResult.getSubstitutor().putAll(substitutor).substitute((PsiTypeParameter)resolved); if (newType instanceof PsiCapturedWildcardType) { newType = ((PsiCapturedWildcardType)newType).getUpperBound(); } if (newType instanceof PsiWildcardType) { newType = ((PsiWildcardType)newType).getBound(); } if (newType == null) { newType = PsiType.getJavaLangObject(resolved.getManager(), resolved.getResolveScope()); } try { replacement.put(typeElement, factory.createTypeElement(newType)); } catch (IncorrectOperationException e) { LOG.error(e); } } } } }); for (PsiElement element : replacement.keySet()) { if (element.isValid()) { element.replace(replacement.get(element)); } } }