Skip to content

Commit

Permalink
test(测试): 增加示例代码
Browse files Browse the repository at this point in the history
  • Loading branch information
godfather1103 committed Sep 28, 2023
1 parent 824c866 commit f7efc02
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@
<rule ref="rulesets/java/ali-flowcontrol.xml">
<exclude name="NeedBraceRule"/>
</rule>

<rule name="HelloWordClassMissHelloWordMethodRule"
language="java"
since="1.6"
message="demo.HelloWordClassMissHelloWordMethodRule.violation.msg" class="demo.HelloWordClassMissHelloWordMethodRule">
<priority>5</priority>
</rule>
</ruleset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package demo;

import com.alibaba.p3c.pmd.lang.java.rule.AbstractAliRule;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;

import java.util.List;

/**
* <p>Title: Godfather1103's Github</p>
* <p>Copyright: Copyright (c) 2023</p>
* <p>Company: https://github.com/godfather1103</p>
* HelloWordClassMissHelloWordMethodRule
*
* @author 作者: Jack Chu E-mail: chuchuanbao@gmail.com<BR>
* 创建时间:2023-09-28 21:33
* @version 1.0
* @since 1.0
*/
public class HelloWordClassMissHelloWordMethodRule extends AbstractAliRule {

private final String hitWord = "HelloWord";

@Override
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
String className = node.getSimpleName();
if (className.contains(hitWord)) {
List<ASTMethodDeclaration> methods = node.findDescendantsOfType(ASTMethodDeclaration.class);
if (methods.stream().noneMatch(it -> hitWord.equalsIgnoreCase(it.getName()))) {
addViolationWithMessage(data, node, "demo.HelloWordClassMissHelloWordMethodRule.violation.msg");
}
}
return super.visit(node, data);
}
}
9 changes: 8 additions & 1 deletion p3c-pmd/src/main/resources/messages.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@
</entry>

<entry key="java.concurrent.AvoidCallStaticSimpleDateFormatRule.violation.msg">【%s()】可能导致线程安全问题</entry>
<entry key="java.concurrent.AvoidCallStaticSimpleDateFormatRule.rule.msg"><![CDATA[SimpleDateFormat 是线程不安全的类,一般不要定义为static变量,如果定义为static,必须加锁,或者使用DateUtils工具类。]]>
<entry key="java.concurrent.AvoidCallStaticSimpleDateFormatRule.rule.msg">
<![CDATA[SimpleDateFormat 是线程不安全的类,一般不要定义为static变量,如果定义为static,必须加锁,或者使用DateUtils工具类。]]>
</entry>
<entry key="java.concurrent.AvoidCallStaticSimpleDateFormatRule.rule.desc"><![CDATA[
说明:如果是JDK8的应用,可以使用instant代替Date,LocalDateTime代替Calendar,DateTimeFormatter代替SimpleDateFormat,官方给出的解释:simple beautiful strong immutable thread-safe。
Expand Down Expand Up @@ -555,4 +556,10 @@
<![CDATA[浮点数之间的等值判断,基本数据类型不能用==来比较,包装数据类型不能用equals来判断。]]>
</entry>

<entry key="demo.HelloWordClassMissHelloWordMethodRule.violation.msg">
<![CDATA[
类名包含HelloWord时,必须要存在一个名为helloword的方法
]]>
</entry>

</properties>
6 changes: 6 additions & 0 deletions p3c-pmd/src/main/resources/messages_en.xml
Original file line number Diff line number Diff line change
Expand Up @@ -560,4 +560,10 @@ Positive example:
<![CDATA[To judge the equivalence of floating-point numbers, == cannot be used for primitive types, while equals cannot be used for wrapper classes.]]>
</entry>

<entry key="demo.HelloWordClassMissHelloWordMethodRule.violation.msg">
<![CDATA[
the name of a class contains HelloWord, the class must have a method called helloword.
]]>
</entry>

</properties>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package demo;

import net.sourceforge.pmd.testframework.SimpleAggregatorTst;

public class HelloWordClassMissHelloWordMethodRuleTest extends SimpleAggregatorTst {
@Override
protected void setUp() {
// addRule("java-demo","HelloWordClassMissHelloWordMethodRule");
addRule("rulesets/java/demo.xml","HelloWordClassMissHelloWordMethodRule");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data xmlns="http://pmd.sourceforge.net/rule-tests"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests https://pmd.sourceforge.io/rule-tests_1_0_0.xsd">
<code-fragment id="finally-has-return">
<![CDATA[
public class ExampleHelloWord1 {
public int fn() {
try {
return 1;
} finally {
return 0;
}
}
}
]]>
</code-fragment>
<test-code>
<description>the class not exist helloword</description>
<expected-problems>1</expected-problems>
<code-ref id="finally-has-return"/>
</test-code>

<code-fragment id="have-helloword">
<![CDATA[
public class ExampleHelloWord2 {
public int helloword() {
try {
return 1;
} finally {
return 0;
}
}
}
]]>
</code-fragment>
<test-code>
<description>the class exist helloword</description>
<expected-problems>0</expected-problems>
<code-ref id="have-helloword"/>
</test-code>
</test-data>
14 changes: 14 additions & 0 deletions p3c-pmd/src/test/resources/rulesets/java/demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>

<ruleset name="AlibabaJavaOthers" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>Demo</description>
<rule name="HelloWordClassMissHelloWordMethodRule"
language="java"
since="1.6"
message="demo.HelloWordClassMissHelloWordMethodRule.violation.msg"
class="demo.HelloWordClassMissHelloWordMethodRule">
<priority>5</priority>
</rule>
</ruleset>

0 comments on commit f7efc02

Please sign in to comment.