PsiModifierList (@NotNull PsiModifierList oldModifierList, @NotNull String newModifiersText) { final PsiElementFactory factory = JavaPsiFacade.getElementFactory(oldModifierList.getProject()); PsiElement parent = oldModifierList.getParent(); if (parent instanceof PsiRequiresStatement) { String text = "requires " + newModifiersText + " x;"; PsiRequiresStatement statement = (PsiRequiresStatement) factory.createModuleStatementFromText(text, oldModifierList); return statement.getModifierList(); } else if (parent instanceof PsiClass) { PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement)factory.createStatementFromText(newModifiersText + " class X {}", oldModifierList); return ((PsiClass)declarationStatement.getDeclaredElements()[0]).getModifierList(); } else { PsiMethod method = factory.createMethodFromText(newModifiersText + " void x() {}", oldModifierList); return method.getModifierList(); } }