diff --git a/eclipse-plugin/com.alibaba.smartfox.eclipse.feature/feature.xml b/eclipse-plugin/com.alibaba.smartfox.eclipse.feature/feature.xml index 552fc5e..5a734b0 100644 --- a/eclipse-plugin/com.alibaba.smartfox.eclipse.feature/feature.xml +++ b/eclipse-plugin/com.alibaba.smartfox.eclipse.feature/feature.xml @@ -2,7 +2,7 @@ diff --git a/eclipse-plugin/com.alibaba.smartfox.eclipse.feature/pom.xml b/eclipse-plugin/com.alibaba.smartfox.eclipse.feature/pom.xml index 0203765..d4ad403 100644 --- a/eclipse-plugin/com.alibaba.smartfox.eclipse.feature/pom.xml +++ b/eclipse-plugin/com.alibaba.smartfox.eclipse.feature/pom.xml @@ -5,7 +5,7 @@ com.alibaba.smartfox.eclipse smartfox-eclipse - 1.0.4-SNAPSHOT + 1.0.5-SNAPSHOT com.alibaba.smartfox.eclipse.feature eclipse-feature diff --git a/eclipse-plugin/com.alibaba.smartfox.eclipse.plugin/META-INF/MANIFEST.MF b/eclipse-plugin/com.alibaba.smartfox.eclipse.plugin/META-INF/MANIFEST.MF index 3a696bc..f2bc058 100644 --- a/eclipse-plugin/com.alibaba.smartfox.eclipse.plugin/META-INF/MANIFEST.MF +++ b/eclipse-plugin/com.alibaba.smartfox.eclipse.plugin/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: com.alibaba.smartfox.eclipse.plugin Bundle-SymbolicName: com.alibaba.smartfox.eclipse.plugin;singleton:=true -Bundle-Version: 1.0.4.qualifier +Bundle-Version: 1.0.5.qualifier Bundle-Activator: com.alibaba.smartfox.eclipse.SmartfoxActivator Bundle-Vendor: Alibaba Require-Bundle: org.eclipse.ui, diff --git a/eclipse-plugin/com.alibaba.smartfox.eclipse.plugin/pom.xml b/eclipse-plugin/com.alibaba.smartfox.eclipse.plugin/pom.xml index 5b461cd..eca2d1c 100644 --- a/eclipse-plugin/com.alibaba.smartfox.eclipse.plugin/pom.xml +++ b/eclipse-plugin/com.alibaba.smartfox.eclipse.plugin/pom.xml @@ -5,7 +5,7 @@ com.alibaba.smartfox.eclipse smartfox-eclipse - 1.0.4-SNAPSHOT + 1.0.5-SNAPSHOT com.alibaba.smartfox.eclipse.plugin eclipse-plugin diff --git a/eclipse-plugin/com.alibaba.smartfox.eclipse.updatesite/pom.xml b/eclipse-plugin/com.alibaba.smartfox.eclipse.updatesite/pom.xml index aecaa4c..88fcdf6 100644 --- a/eclipse-plugin/com.alibaba.smartfox.eclipse.updatesite/pom.xml +++ b/eclipse-plugin/com.alibaba.smartfox.eclipse.updatesite/pom.xml @@ -5,7 +5,7 @@ com.alibaba.smartfox.eclipse smartfox-eclipse - 1.0.4-SNAPSHOT + 1.0.5-SNAPSHOT com.alibaba.smartfox.eclipse.updatesite eclipse-repository diff --git a/eclipse-plugin/pom.xml b/eclipse-plugin/pom.xml index 868ee64..625888e 100644 --- a/eclipse-plugin/pom.xml +++ b/eclipse-plugin/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.alibaba.smartfox.eclipse smartfox-eclipse - 1.0.4-SNAPSHOT + 1.0.5-SNAPSHOT pom 2017 diff --git a/idea-plugin/p3c-idea/src/main/resources/META-INF/plugin.xml b/idea-plugin/p3c-idea/src/main/resources/META-INF/plugin.xml index f6bfe62..662d94a 100755 --- a/idea-plugin/p3c-idea/src/main/resources/META-INF/plugin.xml +++ b/idea-plugin/p3c-idea/src/main/resources/META-INF/plugin.xml @@ -6,10 +6,15 @@ + 1.0.5 +
  • https://github.com/alibaba/p3c/issues/264
  • + +
      1.0.4
    • fix https://github.com/alibaba/p3c/issues/217
    • fix https://github.com/alibaba/p3c/issues/208
    • fix https://github.com/alibaba/p3c/issues/195
    • +
      1.0.3
    • fix https://github.com/alibaba/p3c/issues/191
    • diff --git a/p3c-pmd/pom.xml b/p3c-pmd/pom.xml index 9740714..339889d 100644 --- a/p3c-pmd/pom.xml +++ b/p3c-pmd/pom.xml @@ -9,7 +9,7 @@ 4.0.0 com.alibaba.p3c p3c-pmd - 1.3.4 + 1.3.5 jar p3c-pmd diff --git a/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/comment/EnumConstantsMustHaveCommentRule.java b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/comment/EnumConstantsMustHaveCommentRule.java index c4ee2f6..7e795ee 100644 --- a/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/comment/EnumConstantsMustHaveCommentRule.java +++ b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/comment/EnumConstantsMustHaveCommentRule.java @@ -49,9 +49,11 @@ public class EnumConstantsMustHaveCommentRule extends AbstractAliCommentRule { if (value instanceof ASTEnumDeclaration) { isPreviousEnumDecl = true; } else if (value instanceof ASTEnumConstant && isPreviousEnumDecl) { - addViolationWithMessage(data, value, + Node enumBody = value.jjtGetParent(); + Node enumDeclaration = enumBody.jjtGetParent(); + addViolationWithMessage(data, enumBody, I18nResources.getMessage("java.comment.EnumConstantsMustHaveCommentRule.violation.msg", - value.getImage())); + enumDeclaration.getImage())); isPreviousEnumDecl = false; } else { isPreviousEnumDecl = false; diff --git a/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/flowcontrol/AvoidNegationOperatorRule.java b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/flowcontrol/AvoidNegationOperatorRule.java new file mode 100644 index 0000000..0ee5621 --- /dev/null +++ b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/flowcontrol/AvoidNegationOperatorRule.java @@ -0,0 +1,46 @@ +/* + * 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. + */ +package com.alibaba.p3c.pmd.lang.java.rule.flowcontrol; + +import com.alibaba.p3c.pmd.lang.AbstractXpathRule; +import com.alibaba.p3c.pmd.lang.java.util.ViolationUtils; + +import net.sourceforge.pmd.lang.ast.Node; + +/** + * [Recommended] Avoid using the negation operator '!'. + * Note: The negation operator is not easy to be quickly understood. There must be a positive + * way to represent the same logic. + * + * @author zenghou.fw + * @date 2017/11/21 + */ +public class AvoidNegationOperatorRule extends AbstractXpathRule { + private static final String XPATH = "//UnaryExpressionNotPlusMinus[child::PrimaryExpression" + + "//PrimaryPrefix/Expression/RelationalExpression]" + + "|//UnaryExpressionNotPlusMinus[child::PrimaryExpression" + + "//PrimaryPrefix/Expression/EqualityExpression]"; + + public AvoidNegationOperatorRule() { + setXPath(XPATH); + } + + @Override + public void addViolation(Object data, Node node, String arg) { + ViolationUtils.addViolationWithPrecisePosition(this, node, data, + "java.flowcontrol.AvoidNegationOperatorRule.violation.msg"); + } +} diff --git a/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/AbstractClassShouldStartWithAbstractNamingRule.java b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/AbstractClassShouldStartWithAbstractNamingRule.java index 0e0509e..a549d56 100644 --- a/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/AbstractClassShouldStartWithAbstractNamingRule.java +++ b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/AbstractClassShouldStartWithAbstractNamingRule.java @@ -29,8 +29,8 @@ import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; * @date 2017/04/16 */ public class AbstractClassShouldStartWithAbstractNamingRule extends AbstractXpathRule { - private static final String XPATH = "//ClassOrInterfaceDeclaration\n" - + " [@Abstract='true' and @Interface='false']\n" + " [not (matches(@Image,'^(Abstract|Base).*'))]"; + private static final String XPATH = "//ClassOrInterfaceDeclaration" + + " [@Abstract='true' and @Interface='false'][not (matches(@Image,'^(Abstract|Base).*'))]"; public AbstractClassShouldStartWithAbstractNamingRule() { setXPath(XPATH); diff --git a/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/ArrayNamingShouldHaveBracketRule.java b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/ArrayNamingShouldHaveBracketRule.java index ef09a9c..516b060 100644 --- a/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/ArrayNamingShouldHaveBracketRule.java +++ b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/ArrayNamingShouldHaveBracketRule.java @@ -28,7 +28,7 @@ import net.sourceforge.pmd.lang.ast.Node; * @date 2017/04/16 */ public class ArrayNamingShouldHaveBracketRule extends AbstractXpathRule { - private static final String XPATH = "//VariableDeclaratorId\n" + "[../..[@Array = 'true']]\n" + private static final String XPATH = "//VariableDeclaratorId[../..[@Array = 'true']]" + "[../../Type/ReferenceType[@Array != 'true']]"; public ArrayNamingShouldHaveBracketRule() { diff --git a/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/BooleanPropertyShouldNotStartWithIsRule.java b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/BooleanPropertyShouldNotStartWithIsRule.java index 1e0c96b..368ee6a 100644 --- a/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/BooleanPropertyShouldNotStartWithIsRule.java +++ b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/BooleanPropertyShouldNotStartWithIsRule.java @@ -30,10 +30,10 @@ import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId; * @date 2017/04/16 */ public class BooleanPropertyShouldNotStartWithIsRule extends AbstractXpathRule { - private static final String XPATH = "//VariableDeclaratorId\n" + "[(ancestor::ClassOrInterfaceDeclaration)[\n" - + "@Interface='false'\n" + "and\n" + "( ends-with(@Image, 'DO')\n" + "or ends-with(@Image, 'DTO')\n" - + "or ends-with(@Image, 'VO')\n" + "or ends-with(@Image, 'DAO')\n" + ")\n" + "]]\n" - + "[../../../FieldDeclaration/Type/PrimitiveType[@Image = 'boolean']]\n" + "[.[ starts-with(@Image, 'is')]]"; + private static final String XPATH = "//VariableDeclaratorId[(ancestor::ClassOrInterfaceDeclaration)[" + + "@Interface='false' and ( ends-with(@Image, 'DO') or ends-with(@Image, 'DTO')" + + " or ends-with(@Image, 'VO') or ends-with(@Image, 'DAO'))]]" + + "[../../../FieldDeclaration/Type/PrimitiveType[@Image = 'boolean']][.[ starts-with(@Image, 'is')]]"; public BooleanPropertyShouldNotStartWithIsRule() { setXPath(XPATH); diff --git a/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/LowerCamelCaseVariableNamingRule.java b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/LowerCamelCaseVariableNamingRule.java index 087d7a4..bb414af 100644 --- a/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/LowerCamelCaseVariableNamingRule.java +++ b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/LowerCamelCaseVariableNamingRule.java @@ -21,8 +21,11 @@ import com.alibaba.p3c.pmd.I18nResources; import com.alibaba.p3c.pmd.lang.java.rule.AbstractAliRule; import com.alibaba.p3c.pmd.lang.java.util.ViolationUtils; +import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.java.ast.ASTAnnotationTypeDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator; +import net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId; /** @@ -40,6 +43,12 @@ public class LowerCamelCaseVariableNamingRule extends AbstractAliRule { @Override public Object visit(final ASTVariableDeclaratorId node, Object data) { // Constant named does not apply to this rule + ASTTypeDeclaration typeDeclaration = node.getFirstParentOfType(ASTTypeDeclaration.class); + Node jjtGetChild = typeDeclaration.jjtGetChild(0); + if (jjtGetChild instanceof ASTAnnotationTypeDeclaration) { + return super.visit(node, data); + } + ASTFieldDeclaration astFieldDeclaration = node.getFirstParentOfType(ASTFieldDeclaration.class); boolean isNotCheck = astFieldDeclaration != null && (astFieldDeclaration.isFinal() || astFieldDeclaration .isStatic()); diff --git a/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/PackageNamingRule.java b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/PackageNamingRule.java index d722772..6bd9175 100644 --- a/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/PackageNamingRule.java +++ b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/PackageNamingRule.java @@ -29,7 +29,7 @@ import net.sourceforge.pmd.lang.ast.Node; * @date 2017/04/16 */ public class PackageNamingRule extends AbstractXpathRule { - private static final String XPATH = "//PackageDeclaration/Name\n" + private static final String XPATH = "//PackageDeclaration/Name" + "[not (matches(@Image, '^[a-z0-9]+(\\.[a-z][a-z0-9]*)*$'))]"; public PackageNamingRule() { diff --git a/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/ServiceOrDaoClassShouldEndWithImplRule.java b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/ServiceOrDaoClassShouldEndWithImplRule.java index b9a0b28..35cfd0d 100644 --- a/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/ServiceOrDaoClassShouldEndWithImplRule.java +++ b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/naming/ServiceOrDaoClassShouldEndWithImplRule.java @@ -29,9 +29,9 @@ import net.sourceforge.pmd.lang.ast.Node; * @date 2017/04/16 */ public class ServiceOrDaoClassShouldEndWithImplRule extends AbstractXpathRule { - private static final String XPATH = "//ClassOrInterfaceDeclaration\n" + private static final String XPATH = "//ClassOrInterfaceDeclaration" + "[ .[@Interface='false'] and .[@Abstract='false'] and ./ImplementsList/ClassOrInterfaceType[ ends-with(@Image, 'Service') or " - + "ends-with(@Image, 'DAO')]]\n" + + "ends-with(@Image, 'DAO')]]" + "[not(.[ ends-with(@Image, 'Impl')])]"; public ServiceOrDaoClassShouldEndWithImplRule() { diff --git a/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/other/MethodTooLongRule.java b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/other/MethodTooLongRule.java new file mode 100644 index 0000000..4a276a1 --- /dev/null +++ b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/other/MethodTooLongRule.java @@ -0,0 +1,71 @@ +/* + * 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. + */ +package com.alibaba.p3c.pmd.lang.java.rule.other; + +import com.alibaba.p3c.pmd.I18nResources; +import com.alibaba.p3c.pmd.lang.java.rule.AbstractAliRule; +import com.alibaba.p3c.pmd.lang.java.util.ViolationUtils; + +import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.java.ast.ASTAnnotation; +import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration; +import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration; +import net.sourceforge.pmd.lang.java.ast.Token; + +/** + * [Recommended] The total number of lines for a method should not be more than 80. + * Note: The total number of lines, including the method signature, closing brace, codes, comments, + * blank lines, line breaks and any invisible lines, should not be more than 80. + * + * @author keriezhang + * @date 2018/1/9 + */ +public class MethodTooLongRule extends AbstractAliRule { + + private static final int MAX_LINE_COUNT = 80; + private static final String ANNOTATION_PREFIX = "@"; + + @Override + public Object visit(ASTMethodDeclaration node, Object data) { + // Include method modifiers. + ASTClassOrInterfaceBodyDeclaration classOrInterfaceBodyDecl = + (ASTClassOrInterfaceBodyDeclaration)node.jjtGetParent(); + + int startLine = classOrInterfaceBodyDecl.getBeginLine(); + int endLine = classOrInterfaceBodyDecl.getEndLine(); + + Node firstChild = classOrInterfaceBodyDecl.jjtGetChild(0); + // Method has annotation + if (firstChild instanceof ASTAnnotation) { + Token firstToken = (Token)classOrInterfaceBodyDecl.jjtGetFirstToken(); + // If annotation is before modifier, exclude the annotation. + if (ANNOTATION_PREFIX.equals(firstToken.image)) { + ASTAnnotation annotation = (ASTAnnotation)firstChild; + Token lastToken = (Token)annotation.jjtGetLastToken(); + + // First token after annotation. The same line or next line after annotation. + Token next = lastToken.next; + startLine = next.beginLine; + } + } + + if (endLine - startLine + 1 > MAX_LINE_COUNT) { + ViolationUtils.addViolationWithPrecisePosition(this, node, data, + I18nResources.getMessage("java.other.MethodTooLongRule.violation.msg", node.getName())); + } + return super.visit(node, data); + } +} diff --git a/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/other/UseRightCaseForDateFormatRule.java b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/other/UseRightCaseForDateFormatRule.java new file mode 100644 index 0000000..053f640 --- /dev/null +++ b/p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/other/UseRightCaseForDateFormatRule.java @@ -0,0 +1,76 @@ +package com.alibaba.p3c.pmd.lang.java.rule.other; + +import com.alibaba.p3c.pmd.lang.AbstractXpathRule; + +import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.java.ast.ASTLiteral; +import org.apache.commons.lang3.StringUtils; + +/** + * [Mandatory] When doing date formatting, "y" should be written in lowercase for "year" in a pattern statement. + * + * Note: When doing date formatting, "yyyy" represents the day in which year, while "YYYY" represents the week in which + * year (a concept introduced in JDK7). If a week is across two years, the returning "YYYY"represents the next year. + * Some more points need to be notices: + * Uppercase "M" stands for month. + * Lowercase "m" stands for minute. + * Uppercase "H" stands for 24-hour clock. + * Lowercase "h" stands for 12-hour clock. + * + * Positive Example: Example pattern for date formatting: + * new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + * + * Counter Example: Someone applied "YYYY/MM/dd" pattern for date formatting, and the execution result of 2017/12/31 was + * 2018/12/31, leading to a serious failure. + * + * @author huawen.phw + * @date 2018/1/9 + */ +public class UseRightCaseForDateFormatRule extends AbstractXpathRule { + + private static final String NEW_XPATH + = "//AllocationExpression/ClassOrInterfaceType[@Image='SimpleDateFormat']/../Arguments/ArgumentList" + + "/Expression/PrimaryExpression/PrimaryPrefix/*"; + private static final String LOW_CASE_4Y = "yyyy"; + private static final String LOW_CASE_2Y = "yy"; + private static final String START_QUOTE = "\""; + + public UseRightCaseForDateFormatRule() { + setXPath(NEW_XPATH); + } + + @Override + public void addViolation(Object data, Node node, String arg) { + checkNode(node, data); + } + + /** + * 暂只检查4个y和2个y开头的日期格式化字符串参数,不考虑其他类型 + * + * @param argNode + * @param data + */ + private void checkNode(Node argNode, Object data) { + String image = ""; + if (argNode instanceof ASTLiteral) { + image = argNode.getImage(); + } + // 限定只验证字符串,其他如参数、变量等均不考虑 + if (StringUtils.isEmpty(image) || !image.startsWith(START_QUOTE)) { + return; + } + image = image.replace("\"", ""); + String lowerCaseTmp = image.toLowerCase(); + if (!image.startsWith(LOW_CASE_4Y) && lowerCaseTmp.startsWith(LOW_CASE_4Y)) { + addViolationWithMessage(data, argNode, + "java.other.UseRightCaseForDateFormatRule.rule.msg", + new Object[] {image}); + } else if (!image.startsWith(LOW_CASE_2Y) && lowerCaseTmp.startsWith(LOW_CASE_2Y)) { + addViolationWithMessage(data, argNode, + "java.other.UseRightCaseForDateFormatRule.rule.msg", + new Object[] {image}); + } else { + //暂不考虑 + } + } +} diff --git a/p3c-pmd/src/main/resources/messages.xml b/p3c-pmd/src/main/resources/messages.xml index 19a9e1f..4289746 100644 --- a/p3c-pmd/src/main/resources/messages.xml +++ b/p3c-pmd/src/main/resources/messages.xml @@ -207,6 +207,16 @@ + + + + + + + + + + @@ -411,7 +421,7 @@ - + @@ -461,6 +471,27 @@ + + + + + + + + + + + + + + + + + + + diff --git a/p3c-pmd/src/main/resources/messages_en.xml b/p3c-pmd/src/main/resources/messages_en.xml index 80db028..1127f96 100644 --- a/p3c-pmd/src/main/resources/messages_en.xml +++ b/p3c-pmd/src/main/resources/messages_en.xml @@ -209,6 +209,16 @@ Note: Below are the problems created by usage of Executors for thread pool creat + + + + + + + + + + @@ -411,7 +421,7 @@ The format of date is 'yyyy/MM/dd'. - + @@ -462,6 +472,29 @@ Note: In order to get a more accurate time, use System.nanoTime(). In JDK8, use + + + + + + + + + + + + + + + + + + + + @@ -475,4 +508,4 @@ Note: If attribute is null or does not exist, ${var} will be shown directly on w - \ No newline at end of file + diff --git a/p3c-pmd/src/main/resources/rulesets/java/ali-flowcontrol.xml b/p3c-pmd/src/main/resources/rulesets/java/ali-flowcontrol.xml index 2792ccb..093e326 100644 --- a/p3c-pmd/src/main/resources/rulesets/java/ali-flowcontrol.xml +++ b/p3c-pmd/src/main/resources/rulesets/java/ali-flowcontrol.xml @@ -64,4 +64,30 @@ Positive example: + + java.flowcontrol.AvoidNegationOperatorRule.rule.desc + 3 + + + = 628))` to represent that x is less than 628. + if (!(x >= 628)) { + // ... + } + ]]> + + + + + diff --git a/p3c-pmd/src/main/resources/rulesets/java/ali-other.xml b/p3c-pmd/src/main/resources/rulesets/java/ali-other.xml index af54d86..66da689 100644 --- a/p3c-pmd/src/main/resources/rulesets/java/ali-other.xml +++ b/p3c-pmd/src/main/resources/rulesets/java/ali-other.xml @@ -79,4 +79,29 @@ Positive example: + + java.other.MethodTooLongRule.rule.desc + 3 + + + + java.other.UseRightCaseForDateFormatRule.rule.desc + 2 + + + + + + + diff --git a/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/lang/java/rule/flowcontrol/FlowControlRuleTest.java b/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/lang/java/rule/flowcontrol/FlowControlRuleTest.java index 3face90..c679434 100644 --- a/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/lang/java/rule/flowcontrol/FlowControlRuleTest.java +++ b/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/lang/java/rule/flowcontrol/FlowControlRuleTest.java @@ -26,7 +26,6 @@ import net.sourceforge.pmd.testframework.SimpleAggregatorTst; */ public class FlowControlRuleTest extends SimpleAggregatorTst { - private static final String RULESET = "java-ali-flowcontrol"; @Override @@ -34,5 +33,6 @@ public class FlowControlRuleTest extends SimpleAggregatorTst { addRule(RULESET, "SwitchStatementRule"); addRule(RULESET, "NeedBraceRule"); addRule(RULESET, "AvoidComplexConditionRule"); + addRule(RULESET, "AvoidNegationOperatorRule"); } } diff --git a/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/lang/java/rule/other/OtherRulesTest.java b/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/lang/java/rule/other/OtherRulesTest.java index 181c4f6..ddcc714 100644 --- a/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/lang/java/rule/other/OtherRulesTest.java +++ b/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/lang/java/rule/other/OtherRulesTest.java @@ -16,6 +16,7 @@ package com.alibaba.p3c.pmd.lang.java.rule.other; import net.sourceforge.pmd.testframework.SimpleAggregatorTst; +import org.junit.Test; /** * Test for other java rules. @@ -26,7 +27,7 @@ import net.sourceforge.pmd.testframework.SimpleAggregatorTst; */ public class OtherRulesTest extends SimpleAggregatorTst { - private static final String RULESET = "java-ali-other"; + public static final String RULESET = "java-ali-other"; @Override public void setUp() { @@ -34,5 +35,14 @@ public class OtherRulesTest extends SimpleAggregatorTst { addRule(RULESET, "AvoidNewDateGetTimeRule"); addRule(RULESET, "AvoidPatternCompileInMethodRule"); addRule(RULESET, "AvoidMissUseOfMathRandomRule"); + addRule(RULESET, "MethodTooLongRule"); + addRule(RULESET,"UseRightCaseForDateFormatRule"); } + + @Test + public void testUseRightCaseForDateFormatRule() { + runTests(findRule(RULESET, "UseRightCaseForDateFormatRule")); + } + + } diff --git a/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/lang/java/rule/other/UseRightCaseForDateFormatRuleTest.java b/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/lang/java/rule/other/UseRightCaseForDateFormatRuleTest.java new file mode 100644 index 0000000..fc9ae19 --- /dev/null +++ b/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/lang/java/rule/other/UseRightCaseForDateFormatRuleTest.java @@ -0,0 +1,25 @@ +package com.alibaba.p3c.pmd.lang.java.rule.other; + +import com.alibaba.p3c.pmd.testframework.ExtendRuleTst; + +import net.sourceforge.pmd.Rule; +import org.junit.Test; + +/** + * @author huawen.phw + * @date 2018/2/1 + * Description: + */ +public class UseRightCaseForDateFormatRuleTest extends ExtendRuleTst { + + @Test + public void testExam1() { + String ruleName = "UseRightCaseForDateFormatRule"; + String examFilePath = "java/" + ruleName + "Exam.java"; + String expectedVioLineNumbers = "16,26,32,34,36"; + + Rule rule = findRule(OtherRulesTest.RULESET, ruleName); + runTest(rule, examFilePath, expectedVioLineNumbers); + } + +} diff --git a/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/testframework/ExtendRuleTst.java b/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/testframework/ExtendRuleTst.java new file mode 100644 index 0000000..1ab0f16 --- /dev/null +++ b/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/testframework/ExtendRuleTst.java @@ -0,0 +1,91 @@ +package com.alibaba.p3c.pmd.testframework; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; + +import net.sourceforge.pmd.Rule; +import net.sourceforge.pmd.testframework.RuleTst; +import net.sourceforge.pmd.testframework.TestDescriptor; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; + +/** + * @author huawen.phw + * @date 2018/2/1 + * Description: + */ +public class ExtendRuleTst extends RuleTst { + + + public void runTest(Rule rule, String examFilePath, String expectedVioLineNumbers) { + TestDescriptor descriptor = extractTestsFromJavaFile(rule, examFilePath + , expectedVioLineNumbers); + if (descriptor != null) { + runTest(descriptor); + } + } + + /** + * @param rule + * @return + */ + public TestDescriptor extractTestsFromJavaFile(Rule rule) { + return extractTestsFromJavaFile(rule, "java/" + getCleanRuleName(rule) + ".java"); + } + + /** + * @param rule + * @param javaFilePath + * @return + */ + public TestDescriptor extractTestsFromJavaFile(Rule rule, String javaFilePath) { + return extractTestsFromJavaFile(rule, javaFilePath, ""); + } + + public TestDescriptor extractTestsFromJavaFile(Rule rule, String javaFilePath, String expectedLineNumbers) { + if (StringUtils.isEmpty(javaFilePath)) { + return null; + } + //append file suffix + if (!javaFilePath.toLowerCase().endsWith(".java")) { + javaFilePath = javaFilePath + ".java"; + } + InputStream inputStream = getClass().getResourceAsStream(javaFilePath); + if (inputStream == null) { + throw new RuntimeException("Couldn't find " + javaFilePath); + } + + String fileContents = null; + try { + fileContents = IOUtils.toString(inputStream, Charset.defaultCharset()); + } catch (IOException e) { + e.printStackTrace(); + } finally { + IOUtils.closeQuietly(inputStream); + } + + if (fileContents == null) { + return null; + } + List expectedLineNumber = getExpectedLineNumbers(expectedLineNumbers); + TestDescriptor descriptor = new TestDescriptor(fileContents, rule.getDescription(), + expectedLineNumber.size(), rule); + descriptor.setExpectedLineNumbers(expectedLineNumber); + return descriptor; + } + + public List getExpectedLineNumbers(String lineNumbers) { + List expectedLineNumbers = new ArrayList<>(); + for (String n : lineNumbers.split(" *, *")) { + try { + expectedLineNumbers.add(Integer.valueOf(n)); + } catch (Exception e) { + e.printStackTrace(); + } + } + return expectedLineNumbers; + } +} diff --git a/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/testframework/ExtendSimpleAggregatorTst.java b/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/testframework/ExtendSimpleAggregatorTst.java new file mode 100644 index 0000000..dedc7c1 --- /dev/null +++ b/p3c-pmd/src/test/java/com/alibaba/p3c/pmd/testframework/ExtendSimpleAggregatorTst.java @@ -0,0 +1,15 @@ +package com.alibaba.p3c.pmd.testframework; + +import net.sourceforge.pmd.Rule; +import net.sourceforge.pmd.testframework.SimpleAggregatorTst; + +/** + * + * @author huawen.phw + * @date 2018/2/1 + * Description: 扩展framework,runTest支持检查.java文件 + */ +public class ExtendSimpleAggregatorTst extends SimpleAggregatorTst { + + +} diff --git a/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/comment/xml/ClassMustHaveAuthorRule.xml b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/comment/xml/ClassMustHaveAuthorRule.xml index a1dbec9..fe1e4ea 100644 --- a/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/comment/xml/ClassMustHaveAuthorRule.xml +++ b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/comment/xml/ClassMustHaveAuthorRule.xml @@ -1,93 +1,93 @@ - - + - - Class without author. - 1 - 1 - - + + Class without author. + 1 + 1 + + - - - - Class with author. - 0 - - - - + + Class with author. + 0 + + + + - - - Class with date. - 1 - 4 - - - - + + Class with date. + 1 + 4 + + + + - - - Class with author and date. - 0 - - + + + Class with author and date. + 0 + + - - - - Class with inner class. - 0 - - + + + Class with inner class. + 0 + + - - - - Enum without author - 1 - 1 - - + + + Enum without author + 1 + 1 + + - - - - Enum with author - 0 - - + + + Enum with author + 0 + + - - - - Enum in class - 0 - - + + + Enum in class + 0 + + - - - - Enum in interface - 0 - - + + + Enum in interface + 0 + + - - - - Uppercase author annotation - 0 - - + + + Uppercase author annotation + 0 + + - \ No newline at end of file + diff --git a/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/comment/xml/CommentsMustBeJavadocFormatRule.xml b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/comment/xml/CommentsMustBeJavadocFormatRule.xml index 236c592..cab7aab 100644 --- a/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/comment/xml/CommentsMustBeJavadocFormatRule.xml +++ b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/comment/xml/CommentsMustBeJavadocFormatRule.xml @@ -1,22 +1,22 @@ - - + - - Class have no comment. - 0 - - + + Class have no comment. + 0 + + - - + - - Class have non-javadoc comments. - 3 - 2,4,6 - - + + Class have non-javadoc comments. + 3 + 2,4,6 + + - - - - Class have javadoc comments. - 0 - - + + + Class have javadoc comments. + 0 + + - - - - Anonymous Inner Class have non javadoc comments. - - 2 - 1,3 - - + + + Anonymous Inner Class have non javadoc comments. + + 2 + 1,3 + + - - - - Non-javadoc comments before package and import - - 0 - - - - + + Non-javadoc comments before package and import + + 0 + + + + - - - Ignore comments behind statements. - - 0 - - - - - - - When node has annotation and wrong comment format - - 4 - 1,5,9,14 - - - - - - - When node has annotation and right comment format - - 0 - - + + + Ignore comments behind statements. + + 0 + + diff --git a/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/comment/xml/EnumConstantsMustHaveCommentRule.xml b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/comment/xml/EnumConstantsMustHaveCommentRule.xml index c8d51c7..a3a3511 100644 --- a/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/comment/xml/EnumConstantsMustHaveCommentRule.xml +++ b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/comment/xml/EnumConstantsMustHaveCommentRule.xml @@ -11,7 +11,7 @@ public enum Level { Enum without Comment. 1 - 2 + 1 @@ -31,4 +31,4 @@ public enum Level { - \ No newline at end of file + diff --git a/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/flowcontrol/xml/AvoidNegationOperatorRule.xml b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/flowcontrol/xml/AvoidNegationOperatorRule.xml new file mode 100644 index 0000000..bd23fe4 --- /dev/null +++ b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/flowcontrol/xml/AvoidNegationOperatorRule.xml @@ -0,0 +1,33 @@ + + + + b)) { + return -1; + } + if (!(a == b)) { + return -1; + } + if (!(a != b)) { + return -1; + } + if (!(a > 0 && b > 0)) { + return 1; + } + if (!(a == 0 && b >= 0)) { + return 1; + } + return 0; + } + } + ]]> + + + unnecessary not operator can be removed + 3 + 3,6,9 + + + \ No newline at end of file diff --git a/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/naming/xml/LowerCamelCaseVariableNamingRule.xml b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/naming/xml/LowerCamelCaseVariableNamingRule.xml index 80b711a..a010529 100644 --- a/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/naming/xml/LowerCamelCaseVariableNamingRule.xml +++ b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/naming/xml/LowerCamelCaseVariableNamingRule.xml @@ -124,6 +124,22 @@ public interface BizConstants { 0 + + + + + + Variable name should be lowerCamelCase8 + 0 + + diff --git a/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/naming/xml/PackageNamingRule.xml b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/naming/xml/PackageNamingRule.xml index e15caeb..02ee353 100644 --- a/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/naming/xml/PackageNamingRule.xml +++ b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/naming/xml/PackageNamingRule.xml @@ -31,19 +31,5 @@ - - - - - Package Name should be lowercase - 0 - - - - diff --git a/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/oop/xml/PojoMustOverrideToStringRule.xml b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/oop/xml/PojoMustOverrideToStringRule.xml index 7bc0827..2d0a017 100644 --- a/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/oop/xml/PojoMustOverrideToStringRule.xml +++ b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/oop/xml/PojoMustOverrideToStringRule.xml @@ -133,18 +133,18 @@ - + - - + - - - interface with POJO surfix - 0 - - - - \ No newline at end of file + + + interface with POJO surfix + 0 + + + + diff --git a/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/other/java/UseRightCaseForDateFormatRuleExam.java b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/other/java/UseRightCaseForDateFormatRuleExam.java new file mode 100644 index 0000000..f11d0ff --- /dev/null +++ b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/other/java/UseRightCaseForDateFormatRuleExam.java @@ -0,0 +1,61 @@ +package com.alibaba.p3c.pmd.lang.java.rule.other.java; + +import java.text.SimpleDateFormat; + + +/** + * @author huawen.phw + * @date 2018/2/1 + * Description: + */ +public class UseRightCaseForDateFormatRuleExam { + + private static final String PATTERN= "yyyyMMdd"; + + public void exam1() { + SimpleDateFormat format = new SimpleDateFormat("YYYYMMDD"); //vio + + format = new SimpleDateFormat("yyyy/MM/dd"); + + format = new SimpleDateFormat("yyyy-MM-dd"); + + format = new SimpleDateFormat("yyyymmdd"); + + format = new SimpleDateFormat("yyyy-MM-DD"); + + format = new SimpleDateFormat("YYYY/MM/dd HH:mm:ss"); //p2 error + + format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); //right + + format = new SimpleDateFormat("yy-MM-DD"); + + format = new SimpleDateFormat("YY-MM-DD");//vio + + format = new SimpleDateFormat("YY-md");//vio + + format = new SimpleDateFormat("Yy-md"); //vio + + format = new SimpleDateFormat("yyy-md"); //not checked + + format = new SimpleDateFormat("Y-md"); // not checked + + format = new SimpleDateFormat("y-md"); // not checked + + format = new SimpleDateFormat("dd/MM-YYYY"); //not checked + + exam_2(PATTERN);//can not checked + + exam_2("YYYmmDD");//can not checked + } + + public void exam2(String formatStr) { + SimpleDateFormat format = new SimpleDateFormat(PATTERN);//can not checked + + format = new SimpleDateFormat(formatStr);//can not checked + } + + public void exam3(){ + } + + +} \ No newline at end of file diff --git a/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/other/xml/MethodTooLongRule.xml b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/other/xml/MethodTooLongRule.xml new file mode 100644 index 0000000..0cab1b7 --- /dev/null +++ b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/other/xml/MethodTooLongRule.xml @@ -0,0 +1,407 @@ + + + + + + + + Method equal to 80 lines + 0 + + + + + + + + + Method more than 80 lines + 1 + 2 + + + + + + + + Method less than 80 lines + 0 + + + + + + + Interface method + 0 + + + + + + + + Method signature multi lines + 1 + 4 + + + + + + + + Method signature with annotation multi lines + 1 + 4 + + + + \ No newline at end of file diff --git a/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/other/xml/UseRightCaseForDateFormatRule.xml b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/other/xml/UseRightCaseForDateFormatRule.xml new file mode 100644 index 0000000..9d157d0 --- /dev/null +++ b/p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/other/xml/UseRightCaseForDateFormatRule.xml @@ -0,0 +1,31 @@ + + + + + + + + Use right case to format date + 5 + 4,10,11,12,13 + + + + \ No newline at end of file