Use compiled pattern instead of dynamic regexp in checks. #1555

Fixes some `SpellDynamicRegexReplaceableByCompiledPattern` inspection violations.

Description:
>Reports calls to the regular expression methods of java.lang.String using constants arguments. Such calls may be profitably replaced with a private static final Pattern field so that the regular expression does not have to be compiled each time it is used.
This commit is contained in:
Michal Kordas 2015-08-25 20:19:41 +02:00 committed by Roman Ivanov
parent e23b8a2ffa
commit 3fca3c9250
2 changed files with 11 additions and 4 deletions

View File

@ -313,6 +313,9 @@ public class CustomImportOrderCheck extends Check {
/** NON_GROUP group name. */
private static final String NON_GROUP_RULE_GROUP = "NOT_ASSIGNED_TO_ANY_GROUP";
/** Pattern used to separate groups of imports */
private static final Pattern GROUP_SEPARATOR_PATTERN = Pattern.compile("\\s*###\\s*");
/** RegExp for SAME_PACKAGE group imports */
private String samePackageDomainsRegExp = "";
@ -399,8 +402,7 @@ public class CustomImportOrderCheck extends Check {
*/
public final void setCustomImportOrderRules(final String inputCustomImportOrder) {
customImportOrderRules.clear();
for (String currentState : inputCustomImportOrder
.split("\\s*###\\s*")) {
for (String currentState : GROUP_SEPARATOR_PATTERN.split(inputCustomImportOrder)) {
addRuleastoList(currentState);
}
customImportOrderRules.add(NON_GROUP_RULE_GROUP);

View File

@ -72,6 +72,11 @@ public class SummaryJavadocCheck extends AbstractJavadocCheck {
* file.
*/
public static final String SUMMARY_JAVADOC = "summary.javaDoc";
/**
* This regexp is used to convert multiline javdoc to single line without stars.
*/
private static final Pattern JAVADOC_MULTILINE_TO_SINGLELINE_PATTERN =
Pattern.compile("\n[ ]+(\\*)|^[ ]+(\\*)");
/** Period literal. */
private static final String PERIOD = ".";
@ -178,8 +183,8 @@ public class SummaryJavadocCheck extends AbstractJavadocCheck {
* @return true, if first sentence contains forbidden summary fragment.
*/
private boolean containsForbiddenFragment(String firstSentence) {
// This regexp is used to convert multiline javdoc to single line without stars.
String javadocText = firstSentence.replaceAll("\n[ ]+(\\*)|^[ ]+(\\*)", " ");
String javadocText = JAVADOC_MULTILINE_TO_SINGLELINE_PATTERN
.matcher(firstSentence).replaceAll(" ");
javadocText = CharMatcher.WHITESPACE.trimAndCollapseFrom(javadocText, ' ');
return forbiddenSummaryFragments.matcher(javadocText).find();
}