You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2023/04/22 15:33:51 UTC

[shardingsphere] branch master updated: Add more best practices rules for pmd (#25278)

This is an automated email from the ASF dual-hosted git repository.

zhaojinchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 74f430989e4 Add more best practices rules for pmd (#25278)
74f430989e4 is described below

commit 74f430989e446e337b53fe04cc90913410fd2a6f
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Apr 22 23:33:42 2023 +0800

    Add more best practices rules for pmd (#25278)
    
    * Add SingularField rule for pmd
    
    * Add UnusedPrivateMethod and UnusedPrivateField rules for pmd
    
    * Add UnusedPrivateMethod and UnusedPrivateField rules for pmd
    
    * Add more rules
    
    * Add more rules
    
    * Add more rules
    
    * Add more rules
    
    * Add more rules
    
    * Add more rules
    
    * Add more rules
---
 .../binary/execute/MySQLComStmtExecutePacket.java  |  6 +++---
 ...ptForUseDefaultInsertColumnsTokenGenerator.java |  1 -
 .../CacheableShardingAlgorithmChecker.java         |  4 ++--
 .../pool/creator/DataSourceReflection.java         | 12 +++++------
 .../api/config/TableNameSchemaNameMapping.java     | 21 ++++++++++--------
 .../sqlfederation/row/EmptyRowEnumerator.java      |  1 -
 src/resources/pmd.xml                              | 25 ++++++++++++++++++++--
 7 files changed, 46 insertions(+), 24 deletions(-)

diff --git a/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/binary/execute/MySQLComStmtExecutePacket.java b/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/binary/execute/MySQLComStmtExecutePacket.java
index 08196296359..5ab22a8c8a1 100644
--- a/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/binary/execute/MySQLComStmtExecutePacket.java
+++ b/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/binary/execute/MySQLComStmtExecutePacket.java
@@ -18,6 +18,7 @@
 package org.apache.shardingsphere.db.protocol.mysql.packet.command.query.binary.execute;
 
 import com.google.common.base.Preconditions;
+import lombok.AccessLevel;
 import lombok.Getter;
 import lombok.ToString;
 import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLBinaryColumnType;
@@ -41,6 +42,7 @@ import java.util.Set;
  *
  * @see <a href="https://dev.mysql.com/doc/internals/en/com-stmt-execute.html">COM_STMT_EXECUTE</a>
  */
+@Getter
 @ToString(of = "statementId")
 public final class MySQLComStmtExecutePacket extends MySQLCommandPacket {
     
@@ -50,17 +52,15 @@ public final class MySQLComStmtExecutePacket extends MySQLCommandPacket {
     
     private final MySQLPacketPayload payload;
     
-    @Getter
     private final int statementId;
     
     private final int flags;
     
+    @Getter(AccessLevel.NONE)
     private final MySQLNullBitmap nullBitmap;
     
-    @Getter
     private final MySQLNewParametersBoundFlag newParametersBoundFlag;
     
-    @Getter
     private final List<MySQLPreparedStatementParameterType> newParameterTypes;
     
     public MySQLComStmtExecutePacket(final MySQLPacketPayload payload, final int paramCount) {
diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptForUseDefaultInsertColumnsTokenGenerator.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptForUseDefaultInsertColumnsTokenGenerator.java
index 7a3958c5336..39847b916e5 100644
--- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptForUseDefaultInsertColumnsTokenGenerator.java
+++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptForUseDefaultInsertColumnsTokenGenerator.java
@@ -106,7 +106,6 @@ public final class EncryptForUseDefaultInsertColumnsTokenGenerator implements Op
                 }
                 if (encryptTable.findPlainColumn(columnName).isPresent()) {
                     addPlainColumn(result, encryptTable, columnName, columnIndex);
-                    columnIndex++;
                 }
             }
         }
diff --git a/features/sharding/plugin/cache/src/main/java/org/apache/shardingsphere/sharding/cache/checker/algorithm/CacheableShardingAlgorithmChecker.java b/features/sharding/plugin/cache/src/main/java/org/apache/shardingsphere/sharding/cache/checker/algorithm/CacheableShardingAlgorithmChecker.java
index cda0d2082ef..2616c48a740 100644
--- a/features/sharding/plugin/cache/src/main/java/org/apache/shardingsphere/sharding/cache/checker/algorithm/CacheableShardingAlgorithmChecker.java
+++ b/features/sharding/plugin/cache/src/main/java/org/apache/shardingsphere/sharding/cache/checker/algorithm/CacheableShardingAlgorithmChecker.java
@@ -31,6 +31,8 @@ import java.util.HashSet;
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
 public final class CacheableShardingAlgorithmChecker {
     
+    private static final Collection<Class<? extends ShardingAlgorithm>> CACHEABLE_SHARDING_ALGORITHM_CLASSES;
+    
     static {
         Collection<Class<? extends ShardingAlgorithm>> result = new HashSet<>();
         for (CacheableShardingAlgorithmClassProvider each : ShardingSphereServiceLoader.getServiceInstances(CacheableShardingAlgorithmClassProvider.class)) {
@@ -39,8 +41,6 @@ public final class CacheableShardingAlgorithmChecker {
         CACHEABLE_SHARDING_ALGORITHM_CLASSES = result;
     }
     
-    private static final Collection<Class<? extends ShardingAlgorithm>> CACHEABLE_SHARDING_ALGORITHM_CLASSES;
-    
     /**
      * Check if sharding algorithm is cacheable.
      *
diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourceReflection.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourceReflection.java
index 75f2808932b..da7a45c66b5 100644
--- a/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourceReflection.java
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourceReflection.java
@@ -43,12 +43,6 @@ import java.util.Properties;
  */
 public final class DataSourceReflection {
     
-    static {
-        GENERAL_CLASS_TYPES = new HashSet<>(
-                Arrays.asList(boolean.class, Boolean.class, int.class, Integer.class, long.class, Long.class, String.class, Collection.class, List.class, Properties.class));
-        SKIPPED_PROPERTY_KEYS = new HashSet<>(Arrays.asList("loginTimeout", "driverClassName"));
-    }
-    
     private static final Collection<Class<?>> GENERAL_CLASS_TYPES;
     
     private static final Collection<String> SKIPPED_PROPERTY_KEYS;
@@ -63,6 +57,12 @@ public final class DataSourceReflection {
     
     private final Method[] dataSourceMethods;
     
+    static {
+        GENERAL_CLASS_TYPES = new HashSet<>(
+                Arrays.asList(boolean.class, Boolean.class, int.class, Integer.class, long.class, Long.class, String.class, Collection.class, List.class, Properties.class));
+        SKIPPED_PROPERTY_KEYS = new HashSet<>(Arrays.asList("loginTimeout", "driverClassName"));
+    }
+    
     public DataSourceReflection(final DataSource dataSource) {
         this.dataSource = dataSource;
         dataSourceMethods = dataSource.getClass().getMethods();
diff --git a/kernel/data-pipeline/api/src/main/java/org/apache/shardingsphere/data/pipeline/api/config/TableNameSchemaNameMapping.java b/kernel/data-pipeline/api/src/main/java/org/apache/shardingsphere/data/pipeline/api/config/TableNameSchemaNameMapping.java
index 160ba945b41..ddea9295808 100644
--- a/kernel/data-pipeline/api/src/main/java/org/apache/shardingsphere/data/pipeline/api/config/TableNameSchemaNameMapping.java
+++ b/kernel/data-pipeline/api/src/main/java/org/apache/shardingsphere/data/pipeline/api/config/TableNameSchemaNameMapping.java
@@ -23,6 +23,7 @@ import org.apache.shardingsphere.data.pipeline.api.metadata.LogicTableName;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Map.Entry;
 
 /**
  * Table name and schema name mapping.
@@ -38,17 +39,19 @@ public final class TableNameSchemaNameMapping {
      * @param tableSchemaMap table name and schema name map
      */
     public TableNameSchemaNameMapping(final Map<String, String> tableSchemaMap) {
-        if (null == tableSchemaMap) {
-            mapping = Collections.emptyMap();
-            return;
-        }
-        Map<LogicTableName, String> mapping = new HashMap<>();
-        tableSchemaMap.forEach((tableName, schemaName) -> {
+        mapping = null == tableSchemaMap ? Collections.emptyMap() : getLogicTableNameMap(tableSchemaMap);
+    }
+    
+    private Map<LogicTableName, String> getLogicTableNameMap(final Map<String, String> tableSchemaMap) {
+        Map<LogicTableName, String> result = new HashMap<>(tableSchemaMap.size(), 1);
+        for (Entry<String, String> entry : tableSchemaMap.entrySet()) {
+            String tableName = entry.getKey();
+            String schemaName = entry.getValue();
             if (null != schemaName) {
-                mapping.put(new LogicTableName(tableName), schemaName);
+                result.put(new LogicTableName(tableName), schemaName);
             }
-        });
-        this.mapping = mapping;
+        }
+        return result;
     }
     
     /**
diff --git a/kernel/sql-federation/executor/core/src/main/java/org/apache/shardingsphere/sqlfederation/row/EmptyRowEnumerator.java b/kernel/sql-federation/executor/core/src/main/java/org/apache/shardingsphere/sqlfederation/row/EmptyRowEnumerator.java
index be5c7a7acad..338c29c2a2f 100644
--- a/kernel/sql-federation/executor/core/src/main/java/org/apache/shardingsphere/sqlfederation/row/EmptyRowEnumerator.java
+++ b/kernel/sql-federation/executor/core/src/main/java/org/apache/shardingsphere/sqlfederation/row/EmptyRowEnumerator.java
@@ -32,7 +32,6 @@ public final class EmptyRowEnumerator<T> implements Enumerator<T> {
     @Override
     public T current() {
         ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
-        System.out.println(type.getActualTypeArguments()[0].getTypeName());
         if ("Object".equals(type.getActualTypeArguments()[0].getTypeName())) {
             return (T) new Object();
         } else {
diff --git a/src/resources/pmd.xml b/src/resources/pmd.xml
index 89abc503d32..1e9b5336112 100644
--- a/src/resources/pmd.xml
+++ b/src/resources/pmd.xml
@@ -20,6 +20,7 @@
          name="full-pmd-ruleset" xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
     <description>PMD of Apache ShardingSphere</description>
     <exclude-pattern>.*/target/.*</exclude-pattern>
+    
     <rule ref="rulesets/java/quickstart.xml">
         <exclude name="AbstractClassWithoutAnyMethod" />
         <exclude name="AvoidAccessibilityAlteration" />
@@ -33,24 +34,44 @@
         <exclude name="GuardLogStatement" />
         <exclude name="OverrideBothEqualsAndHashcode" />
         <exclude name="PreserveStackTrace" />
-        <exclude name="SingularField" />
         <exclude name="UncommentedEmptyMethodBody" />
-        <exclude name="UnusedPrivateField" />
         <exclude name="UnusedPrivateMethod" />
         <exclude name="UseLocaleWithCaseConversions" />
     </rule>
     
+    <rule ref="category/java/bestpractices.xml/AvoidPrintStackTrace" />
+    <rule ref="category/java/bestpractices.xml/AvoidReassigningCatchVariables" />
+    <rule ref="category/java/bestpractices.xml/AvoidReassigningLoopVariables" />
+    <rule ref="category/java/bestpractices.xml/AvoidReassigningParameters" />
+    <rule ref="category/java/bestpractices.xml/ForLoopVariableCount">
+        <properties>
+            <property name="maximumVariables" value="2" />
+        </properties>
+    </rule>
+    <rule ref="category/java/bestpractices.xml/JUnit5TestShouldBePackagePrivate" />
+    <rule ref="category/java/bestpractices.xml/JUnitTestsShouldIncludeAssert" />
+    <rule ref="category/java/bestpractices.xml/MethodReturnsInternalArray" />
+    <rule ref="category/java/bestpractices.xml/ReplaceEnumerationWithIterator" />
+    <rule ref="category/java/bestpractices.xml/ReplaceHashtableWithMap" />
+    <rule ref="category/java/bestpractices.xml/ReplaceVectorWithList" />
+    <rule ref="category/java/bestpractices.xml/SystemPrintln" />
+    <rule ref="category/java/bestpractices.xml/UnusedAssignment" />
+    <rule ref="category/java/bestpractices.xml/UseTryWithResources" />
+    <rule ref="category/java/bestpractices.xml/WhileLoopWithLiteralBoolean" />
+    
     <rule ref="category/java/documentation.xml/CommentSize">
         <properties>
             <property name="maxLines" value="40" />
             <property name="maxLineLength" value="200" />
         </properties>
     </rule>
+    
     <rule ref="category/java/design.xml/UseUtilityClass">
         <properties>
             <property name="ignoredAnnotations" value="lombok.NoArgsConstructor | org.springframework.boot.autoconfigure.SpringBootApplication" />
         </properties>
     </rule>
+    
     <rule ref="category/java/errorprone.xml/AssignmentInOperand">
         <properties>
             <property name="allowIf" value="true" />