PsiTypeParameterList (@Nullable final PsiTypeParameterList fromList, Condition<? super PsiTypeParameter> filter, final PsiElement @NotNull ... elements) { if (elements.length == 0) return null; final Set<PsiTypeParameter> used = new HashSet<>(); for (final PsiElement element : elements) { if (element == null) continue; collectTypeParameters(used, element, filter); //pull up extends cls class with type params } collectTypeParametersInDependencies(filter, used); if (fromList != null) { used.retainAll(Arrays.asList(fromList.getTypeParameters())); } PsiTypeParameter[] typeParameters = used.toArray(PsiTypeParameter.EMPTY_ARRAY); Arrays.sort(typeParameters, Comparator.comparingInt(tp -> tp.getTextRange().getStartOffset())); final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(elements[0].getProject()); try { final PsiClass aClass = elementFactory.createClassFromText("class A {}", null); PsiTypeParameterList list = aClass.getTypeParameterList(); assert list != null; for (final PsiTypeParameter typeParameter : typeParameters) { list.add(typeParameter); } return list; } catch (IncorrectOperationException e) { LOG.error(e); assert false; return null; } }