diff --git a/checkstyle-sonar-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporter.java b/checkstyle-sonar-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporter.java
index 894ad43d..cf7e1a4a 100644
--- a/checkstyle-sonar-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporter.java
+++ b/checkstyle-sonar-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporter.java
@@ -50,7 +50,7 @@ public class CheckstyleProfileImporter extends ProfileImporter {
private static final String CHECKER_MODULE = "Checker";
private static final String TREEWALKER_MODULE = "TreeWalker";
private static final String MODULE_NODE = "module";
- private static final String[] FILTERS = new String[] {
+ private static final String[] FILTERS = {
"SeverityMatchFilter",
"SuppressionFilter",
"SuppressWarningsFilter",
@@ -61,8 +61,8 @@ public class CheckstyleProfileImporter extends ProfileImporter {
private static class Module {
private String name;
- private Map Checks that any combination of String literals with optional assignment is on the left side of an equals() comparison. Rationale: Calling the equals() method on String literals will avoid a potential NullPointerException. Also, it is pretty common to see null check right before equals comparisons which is not necessary in the below example. For example:
+ For example:
String nullString = null;
nullString.equals("My_Sweet_String");
-
-should be refactored to: +
should be refactored to:
String nullString = null; "My_Sweet_String".equals(nullString);-
Limitations: If the equals method is overridden or a covariant equals method is defined and the implementation is incorrect (where s.equals(t) does not return the same result as t.equals(s)) then rearranging the called on object and parameter may have unexpected results.
Java's Autoboxing feature has an affect on how this check is implemented. Pre Java 5 all IDENT + IDENT object concatenations would not cause a NullPointerException even if null. Those situations could have been included in this check. They would simply act as if they surrounded by String.valueof() which would concatenate the String null.
The following example will cause a NullPointerException as a result of what autoboxing does.
diff --git a/checkstyle-sonar-plugin/src/main/resources/org/sonar/l10n/checkstyle/rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.coding.NoCloneCheck.html b/checkstyle-sonar-plugin/src/main/resources/org/sonar/l10n/checkstyle/rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.coding.NoCloneCheck.html index 906be8cf..7390a0cc 100644 --- a/checkstyle-sonar-plugin/src/main/resources/org/sonar/l10n/checkstyle/rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.coding.NoCloneCheck.html +++ b/checkstyle-sonar-plugin/src/main/resources/org/sonar/l10n/checkstyle/rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.coding.NoCloneCheck.html @@ -1,6 +1,6 @@Checks that the clone method is not overridden from the Object class.
-Rationale: The clone method relies on strange/hard to follow rules that do not work it all situations. Consequently, it is difficult to override correctly. Below are some of the rules/reasons why the clone method should be avoided. +
Rationale: The clone method relies on strange/hard to follow rules that do not work it all situations. Consequently, it is difficult to override correctly. Below are some of the rules/reasons why the clone method should be avoided.
Two alternatives to the clone method, in some cases, is a copy constructor or a static factory method to return copies of an object. Both of these approaches are simpler and do not conflict with final fields. The do not force the calling client to handle a CloneNotSuportException. They also are typed therefore no casting is necessary. Finally, they are more flexible since they can take interface types rather than concrete classes.
Sometimes a copy constructor or static factory is not an acceptable alternative to the clone method. The example below highlights the limitation of a copy constructor (or static factory). Assume Square is a subclass for Shape.
-
Shape s1 = new Square(); System.out.println(s1 instanceof Square); //true -+
...assume at this point the code knows nothing of s1 being a Square that's the beauty of polymorphism but the code wants to copy the Square which is declared as a Shape, its super type...
-
Shape s2 = new Shape(s1); //using the copy constructor System.out.println(s2 instanceof Square); //false -+ -
The working solution (without knowing about all subclasses and doing many casts) is to do the following (assuming correct clone implementation).
+
The working solution (without knowing about all subclasses and doing many casts) is to do the following (assuming correct clone implementation).
Shape s2 = s1.clone(); System.out.println(s2 instanceof Square); //true -+
Just keep in mind if this type of polymorphic cloning is required then a properly implemented clone method may be the best choice.
diff --git a/checkstyle-sonar-plugin/src/main/resources/org/sonar/l10n/checkstyle/rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck.html b/checkstyle-sonar-plugin/src/main/resources/org/sonar/l10n/checkstyle/rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck.html index f2dc2f11..6a0d331f 100644 --- a/checkstyle-sonar-plugin/src/main/resources/org/sonar/l10n/checkstyle/rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck.html +++ b/checkstyle-sonar-plugin/src/main/resources/org/sonar/l10n/checkstyle/rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck.html @@ -1,10 +1,10 @@Checks that a source file begins with a specified header. Property headerFile specifies a file that contains the required header. Alternatively, the header specification can be set directly in the header property without the need for an external file.
Property ignoreLines specifies the line numbers to ignore when matching lines in a header file. This property is very useful for supporting headers that contain copyright dates. For example, consider the following header:
- line 1: //////////////////////////////////////////////////////////////////// - line 2: // checkstyle: - line 3: // Checks Java source code for adherence to a set of rules. - line 4: // Copyright (C) 2002 Oliver Burn - line 5: //////////////////////////////////////////////////////////////////// + line 1: //////////////////////////////////////////////////////////////////// + line 2: // checkstyle: + line 3: // Checks Java source code for adherence to a set of rules. + line 4: // Copyright (C) 2002 Oliver Burn + line 5: ////////////////////////////////////////////////////////////////////
Since the year information will change over time, you can tell Checkstyle to ignore line 4 by setting property ignoreLines to 4.
\ No newline at end of file diff --git a/checkstyle-sonar-plugin/src/main/resources/org/sonar/l10n/checkstyle/rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.header.RegexpHeaderCheck.html b/checkstyle-sonar-plugin/src/main/resources/org/sonar/l10n/checkstyle/rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.header.RegexpHeaderCheck.html index e725361a..d97b42db 100644 --- a/checkstyle-sonar-plugin/src/main/resources/org/sonar/l10n/checkstyle/rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.header.RegexpHeaderCheck.html +++ b/checkstyle-sonar-plugin/src/main/resources/org/sonar/l10n/checkstyle/rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.header.RegexpHeaderCheck.html @@ -1,30 +1,30 @@Checks the header of a source file against a header that contains a regular expression for each line of the source header.
-Rationale: In some projects checking against a fixed header is not sufficient, e.g. the header might require a copyright line where the year information is not static. For example, consider the following header:
+Rationale: In some projects checking against a fixed header is not sufficient, e.g. the header might require a copyright line where the year information is not static. For example, consider the following header:
- line 1: ^/{71}$ - line 2: ^// checkstyle:$ - line 3: ^// Checks Java source code for adherence to a set of rules\.$ - line 4: ^// Copyright \(C\) \d\d\d\d Oliver Burn$ - line 5: ^// Last modification by \$Author.*\$$ - line 6: ^/{71}$ - line 7: - line 8: ^package - line 9: - line 10: ^import - line 11: - line 12: ^/\*\* - line 13: ^ \*([^/]|$) - line 14: ^ \*/ + line 1: ^/{71}$ + line 2: ^// checkstyle:$ + line 3: ^// Checks Java source code for adherence to a set of rules\.$ + line 4: ^// Copyright \(C\) \d\d\d\d Oliver Burn$ + line 5: ^// Last modification by \$Author.*\$$ + line 6: ^/{71}$ + line 7: + line 8: ^package + line 9: + line 10: ^import + line 11: + line 12: ^/\*\* + line 13: ^ \*([^/]|$) + line 14: ^ \*/
Lines 1 and 6 demonstrate a more compact notation for 71 '/' characters. Line 4 enforces that the copyright notice includes a four digit year. Line 5 is an example how to enforce revision control keywords in a file header. Lines 12-14 is a template for javadoc (line 13 is so complicated to remove conflict with and of javadoc comment).
Different programming languages have different comment syntax rules, but all of them start a comment with a non-word character. Hence you can often use the non-word character class to abstract away the concrete comment syntax and allow checking the header for different languages with a single header definition. For example, consider the following header specification (note that this is not the full Apache license header):
- line 1: ^#! - line 2: ^<\?xml.*>$ - line 3: ^\W*$ - line 4: ^\W*Copyright 2006 The Apache Software Foundation or its licensors, as applicable\.$ - line 5: ^\W*Licensed under the Apache License, Version 2\.0 \(the "License"\);$ - line 6: ^\W*$ + line 1: ^#! + line 2: ^<\?xml.*>$ + line 3: ^\W*$ + line 4: ^\W*Copyright 2006 The Apache Software Foundation or its licensors, as applicable\.$ + line 5: ^\W*Licensed under the Apache License, Version 2\.0 \(the "License"\);$ + line 6: ^\W*$
Lines 1 and 2 leave room for technical header lines, e.g. the "#!/bin/sh" line in Unix shell scripts, or the xml file header of XML files. Set the multiline property to "1, 2" so these lines can be ignored for file types where they do no apply. Lines 3 through 6 define the actual header content. Note how lines 2, 4 and 5 use escapes for characters that have special regexp semantics.
Note: ignoreLines property has been removed from this check to simplify it. To make some line optional use "^.*$" regexp for this line.
diff --git a/checkstyle-sonar-plugin/src/main/resources/org/sonar/l10n/checkstyle/rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.imports.ImportControlCheck.html b/checkstyle-sonar-plugin/src/main/resources/org/sonar/l10n/checkstyle/rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.imports.ImportControlCheck.html index e0a75357..00d2d1db 100644 --- a/checkstyle-sonar-plugin/src/main/resources/org/sonar/l10n/checkstyle/rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.imports.ImportControlCheck.html +++ b/checkstyle-sonar-plugin/src/main/resources/org/sonar/l10n/checkstyle/rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.imports.ImportControlCheck.html @@ -1,6 +1,6 @@diff --git a/checkstyle-sonar-plugin/src/main/resources/org/sonar/l10n/checkstyle/rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.whitespace.FileTabCharacterCheck.html b/checkstyle-sonar-plugin/src/main/resources/org/sonar/l10n/checkstyle/rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.whitespace.FileTabCharacterCheck.html index 91498462..80068566 100644 --- a/checkstyle-sonar-plugin/src/main/resources/org/sonar/l10n/checkstyle/rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.whitespace.FileTabCharacterCheck.html +++ b/checkstyle-sonar-plugin/src/main/resources/org/sonar/l10n/checkstyle/rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.whitespace.FileTabCharacterCheck.html @@ -1,7 +1,6 @@ -
Checks that there are no tab characters ('\t') in the source code. Rationale: +
Checks that there are no tab characters ('\t') in the source code. Rationale: