void (@NotNull PsiElement methodOrClass, @NotNull String name) { Project project = methodOrClass.getProject(); PsiTypeParameterListOwner typeParameterListOwner = tryCast(methodOrClass, PsiTypeParameterListOwner.class); if (typeParameterListOwner == null) { throw new IllegalStateException("Only methods and classes allowed here, but was: " + methodOrClass.getClass()); } PsiTypeParameterList typeParameterList = typeParameterListOwner.getTypeParameterList(); final String typeParameterListText; if (typeParameterList == null) { typeParameterListText = "<" + name + ">"; } else { String existingTypeParameterText = typeParameterList.getText(); if (typeParameterList.getTypeParameters().length == 0) { typeParameterListText = "<" + name + ">"; } else { String prefix = existingTypeParameterText.substring(0, existingTypeParameterText.length() - 1); typeParameterListText = prefix + ", " + name + ">"; } } PsiTypeParameterList newTypeParameterList = createTypeParameterList(typeParameterListText, project); replaceOrAddTypeParameterList(methodOrClass, typeParameterList, newTypeParameterList); }