-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
824c866
commit f7efc02
Showing
7 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
p3c-pmd/src/main/java/demo/HelloWordClassMissHelloWordMethodRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
p3c-pmd/src/test/java/demo/HelloWordClassMissHelloWordMethodRuleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
p3c-pmd/src/test/resources/demo/xml/HelloWordClassMissHelloWordMethodRule.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |