Maven checkstyle 插件给出 "Parameter s should be final." 的错误,即使 s 被修改
Maven checkstyle plugin gives error for "Parameter s should be final.", even s is modified
我们正在使用 maven checkstyle 插件。下面是 pom.xml 配置。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<configuration>
<configLocation>sun_checks.xml</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<logViolationsToConsole>true</logViolationsToConsole>
<failsOnError>true</failsOnError>
<failOnViolation>true</failOnViolation>
<violationSeverity>error</violationSeverity>
</configuration>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>7.8.1</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
我们有以下 java 代码。
package com.comp.teststyleguide;
public class A {
public A(String s) {
s = "test1";
}
}
现在,mvn clean package
给出以下控制台消息。
[ERROR]
/work/checkstyle/teststyleguide/src/main/java/com/comp/teststyleguide/A.java:5:12:
Parameter s should be final. [FinalParameters]
您不应该重新分配参数。
Checkstyle 有一个单独的 ParameterAssignmentCheck 用于您不想设置参数的情况 final
。
来自 JavaDoc:
Disallow assignment of parameters.
Rationale: Parameter assignment is often considered poor programming
practice. Forcing developers to declare parameters as final is often
onerous. Having a check ensure that parameters are never assigned
would give the best of both worlds.
在方法中重新分配方法参数值不是好的编程习惯,
Check that parameters for methods, constructors, and catch blocks are
final. Interface, abstract, and native methods are not checked: the
final keyword does not make sense for interface, abstract, and native
method parameters as there is no code that could modify the parameter.
Rationale: Changing the value of parameters during the execution of
the method's algorithm can be confusing and should be avoided. A great
way to let the Java compiler prevent this coding style is to declare
parameters final.
您可以在 checkstyle 文档中找到更多信息。 http://checkstyle.sourceforge.net/config_misc.html#FinalParameters
我们正在使用 maven checkstyle 插件。下面是 pom.xml 配置。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<configuration>
<configLocation>sun_checks.xml</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<logViolationsToConsole>true</logViolationsToConsole>
<failsOnError>true</failsOnError>
<failOnViolation>true</failOnViolation>
<violationSeverity>error</violationSeverity>
</configuration>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>7.8.1</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
我们有以下 java 代码。
package com.comp.teststyleguide;
public class A {
public A(String s) {
s = "test1";
}
}
现在,mvn clean package
给出以下控制台消息。
[ERROR] /work/checkstyle/teststyleguide/src/main/java/com/comp/teststyleguide/A.java:5:12: Parameter s should be final. [FinalParameters]
您不应该重新分配参数。
Checkstyle 有一个单独的 ParameterAssignmentCheck 用于您不想设置参数的情况 final
。
来自 JavaDoc:
Disallow assignment of parameters.
Rationale: Parameter assignment is often considered poor programming practice. Forcing developers to declare parameters as final is often onerous. Having a check ensure that parameters are never assigned would give the best of both worlds.
在方法中重新分配方法参数值不是好的编程习惯,
Check that parameters for methods, constructors, and catch blocks are final. Interface, abstract, and native methods are not checked: the final keyword does not make sense for interface, abstract, and native method parameters as there is no code that could modify the parameter.
Rationale: Changing the value of parameters during the execution of the method's algorithm can be confusing and should be avoided. A great way to let the Java compiler prevent this coding style is to declare parameters final.
您可以在 checkstyle 文档中找到更多信息。 http://checkstyle.sourceforge.net/config_misc.html#FinalParameters