void (PsiMethod method, String... exceptionNames) { final PsiReferenceList throwsList = method.getThrowsList(); final PsiClassType[] referencedTypes = throwsList.getReferencedTypes(); final Set<String> presentExceptionNames = new HashSet<>(); for (PsiClassType referencedType : referencedTypes) { final String exceptionName = referencedType.getCanonicalText(); presentExceptionNames.add(exceptionName); } final Project project = method.getProject(); final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project); final GlobalSearchScope scope = method.getResolveScope(); for (String exceptionName : exceptionNames) { if (presentExceptionNames.contains(exceptionName)) { continue; } final PsiJavaCodeReferenceElement throwsReference = factory.createReferenceElementByFQClassName(exceptionName, scope); final PsiElement element = throwsList.add(throwsReference); codeStyleManager.shortenClassReferences(element); } }