Fix PMD violations from 'strings' ruleset, issue #873

This commit is contained in:
Michal Kordas 2015-04-11 16:07:30 +02:00
parent a1ad6dbd26
commit 1c42e3eeb9
22 changed files with 52 additions and 42 deletions

View File

@ -17,6 +17,7 @@
<allow pkg="javax.xml.parsers"/>
<allow pkg="org.apache.commons.beanutils"/>
<allow pkg="org.apache.commons.logging"/>
<allow pkg="org.apache.commons.lang3"/>
<allow pkg="org.xml.sax"/>
<allow pkg="com.puppycrawl.tools.checkstyle"/>

View File

@ -248,7 +248,7 @@
<!--<ruleset>rulesets/java/naming.xml</ruleset>-->
<!--<ruleset>rulesets/java/optimizations.xml</ruleset>-->
<!--<ruleset>rulesets/java/strictexception.xml</ruleset>-->
<!--<ruleset>rulesets/java/strings.xml</ruleset>-->
<ruleset>rulesets/java/strings.xml</ruleset>
<!--<ruleset>rulesets/java/sunsecure.xml</ruleset>-->
<ruleset>rulesets/java/typeresolution.xml</ruleset>
<ruleset>rulesets/java/unnecessary.xml</ruleset>
@ -765,7 +765,7 @@
<regex><pattern>.*.checks.design.ThrowsCountCheck</pattern><branchRate>75</branchRate><lineRate>83</lineRate></regex>
<regex><pattern>.*.checks.design.VisibilityModifierCheck</pattern><branchRate>95</branchRate><lineRate>95</lineRate></regex>
<regex><pattern>.*.checks.header.AbstractHeaderCheck</pattern><branchRate>79</branchRate><lineRate>85</lineRate></regex>
<regex><pattern>.*.checks.header.AbstractHeaderCheck</pattern><branchRate>85</branchRate><lineRate>85</lineRate></regex>
<regex><pattern>.*.checks.header.HeaderCheck</pattern><branchRate>18</branchRate><lineRate>45</lineRate></regex>
<regex><pattern>.*.checks.header.RegexpHeaderCheck</pattern><branchRate>87</branchRate><lineRate>93</lineRate></regex>
@ -775,7 +775,7 @@
<regex><pattern>.*.checks.imports.CustomImportOrderCheck</pattern><branchRate>93</branchRate><lineRate>91</lineRate></regex>
<regex><pattern>.*.checks.imports.Guard</pattern><branchRate>86</branchRate><lineRate>100</lineRate></regex>
<regex><pattern>.*.checks.imports.IllegalImportCheck</pattern><branchRate>100</branchRate><lineRate>94</lineRate></regex>
<regex><pattern>.*.checks.imports.ImportControlCheck</pattern><branchRate>75</branchRate><lineRate>70</lineRate></regex>
<regex><pattern>.*.checks.imports.ImportControlCheck</pattern><branchRate>85</branchRate><lineRate>70</lineRate></regex>
<regex><pattern>.*.checks.imports.ImportControlLoader</pattern><branchRate>72</branchRate><lineRate>88</lineRate></regex>
<regex><pattern>.*.checks.imports.ImportOrderCheck</pattern><branchRate>91</branchRate><lineRate>99</lineRate></regex>
<regex><pattern>.*.checks.imports.PkgControl</pattern><branchRate>80</branchRate><lineRate>100</lineRate></regex>
@ -875,7 +875,7 @@
<regex><pattern>.*.checks.whitespace.MethodParamPadCheck</pattern><branchRate>100</branchRate><lineRate>95</lineRate></regex>
<regex><pattern>.*.checks.whitespace.NoWhitespaceAfterCheck</pattern><branchRate>94</branchRate><lineRate>98</lineRate></regex>
<regex><pattern>.*.checks.whitespace.NoWhitespaceBeforeCheck</pattern><branchRate>90</branchRate><lineRate>100</lineRate></regex>
<regex><pattern>.*.checks.whitespace.OperatorWrapCheck</pattern><branchRate>68</branchRate><lineRate>82</lineRate></regex>
<regex><pattern>.*.checks.whitespace.OperatorWrapCheck</pattern><branchRate>68</branchRate><lineRate>81</lineRate></regex>
<regex><pattern>.*.checks.whitespace.ParenPadCheck</pattern><branchRate>86</branchRate><lineRate>95</lineRate></regex>
<regex><pattern>.*.checks.whitespace.SeparatorWrapCheck</pattern><branchRate>100</branchRate><lineRate>93</lineRate></regex>
<regex><pattern>.*.checks.whitespace.TypecastParenPadCheck</pattern><branchRate>87</branchRate><lineRate>88</lineRate></regex>

