骏烈 7 years ago
parent 1ade912a4e
commit 40eaededd1

@ -61,7 +61,7 @@ object CodeAnalysis {
if (monitor.isCanceled) { if (monitor.isCanceled) {
return@run Status.CANCEL_STATUS return@run Status.CANCEL_STATUS
} }
if(it.isAccessible){ if (it.isAccessible) {
it.accept(fileVisitor) it.accept(fileVisitor)
} }
} }

@ -95,7 +95,7 @@ Counter example:
a.add("1"); a.add("1");
a.add("2"); a.add("2");
for (String temp : a) { for (String temp : a) {
if ("1".equals(temp)){ if ("1".equals(temp)) {
a.remove(temp); a.remove(temp);
} }
} }
@ -204,7 +204,7 @@ Positive example:
// please refer to the pseudo-code as follows // please refer to the pseudo-code as follows
boolean existed = (file.open(fileName, "w") != null) && (...) || (...); boolean existed = (file.open(fileName, "w") != null) && (...) || (...);
if (existed) { if (existed) {
... //...
} }
``` ```
@ -212,7 +212,7 @@ Positive example:
```java ```java
if ((file.open(fileName, "w") != null) && (...) || (...)) { if ((file.open(fileName, "w") != null) && (...) || (...)) {
... // ...
} }
``` ```

@ -25,15 +25,15 @@ import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
* @date 2016/11/16 * @date 2016/11/16
*/ */
public class NodeUtils { public class NodeUtils {
public static boolean isParentOrSelf(Node descendant,Node ancestor){ public static boolean isParentOrSelf(Node descendant, Node ancestor) {
if(descendant == ancestor) { if (descendant == ancestor) {
return true; return true;
} }
if(descendant == null || ancestor == null){ if (descendant == null || ancestor == null) {
return false; return false;
} }
Node parent = descendant.jjtGetParent(); Node parent = descendant.jjtGetParent();
while(parent != ancestor && parent != null){ while (parent != ancestor && parent != null) {
parent = parent.jjtGetParent(); parent = parent.jjtGetParent();
} }
return parent == ancestor; return parent == ancestor;
@ -41,6 +41,7 @@ public class NodeUtils {
/** /**
* TODO optimize * TODO optimize
*
* @param expression expression * @param expression expression
* @return true if wrapper type * @return true if wrapper type
*/ */

@ -11,14 +11,14 @@
<priority>1</priority> <priority>1</priority>
<example> <example>
<![CDATA[ <![CDATA[
Negative example: Negative example:
//It is hard to tell whether it is number 11 or Long 1. //It is hard to tell whether it is number 11 or Long 1.
Long warn = 1l; Long warn = 1l;
]]> ]]>
</example> </example>
<example> <example>
<![CDATA[ <![CDATA[
Positive example: Positive example:
Long notwarn = 1L; Long notwarn = 1L;
]]> ]]>
</example> </example>
@ -31,18 +31,18 @@
<example> <example>
<![CDATA[ <![CDATA[
Negative example: Negative example:
//Magic values, except for predefined, are forbidden in coding. //Magic values, except for predefined, are forbidden in coding.
if(key.equals("Id#taobao_1")){ if (key.equals("Id#taobao_1")) {
//... //...
} }
]]> ]]>
</example> </example>
<example> <example>
<![CDATA[ <![CDATA[
Positive example: Positive example:
String KEY_PRE = "Id#taobao_1"; String KEY_PRE = "Id#taobao_1";
if(key.equals(KEY_PRE)){ if (KEY_PRE.equals(key)) {
//... //...
} }
]]> ]]>

@ -12,12 +12,12 @@
<example> <example>
<![CDATA[ <![CDATA[
switch( x ){ switch (x) {
case 1 : case 1:
break ; break;
case 2 : case 2:
break ; break;
default : default:
} }
]]> ]]>
</example> </example>
@ -31,7 +31,7 @@
<example> <example>
<![CDATA[ <![CDATA[
if(flag) { if (flag) {
System.out.println("hello world"); System.out.println("hello world");
} }
]]> ]]>
@ -49,7 +49,7 @@
<![CDATA[ <![CDATA[
Negative example: Negative example:
if ((file.open(fileName, "w") != null) && (...) || (...)) { if ((file.open(fileName, "w") != null) && (...) || (...)) {
... // ...
} }
]]> ]]>
</example> </example>
@ -58,7 +58,7 @@ Negative example:
Positive example: Positive example:
boolean existed = (file.open(fileName, "w") != null) && (...) || (...); boolean existed = (file.open(fileName, "w") != null) && (...) || (...);
if (existed) { if (existed) {
... //...
} }
]]> ]]>
</example> </example>

@ -14,7 +14,7 @@
<![CDATA[ <![CDATA[
public void f(String str){ public void f(String str){
String inner = "hi"; String inner = "hi";
if(inner.equals(str)){ if (inner.equals(str)) {
System.out.println("hello world"); System.out.println("hello world");
} }
} }
@ -34,7 +34,7 @@
Integer a = 235; Integer a = 235;
Integer b = 235; Integer b = 235;
if (a.equals(b)) { if (a.equals(b)) {
//相等 // code
} }
]]> ]]>
</example> </example>
@ -112,7 +112,7 @@
<example> <example>
<![CDATA[ <![CDATA[
反例: Negative example:
String result; String result;
for (String string : tagNameList) { for (String string : tagNameList) {
result = result + string; result = result + string;
@ -121,7 +121,7 @@
</example> </example>
<example> <example>
<![CDATA[ <![CDATA[
正例: Positive example:
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
for (String string : tagNameList) { for (String string : tagNameList) {
stringBuilder.append(string); stringBuilder.append(string);

@ -101,10 +101,9 @@ Negative example:
Iterator<Integer> it=b.iterator(); Iterator<Integer> it=b.iterator();
while(it.hasNext()){ while(it.hasNext()){
Integer temp = it.next(); Integer temp = it.next();
if(delCondition){ if (delCondition) {
it.remove(); it.remove();
} }
} }
]]> ]]>
</example> </example>

@ -11,7 +11,7 @@
<test-code> <test-code>
<description>UndefineMagicConstant.</description> <description>UndefineMagicConstant.</description>
<expected-problems>0</expected-problems> <expected-problems>0</expected-problems>
<code-ref id="constants-ok" /> <code-ref id="constants-ok"/>
</test-code> </test-code>
<code-fragment id="constants-err"><![CDATA[ <code-fragment id="constants-err"><![CDATA[
@ -22,27 +22,27 @@
Boolean h = false; Boolean h = false;
Long m = 2L; Long m = 2L;
String n = ""; String n = "";
if(i > 2){ if (i > 2) {
} }
if(i > 1){ if (i > 1) {
} }
if(m > 1L){ if (m > 1L) {
} }
if(i != null){ if (i != null) {
} }
if(h != false){ if (h != false) {
} }
if(n.equals("")){ if (n.equals("")) {
} }
for(int j=0 ; j< 10 ; i++){ for (int j = 0; j < 10; i++) {
if(i > 2){ if (i > 2) {
} }
if(i != null){ if (i != null) {
} }
} }
while(k < 1){ while (k < 1) {
if(i > 2){ if (i > 2) {
} }
k++; k++;
} }
@ -55,7 +55,7 @@
<description>UndefineMagicConstant.</description> <description>UndefineMagicConstant.</description>
<expected-problems>2</expected-problems> <expected-problems>2</expected-problems>
<expected-linenumbers>8,20</expected-linenumbers> <expected-linenumbers>8,20</expected-linenumbers>
<code-ref id="constants-err" /> <code-ref id="constants-err"/>
</test-code> </test-code>
<code-fragment id="constants-err-2"><![CDATA[ <code-fragment id="constants-err-2"><![CDATA[
@ -73,7 +73,7 @@
<description>UndefineMagicConstant.</description> <description>UndefineMagicConstant.</description>
<expected-problems>1</expected-problems> <expected-problems>1</expected-problems>
<expected-linenumbers>3</expected-linenumbers> <expected-linenumbers>3</expected-linenumbers>
<code-ref id="constants-err-2" /> <code-ref id="constants-err-2"/>
</test-code> </test-code>
</test-data> </test-data>

@ -13,14 +13,14 @@
<test-code> <test-code>
<description>sets-UnsupportedExceptionWithModifyAsListRule-ok.</description> <description>sets-UnsupportedExceptionWithModifyAsListRule-ok.</description>
<expected-problems>0</expected-problems> <expected-problems>0</expected-problems>
<code-ref id="sets-UnsupportedExceptionWithModifyAsListRule-ok" /> <code-ref id="sets-UnsupportedExceptionWithModifyAsListRule-ok"/>
</test-code> </test-code>
<code-fragment id="sets-UnsupportedExceptionWithModifyAsListRule-warn"><![CDATA[ <code-fragment id="sets-UnsupportedExceptionWithModifyAsListRule-warn"><![CDATA[
public class Foo { public class Foo {
private void method1() { private void method1() {
if(true){ if (true) {
List<String> list = Arrays.asList("a","b","c"); List<String> list = Arrays.asList("a", "b", "c");
list.add("d"); list.add("d");
list.remove("22"); list.remove("22");
list.clear(); list.clear();
@ -31,8 +31,8 @@
} }
private void method2() { private void method2() {
if(true){ if (true) {
List<String> list = Arrays.asList("a","b","c"); List<String> list = Arrays.asList("a", "b", "c");
list.add("d"); list.add("d");
} }
List<String> list = new ArrayList<String>(); List<String> list = new ArrayList<String>();
@ -46,7 +46,7 @@
<description>sets-UnsupportedExceptionWithModifyAsListRule-warn.</description> <description>sets-UnsupportedExceptionWithModifyAsListRule-warn.</description>
<expected-problems>4</expected-problems> <expected-problems>4</expected-problems>
<expected-linenumbers>5,6,7,17</expected-linenumbers> <expected-linenumbers>5,6,7,17</expected-linenumbers>
<code-ref id="sets-UnsupportedExceptionWithModifyAsListRule-warn" /> <code-ref id="sets-UnsupportedExceptionWithModifyAsListRule-warn"/>
</test-code> </test-code>
</test-data> </test-data>
Loading…
Cancel
Save