Deleted cache from Utils class + unified setters with patterns

Issue #845
This commit is contained in:
Damian Szczepanik 2015-03-24 21:25:23 +01:00
parent c2c34c8408
commit ebd4afdebe
28 changed files with 123 additions and 236 deletions

View File

@ -589,7 +589,7 @@
<regex><pattern>.*.PropertiesExpander</pattern><branchRate>50</branchRate><lineRate>83</lineRate></regex>
<regex><pattern>.*.PropertyCacheFile</pattern><branchRate>22</branchRate><lineRate>19</lineRate></regex>
<regex><pattern>.*.TreeWalker</pattern><branchRate>90</branchRate><lineRate>90</lineRate></regex>
<regex><pattern>com.puppycrawl.tools.checkstyle.Utils</pattern><branchRate>84</branchRate><lineRate>92</lineRate></regex>
<regex><pattern>com.puppycrawl.tools.checkstyle.Utils</pattern><branchRate>84</branchRate><lineRate>91</lineRate></regex>
<regex><pattern>.*.XMLLogger</pattern><branchRate>86</branchRate><lineRate>97</lineRate></regex>

View File

@ -19,7 +19,6 @@
package com.puppycrawl.tools.checkstyle;
import java.io.File;
import java.util.concurrent.ConcurrentMap;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
@ -27,9 +26,6 @@ import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.google.common.collect.Maps;
import static com.google.common.base.MoreObjects.firstNonNull;
/**
* Contains utility methods.
@ -38,10 +34,6 @@ import static com.google.common.base.MoreObjects.firstNonNull;
*/
public final class Utils
{
/** Map of all created regular expressions **/
private static final ConcurrentMap<String, Pattern> CREATED_RES =
Maps.newConcurrentMap();
/** Shared instance of logger for exception logging. */
private static final Log EXCEPTION_LOG =
LogFactory.getLog("com.puppycrawl.tools.checkstyle.ExceptionLog");
@ -170,7 +162,7 @@ public final class Utils
public static boolean isPatternValid(String pattern)
{
try {
Utils.getPattern(pattern);
Pattern.compile(pattern);
}
catch (final PatternSyntaxException e) {
return false;
@ -178,40 +170,6 @@ public final class Utils
return true;
}
/**
* This is a factory method to return an Pattern object for the specified regular expression. It
* calls {@link #getPattern(String, int)} with the compile flags defaults to 0.
* @return an Pattern object for the supplied pattern
* @param pattern the regular expression pattern
* @throws PatternSyntaxException an invalid pattern was supplied
**/
public static Pattern getPattern(String pattern)
throws PatternSyntaxException
{
return getPattern(pattern, 0);
}
/**
* This is a factory method to return an Pattern object for the specified
* regular expression and compile flags.
* @return an Pattern object for the supplied pattern
* @param pattern the regular expression pattern
* @param compileFlags the compilation flags
* @throws PatternSyntaxException an invalid pattern was supplied
**/
public static Pattern getPattern(String pattern, int compileFlags)
throws PatternSyntaxException
{
final String key = pattern + ":flags-" + compileFlags;
Pattern retVal = CREATED_RES.get(key);
if (retVal == null) {
final Pattern compiledPattern = Pattern.compile(pattern, compileFlags);
retVal = CREATED_RES.putIfAbsent(key, compiledPattern);
retVal = firstNonNull(retVal, compiledPattern);
}
return retVal;
}
/**
* Helper method to create a regular expression.
* @param pattern the pattern to match
@ -221,15 +179,13 @@ public final class Utils
public static Pattern createPattern(String pattern)
throws ConversionException
{
Pattern retVal = null;
try {
retVal = getPattern(pattern);
return Pattern.compile(pattern);
}
catch (final PatternSyntaxException e) {
throw new ConversionException(
"Failed to initialise regexp expression " + pattern, e);
}
return retVal;
}
/**

View File

@ -40,7 +40,6 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.google.common.io.Closeables;
import com.puppycrawl.tools.checkstyle.Utils;
/**
* Represents the text contents of a file of arbitrary plain text type.
@ -65,8 +64,7 @@ public final class FileText extends AbstractList<String>
/**
* Regular expression pattern matching all line terminators.
*/
private static final Pattern LINE_TERMINATOR =
Utils.getPattern("\\n|\\r\\n?");
private static final Pattern LINE_TERMINATOR = Pattern.compile("\\n|\\r\\n?");
// For now, we always keep both full text and lines array.
// In the long run, however, the one passed at initialization might be

View File

@ -19,7 +19,6 @@
package com.puppycrawl.tools.checkstyle.checks;
import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.Utils;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
@ -110,7 +109,7 @@ public abstract class AbstractFormatCheck
private void updateRegexp(String format, int compileFlagsParam)
{
try {
regexp = Utils.getPattern(format, compileFlagsParam);
regexp = Pattern.compile(format, compileFlagsParam);
this.format = format;
compileFlags |= compileFlagsParam;
}

View File

@ -24,7 +24,6 @@ import java.util.regex.Pattern;
import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.Utils;
/**
* <p>
@ -107,27 +106,26 @@ import com.puppycrawl.tools.checkstyle.Utils;
public class AvoidEscapedUnicodeCharactersCheck
extends Check
{
/** Regexp for Unicode chars */
private static Pattern sUnicodeRegexp =
Utils.getPattern("\\\\u[a-fA-F0-9]{4}");
/** Regular expression for Unicode chars */
private static Pattern sUnicodeRegexp = Pattern.compile("\\\\u[a-fA-F0-9]{4}");
/** Regexp Unicode control characters */
private static Pattern sUnicodeControl = Utils.getPattern("\\\\(u|U)"
/** Regular expression Unicode control characters */
private static Pattern sUnicodeControl = Pattern.compile("\\\\(u|U)"
+ "(00[0-1][0-1A-Fa-f]|00[8-9][0-9A-Fa-f]|034(f|F)|070(f|F)"
+ "|180(e|E)|200[b-fB-F]|202[b-eB-E]|206[0-4a-fA-F]"
+ "|[fF]{3}[9a-bA-B]|[fF][eE][fF]{2})");
/** Regexp for trail comment */
private static Pattern sCommentRegexp = Utils.getPattern(";[ ]*//+"
/** Regular expression for trail comment */
private static Pattern sCommentRegexp = Pattern.compile(";[ ]*//+"
+ "[a-zA-Z0-9 ]*|;[ ]*/[*]{1}+[a-zA-Z0-9 ]*");
/** Regexp for all escaped chars*/
/** Regular expression for all escaped chars */
private static Pattern sAllEscapedChars =
Utils.getPattern("^((\\\\u)[a-fA-F0-9]{4}"
Pattern.compile("^((\\\\u)[a-fA-F0-9]{4}"
+ "||\\\\b|\\\\t|\\\\n|\\\\f|\\\\r|\\\\|\\\"|\\\')+$");
/** Regexp for non-printable unicode chars*/
private static Pattern sNonPrintableChars = Utils.getPattern("\\\\u1680|\\\\u2028"
/** Regular expression for non-printable unicode chars */
private static Pattern sNonPrintableChars = Pattern.compile("\\\\u1680|\\\\u2028"
+ "|\\\\u2029|\\\\u205(f|F)|\\\\u3000|\\\\u2007|\\\\u2000|\\\\u200(a|A)"
+ "|\\\\u007(F|f)|\\\\u009(f|F)|\\\\u(f|F){4}|\\\\u007(F|f)|\\\\u00(a|A)(d|D)"
+ "|\\\\u0600|\\\\u061(c|C)|\\\\u06(d|D){2}|\\\\u070(f|F)|\\\\u1680|\\\\u180(e|E)"

View File

@ -20,6 +20,7 @@ package com.puppycrawl.tools.checkstyle.checks;
import java.util.regex.Pattern;
import com.puppycrawl.tools.checkstyle.Utils;
import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
@ -77,12 +78,15 @@ public class TodoCommentCheck
/**
* Setter for todo comment format.
* @param format format of todo comment.
* @param format
* format of todo comment.
* @throws org.apache.commons.beanutils.ConversionException
* if unable to create Pattern object.
*/
public void setFormat(String format)
{
this.format = format;
regexp = Pattern.compile(format);
regexp = Utils.createPattern(format);
}
@Override

View File

@ -26,7 +26,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.apache.commons.beanutils.ConversionException;
/**
@ -115,17 +115,12 @@ public class TrailingCommentCheck extends AbstractFormatCheck
/**
* Sets patter for legal trailing comments.
* @param format format to set.
* @throws ConversionException unable to parse a given format.
* @throws ConversionException if unable to create Pattern object
*/
public void setLegalComment(final String format)
throws ConversionException
{
try {
legalComment = Utils.getPattern(format);
}
catch (final PatternSyntaxException e) {
throw new ConversionException("unable to parse " + format, e);
}
legalComment = Utils.createPattern(format);
}
/**
* Creates new instance of the check.

View File

@ -25,7 +25,6 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.Utils;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.apache.commons.beanutils.ConversionException;
@ -66,20 +65,13 @@ public class UncommentedMainCheck
/**
* Set the excluded classes pattern.
* @param excludedClasses a <code>String</code> value
* @throws ConversionException unable to parse excludedClasses
* @throws ConversionException if unable to create Pattern object
*/
public void setExcludedClasses(String excludedClasses)
throws ConversionException
{
try {
this.excludedClasses = excludedClasses;
excludedClassesPattern = Utils.getPattern(excludedClasses);
}
catch (final PatternSyntaxException e) {
throw new ConversionException("unable to parse "
+ excludedClasses,
e);
}
this.excludedClasses = excludedClasses;
excludedClassesPattern = Utils.createPattern(excludedClasses);
}
@Override

View File

@ -20,6 +20,7 @@ package com.puppycrawl.tools.checkstyle.checks.blocks;
import java.util.regex.Pattern;
import com.puppycrawl.tools.checkstyle.Utils;
import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
@ -153,22 +154,28 @@ public class EmptyCatchBlockCheck extends Check
/**
* Setter for exception's variable name format.
* @param exceptionVariableName format of exception's variable name.
* @param exceptionVariableName
* format of exception's variable name.
* @throws org.apache.commons.beanutils.ConversionException
* if unable to create Pattern object.
*/
public void setExceptionVariableName(String exceptionVariableName)
{
this.exceptionVariableName = exceptionVariableName;
variableNameRegexp = Pattern.compile(exceptionVariableName);
variableNameRegexp = Utils.createPattern(exceptionVariableName);
}
/**
* Setter for comment format.
* @param commentFormat format of comment.
* @param commentFormat
* format of comment.
* @throws org.apache.commons.beanutils.ConversionException
* if unable to create Pattern object.
*/
public void setCommentFormat(String commentFormat)
{
this.commentFormat = commentFormat;
commentRegexp = Pattern.compile(commentFormat);
commentRegexp = Utils.createPattern(commentFormat);
}
@Override

View File

@ -24,7 +24,6 @@ import java.util.regex.Pattern;
import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.Utils;
/**
* Checks for fall through in switch statements
@ -135,7 +134,7 @@ public class FallThroughCheck extends Check
public void init()
{
super.init();
regExp = Utils.getPattern(reliefPattern);
regExp = Pattern.compile(reliefPattern);
}
@Override

View File

@ -28,7 +28,6 @@ import com.puppycrawl.tools.checkstyle.Utils;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.apache.commons.beanutils.ConversionException;
/**
@ -466,17 +465,12 @@ public class HiddenFieldCheck
/**
* Set the ignore format to the specified regular expression.
* @param format a <code>String</code> value
* @throws ConversionException unable to parse format
* @throws ConversionException if unable to create Pattern object
*/
public void setIgnoreFormat(String format)
throws ConversionException
{
try {
regexp = Utils.getPattern(format);
}
catch (final PatternSyntaxException e) {
throw new ConversionException("unable to parse " + format, e);
}
regexp = Utils.createPattern(format);
}
/**

View File

@ -30,6 +30,7 @@ import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
/**
* Checks for multiple occurrences of the same string literal within a
* single file.
@ -87,15 +88,18 @@ public class MultipleStringLiteralsCheck extends Check
}
/**
* Sets regexp pattern for ignored strings.
* @param ignoreStringsRegexp regexp pattern for ignored strings
* Sets regular expression pattern for ignored strings.
* @param ignoreStringsRegexp
* regular expression pattern for ignored strings
* @throws org.apache.commons.beanutils.ConversionException
* if unable to create Pattern object
*/
public void setIgnoreStringsRegexp(String ignoreStringsRegexp)
{
if (ignoreStringsRegexp != null
&& ignoreStringsRegexp.length() > 0)
{
pattern = Utils.getPattern(ignoreStringsRegexp);
pattern = Utils.createPattern(ignoreStringsRegexp);
}
else {
pattern = null;

View File

@ -27,6 +27,7 @@ import java.util.regex.Pattern;
import antlr.collections.ASTEnumeration;
import com.puppycrawl.tools.checkstyle.Utils;
import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FullIdent;
@ -214,14 +215,15 @@ public class VariableDeclarationUsageDistanceCheck extends Check
}
/**
* Sets RegExp pattern to ignore distance calculation for variables listed
* in this pattern.
* Sets RegExp pattern to ignore distance calculation for variables listed in this pattern.
* @param ignorePattern
* Pattern contains ignored variables.
* @throws org.apache.commons.beanutils.ConversionException
* if unable to create Pattern object.
*/
public void setIgnoreVariablePattern(String ignorePattern)
{
this.ignoreVariablePattern = Pattern.compile(ignorePattern);
this.ignoreVariablePattern = Utils.createPattern(ignorePattern);
}
/**

View File

@ -24,9 +24,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.apache.commons.beanutils.ConversionException;
import antlr.collections.AST;
@ -350,17 +347,15 @@ public class VisibilityModifierCheck
/**
* Set the pattern for public members to ignore.
* @param pattern pattern for public members to ignore.
* @param pattern
* pattern for public members to ignore.
* @throws org.apache.commons.beanutils.ConversionException
* if unable to create Pattern object
*/
public void setPublicMemberPattern(String pattern)
{
try {
publicMemberPattern = Utils.getPattern(pattern);
publicMemberFormat = pattern;
}
catch (final PatternSyntaxException e) {
throw new ConversionException("unable to parse " + pattern, e);
}
publicMemberPattern = Utils.createPattern(pattern);
publicMemberFormat = pattern;
}
/**

View File

@ -29,7 +29,6 @@ import java.util.regex.PatternSyntaxException;
import org.apache.commons.beanutils.ConversionException;
import com.google.common.collect.Lists;
import com.puppycrawl.tools.checkstyle.Utils;
/**
* Checks the header of the source against a header file that contains a
@ -135,8 +134,7 @@ public class RegexpHeaderCheck extends AbstractHeaderCheck
headerRegexps.clear();
for (String line : headerLines) {
try {
// TODO: Not sure if cache in Utils is still necessary
headerRegexps.add(Utils.getPattern(line));
headerRegexps.add(Pattern.compile(line));
}
catch (final PatternSyntaxException ex) {
throw new ConversionException("line "

View File

@ -220,13 +220,13 @@ public class CustomImportOrderCheck extends Check
private String samePackageDomainsRegExp = "";
/** RegExp for STANDARD_JAVA_PACKAGE group imports */
private Pattern standardPackageRegExp = Utils.getPattern("java|javax");
private Pattern standardPackageRegExp = Pattern.compile("java|javax");
/** RegExp for THIRDPARTY_PACKAGE group imports */
private Pattern thirdPartyPackageRegExp = Utils.getPattern(".*");
private Pattern thirdPartyPackageRegExp = Pattern.compile(".*");
/** RegExp for SPECIAL_IMPORTS group imports */
private Pattern specialImportsRegExp = Utils.getPattern("^$");
private Pattern specialImportsRegExp = Pattern.compile("^$");
/** Force empty line separator between import groups */
private boolean separateLineBetweenGroups = true;
@ -247,30 +247,36 @@ public class CustomImportOrderCheck extends Check
* Sets standardRegExp specified by user.
* @param regexp
* user value.
* @throws org.apache.commons.beanutils.ConversionException
* if unable to create Pattern object.
*/
public final void setStandardPackageRegExp(String regexp)
{
standardPackageRegExp = Utils.getPattern(regexp);
standardPackageRegExp = Utils.createPattern(regexp);
}
/**
* Sets thirdPartyRegExp specified by user.
* @param regexp
* user value.
* @throws org.apache.commons.beanutils.ConversionException
* if unable to create Pattern object.
*/
public final void setThirdPartyPackageRegExp(String regexp)
{
thirdPartyPackageRegExp = Utils.getPattern(regexp);
thirdPartyPackageRegExp = Utils.createPattern(regexp);
}
/**
* Sets specialImportsRegExp specified by user.
* @param regexp
* user value.
* @throws org.apache.commons.beanutils.ConversionException
* if unable to create Pattern object.
*/
public final void setSpecialImportsRegExp(String regexp)
{
specialImportsRegExp = Utils.getPattern(regexp);
specialImportsRegExp = Utils.createPattern(regexp);
}
/**

View File

@ -31,7 +31,6 @@ import com.puppycrawl.tools.checkstyle.checks.CheckUtils;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.apache.commons.beanutils.ConversionException;
/**
@ -121,36 +120,25 @@ public class JavadocTypeCheck
/**
* Set the author tag pattern.
* @param format a <code>String</code> value
* @throws ConversionException unable to parse aFormat
* @throws ConversionException if unable to create Pattern object.
*/
public void setAuthorFormat(String format)
throws ConversionException
{
try {
authorFormat = format;
authorFormatPattern = Utils.getPattern(format);
}
catch (final PatternSyntaxException e) {
throw new ConversionException("unable to parse " + format, e);
}
authorFormat = format;
authorFormatPattern = Utils.createPattern(format);
}
/**
* Set the version format pattern.
* @param format a <code>String</code> value
* @throws ConversionException unable to parse aFormat
* @throws ConversionException if unable to create Pattern object.
*/
public void setVersionFormat(String format)
throws ConversionException
{
try {
versionFormat = format;
versionFormatPattern = Utils.getPattern(format);
}
catch (final PatternSyntaxException e) {
throw new ConversionException("unable to parse " + format, e);
}
versionFormat = format;
versionFormatPattern = Utils.createPattern(format);
}
/**
@ -334,7 +322,7 @@ public class JavadocTypeCheck
final List<JavadocTag> tags,
final List<String> typeParamNames)
{
final Pattern pattern = Utils.getPattern("\\s*<([^>]+)>.*");
final Pattern pattern = Pattern.compile("\\s*<([^>]+)>.*");
for (int i = tags.size() - 1; i >= 0; i--) {
final JavadocTag tag = tags.get(i);
if (tag.isParamTag()) {

View File

@ -31,7 +31,6 @@ import com.puppycrawl.tools.checkstyle.api.DetailNode;
import com.puppycrawl.tools.checkstyle.api.JavadocTagInfo;
import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
import com.puppycrawl.tools.checkstyle.api.TextBlock;
import com.puppycrawl.tools.checkstyle.Utils;
/**
* Contains utility methods for working with Javadoc.
@ -109,8 +108,7 @@ public final class JavadocUtils
final String[] text = cmt.getText();
final List<JavadocTag> tags = Lists.newArrayList();
final List<InvalidJavadocTag> invalidTags = Lists.newArrayList();
Pattern blockTagPattern =
Utils.getPattern("/\\*{2,}\\s*@(\\p{Alpha}+)\\s");
Pattern blockTagPattern = Pattern.compile("/\\*{2,}\\s*@(\\p{Alpha}+)\\s");
for (int i = 0; i < text.length; i++) {
final String s = text[i];
final Matcher blockTagMatcher = blockTagPattern.matcher(s);
@ -139,8 +137,7 @@ public final class JavadocUtils
else if (tagType == JavadocTagType.ALL || tagType == JavadocTagType.INLINE)
{
// Match Javadoc text after comment characters
final Pattern commentPattern =
Utils.getPattern("^\\s*(?:/\\*{2,}|\\*+)\\s*(.*)");
final Pattern commentPattern = Pattern.compile("^\\s*(?:/\\*{2,}|\\*+)\\s*(.*)");
final Matcher commentMatcher = commentPattern.matcher(s);
final String commentContents;
final int commentOffset; // offset including comment characters
@ -152,8 +149,7 @@ public final class JavadocUtils
commentContents = commentMatcher.group(1);
commentOffset = commentMatcher.start(1) - 1;
}
final Pattern tagPattern =
Utils.getPattern(".*?\\{@(\\p{Alpha}+)\\s+(.*?)\\}");
final Pattern tagPattern = Pattern.compile(".*?\\{@(\\p{Alpha}+)\\s+(.*?)\\}");
final Matcher tagMatcher = tagPattern.matcher(commentContents);
while (tagMatcher.find()) {
if (tagMatcher.groupCount() == 2) {
@ -177,8 +173,7 @@ public final class JavadocUtils
// tag!
}
}
blockTagPattern =
Utils.getPattern("^\\s*\\**\\s*@(\\p{Alpha}+)\\s");
blockTagPattern = Pattern.compile("^\\s*\\**\\s*@(\\p{Alpha}+)\\s");
}
return new JavadocTags(tags, invalidTags);
}

View File

@ -18,6 +18,7 @@
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks.javadoc;
import com.puppycrawl.tools.checkstyle.Utils;
import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FileContents;
@ -75,13 +76,14 @@ public class JavadocVariableCheck
/**
* Sets the variable names to ignore in the check.
* @param regexp regexp to define variable names to ignore.
* @param regexp regular expression to define variable names to ignore.
* @throws org.apache.commons.beanutils.ConversionException if unable to create Pattern object.
*/
public void setIgnoreNamePattern(String regexp)
{
ignoreNameRegexp = regexp;
if (!(regexp == null || regexp.length() == 0)) {
ignoreNamePattern = Pattern.compile(regexp);
ignoreNamePattern = Utils.createPattern(regexp);
}
else {
ignoreNamePattern = null;

View File

@ -27,7 +27,6 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.Utils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.apache.commons.beanutils.ConversionException;
/**
@ -100,35 +99,25 @@ public class WriteTagCheck
/**
* Sets the tag to check.
* @param tag tag to check
* @throws ConversionException If the tag is not a valid regular exception.
* @throws ConversionException if unable to create Pattern object.
*/
public void setTag(String tag)
throws ConversionException
{
try {
this.tag = tag;
tagRE = Utils.getPattern(tag + "\\s*(.*$)");
}
catch (final PatternSyntaxException e) {
throw new ConversionException("unable to parse " + tag, e);
}
this.tag = tag;
tagRE = Utils.createPattern(tag + "\\s*(.*$)");
}
/**
* Set the tag format.
* @param format a <code>String</code> value
* @throws ConversionException unable to parse format
* @throws ConversionException if unable to create Pattern object
*/
public void setTagFormat(String format)
throws ConversionException
{
try {
tagFormat = format;
tagFormatRE = Utils.getPattern(format);
}
catch (final PatternSyntaxException e) {
throw new ConversionException("unable to parse " + format, e);
}
tagFormat = format;
tagFormatRE = Utils.createPattern(format);
}
/**

View File

@ -23,7 +23,6 @@ import java.util.regex.Pattern;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.ScopeUtils;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.Utils;
/**
* <p>
@ -80,7 +79,7 @@ public class LocalVariableNameCheck
private boolean allowOneCharVarInForLoop;
/** Regexp for one-char loop variables. */
private static Pattern sSingleChar = Utils.getPattern("^[a-z]$");
private static Pattern sSingleChar = Pattern.compile("^[a-z]$");
/** Creates a new <code>LocalVariableNameCheck</code> instance. */
public LocalVariableNameCheck()

View File

@ -19,7 +19,7 @@
package com.puppycrawl.tools.checkstyle.checks.regexp;
import com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter;
import com.puppycrawl.tools.checkstyle.Utils;
import java.util.regex.Pattern;
/**
@ -197,6 +197,6 @@ class DetectorOptions
{
final int options = ignoreCase ? compileFlags
| Pattern.CASE_INSENSITIVE : compileFlags;
return Utils.getPattern(format, options);
return Pattern.compile(format, options);
}
}

View File

@ -23,7 +23,7 @@ import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.Utils;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.apache.commons.beanutils.ConversionException;
/**
@ -136,16 +136,11 @@ public class LineLengthCheck extends Check
/**
* Set the ignore pattern.
* @param format a <code>String</code> value
* @throws ConversionException unable to parse format
* @throws ConversionException if unable to create Pattern object
*/
public void setIgnorePattern(String format)
throws ConversionException
{
try {
ignorePattern = Utils.getPattern(format);
}
catch (final PatternSyntaxException e) {
throw new ConversionException("unable to parse " + format, e);
}
ignorePattern = Utils.createPattern(format);
}
}

View File

@ -22,7 +22,8 @@ import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import com.puppycrawl.tools.checkstyle.api.Filter;
import com.puppycrawl.tools.checkstyle.Utils;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.apache.commons.beanutils.ConversionException;
/**
* This filter processes {@link com.puppycrawl.tools.checkstyle.api.AuditEvent}
@ -75,23 +76,25 @@ public class SuppressElement
* file name pattern. Must either call {@link #setColumns(String)} or
* {@link #setModuleId(String)} before using this object.
* @param files regular expression for names of filtered files.
* @throws PatternSyntaxException if there is an error.
* @throws ConversionException if unable to create Pattern object.
*/
public SuppressElement(String files)
throws PatternSyntaxException
throws ConversionException
{
filePattern = files;
fileRegexp = Utils.getPattern(files);
fileRegexp = Pattern.compile(files);
}
/**
* Set the check class pattern.
* @param checks regular expression for filtered check classes.
* @throws ConversionException if unable to create Pattern object
*/
public void setChecks(final String checks)
throws ConversionException
{
checkPattern = checks;
checkRegexp = Utils.getPattern(checks);
checkRegexp = Utils.createPattern(checks);
}
/**

View File

@ -323,17 +323,12 @@ public class SuppressWithNearbyCommentFilter
/**
* Set the format for a comment that turns off reporting.
* @param format a <code>String</code> value.
* @throws ConversionException unable to parse format.
* @throws ConversionException if unable to create Pattern object.
*/
public void setCommentFormat(String format)
throws ConversionException
{
try {
commentRegexp = Utils.getPattern(format);
}
catch (final PatternSyntaxException e) {
throw new ConversionException("unable to parse " + format, e);
}
commentRegexp = Utils.createPattern(format);
}
/** @return the FileContents for this filter. */
@ -354,31 +349,24 @@ public class SuppressWithNearbyCommentFilter
/**
* Set the format for a check.
* @param format a <code>String</code> value
* @throws ConversionException unable to parse format
* @throws ConversionException if unable to create Pattern object
*/
public void setCheckFormat(String format)
throws ConversionException
{
try {
checkRegexp = Utils.getPattern(format);
checkFormat = format;
}
catch (final PatternSyntaxException e) {
throw new ConversionException("unable to parse " + format, e);
}
checkRegexp = Utils.createPattern(format);
checkFormat = format;
}
/**
* Set the format for a message.
* @param format a <code>String</code> value
* @throws ConversionException unable to parse format
* @throws ConversionException if unable to create Pattern object
*/
public void setMessageFormat(String format)
throws ConversionException
{
if (!Utils.isPatternValid(format)) {
throw new ConversionException("Unable to parse format: " + format);
}
Utils.createPattern(format);
messageFormat = format;
}

View File

@ -305,33 +305,23 @@ public class SuppressionCommentFilter
/**
* Set the format for a comment that turns off reporting.
* @param format a <code>String</code> value.
* @throws ConversionException unable to parse format.
* @throws ConversionException if unable to create Pattern object.
*/
public void setOffCommentFormat(String format)
throws ConversionException
{
try {
offRegexp = Utils.getPattern(format);
}
catch (final PatternSyntaxException e) {
throw new ConversionException("unable to parse " + format, e);
}
offRegexp = Utils.createPattern(format);
}
/**
* Set the format for a comment that turns on reporting.
* @param format a <code>String</code> value
* @throws ConversionException unable to parse format
* @throws ConversionException if unable to create Pattern object.
*/
public void setOnCommentFormat(String format)
throws ConversionException
{
try {
onRegexp = Utils.getPattern(format);
}
catch (final PatternSyntaxException e) {
throw new ConversionException("unable to parse " + format, e);
}
onRegexp = Utils.createPattern(format);
}
/** @return the FileContents for this filter. */
@ -352,18 +342,14 @@ public class SuppressionCommentFilter
/**
* Set the format for a check.
* @param format a <code>String</code> value
* @throws ConversionException unable to parse format
* @throws ConversionException if unable to create Pattern object
*/
public void setCheckFormat(String format)
throws ConversionException
{
try {
checkRegexp = Utils.getPattern(format);
checkFormat = format;
}
catch (final PatternSyntaxException e) {
throw new ConversionException("unable to parse " + format, e);
}
checkRegexp = Utils.createPattern(format);
checkFormat = format;
}
/**

View File

@ -25,7 +25,6 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.util.regex.Pattern;
import org.apache.commons.beanutils.ConversionException;
import org.junit.Test;
@ -57,10 +56,6 @@ public class UtilsTest
assertEquals(0, Utils.lengthMinusTrailingWhitespace(" \t "));
assertEquals(3, Utils.lengthMinusTrailingWhitespace(" 23"));
assertEquals(3, Utils.lengthMinusTrailingWhitespace(" 23 \t "));
final Pattern r1 = Utils.getPattern("a");
final Pattern r2 = Utils.getPattern("a");
assertEquals(r1, r2);
}
@Test(expected = ConversionException.class)

View File

@ -216,7 +216,7 @@ public class XMLLoggerTest
assertEquals("first line.",
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
lines[0]);
Pattern checkstyleOpenTag = Utils.getPattern("^<checkstyle version=\".*\">$");
Pattern checkstyleOpenTag = Pattern.compile("^<checkstyle version=\".*\">$");
assertTrue("second line.", checkstyleOpenTag.matcher(lines[1]).matches());
for (int i = 0; i < expectedLines.length; i++) {
assertEquals("line " + i + ".", expectedLines[i], lines[i + 2]);