Skip to content

Commit

Permalink
Check null or empty instead of catch NPE (apache#11655) (apache#11655)
Browse files Browse the repository at this point in the history
  • Loading branch information
Technoboy- authored Aug 16, 2021
1 parent 25b9785 commit e505018
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.apache.pulsar.broker.resourcegroup;

import static com.google.common.base.Preconditions.checkNotNull;
import io.prometheus.client.Counter;
import io.prometheus.client.Summary;
import java.util.Map;
Expand Down Expand Up @@ -130,9 +129,7 @@ public ResourceGroup resourceGroupGet(String resourceGroupName) {
*/
public void resourceGroupUpdate(String rgName, org.apache.pulsar.common.policies.data.ResourceGroup rgConfig)
throws PulsarAdminException {
try {
checkNotNull(rgConfig);
} catch (NullPointerException e) {
if (rgConfig == null) {
throw new IllegalArgumentException("ResourceGroupUpdate: Invalid null ResourceGroup config");
}

Expand Down Expand Up @@ -392,9 +389,7 @@ protected BytesAndMessagesCount getRGUsage(String rgName, ResourceGroupMonitorin
* 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);
}

Expand Down Expand Up @@ -678,9 +673,7 @@ private void initialize() {

private void checkRGCreateParams(String rgName, org.apache.pulsar.common.policies.data.ResourceGroup rgConfig)
throws PulsarAdminException {
try {
checkNotNull(rgConfig);
} catch (NullPointerException e) {
if (rgConfig == null) {
throw new IllegalArgumentException("ResourceGroupCreate: Invalid null ResourceGroup config");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,9 +59,7 @@ public static NamespaceName get(String tenant, String cluster, String namespace)
}

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 {
Expand Down Expand Up @@ -93,10 +90,6 @@ public static Optional<NamespaceName> getIfValid(String namespace) {
}

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:
Expand Down Expand Up @@ -158,13 +151,11 @@ public String getPersistentTopicName(String localTopic) {
* @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
Expand All @@ -188,37 +179,23 @@ public int hashCode() {
}

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
Expand Down

0 comments on commit e505018

Please sign in to comment.