PMD规则之String and StringBuffer Rules

来源:互联网 发布:最好拆卸软件 编辑:程序博客网 时间:2024/03/29 19:40

·  AvoidDuplicateLiterals: Code containing duplicate String literals can usually be improved by declaring the String as a constant field.

翻译   避免重复的字面量:代码包含重复的字符串常常可以重构为将此字符串声明为常量

·  StringInstantiation: Avoid instantiating String objects; this is usually unnecessary.

翻译   字符串初始化:避免初始化字符串对象;这是不必要的。

·  StringToString: Avoid calling toString() on String objects; this is unnecessary.

翻译   String.toString():避免对字符串对象调用toString()方法,这是不必要的

·  InefficientStringBuffering: Avoid concatenating non literals in a StringBuffer constructor or append().

翻译   低效的StringBuffering:避免在StringBuffer的构造器或append()方法中连接非字面量类型

·  UnnecessaryCaseChange: Using equalsIgnoreCase() is faster than using toUpperCase/toLowerCase().equals()

翻译   不必要的大小写转换:使用equalsIgnoreCase()比将字符串大小写转换一致后再比较要快。

·  UseStringBufferLength: Use StringBuffer.length() to determine StringBuffer length rather than using StringBuffer.toString().equals("") or StringBuffer.toString().length() ==.

翻译   使用StringBufferlength()方法:使用StringBuffer对象的length()方法来计算StringBuffer对象的长度,而不是使用StringBuffer.toString().equals("") or StringBuffer.toString().length() ==.等方法

·  AppendCharacterWithChar: Avoid concatenating characters as strings in StringBuffer.append.

翻译   char类型连接字符:在使用StringBufferappend()方法连接字符时,避免使用string类型。

·  ConsecutiveLiteralAppends: Consecutively calling StringBuffer.append with String literals

翻译   连续的字面量连接:连接字符串时连续的调用StringBufferappend()方法

·  UseIndexOfChar: Use String.indexOf(char) when checking for the index of a single character; it executes faster.

翻译   使用indexOf(字符):当你检测单个字符的位置时使用String.indexOf(字符),它执行的很快。不要使用indexOf(字符串)

·  InefficientEmptyStringCheck: String.trim().length() is an inefficient way to check if a String is really empty, as it creates a new String object just to check its size. Consider creating a static function that loops through a string, checking Character.isWhitespace() on each character and returning false if a non-whitespace character is found.

翻译   低效的空字符串检查:用String.trim().length()来判断字符串是否空是低效的做法,因为它会创建一个新的字符串对象然后判断大小。考虑创建一个静态的方法循环String,用isWhitespace()检查每个字符如果遇到非空白字符就返回false

·  InsufficientStringBufferDeclaration: Failing to pre-size a StringBuffer properly could cause it to re-size many times during runtime. This rule checks the characters that are actually passed into StringBuffer.append(), but represents a best guess "worst case" scenario. An empty StringBuffer constructor initializes the object to 16 characters. This default is assumed if the length of the constructor can not be determined.

翻译   不充分的StringBuffer声明:如果不能在事前声明合适大小的StringBuffer容量可能导致运行期不断地重新分配大小。本规则检查字符事实上传递给StringBuffer.append(),但是表明了在最坏情况下的最好的预测。空参数的StringBuffer构造器默认将对象初始化为16个字符的容量。这个默认情况是在构造长度无法确定的情况下假定的。

·  UselessStringValueOf: No need to call String.valueOf to append to a string; just use the valueOf() argument directly.

翻译   无用的valueOf方法:调用append()方法时不需要把参数用valueOf()转换一次,直接将非String类型的值作为参数放在append()里面。

·  StringBufferInstantiationWithChar: StringBuffer sb = new StringBuffer('c'); The char will be converted into int to intialize StringBuffer size.

翻译   StringBuffer使用字符初始化:StringBuffer sb = new StringBuffer('c');字符c会转换为int值,作为StringBuffer的初始化大小参数。

·  UseEqualsToCompareStrings: Using '==' or '!=' to compare strings only works if intern version is used on both sides

翻译   使用equals方法比较字符串:使用‘==’或‘!=’比较字符串大小只是比较两边的常量池的引用。

·  AvoidStringBufferField: StringBuffers can grow quite a lot, and so may become a source of memory leak (if the owning class has a long life time).

翻译   避免在类中使用StringBuffer属性:StringBuffer类型变量可以变得非常庞大,所以可能造成内存泄漏。(如果宿主类有很长的生命期)