View File

@ -512,7 +512,7 @@ public final class ConfigurationLoader
int prev = 0;
int pos;
//search for the next instance of $ from the 'prev' position
while ((pos = value.indexOf("$", prev)) >= 0) {
while ((pos = value.indexOf('$', prev)) >= 0) {
//if there was any text before this, add it as a fragment
//TODO, this check could be modified to go if pos>prev;

View File

@ -113,8 +113,7 @@ public class DefaultLogger
+ BUFFER_CUSHION;
final StringBuilder sb = new StringBuilder(bufLen);
sb.append(fileName);
sb.append(':').append(evt.getLine());
sb.append(fileName).append(':').append(evt.getLine());
if (evt.getColumn() > 0) {
sb.append(':').append(evt.getColumn());
}

View File

@ -113,7 +113,7 @@ public final class PackageNamesLoader
final String subPackage = iterator.next();
buf.append(subPackage);
if (!subPackage.endsWith(".")) {
buf.append(".");
buf.append('.');
}
}
return buf.toString();

View File

@ -179,7 +179,7 @@ public final class Utils
*/
public static String baseClassname(String type)
{
final int i = type.lastIndexOf(".");
final int i = type.lastIndexOf('.');
return i == -1 ? type : type.substring(i + 1);
}

View File

@ -187,7 +187,7 @@ public class XMLLogger
sb.append("&quot;");
break;
case '&':
final int nextSemi = value.indexOf(";", i);
final int nextSemi = value.indexOf(';', i);
if (nextSemi < 0
|| !isReference(value.substring(i, nextSemi + 1)))
{

View File

@ -18,6 +18,7 @@
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.api;
import org.apache.commons.lang3.StringUtils;
/**
* Contains utility methods designed to work with annotations.
@ -142,7 +143,7 @@ public final class AnnotationUtility
throw new NullPointerException("the annotation is null");
}
if (annotation.trim().length() == 0) {
if (StringUtils.isBlank(annotation)) {
throw new IllegalArgumentException("the annotation"
+ "is empty or spaces");
}

View File

@ -18,6 +18,11 @@
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.api;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
/**
* Represents a full identifier, including dots, with associated
* position information.
@ -35,8 +40,8 @@ package com.puppycrawl.tools.checkstyle.api;
**/
public final class FullIdent
{
/** the string **/
private final StringBuffer buffer = new StringBuffer();
/** the list holding subsequent elements of identifier **/
private final List<String> elements = new ArrayList<>();
/** the line number **/
private int lineNo;
/** the column number **/
@ -50,7 +55,7 @@ public final class FullIdent
/** @return the text **/
public String getText()
{
return buffer.toString();
return StringUtils.join(elements, "");
}
/** @return the line number **/
@ -71,7 +76,7 @@ public final class FullIdent
*/
private void append(String text)
{
buffer.append(text);
elements.add(text);
}
/**
@ -81,7 +86,7 @@ public final class FullIdent
*/
private void append(DetailAST ast)
{
buffer.append(ast.getText());
elements.add(ast.getText());
if (lineNo == 0) {
lineNo = ast.getLineNo();
}

View File

@ -160,10 +160,10 @@ public abstract class AbstractTypeAwareCheck extends Check
|| ast.getType() == TokenTypes.ENUM_DEF)
{
// perhaps it was inner class
int dotIdx = currentClass.lastIndexOf("$");
int dotIdx = currentClass.lastIndexOf('$');
if (dotIdx == -1) {
// perhaps just a class
dotIdx = currentClass.lastIndexOf(".");
dotIdx = currentClass.lastIndexOf('.');
}
if (dotIdx == -1) {
// looks like a topmost class

View File

@ -164,7 +164,7 @@ public class ClassResolver
return safeLoad(name);
}
//Perhaps it's fully-qualified inner class
final int dot = name.lastIndexOf(".");
final int dot = name.lastIndexOf('.');
if (dot != -1) {
final String innerName =
name.substring(0, dot) + "$" + name.substring(dot + 1);

View File

@ -177,7 +177,7 @@ public class SuppressWarningsHolder
public void setAliasList(String aliasList)
{
for (String sourceAlias : aliasList.split(",")) {
final int index = sourceAlias.indexOf("=");
final int index = sourceAlias.indexOf('=');
if (index > 0) {
registerAlias(sourceAlias.substring(0, index), sourceAlias
.substring(index + 1));

View File

@ -319,7 +319,7 @@ public final class AnnotationUseStyleCheck extends Check
&& AnnotationUseStyleCheck.ANNOTATION_ELEMENT_SINGLE_NAME.equals(
valuePair.getFirstChild().getText()))
{
this.log(annotation.getLineNo(), "annotation.incorrect.style",
this.log(annotation.getLineNo(), MSG_KEY_ANNOTATION_INCORRECT_STYLE,
ElementStyle.COMPACT);
}
}
@ -344,7 +344,7 @@ public final class AnnotationUseStyleCheck extends Check
if (arrayInit != null
&& arrayInit.getChildCount(TokenTypes.EXPR) == 1)
{
this.log(annotation.getLineNo(), "annotation.incorrect.style",
this.log(annotation.getLineNo(), MSG_KEY_ANNOTATION_INCORRECT_STYLE,
ElementStyle.COMPACT_NO_ARRAY);
}
//in expanded style with one value and the correct element name
@ -358,7 +358,7 @@ public final class AnnotationUseStyleCheck extends Check
valuePair.getFirstChild().getText())
&& nestedArrayInit.getChildCount(TokenTypes.EXPR) == 1)
{
this.log(annotation.getLineNo(), "annotation.incorrect.style",
this.log(annotation.getLineNo(), MSG_KEY_ANNOTATION_INCORRECT_STYLE,
ElementStyle.COMPACT_NO_ARRAY);
}
}

View File

@ -22,6 +22,8 @@ import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
/**
* Checks for empty blocks. The policy to verify is specified using the {@link
* BlockOption} class and defaults to {@link BlockOption#STMT}.
@ -175,7 +177,7 @@ public class EmptyBlockCheck
// Handle braces on the same line
final String txt = lines[slistLineNo - 1]
.substring(slistColNo + 1, rcurlyColNo);
if (txt.trim().length() != 0) {
if (isNotBlank(txt)) {
retVal = true;
}
}

View File

@ -65,10 +65,10 @@ public abstract class AbstractIllegalCheck extends Check
illegalClassNames.clear();
for (final String name : classNames) {
illegalClassNames.add(name);
final int lastDot = name.lastIndexOf(".");
final int lastDot = name.lastIndexOf('.');
if (lastDot > 0 && lastDot < name.length() - 1) {
final String shortName = name
.substring(name.lastIndexOf(".") + 1);
.substring(name.lastIndexOf('.') + 1);
illegalClassNames.add(shortName);
}
}

View File

@ -312,7 +312,7 @@ public final class IllegalTypeCheck extends AbstractFormatCheck
*/
private boolean isMatchingClassName(String className)
{
final String shortName = className.substring(className.lastIndexOf(".") + 1);
final String shortName = className.substring(className.lastIndexOf('.') + 1);
return illegalClassNames.contains(className)
|| illegalClassNames.contains(shortName)
|| !legalAbstractClassNames.contains(className)
@ -329,7 +329,7 @@ public final class IllegalTypeCheck extends AbstractFormatCheck
{
if (illegalClassNames.contains(canonicalName)) {
final String shortName = canonicalName.
substring(canonicalName.lastIndexOf(".") + 1);
substring(canonicalName.lastIndexOf('.') + 1);
illegalClassNames.add(shortName);
}
}

View File

@ -740,7 +740,7 @@ public class VisibilityModifierCheck
final List<String> shortNames = new ArrayList<>();
for (String canonicalClassName : canonicalClassNames) {
final String shortClassName = canonicalClassName
.substring(canonicalClassName.lastIndexOf(".") + 1,
.substring(canonicalClassName.lastIndexOf('.') + 1,
canonicalClassName.length());
shortNames.add(shortClassName);
}
@ -755,7 +755,7 @@ public class VisibilityModifierCheck
private static String getClassShortName(String canonicalClassName)
{
final String shortClassName = canonicalClassName
.substring(canonicalClassName.lastIndexOf(".") + 1,
.substring(canonicalClassName.lastIndexOf('.') + 1,
canonicalClassName.length());
return shortClassName;
}

View File

@ -41,6 +41,7 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import org.apache.commons.lang3.StringUtils;
/**
* Abstract super class for header checks.
@ -89,7 +90,7 @@ public abstract class AbstractHeaderCheck extends AbstractFileSetCheck
public void setHeaderFile(String fileName)
{
// Handle empty param
if (fileName == null || fileName.trim().length() == 0) {
if (StringUtils.isBlank(fileName)) {
return;
}
@ -183,7 +184,7 @@ public abstract class AbstractHeaderCheck extends AbstractFileSetCheck
*/
public void setHeader(String header)
{
if (header == null || header.trim().length() == 0) {
if (StringUtils.isBlank(header)) {
return;
}

View File

@ -540,7 +540,7 @@ public class CustomImportOrderCheck extends Check
private boolean matchesSamePackageImportGroup(boolean isStatic,
String importFullPath, String currentGroup)
{
final String importPath = importFullPath.substring(0, importFullPath.lastIndexOf("."));
final String importPath = importFullPath.substring(0, importFullPath.lastIndexOf('.'));
return !isStatic && SAME_PACKAGE_RULE_GROUP.equals(currentGroup)
&& samePackageDomainsRegExp.contains(importPath);
}
@ -670,8 +670,8 @@ public class CustomImportOrderCheck extends Check
}
else if (ruleStr.startsWith(SAME_PACKAGE_RULE_GROUP)) {
final String rule = ruleStr.substring(ruleStr.indexOf("(") + 1,
ruleStr.indexOf(")"));
final String rule = ruleStr.substring(ruleStr.indexOf('(') + 1,
ruleStr.indexOf(')'));
try {
samePackageMatchingDepth = Integer.parseInt(rule);
}
@ -703,7 +703,7 @@ public class CustomImportOrderCheck extends Check
int count = firstPackageDomainsCount;
while (tokens.hasMoreTokens() && count > 0) {
builder.append(tokens.nextToken()).append(".");
builder.append(tokens.nextToken()).append('.');
count--;
}
return builder.append("*").toString();

View File

@ -27,6 +27,7 @@ import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FullIdent;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.lang3.StringUtils;
/**
* Check that controls what packages can be imported in each package. Useful
@ -137,7 +138,7 @@ public class ImportControlCheck extends Check
public void setUrl(final String url)
{
// Handle empty param
if (url == null || url.trim().length() == 0) {
if (StringUtils.isBlank(url)) {
return;
}
final URI uri;
@ -164,7 +165,7 @@ public class ImportControlCheck extends Check
public void setFile(final String name)
{
// Handle empty param
if (name == null || name.trim().length() == 0) {
if (StringUtils.isBlank(name)) {
return;
}

View File

@ -23,6 +23,7 @@ import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.Utils;
import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck;
import org.apache.commons.lang3.StringUtils;
/**
* <p>
@ -207,9 +208,8 @@ public class OperatorWrapCheck
// by itself. This last bit is to handle the operator on a line by
// itself.
if (wOp == WrapOption.NL
&& !text.equals(currentLine.trim())
&& currentLine.substring(colNo + text.length())
.trim().length() == 0)
&& !text.equals(currentLine.trim())
&& StringUtils.isBlank(currentLine.substring(colNo + text.length())))
{
log(lineNo, colNo, LINE_NEW, text);
}

View File

@ -68,7 +68,7 @@ class CSVFilter implements IntFilter
final StringTokenizer tokenizer = new StringTokenizer(pattern, ",");
while (tokenizer.hasMoreTokens()) {
final String token = tokenizer.nextToken().trim();
final int index = token.indexOf("-");
final int index = token.indexOf('-');
if (index == -1) {
final int matchValue = Integer.parseInt(token);
addFilter(new IntMatchFilter(matchValue));