From e73875729a9937a59eecc8bcb1765095b43db2e4 Mon Sep 17 00:00:00 2001 From: libaoxu Date: Wed, 16 Oct 2019 09:49:05 +0800 Subject: [PATCH 1/2] =?UTF-8?q?1.=20=E5=8F=AA=E8=A6=81=E9=87=8D=E5=86=99eq?= =?UTF-8?q?uals=EF=BC=8C=E5=B0=B1=E5=BF=85=E9=A1=BB=E9=87=8D=E5=86=99hashC?= =?UTF-8?q?ode=202.=20=E7=9B=B8=E5=90=8C=E5=8F=82=E6=95=B0=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=EF=BC=8C=E7=9B=B8=E5=90=8C=E4=B8=9A=E5=8A=A1=E5=90=AB?= =?UTF-8?q?=E4=B9=89=EF=BC=8C=E6=89=8D=E5=8F=AF=E4=BB=A5=E4=BD=BF=E7=94=A8?= =?UTF-8?q?Java=E7=9A=84=E5=8F=AF=E5=8F=98=E5=8F=82=E6=95=B0=EF=BC=8C?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E4=BD=BF=E7=94=A8Object?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/rule/oop/VarargsParameterRule.java | 44 ++++++++++++++++ .../java/rule/set/EqualsHashCodeRule.java | 51 +++++++++++++++++++ p3c-pmd/src/main/resources/messages.xml | 6 +++ p3c-pmd/src/main/resources/messages_en.xml | 6 +++ .../main/resources/rulesets/java/ali-oop.xml | 12 +++++ .../main/resources/rulesets/java/ali-set.xml | 18 +++++++ 6 files changed, 137 insertions(+) create mode 100644 p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/oop/VarargsParameterRule.java create mode 100644 p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/set/EqualsHashCodeRule.java diff --git a/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/oop/VarargsParameterRule.java b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/oop/VarargsParameterRule.java new file mode 100644 index 000000000..a44832732 --- /dev/null +++ b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/oop/VarargsParameterRule.java @@ -0,0 +1,44 @@ +package com.alibaba.p3c.pmd.lang.java.rule.oop; + +/* + * Copyright 1999-2017 Alibaba Group. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import com.alibaba.p3c.pmd.I18nResources; +import com.alibaba.p3c.pmd.lang.AbstractXpathRule; +import com.alibaba.p3c.pmd.lang.java.util.ViolationUtils; +import net.sourceforge.pmd.lang.ast.Node; + +/** + * [Mandatory] + * + * @author leonard99559 + * @date 2019/10/16 + */ +public class VarargsParameterRule extends AbstractXpathRule { + private static final String XPATH = "//FormalParameter[@Varargs = 'true' and ./Type[@TypeImage = 'Object']]"; + + public VarargsParameterRule() { + setXPath(XPATH); + } + + @Override + public void addViolation(Object data, Node node, String arg) { + ViolationUtils.addViolationWithPrecisePosition(this, node, data, + I18nResources.getMessage("java.oop.VarargsParameterRule.rule.msg", + node.getImage())); + } + +} \ No newline at end of file diff --git a/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/set/EqualsHashCodeRule.java b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/set/EqualsHashCodeRule.java new file mode 100644 index 000000000..810e5caab --- /dev/null +++ b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/set/EqualsHashCodeRule.java @@ -0,0 +1,51 @@ +package com.alibaba.p3c.pmd.lang.java.rule.set; + +/* + * Copyright 1999-2017 Alibaba Group. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import com.alibaba.p3c.pmd.lang.java.rule.AbstractAliRule; +import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit; +import org.jaxen.JaxenException; + +import java.util.List; + +/** + * [Mendatory] Equals method must be with hashCode() method. + * + * @author leonard99559 + * @date 2019/10/16 + */ +public class EqualsHashCodeRule extends AbstractAliRule { + + @Override + public Object visit(ASTCompilationUnit rootNode, Object data) { + + try { + List nodeList = rootNode.findChildNodesWithXPath( + "//ClassOrInterfaceBodyDeclaration[./Annotation[@AnnotationName = 'Override']]" + + "//MethodDeclaration[@MethodName = 'hashCode' or @MethodName = 'equals']"); + if (nodeList.size() == 1) { + addViolationWithMessage(data, nodeList.get(0), + "java.set.EqualsHashCodeRule.rule.msg"); + } + } catch (JaxenException e) { + e.printStackTrace(); + } + return super.visit(rootNode, data); + } + +} diff --git a/p3c-pmd/src/main/resources/messages.xml b/p3c-pmd/src/main/resources/messages.xml index b5b1cba95..417e0f5c5 100644 --- a/p3c-pmd/src/main/resources/messages.xml +++ b/p3c-pmd/src/main/resources/messages.xml @@ -260,6 +260,9 @@ + + + @@ -348,6 +351,9 @@ + + + diff --git a/p3c-pmd/src/main/resources/messages_en.xml b/p3c-pmd/src/main/resources/messages_en.xml index 4666a7f33..08bd7cbfe 100644 --- a/p3c-pmd/src/main/resources/messages_en.xml +++ b/p3c-pmd/src/main/resources/messages_en.xml @@ -262,6 +262,9 @@ Note: Below are the problems created by usage of Executors for thread pool creat + + + @@ -348,6 +351,9 @@ Note: Below are the problems created by usage of Executors for thread pool creat + + + diff --git a/p3c-pmd/src/main/resources/rulesets/java/ali-oop.xml b/p3c-pmd/src/main/resources/rulesets/java/ali-oop.xml index 77ce92875..ca6837b9d 100644 --- a/p3c-pmd/src/main/resources/rulesets/java/ali-oop.xml +++ b/p3c-pmd/src/main/resources/rulesets/java/ali-oop.xml @@ -153,5 +153,17 @@ Positive example: ]]> + + 1 + + + + diff --git a/p3c-pmd/src/main/resources/rulesets/java/ali-set.xml b/p3c-pmd/src/main/resources/rulesets/java/ali-set.xml index 1df62235e..121b63b7f 100644 --- a/p3c-pmd/src/main/resources/rulesets/java/ali-set.xml +++ b/p3c-pmd/src/main/resources/rulesets/java/ali-set.xml @@ -129,5 +129,23 @@ Negative example: ]]> + + 1 + + + + From 3feca7f3f5a1bbbb430f18856b94476e944e2308 Mon Sep 17 00:00:00 2001 From: 11102769 <11102769@vivo.xyz> Date: Mon, 20 Jan 2020 13:33:17 +0800 Subject: [PATCH 2/2] =?UTF-8?q?1.=20=E5=8A=A0=E5=85=A5unit=20testcase=202.?= =?UTF-8?q?=20=E5=AE=8C=E5=96=84=E6=8F=90=E7=A4=BAtips?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- p3c-pmd/src/main/resources/messages.xml | 10 ++- p3c-pmd/src/main/resources/messages_en.xml | 10 ++- .../main/resources/rulesets/java/ali-oop.xml | 6 +- .../main/resources/rulesets/java/ali-set.xml | 33 ++++++-- .../pmd/lang/java/rule/oop/OopRuleTest.java | 1 + .../pmd/lang/java/rule/set/SetRulesTest.java | 1 + .../rule/oop/xml/VarargsParameterRule.xml | 27 +++++++ .../java/rule/set/xml/EqualsHashCodeRule.xml | 80 +++++++++++++++++++ 8 files changed, 157 insertions(+), 11 deletions(-) create mode 100644 p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/oop/xml/VarargsParameterRule.xml create mode 100644 p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/set/xml/EqualsHashCodeRule.xml diff --git a/p3c-pmd/src/main/resources/messages.xml b/p3c-pmd/src/main/resources/messages.xml index 417e0f5c5..a3d7e0ed4 100644 --- a/p3c-pmd/src/main/resources/messages.xml +++ b/p3c-pmd/src/main/resources/messages.xml @@ -261,7 +261,13 @@ - + @@ -352,7 +358,7 @@ - + diff --git a/p3c-pmd/src/main/resources/messages_en.xml b/p3c-pmd/src/main/resources/messages_en.xml index 08bd7cbfe..4b4cbc55d 100644 --- a/p3c-pmd/src/main/resources/messages_en.xml +++ b/p3c-pmd/src/main/resources/messages_en.xml @@ -263,7 +263,13 @@ Note: Below are the problems created by usage of Executors for thread pool creat - + @@ -352,7 +358,7 @@ Note: Below are the problems created by usage of Executors for thread pool creat - + diff --git a/p3c-pmd/src/main/resources/rulesets/java/ali-oop.xml b/p3c-pmd/src/main/resources/rulesets/java/ali-oop.xml index ca6837b9d..22eb84945 100644 --- a/p3c-pmd/src/main/resources/rulesets/java/ali-oop.xml +++ b/p3c-pmd/src/main/resources/rulesets/java/ali-oop.xml @@ -159,8 +159,12 @@ Positive example: 1 diff --git a/p3c-pmd/src/main/resources/rulesets/java/ali-set.xml b/p3c-pmd/src/main/resources/rulesets/java/ali-set.xml index 121b63b7f..6e1cd3196 100644 --- a/p3c-pmd/src/main/resources/rulesets/java/ali-set.xml +++ b/p3c-pmd/src/main/resources/rulesets/java/ali-set.xml @@ -136,14 +136,35 @@ Negative example: diff --git a/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/lang/java/rule/oop/OopRuleTest.java b/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/lang/java/rule/oop/OopRuleTest.java index 9ab81aa27..47be8e49c 100644 --- a/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/lang/java/rule/oop/OopRuleTest.java +++ b/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/lang/java/rule/oop/OopRuleTest.java @@ -38,5 +38,6 @@ public void setUp() { addRule(RULESET, "PojoMustOverrideToStringRule"); addRule(RULESET, "StringConcatRule"); addRule(RULESET, "BigDecimalAvoidDoubleConstructorRule"); + addRule(RULESET, "VarargsParameterRule"); } } diff --git a/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/lang/java/rule/set/SetRulesTest.java b/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/lang/java/rule/set/SetRulesTest.java index b96c6e3e9..b3b736e14 100644 --- a/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/lang/java/rule/set/SetRulesTest.java +++ b/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/lang/java/rule/set/SetRulesTest.java @@ -35,5 +35,6 @@ public void setUp() { addRule(RULESET, "ConcurrentExceptionWithModifyOriginSubListRule"); addRule(RULESET, "DontModifyInForeachCircleRule"); addRule(RULESET, "UnsupportedExceptionWithModifyAsListRule"); + addRule(RULESET, "EqualsHashCodeRule"); } } diff --git a/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/oop/xml/VarargsParameterRule.xml b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/oop/xml/VarargsParameterRule.xml new file mode 100644 index 000000000..0db9bf1e7 --- /dev/null +++ b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/oop/xml/VarargsParameterRule.xml @@ -0,0 +1,27 @@ + + + + + + + + varargs-with-object-type + 1 + 2 + + + + + + \ No newline at end of file diff --git a/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/set/xml/EqualsHashCodeRule.xml b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/set/xml/EqualsHashCodeRule.xml new file mode 100644 index 000000000..ffabef18c --- /dev/null +++ b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/set/xml/EqualsHashCodeRule.xml @@ -0,0 +1,80 @@ + + + + + + + + sets-EqualsHashCodeRule-ok. + 0 + + + + + + + + sets-EqualsHashCodeRule-missHashCodeWarn. + 1 + 5 + + + + + + + + sets-EqualsHashCodeRule-missEqualsWarn. + 1 + 5 + + +