PsiMethod (PsiClass clazz, String beforeClassAnnotationName, String beforeAnnotationName) { PsiMethod method = findSetUpMethod(clazz); if (method != null) return method; PsiManager manager = clazz.getManager(); PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory(); method = createSetUpPatternMethod(factory); PsiMethod existingMethod = clazz.findMethodBySignature(method, false); if (existingMethod != null) { if (AnnotationUtil.isAnnotated(existingMethod, beforeClassAnnotationName, 0)) return existingMethod; int exit = ApplicationManager.getApplication().isUnitTestMode() ? Messages.OK : Messages.showOkCancelDialog(JUnitBundle.message("create.setup.dialog.message", "@Before"), JUnitBundle.message("create.setup.dialog.title"), Messages.getWarningIcon()); if (exit == Messages.OK) { new AddAnnotationFix(beforeAnnotationName, existingMethod).invoke(existingMethod.getProject(), null, existingMethod.getContainingFile()); return existingMethod; } } final PsiMethod testMethod = JUnitUtil.findFirstTestMethod(clazz); if (testMethod != null) { method = (PsiMethod)clazz.addBefore(method, testMethod); } else { method = (PsiMethod)clazz.add(method); } JavaCodeStyleManager.getInstance(manager.getProject()).shortenClassReferences(method); return method; }