IG: extract note to constant

This commit is contained in:
Bas Leijdekkers
2018-01-01 14:08:38 +01:00
parent 171f715eef
commit 4770f2be1b
@@ -91,6 +91,7 @@ public class ComparableImplementedButEqualsNotOverriddenInspection extends BaseI
private static class AddNoteFix extends InspectionGadgetsFix {
private static final Pattern PARAM_PATTERN = Pattern.compile("\\*[ \t]+@");
private static final String NOTE = " * Note: this class has a natural ordering that is inconsistent with equals.\n";
@Nls
@NotNull
@@ -103,28 +104,17 @@ public class ComparableImplementedButEqualsNotOverriddenInspection extends BaseI
protected void doFix(Project project, ProblemDescriptor descriptor) {
final PsiClass aClass = (PsiClass)descriptor.getPsiElement().getParent();
final PsiDocComment comment = aClass.getDocComment();
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
if (comment == null) {
final PsiDocComment newComment = factory.createDocCommentFromText(
"/**\n" +
"* Note: this class has a natural ordering that is inconsistent with equals.\n" +
"*/", aClass);
final PsiDocComment newComment = JavaPsiFacade.getElementFactory(project).createDocCommentFromText("/**\n" + NOTE + "*/", aClass);
aClass.addBefore(newComment, aClass.getFirstChild());
}
else {
final String text = comment.getText();
final Matcher matcher = PARAM_PATTERN.matcher(text);
String newCommentText;
if (matcher.find()) {
newCommentText = text.substring(0, matcher.start()) +
" * Note: this class has a natural ordering that is inconsistent with equals.\n" +
text.substring(matcher.start());
}
else {
newCommentText = text.substring(0, text.length() - 2) +
" * Note: this class has a natural ordering that is inconsistent with equals.\n*/";
}
final PsiDocComment newComment = factory.createDocCommentFromText(newCommentText);
final String newCommentText = matcher.find()
? text.substring(0, matcher.start()) + NOTE + text.substring(matcher.start())
: text.substring(0, text.length() - 2) + NOTE + "*/";
final PsiDocComment newComment = JavaPsiFacade.getElementFactory(project).createDocCommentFromText(newCommentText);
comment.replace(newComment);
}
}