· UnnecessaryConstructor: This rule detects when a constructor is not necessary; i.e., when there's only one constructor, it's public, has an empty body, and takes no arguments.
翻译 非必要的构造器:本规则检查不必要的构造器,例如:只存在一个公共的,空方法体的,无参的构造器
· NullAssignment: Assigning a "null" to a variable (outside of its declaration) is usually bad form. Some times, the assignment is an indication that the programmer doesn't completely understand what is going on in the code. NOTE: This sort of assignment may in rare cases be useful to encourage garbage collection. If that's what you're using it for, by all means, disregard this rule :-)
翻译 Null赋值:将null赋值给变量(在声明之外)常常是不好的形式。某些时候这种赋值表示程序员没有想好代码的下一步该做什么。
备注:当你需要把变量赋值为null提示垃圾收集器去进行垃圾收集时这是有用的,那么请忽略这个规则
· OnlyOneReturn: A method should have only one exit point, and that should be the last statement in the method.
翻译 只有一个返回:一个方法应该有且只有一处返回点,且应该是方法的最后一条语句。
· UnusedModifier: Fields in interfaces are automatically public static final, and methods are public abstract. Classes or interfaces nested in an interface are automatically public and static (all nested interfaces are automatically static). For historical reasons, modifiers which are implied by the context are accepted by the compiler, but are superfluous.
翻译 无用的修饰符:在接口中定义的域自动为public static final的,方法自动是public abstract的,接口中嵌套的类或接口自动是public static的。由于历史原因,上下文暗示的修饰符是被编译器接受的,但是是多余的。
· AssignmentInOperand: Avoid assignments in operands; this can make code more complicated and harder to read.
翻译 在操作中赋值:避免在操作中赋值;这会使代码复杂并且难以阅读
public class Foo {
public void bar() {
int x = 2;
if ((x = getX()) == 3) {
System.out.println("3!");
}
}
private int getX() {
return 3;
}
}
· AtLeastOneConstructor: Each class should declare at least one constructor.
翻译 至少有一个构造器:每个类应该至少声明一个构造器
· DontImportSun: Avoid importing anything from the 'sun.*' packages. These packages are not portable and are likely to change.
翻译 不要引入Sun包:避免从”sun.*”引入任何类,这些包不是轻便的而且可能更改
· SuspiciousOctalEscape: A suspicious octal escape sequence was found inside a String literal. The Java language specification (section 3.10.6) says an octal escape sequence inside a literal String shall consist of a backslash followed by: OctalDigit | OctalDigit OctalDigit | ZeroToThree OctalDigit OctalDigit Any octal escape sequence followed by non-octal digits can be confusing, e.g. "/038" is interpreted as the octal escape sequence "/03" followed by the literal character "8".
翻译 令人迷惑的八进制转义序列:在字符串字面量中出现令人迷惑的八进制转义序列。Java语言规范(第3.10.6节)讲到:在一个字面量字符串中的八进制转义序列应该包含一个反斜杠,后续可以是:八进制数字|八进制数字 八进制数字|0~3 八进制数字 八进制数字的格式。所以任何八进制转义序列后面紧跟着一个非八进制的数字就可能造成迷惑,例如:“/038”
· CallSuperInConstructor: It is a good practice to call super() in a constructor. If super() is not called but another constructor (such as an overloaded constructor) is called, this rule will not report it.
翻译 在构造器中调用super():在构造器中调用super()方法是很好的做法.如果没有调用super(),但是调用了另外的构造器,那么这个规则不会报告出来。
· UnnecessaryParentheses: Sometimes expressions are wrapped in unnecessary parentheses, making them look like a function call.
翻译 不必要的圆括号:有时候表达式被包在一个不必要的圆括号中,使它们看起来像是一个函数调用
public class Foo {
boolean bar() {
return (true);
}
}
· DefaultPackage: Use explicit scoping instead of the default package private level.
翻译 默认的包:使用明确的范围代替默认的包私有的级别
· BooleanInversion: Use bitwise inversion to invert boolean values - it's the fastest way to do this. See http://www.javaspecialists.co.za/archive/newsletter.do?issue=042&locale=en_US for specific details
翻译 布尔转换:使用按位转换来转换布尔值-这是最快的方法,参考:http://www.javaspecialists.co.za/archive/newsletter.do?issue=042&locale=en_US for specific details
public class Foo {
public void main(bar) {
boolean b = true;
b = !b; // slow
b ^= true; // fast
}
}
· DataflowAnomalyAnalysis: The dataflow analysis tracks local definitions, undefinitions and references to variables on different paths on the data flow. From those informations there can be found various problems. 1. UR - Anomaly: There is a reference to a variable that was not defined before. This is a bug and leads to an error. 2. DU - Anomaly: A recently defined variable is undefined. These anomalies may appear in normal source text. 3. DD - Anomaly: A recently defined variable is redefined. This is ominous but don't have to be a bug.
翻译 数据流异常分析:数据流分析是跟踪本地的变量定义与否及在数据流中不同路径的变量引用。由此可以发现多种问题:1.UR-异常:指向一个之前没有定义的变量,这是bug且可导致错误2.DU-异常:一个刚刚定义的变量是未定义的。这些异常可能出现在普通的源代码文本中3.DD-异常:一个刚刚定义的变量重新定义。这是不好的但并非一定是个bug。
注:这个规则实在有点绕,具体含义我也不是很透彻理解!
public class Foo {
public void foo() {
int buz = 5;
buz = 6; // redefinition of buz -> dd-anomaly
foo(buz);
buz = 2;
} // buz is undefined when leaving scope -> du-anomaly
}
· AvoidFinalLocalVariable: Avoid using final local variables, turn them into fields.
翻译 避免Final类型的本地变量:避免使用final类型的本地变量,将它们转为类域
public class MyClass {
public void foo() {
final String finalLocalVariable;
}
}
· AvoidUsingShortType: Java uses the 'short' type to reduce memory usage, not to optimize calculation. In fact, the jvm does not have any arithmetic capabilities for the short type: the jvm must convert the short into an int, do the proper caculation and convert the int back to a short. So, the use of the 'short' type may have a greater impact than memory usage.
翻译 避免使用short类型:Java使用’short’类型来减少内存开销,而不是优化计算。事实上,JVM不具备short类型的算术能力:jvm必须将short类型转化为int类型,然后进行适当的计算再把int类型转回short类型。因此,和内存开销比起来使用’short’类型会对性能有更大的影响
· AvoidUsingVolatile: Use of the keyword 'volatile' is general used to fine tune a Java application, and therefore, requires a good expertise of the Java Memory Model. Moreover, its range of action is somewhat misknown. Therefore, the volatile keyword should not be used for maintenance purpose and portability.
翻译 避免使用Volatile:使用关键字’volatile’一般用来调整一个Java应用,因此,需要一个专业的Java内存模型。此外,它的作用范围一定程度上是令人误解的。因此,volatile关键字应该不要被用做维护和移植的目的。
· AvoidUsingNativeCode: As JVM and Java language offer already many help in creating application, it should be very rare to have to rely on non-java code. Even though, it is rare to actually have to use Java Native Interface (JNI). As the use of JNI make application less portable, and harder to maintain, it is not recommended.
翻译 避免使用本地代码:jvm和Java语言已经提供了很多创建应用程序的帮助,依赖非Java代码应该是非常罕见的。即使如此,事实上必须使用Java本地接口也是罕见的。因为使用JNI使得应用可移植性降低,而且难以维护,所以是不推荐的。
· AvoidAccessibilityAlteration: Methods such as getDeclaredConstructors(), getDeclaredConstructor(Class[]) and setAccessible(), as the interface PrivilegedAction, allow to alter, at runtime, the visilibilty of variable, classes, or methods, even if they are private. Obviously, no one should do so, as such behavior is against everything encapsulation principal stands for.
翻译 避免改变访问控制:getDeclaredConstructors(), getDeclaredConstructor(Class[]) 和 setAccessible(),还有PrivilegedAction接口,允许在运行时改变变量、类和方法的可见性,甚至它们是私有的。显然,这是不应该的,因为这种动作违背了封装原则
· DoNotCallGarbageCollectionExplicitly: Calls to System.gc(), Runtime.getRuntime().gc(), and System.runFinalization() are not advised. Code should have the same behavior whether the garbage collection is disabled using the option -Xdisableexplicitgc or not. Moreover, "modern" jvms do a very good job handling garbage collections. If memory usage issues unrelated to memory leaks develop within an application, it should be dealt with JVM options rather than within the code itself.
翻译 不要显示的调用垃圾收集器:调用System.gc(), Runtime.getRuntime().gc(), 和 System.runFinalization()是不推荐的。当垃圾收集器使用配置项-Xdisableexplicitgc关闭时,使用代码可以同样进行垃圾收集。此外,现代JVM对于垃圾收集工作做得很棒。当开发一个应用时内存使用的影响无关于内存泄露时,垃圾收集应该交给JVM配置项进行管理而非代码本身。