Added Utils.isPatternValid method to hide try-catch blocks when parsing is used only for validation
Pull #835
This commit is contained in:
parent
91d979ef8e
commit
1c8180d74f
6
pom.xml
6
pom.xml
|
|
@ -584,7 +584,7 @@
|
|||
<regex><pattern>.*.PropertiesExpander</pattern><branchRate>50</branchRate><lineRate>83</lineRate></regex>
|
||||
<regex><pattern>.*.PropertyCacheFile</pattern><branchRate>22</branchRate><lineRate>18</lineRate></regex>
|
||||
<regex><pattern>.*.TreeWalker</pattern><branchRate>90</branchRate><lineRate>83</lineRate></regex>
|
||||
<regex><pattern>com.puppycrawl.tools.checkstyle.Utils</pattern><branchRate>69</branchRate><lineRate>63</lineRate></regex>
|
||||
<regex><pattern>com.puppycrawl.tools.checkstyle.Utils</pattern><branchRate>69</branchRate><lineRate>69</lineRate></regex>
|
||||
<regex><pattern>.*.XMLLogger</pattern><branchRate>86</branchRate><lineRate>97</lineRate></regex>
|
||||
|
||||
|
||||
|
|
@ -830,11 +830,11 @@
|
|||
<regex><pattern>.*.filters.IntMatchFilter</pattern><branchRate>100</branchRate><lineRate>90</lineRate></regex>
|
||||
<regex><pattern>.*.filters.IntRangeFilter</pattern><branchRate>100</branchRate><lineRate>90</lineRate></regex>
|
||||
<regex><pattern>.*.filters.SuppressElement</pattern><branchRate>69</branchRate><lineRate>78</lineRate></regex>
|
||||
<regex><pattern>.*.filters.SuppressionCommentFilter</pattern><branchRate>85</branchRate><lineRate>86</lineRate></regex>
|
||||
<regex><pattern>.*.filters.SuppressionCommentFilter</pattern><branchRate>83</branchRate><lineRate>87</lineRate></regex>
|
||||
<regex><pattern>.*.filters.SuppressionCommentFilter\$.*</pattern><branchRate>87</branchRate><lineRate>84</lineRate></regex>
|
||||
<regex><pattern>.*.filters.SuppressionFilter</pattern><branchRate>0</branchRate><lineRate>0</lineRate></regex>
|
||||
<regex><pattern>.*.filters.SuppressionsLoader</pattern><branchRate>68</branchRate><lineRate>77</lineRate></regex>
|
||||
<regex><pattern>.*.filters.SuppressWithNearbyCommentFilter</pattern><branchRate>80</branchRate><lineRate>87</lineRate></regex>
|
||||
<regex><pattern>.*.filters.SuppressWithNearbyCommentFilter</pattern><branchRate>76</branchRate><lineRate>89</lineRate></regex>
|
||||
<regex><pattern>.*.filters.SuppressWithNearbyCommentFilter\$.*</pattern><branchRate>81</branchRate><lineRate>75</lineRate></regex>
|
||||
|
||||
</regexes>
|
||||
|
|
|
|||
|
|
@ -168,9 +168,25 @@ public final class Utils
|
|||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* Validates whether passed string is a valid pattern or not.
|
||||
* @param pattern
|
||||
* string to validate
|
||||
* @return true if the pattern is valid false otherwise
|
||||
*/
|
||||
public static boolean isPatternValid(String pattern)
|
||||
{
|
||||
try {
|
||||
Utils.getPattern(pattern);
|
||||
}
|
||||
catch (final PatternSyntaxException e) {
|
||||
return false;
|
||||
}
|
||||
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
|
||||
|
|
|
|||
|
|
@ -376,12 +376,8 @@ public class SuppressWithNearbyCommentFilter
|
|||
public void setMessageFormat(String format)
|
||||
throws ConversionException
|
||||
{
|
||||
// check that format parses
|
||||
try {
|
||||
Utils.getPattern(format);
|
||||
}
|
||||
catch (final PatternSyntaxException e) {
|
||||
throw new ConversionException("unable to parse " + format, e);
|
||||
if (!Utils.isPatternValid(format)) {
|
||||
throw new ConversionException("Unable to parse format: " + format);
|
||||
}
|
||||
messageFormat = format;
|
||||
}
|
||||
|
|
@ -394,12 +390,8 @@ public class SuppressWithNearbyCommentFilter
|
|||
public void setInfluenceFormat(String format)
|
||||
throws ConversionException
|
||||
{
|
||||
// check that format parses
|
||||
try {
|
||||
Utils.getPattern(format);
|
||||
}
|
||||
catch (final PatternSyntaxException e) {
|
||||
throw new ConversionException("unable to parse " + format, e);
|
||||
if (!Utils.isPatternValid(format)) {
|
||||
throw new ConversionException("Unable to parse format: " + format);
|
||||
}
|
||||
influenceFormat = format;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -374,12 +374,8 @@ public class SuppressionCommentFilter
|
|||
public void setMessageFormat(String format)
|
||||
throws ConversionException
|
||||
{
|
||||
// check that format parses
|
||||
try {
|
||||
Utils.getPattern(format);
|
||||
}
|
||||
catch (final PatternSyntaxException e) {
|
||||
throw new ConversionException("unable to parse " + format, e);
|
||||
if (!Utils.isPatternValid(format)) {
|
||||
throw new ConversionException("Unable to parse format: " + format);
|
||||
}
|
||||
messageFormat = format;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue