This commit is contained in:
Clément Fournier committed 2020-11-03 16:45:21 +01:00
1 parent 0a2532381e
commit 8b63dae494
8 files changed
+29 -51

No files matched your search

@@ -7,7 +7,7 @@ package net.sourceforge.pmd.lang.java.rule.codestyle;
import java.util.regex.Pattern;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule;
import net.sourceforge.pmd.properties.PropertyBuilder.RegexPropertyBuilder;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyFactory;
@@ -24,11 +24,16 @@ import net.sourceforge.pmd.util.StringUtil.CaseConvention;
* @author Clément Fournier
* @since 6.5.0
*/
abstract class AbstractNamingConventionRule<T extends JavaNode> extends AbstractJavaRule {
abstract class AbstractNamingConventionRule<T extends JavaNode> extends AbstractJavaRulechainRule {
static final String CAMEL_CASE = "[a-z][a-zA-Z0-9]*";
static final String PASCAL_CASE = "[A-Z][a-zA-Z0-9]*";
@SafeVarargs
protected AbstractNamingConventionRule(Class<? extends JavaNode> first, Class<? extends JavaNode>... visits) {
super(first, visits);
}
/** The argument is interpreted as the display name, and is converted to camel case to get the property name. */
RegexPropertyBuilder defaultProp(String displayName) {
return defaultProp(CaseConvention.SPACE_SEPARATED.convertTo(CaseConvention.CAMEL_CASE, displayName), displayName);
@@ -6,8 +6,6 @@ package net.sourceforge.pmd.lang.java.rule.codestyle;
import java.util.regex.Pattern;
import org.checkerframework.checker.nullness.qual.NonNull;
import net.sourceforge.pmd.lang.java.ast.ASTAnnotationTypeDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
@@ -15,7 +13,6 @@ import net.sourceforge.pmd.lang.java.ast.ASTEnumDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTRecordDeclaration;
import net.sourceforge.pmd.lang.java.ast.internal.PrettyPrintingUtil;
import net.sourceforge.pmd.lang.java.rule.internal.JavaRuleUtil;
import net.sourceforge.pmd.lang.rule.RuleTargetSelector;
import net.sourceforge.pmd.properties.PropertyDescriptor;
@@ -33,6 +30,10 @@ public class ClassNamingConventionsRule extends AbstractNamingConventionRule<AST
public ClassNamingConventionsRule() {
super(ASTAnyTypeDeclaration.class,
ASTEnumDeclaration.class,
ASTAnnotationTypeDeclaration.class,
ASTRecordDeclaration.class);
definePropertyDescriptor(classRegex);
definePropertyDescriptor(abstractClassRegex);
definePropertyDescriptor(interfaceRegex);
@@ -41,15 +42,6 @@ public class ClassNamingConventionsRule extends AbstractNamingConventionRule<AST
definePropertyDescriptor(utilityClassRegex);
}
@Override
protected @NonNull RuleTargetSelector buildTargetSelector() {
return RuleTargetSelector.forTypes(ASTClassOrInterfaceDeclaration.class,
ASTEnumDeclaration.class,
ASTAnnotationTypeDeclaration.class,
ASTRecordDeclaration.class);
}
@Override
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
@@ -73,6 +65,11 @@ public class ClassNamingConventionsRule extends AbstractNamingConventionRule<AST
return data;
}
@Override
public Object visit(ASTRecordDeclaration node, Object data) {
checkMatches(node, classRegex, data); // property?
return data;
}
@Override
public Object visit(ASTAnnotationTypeDeclaration node, Object data) {
@@ -11,12 +11,9 @@ import static net.sourceforge.pmd.lang.java.ast.JModifier.STATIC;
import java.util.List;
import java.util.regex.Pattern;
import org.checkerframework.checker.nullness.qual.NonNull;
import net.sourceforge.pmd.lang.java.ast.ASTEnumConstant;
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.lang.rule.RuleTargetSelector;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyFactory;
@@ -46,6 +43,7 @@ public class FieldNamingConventionsRule extends AbstractNamingConventionRule<AST
public FieldNamingConventionsRule() {
super(ASTFieldDeclaration.class, ASTEnumConstant.class);
definePropertyDescriptor(publicConstantFieldRegex);
definePropertyDescriptor(constantFieldRegex);
definePropertyDescriptor(enumConstantRegex);
@@ -55,11 +53,6 @@ public class FieldNamingConventionsRule extends AbstractNamingConventionRule<AST
definePropertyDescriptor(EXCLUDED_NAMES);
}
@Override
protected @NonNull RuleTargetSelector buildTargetSelector() {
return RuleTargetSelector.forTypes(ASTFieldDeclaration.class, ASTEnumConstant.class);
}
@Override
public Object visit(ASTFieldDeclaration node, Object data) {
for (ASTVariableDeclaratorId id : node) {
@@ -5,14 +5,9 @@
package net.sourceforge.pmd.lang.java.rule.codestyle;
import static net.sourceforge.pmd.util.CollectionUtil.setOf;
import java.util.regex.Pattern;
import org.checkerframework.checker.nullness.qual.NonNull;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.lang.rule.RuleTargetSelector;
import net.sourceforge.pmd.properties.PropertyDescriptor;
@@ -35,17 +30,13 @@ public final class FormalParameterNamingConventionsRule extends AbstractNamingCo
public FormalParameterNamingConventionsRule() {
super(ASTVariableDeclaratorId.class);
definePropertyDescriptor(formalParamRegex);
definePropertyDescriptor(finalFormalParamRegex);
definePropertyDescriptor(lambdaParamRegex);
definePropertyDescriptor(explicitLambdaParamRegex);
}
@Override
protected @NonNull RuleTargetSelector buildTargetSelector() {
return RuleTargetSelector.forTypes(setOf(ASTVariableDeclaratorId.class));
}
@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
@@ -27,6 +27,8 @@ public final class LocalVariableNamingConventionsRule extends AbstractNamingConv
public LocalVariableNamingConventionsRule() {
super(ASTVariableDeclaratorId.class);
definePropertyDescriptor(localVarRegex);
definePropertyDescriptor(finalVarRegex);
definePropertyDescriptor(exceptionBlockParameterRegex);
@@ -10,8 +10,6 @@ import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import org.checkerframework.checker.nullness.qual.NonNull;
import net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression;
import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
@@ -19,7 +17,6 @@ import net.sourceforge.pmd.lang.java.ast.ASTEnumConstant;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
import net.sourceforge.pmd.lang.rule.RuleTargetSelector;
import net.sourceforge.pmd.properties.PropertyBuilder.RegexPropertyBuilder;
import net.sourceforge.pmd.properties.PropertyDescriptor;
@@ -37,6 +34,7 @@ public class MethodNamingConventionsRule extends AbstractNamingConventionRule<AS
public MethodNamingConventionsRule() {
super(ASTMethodDeclaration.class);
definePropertyDescriptor(instanceRegex);
definePropertyDescriptor(staticRegex);
definePropertyDescriptor(nativeRegex);
@@ -45,11 +43,6 @@ public class MethodNamingConventionsRule extends AbstractNamingConventionRule<AS
definePropertyDescriptor(junit5Regex);
}
@Override
protected @NonNull RuleTargetSelector buildTargetSelector() {
return RuleTargetSelector.forTypes(ASTMethodDeclaration.class);
}
private boolean isJunit5Test(ASTMethodDeclaration node) {
return node.isAnnotationPresent("org.junit.jupiter.api.Test");
}
@@ -4,15 +4,12 @@
package net.sourceforge.pmd.lang.java.rule.performance;
import org.checkerframework.checker.nullness.qual.NonNull;
import net.sourceforge.pmd.lang.java.ast.ASTArgumentList;
import net.sourceforge.pmd.lang.java.ast.ASTMethodCall;
import net.sourceforge.pmd.lang.java.ast.ASTStringLiteral;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
import net.sourceforge.pmd.lang.rule.RuleTargetSelector;
/**
* This rule finds the following:
@@ -27,16 +24,16 @@ import net.sourceforge.pmd.lang.rule.RuleTargetSelector;
*
* @see <a href="https://sourceforge.net/p/pmd/feature-requests/381/">feature request #381 Single character StringBuffer.append </a>
*/
public class AppendCharacterWithCharRule extends AbstractJavaRule {
public class AppendCharacterWithCharRule extends AbstractJavaRulechainRule {
@Override
protected @NonNull RuleTargetSelector buildTargetSelector() {
return RuleTargetSelector.forTypes(ASTStringLiteral.class);
public AppendCharacterWithCharRule() {
super(ASTStringLiteral.class);
}
@Override
public Object visit(ASTStringLiteral node, Object data) {
if (node.length() == 1 && node.getParent() instanceof ASTArgumentList
if (node.getParent() instanceof ASTArgumentList
&& node.length() == 1
&& ((ASTArgumentList) node.getParent()).size() == 1) {
JavaNode callParent = node.getParent().getParent();
if (callParent instanceof ASTMethodCall) {
@@ -1663,8 +1663,8 @@ and should be removed.
<![CDATA[
//EmptyStatement
[not(
../self::WhileStatement
or ../self::ForStatement
parent::WhileStatement
or parent::ForStatement
or preceding-sibling::*[1]/self::LocalClassStatement
)
]