void (final PsiElement member, final Iterable<? extends PsiTypeParameter> parametersIterable, final PsiSubstitutor substitutor, final GroovyPsiElementFactory factory) { final Map<PsiElement, PsiElement> replacement = new LinkedHashMap<>(); for (PsiTypeParameter parameter : parametersIterable) { PsiType substitutedType = substitutor.substitute(parameter); PsiType type = substitutedType != null ? substitutedType : TypeConversionUtil.erasure(factory.createType(parameter)); PsiElement scopeElement = member instanceof GrField ? member.getParent() : member; for (PsiReference reference : ReferencesSearch.search(parameter, new LocalSearchScope(scopeElement))) { final PsiElement element = reference.getElement(); final PsiElement parent = element.getParent(); if (parent instanceof PsiTypeElement) { replacement.put(parent, factory.createTypeElement(type)); } else if (element instanceof GrCodeReferenceElement && type instanceof PsiClassType) { replacement.put(element, factory.createReferenceElementByType((PsiClassType)type)); } } } final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(member.getProject()); for (PsiElement element : replacement.keySet()) { if (element.isValid()) { final PsiElement replaced = element.replace(replacement.get(element)); codeStyleManager.shortenClassReferences(replaced); } } }