You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by pe...@apache.org on 2021/09/09 06:59:02 UTC

[pulsar] 05/07: Check null or empty instead of catch NPE (#11655) (#11655)

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

penghui pushed a commit to branch branch-2.8
in repository https://gitbox.apache.org/repos/asf/pulsar.git

commit 26cb0cd9eb7f04e598c0e4e0c9e77456a3e0c52b
Author: GuoJiwei <te...@apache.org>
AuthorDate: Mon Aug 16 10:10:58 2021 +0800

    Check null or empty instead of catch NPE (#11655) (#11655)
    
    (cherry picked from commit e5050181429deaaa19fdc7469d0ebbbbcced4f33)
---
 .../broker/resourcegroup/ResourceGroupService.java | 17 ++-----
 .../apache/pulsar/common/naming/NamespaceName.java | 53 ++++++----------------
 2 files changed, 20 insertions(+), 50 deletions(-)

diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/resourcegroup/ResourceGroupService.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/resourcegroup/ResourceGroupService.java
index 364e22f..2e81fd8 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/resourcegroup/ResourceGroupService.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/resourcegroup/ResourceGroupService.java
@@ -18,7 +18,6 @@
  */
 package org.apache.pulsar.broker.resourcegroup;
 
-import static com.google.common.base.Preconditions.checkNotNull;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ScheduledFuture;
@@ -118,10 +117,8 @@ public class ResourceGroupService {
      * @throws if RG with that name does not exist.
      */
     public void resourceGroupUpdate(ResourceGroupConfigInfo rgConfig) throws PulsarAdminException {
-        try {
-            checkNotNull(rgConfig);
-        } catch (NullPointerException e) {
-            throw new IllegalArgumentException("ResourceGroupUpdate: Invalid null ResourceGroupConfigInfo");
+        if (rgConfig == null) {
+            throw new IllegalArgumentException("ResourceGroupUpdate: Invalid null ResourceGroup config");
         }
         ResourceGroup rg = this.getResourceGroupInternal(rgConfig.getName());
         if (rg == null) {
@@ -331,9 +328,7 @@ public class ResourceGroupService {
      * Get the RG with the given name. For internal operations only.
      */
     private ResourceGroup getResourceGroupInternal(String resourceGroupName) {
-        try {
-            checkNotNull(resourceGroupName);
-        } catch (NullPointerException e) {
+        if (resourceGroupName == null) {
             throw new IllegalArgumentException("Invalid null resource group name: " + resourceGroupName);
         }
 
@@ -542,10 +537,8 @@ public class ResourceGroupService {
     }
 
     private void checkRGCreateParams(ResourceGroupConfigInfo rgConfig) throws PulsarAdminException {
-        try {
-            checkNotNull(rgConfig);
-        } catch (NullPointerException e) {
-            throw new IllegalArgumentException("ResourceGroupCreate: Invalid null ResourceGroupConfigInfo");
+        if (rgConfig == null) {
+            throw new IllegalArgumentException("ResourceGroupCreate: Invalid null ResourceGroup config");
         }
 
         if (rgConfig.getName().isEmpty()) {
diff --git a/pulsar-common/src/main/java/org/apache/pulsar/common/naming/NamespaceName.java b/pulsar-common/src/main/java/org/apache/pulsar/common/naming/NamespaceName.java
index cf975ce..1a22e1a 100644
--- a/pulsar-common/src/main/java/org/apache/pulsar/common/naming/NamespaceName.java
+++ b/pulsar-common/src/main/java/org/apache/pulsar/common/naming/NamespaceName.java
@@ -18,7 +18,6 @@
  */
 package org.apache.pulsar.common.naming;
 
-import static com.google.common.base.Preconditions.checkNotNull;
 import com.google.common.base.Objects;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
@@ -59,9 +58,7 @@ public class NamespaceName implements ServiceUnitId {
     }
 
     public static NamespaceName get(String namespace) {
-        try {
-            checkNotNull(namespace);
-        } catch (NullPointerException e) {
+        if (namespace == null || namespace.isEmpty()) {
             throw new IllegalArgumentException("Invalid null namespace: " + namespace);
         }
         try {
@@ -74,10 +71,6 @@ public class NamespaceName implements ServiceUnitId {
     }
 
     private NamespaceName(String namespace) {
-        if (namespace == null || namespace.isEmpty()) {
-            throw new IllegalArgumentException("Invalid null namespace: " + namespace);
-        }
-
         // Verify it's a proper namespace
         // The namespace name is composed of <tenant>/<namespace>
         // or in the legacy format with the cluster name:
@@ -139,13 +132,11 @@ public class NamespaceName implements ServiceUnitId {
      * @return
      */
     String getTopicName(TopicDomain domain, String topic) {
-        try {
-            checkNotNull(domain);
-            NamedEntity.checkName(topic);
-            return String.format("%s://%s/%s", domain.toString(), namespace, topic);
-        } catch (NullPointerException e) {
-            throw new IllegalArgumentException("Null pointer is invalid as domain for topic.", e);
+        if (domain == null) {
+            throw new IllegalArgumentException("invalid null domain");
         }
+        NamedEntity.checkName(topic);
+        return String.format("%s://%s/%s", domain.toString(), namespace, topic);
     }
 
     @Override
@@ -169,37 +160,23 @@ public class NamespaceName implements ServiceUnitId {
     }
 
     public static void validateNamespaceName(String tenant, String namespace) {
-        try {
-            checkNotNull(tenant);
-            checkNotNull(namespace);
-            if (tenant.isEmpty() || namespace.isEmpty()) {
-                throw new IllegalArgumentException(
-                        String.format("Invalid namespace format. namespace: %s/%s", tenant, namespace));
-            }
-            NamedEntity.checkName(tenant);
-            NamedEntity.checkName(namespace);
-        } catch (NullPointerException e) {
+        if ((tenant == null || tenant.isEmpty()) || (namespace == null || namespace.isEmpty())) {
             throw new IllegalArgumentException(
-                    String.format("Invalid namespace format. namespace: %s/%s", tenant, namespace), e);
+                    String.format("Invalid namespace format. namespace: %s/%s", tenant, namespace));
         }
+        NamedEntity.checkName(tenant);
+        NamedEntity.checkName(namespace);
     }
 
     public static void validateNamespaceName(String tenant, String cluster, String namespace) {
-        try {
-            checkNotNull(tenant);
-            checkNotNull(cluster);
-            checkNotNull(namespace);
-            if (tenant.isEmpty() || cluster.isEmpty() || namespace.isEmpty()) {
-                throw new IllegalArgumentException(
-                        String.format("Invalid namespace format. namespace: %s/%s/%s", tenant, cluster, namespace));
-            }
-            NamedEntity.checkName(tenant);
-            NamedEntity.checkName(cluster);
-            NamedEntity.checkName(namespace);
-        } catch (NullPointerException e) {
+        if ((tenant == null || tenant.isEmpty()) || (cluster == null || cluster.isEmpty())
+                || (namespace == null || namespace.isEmpty())) {
             throw new IllegalArgumentException(
-                    String.format("Invalid namespace format. namespace: %s/%s/%s", tenant, cluster, namespace), e);
+                    String.format("Invalid namespace format. namespace: %s/%s/%s", tenant, cluster, namespace));
         }
+        NamedEntity.checkName(tenant);
+        NamedEntity.checkName(cluster);
+        NamedEntity.checkName(namespace);
     }
 
     @Override