private void addSuppressAnnotation()

in src/main/java/org/jetbrains/plugins/spotbugs/intentions/SuppressReportBugIntentionAction.java [206:275]


	private void addSuppressAnnotation(
			final Project project,
			final Editor editor,
			@NotNull final PsiElement container,
			@NotNull final PsiModifierList modifierList,
			final String id
	) throws IncorrectOperationException {

		final String suppressWarningsClassName = getSuppressWarningsClassName(container);
		PsiAnnotation annotation = modifierList.findAnnotation(suppressWarningsClassName);
		if (annotation != null) {

			String value = null;
			String justification = null;
			String name;
			final PsiNameValuePair[] attributes = annotation.getParameterList().getAttributes();
			for (PsiNameValuePair attribute : attributes) {
				name = attribute.getName();
				if (name == null || "value".equalsIgnoreCase(name)) { // name is null if annotation use "simple" syntax (=only default value is specified)
					value = attribute.getLiteralValue();
					if (value == null) { // getLiteralValue return null if syntax use array {}
						//noinspection ConstantConditions
						value = attribute.getValue().getText();
						if (value.startsWith("{\"") && value.endsWith("\"}")) {
							value = value.substring(2, value.length() - 2);
						}
					}
				} else if ("justification".equalsIgnoreCase(name)) {
					justification = attribute.getLiteralValue();
				}
			}

			if (value != null) {
				// LATER: respect Java CodeStyle Space settings (because, at the moment, createAnnotationFromText is the only way to create an annotation)
				if (justification != null) {
					value = "value = {\"" + value + "\", \"" + id + "\"}";
					justification = ", justification = \"" + justification + "\"";
				} else {
					value = "{\"" + value + "\", \"" + id + "\"}";
					justification = "";
				}
				final String annotationText = '@' + suppressWarningsClassName + "(" + value + justification + ")\r\n";
				annotation.replace(createAnnotationFromText(project, annotationText, container));
			} else {
				if (!ApplicationManager.getApplication().isUnitTestMode() && editor != null) {
					Messages.showErrorDialog(editor.getComponent(), "Incorrect annotation syntax: " + annotation.getText());
				}
			}

		} else {
			annotation = createAnnotationFromText(project, '@' + suppressWarningsClassName + "(\"" + id + "\")\r\n", container);
			modifierList.addBefore(annotation, modifierList.getFirstChild());
			JavaCodeStyleManager.getInstance(project).shortenClassReferences(modifierList);
			if (needsImportStatement(suppressWarningsClassName)) {
				addImport(project, container);

				final Module module = ModuleUtilCore.findModuleForPsiElement(container.getContainingFile());
				assert module != null;

				final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);

				final String qname = annotation.getQualifiedName();
				assert qname != null;

				if (facade.findClass(qname, container.getContainingFile().getResolveScope()) == null) {
						ApplicationManager.getApplication().invokeLater(() -> addAnnotationsDependency(project, module));
				}
			}
		}
	}