Merge branch 'master'

This commit is contained in:
Clément Fournier
2018-03-24 19:05:13 +01:00
45 changed files with 193 additions and 621 deletions

View File

@@ -10,10 +10,6 @@
<version>6.2.0-SNAPSHOT</version>
</parent>
<properties>
<config.basedir>${basedir}/../pmd-core</config.basedir>
</properties>
<build>
<testResources>
<testResource>

View File

@@ -5,6 +5,7 @@
package net.sourceforge.pmd.lang.java.ast;
import java.util.List;
import java.util.Locale;
import net.sourceforge.pmd.lang.java.qname.JavaTypeQualifiedName;
@@ -46,7 +47,12 @@ public interface ASTAnyTypeDeclaration extends TypeNode, JavaQualifiableNode, Ac
* The kind of type this node declares.
*/
enum TypeKind {
CLASS, INTERFACE, ENUM, ANNOTATION
CLASS, INTERFACE, ENUM, ANNOTATION;
public String getPrintableName() {
return name().toLowerCase(Locale.ROOT);
}
}
}

View File

@@ -4,6 +4,8 @@
package net.sourceforge.pmd.lang.java.ast;
import java.util.Locale;
import net.sourceforge.pmd.lang.java.qname.JavaOperationQualifiedName;
@@ -34,7 +36,11 @@ public interface MethodLikeNode extends AccessNode, JavaQualifiableNode, JavaNod
enum MethodLikeKind {
METHOD,
CONSTRUCTOR,
LAMBDA
LAMBDA;
public String getPrintableName() {
return name().toLowerCase(Locale.ROOT);
}
}

View File

@@ -6,14 +6,12 @@ package net.sourceforge.pmd.lang.java.rule.design;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Logger;
import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
import net.sourceforge.pmd.lang.java.ast.MethodLikeNode;
import net.sourceforge.pmd.lang.java.ast.MethodLikeNode.MethodLikeKind;
import net.sourceforge.pmd.lang.java.metrics.JavaMetrics;
import net.sourceforge.pmd.lang.java.metrics.api.JavaClassMetricKey;
import net.sourceforge.pmd.lang.java.metrics.api.JavaOperationMetricKey;
@@ -126,7 +124,7 @@ public class CyclomaticComplexityRule extends AbstractJavaMetricsRule {
if (classWmc >= classReportLevel) {
int classHighest = (int) JavaMetrics.get(JavaOperationMetricKey.CYCLO, node, cycloOptions, ResultOption.HIGHEST);
String[] messageParams = {node.getTypeKind().name().toLowerCase(Locale.ROOT),
String[] messageParams = {node.getTypeKind().getPrintableName(),
node.getImage(),
" total",
classWmc + " (highest " + classHighest + ")", };
@@ -143,13 +141,8 @@ public class CyclomaticComplexityRule extends AbstractJavaMetricsRule {
int cyclo = (int) JavaMetrics.get(JavaOperationMetricKey.CYCLO, node, cycloOptions);
if (cyclo >= methodReportLevel) {
String nodeType = node.getKind() == MethodLikeKind.METHOD
? "method"
: node.getKind() == MethodLikeKind.CONSTRUCTOR
? "constructor"
: "lambda";
addViolation(data, node, new String[]{nodeType,
addViolation(data, node, new String[]{node.getKind().getPrintableName(),
node.getQualifiedName().getOperation(),
"",
"" + cyclo, });

View File

@@ -745,9 +745,6 @@ public class SecureSystem {
<description>
Annotating overridden methods with @Override ensures at compile time that
the method really overrides one, which helps refactoring and clarifies intent.
This rule is limited to resolving methods inherited from non-parameterized
or raw supertypes.
</description>
<priority>3</priority>
<example>

View File

@@ -198,9 +198,35 @@ public void bar() {
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_design.html#avoidthrowingnullpointerexception">
<description>
Avoid throwing NullPointerExceptions. These are confusing because most people will assume that the
virtual machine threw it. Consider using an IllegalArgumentException instead; this will be
clearly seen as a programmer-initiated exception.
<![CDATA[
Avoid throwing NullPointerExceptions manually. These are confusing because most people will assume that the
virtual machine threw it. To avoid a method being called with a null parameter, you may consider
using an IllegalArgumentException instead, making it clearly seen as a programmer-initiated exception.
However, there are better ways to handle this:
>*Effective Java, 3rd Edition, Item 72: Favor the use of standard exceptions*
>
>Arguably, every erroneous method invocation boils down to an illegal argument or state,
but other exceptions are standardly used for certain kinds of illegal arguments and states.
If a caller passes null in some parameter for which null values are prohibited, convention dictates that
NullPointerException be thrown rather than IllegalArgumentException.
To implement that, you are encouraged to use `java.util.Objects.requireNonNull()`
(introduced in Java 1.7). This method is designed primarily for doing parameter
validation in methods and constructors with multiple parameters.
Your parameter validation could thus look like the following:
```
public class Foo {
private String exampleValue;
void setExampleValue(String exampleValue) {
// check, throw and assignment in a single standard call
this.exampleValue = Objects.requireNonNull(exampleValue, "exampleValue must not be null!");
}
}
```
]]>
</description>
<priority>1</priority>
<properties>