This is an automated email from the ASF dual-hosted git repository. liuhongyu pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push: new b944430239 [type:refactor] Optimize code for assert keyword #5967 (#5968) b944430239 is described below commit b9444302397e04e24124e74f6258f98f5908c34e Author: yqw570994511 <49865334+yqw570994...@users.noreply.github.com> AuthorDate: Thu Mar 27 10:41:24 2025 +0800 [type:refactor] Optimize code for assert keyword #5967 (#5968) * [type:refactor] Optimize code for assert keyword #5967 Replace the code that uses the assert keyword for validation with a more commonly used approach. * [type:refactor] Optimize code for assert keyword #5967 Replace the code that uses the assert keyword for validation with a more commonly used approach. --------- Co-authored-by: yuqianwei <qq120405> Co-authored-by: aias00 <liuhon...@apache.org> --- .../shenyu/alert/strategy/DingTalkRobotAlertNotifyStrategy.java | 2 +- .../src/main/java/org/apache/shenyu/common/utils/GsonUtils.java | 2 +- .../java/org/apache/shenyu/plugin/ai/proxy/AiProxyPlugin.java | 2 +- .../plugin/ai/token/limiter/redis/RedisConnectionFactory.java | 2 +- .../src/main/java/org/apache/shenyu/plugin/api/ShenyuPlugin.java | 2 +- .../java/org/apache/shenyu/plugin/api/utils/RequestUrlUtils.java | 4 ++-- .../org/apache/shenyu/plugin/api/utils/WebFluxResultUtils.java | 2 +- .../apache/shenyu/plugin/cache/redis/RedisConnectionFactory.java | 2 +- .../org/apache/shenyu/plugin/context/path/ContextPathPlugin.java | 2 +- .../main/java/org/apache/shenyu/plugin/hystrix/HystrixPlugin.java | 2 +- .../java/org/apache/shenyu/plugin/sentinel/SentinelPlugin.java | 2 +- .../apache/shenyu/plugin/httpclient/AbstractHttpClientPlugin.java | 2 +- .../plugin/logging/common/body/LoggingServerHttpResponse.java | 8 ++++---- .../main/java/org/apache/shenyu/plugin/metrics/MetricsPlugin.java | 2 +- .../shenyu/plugin/modify/response/ModifyResponsePlugin.java | 2 +- .../main/java/org/apache/shenyu/plugin/divide/DividePlugin.java | 2 +- .../java/org/apache/shenyu/plugin/divide/DividePluginTest.java | 4 ++-- .../apache/shenyu/plugin/dubbo/common/AbstractDubboPlugin.java | 2 +- .../src/main/java/org/apache/shenyu/plugin/grpc/GrpcPlugin.java | 4 ++-- .../src/main/java/org/apache/shenyu/plugin/motan/MotanPlugin.java | 4 ++-- .../src/main/java/org/apache/shenyu/plugin/sofa/SofaPlugin.java | 4 ++-- .../shenyu/plugin/sofa/param/SofaParamResolveServiceImpl.java | 3 ++- .../src/main/java/org/apache/shenyu/plugin/tars/TarsPlugin.java | 4 ++-- .../apache/shenyu/plugin/tars/cache/ApplicationConfigCache.java | 2 +- .../java/org/apache/shenyu/plugin/response/ResponsePlugin.java | 2 +- .../main/java/org/apache/shenyu/plugin/rewrite/RewritePlugin.java | 4 ++-- .../apache/shenyu/plugin/sign/service/ComposableSignService.java | 2 +- 27 files changed, 38 insertions(+), 37 deletions(-) diff --git a/shenyu-alert/src/main/java/org/apache/shenyu/alert/strategy/DingTalkRobotAlertNotifyStrategy.java b/shenyu-alert/src/main/java/org/apache/shenyu/alert/strategy/DingTalkRobotAlertNotifyStrategy.java index 2cdce6e9c2..897b19b50e 100644 --- a/shenyu-alert/src/main/java/org/apache/shenyu/alert/strategy/DingTalkRobotAlertNotifyStrategy.java +++ b/shenyu-alert/src/main/java/org/apache/shenyu/alert/strategy/DingTalkRobotAlertNotifyStrategy.java @@ -56,7 +56,7 @@ final class DingTalkRobotAlertNotifyStrategy extends AbstractAlertNotifyHandler ResponseEntity<CommonRobotNotifyResp> responseEntity = getRestTemplate().postForEntity(webHookUrl, httpEntity, CommonRobotNotifyResp.class); if (responseEntity.getStatusCode() == HttpStatus.OK) { - assert Objects.nonNull(responseEntity.getBody()); + Objects.requireNonNull(responseEntity.getBody()); if (responseEntity.getBody().getErrCode() == 0) { log.debug("Send dingTalk webHook: {} Success", webHookUrl); } else { diff --git a/shenyu-common/src/main/java/org/apache/shenyu/common/utils/GsonUtils.java b/shenyu-common/src/main/java/org/apache/shenyu/common/utils/GsonUtils.java index e59def0687..edd7306b03 100644 --- a/shenyu-common/src/main/java/org/apache/shenyu/common/utils/GsonUtils.java +++ b/shenyu-common/src/main/java/org/apache/shenyu/common/utils/GsonUtils.java @@ -357,7 +357,7 @@ public class GsonUtils { } Map<T, U> resultMap = null; - assert Objects.nonNull(mapClass); + Objects.requireNonNull(mapClass); if (Objects.requireNonNull(mapClass).isInterface()) { resultMap = new LinkedHashMap<>(); } else { diff --git a/shenyu-plugin/shenyu-plugin-ai-proxy/src/main/java/org/apache/shenyu/plugin/ai/proxy/AiProxyPlugin.java b/shenyu-plugin/shenyu-plugin-ai-proxy/src/main/java/org/apache/shenyu/plugin/ai/proxy/AiProxyPlugin.java index b3854adf94..e51f955f5f 100644 --- a/shenyu-plugin/shenyu-plugin-ai-proxy/src/main/java/org/apache/shenyu/plugin/ai/proxy/AiProxyPlugin.java +++ b/shenyu-plugin/shenyu-plugin-ai-proxy/src/main/java/org/apache/shenyu/plugin/ai/proxy/AiProxyPlugin.java @@ -63,7 +63,7 @@ public class AiProxyPlugin extends AbstractShenyuPlugin { aiProxyConfig = new AiProxyConfig(); } final ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT); - assert Objects.nonNull(shenyuContext); + Objects.requireNonNull(shenyuContext); // Get selector handle from cache AiProxyHandle selectorHandle = AiProxyPluginHandler.SELECTOR_CACHED_HANDLE.get() diff --git a/shenyu-plugin/shenyu-plugin-ai-token-limiter/src/main/java/org/apache/shenyu/plugin/ai/token/limiter/redis/RedisConnectionFactory.java b/shenyu-plugin/shenyu-plugin-ai-token-limiter/src/main/java/org/apache/shenyu/plugin/ai/token/limiter/redis/RedisConnectionFactory.java index c93e8161e3..cf46d1fc55 100644 --- a/shenyu-plugin/shenyu-plugin-ai-token-limiter/src/main/java/org/apache/shenyu/plugin/ai/token/limiter/redis/RedisConnectionFactory.java +++ b/shenyu-plugin/shenyu-plugin-ai-token-limiter/src/main/java/org/apache/shenyu/plugin/ai/token/limiter/redis/RedisConnectionFactory.java @@ -92,7 +92,7 @@ public class RedisConnectionFactory { protected final RedisStandaloneConfiguration redisStandaloneConfiguration(final RedisConfigProperties redisConfigProperties) { RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(); String[] parts = StringUtils.split(redisConfigProperties.getUrl(), ":"); - assert Objects.nonNull(parts); + Objects.requireNonNull(parts); config.setHostName(parts[0]); config.setPort(Integer.parseInt(parts[1])); if (Objects.nonNull(redisConfigProperties.getPassword())) { diff --git a/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/ShenyuPlugin.java b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/ShenyuPlugin.java index 1d1fbe7c4a..22cfe72963 100644 --- a/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/ShenyuPlugin.java +++ b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/ShenyuPlugin.java @@ -97,7 +97,7 @@ public interface ShenyuPlugin { return false; } ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT); - assert Objects.nonNull(shenyuContext); + Objects.requireNonNull(shenyuContext); return Arrays.stream(rpcTypes).anyMatch(type -> Objects.equals(shenyuContext.getRpcType(), type.getName())); } diff --git a/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/RequestUrlUtils.java b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/RequestUrlUtils.java index abad941bda..d48b567652 100644 --- a/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/RequestUrlUtils.java +++ b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/RequestUrlUtils.java @@ -48,7 +48,7 @@ public final class RequestUrlUtils { path = path + rewriteUri; } else { ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT); - assert Objects.nonNull(shenyuContext); + Objects.requireNonNull(shenyuContext); String realUrl = shenyuContext.getRealUrl(); if (StringUtils.isNoneBlank(realUrl)) { path = path + realUrl; @@ -63,7 +63,7 @@ public final class RequestUrlUtils { if (StringUtils.isNotEmpty(uri.getQuery())) { path = path + "?" + uri.getQuery(); } - assert Objects.nonNull(path); + Objects.requireNonNull(path); return UriComponentsBuilder.fromUriString(path).build(false).toUri(); } } diff --git a/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/WebFluxResultUtils.java b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/WebFluxResultUtils.java index 1e4392e0ec..c7f701c59b 100644 --- a/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/WebFluxResultUtils.java +++ b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/WebFluxResultUtils.java @@ -64,7 +64,7 @@ public final class WebFluxResultUtils { } exchange.getResponse().getHeaders().setContentType(mediaType); final Object responseData = shenyuResult.result(exchange, resultData); - assert Objects.nonNull(responseData); + Objects.requireNonNull(responseData); final byte[] bytes = (responseData instanceof byte[]) ? (byte[]) responseData : responseData.toString().getBytes(StandardCharsets.UTF_8); return exchange.getResponse().writeWith(Mono.just(exchange.getResponse() diff --git a/shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-redis/src/main/java/org/apache/shenyu/plugin/cache/redis/RedisConnectionFactory.java b/shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-redis/src/main/java/org/apache/shenyu/plugin/cache/redis/RedisConnectionFactory.java index 9342646311..cadd2755ef 100644 --- a/shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-redis/src/main/java/org/apache/shenyu/plugin/cache/redis/RedisConnectionFactory.java +++ b/shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-redis/src/main/java/org/apache/shenyu/plugin/cache/redis/RedisConnectionFactory.java @@ -92,7 +92,7 @@ public class RedisConnectionFactory { protected final RedisStandaloneConfiguration redisStandaloneConfiguration(final RedisConfigProperties redisConfigProperties) { RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(); String[] parts = StringUtils.split(redisConfigProperties.getUrl(), ":"); - assert Objects.nonNull(parts); + Objects.requireNonNull(parts); config.setHostName(parts[0]); config.setPort(Integer.parseInt(parts[1])); if (Objects.nonNull(redisConfigProperties.getPassword())) { diff --git a/shenyu-plugin/shenyu-plugin-context-path/src/main/java/org/apache/shenyu/plugin/context/path/ContextPathPlugin.java b/shenyu-plugin/shenyu-plugin-context-path/src/main/java/org/apache/shenyu/plugin/context/path/ContextPathPlugin.java index dc6a70a8c5..7d30a6429b 100644 --- a/shenyu-plugin/shenyu-plugin-context-path/src/main/java/org/apache/shenyu/plugin/context/path/ContextPathPlugin.java +++ b/shenyu-plugin/shenyu-plugin-context-path/src/main/java/org/apache/shenyu/plugin/context/path/ContextPathPlugin.java @@ -50,7 +50,7 @@ public class ContextPathPlugin extends AbstractShenyuPlugin { @Override protected Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPluginChain chain, final SelectorData selector, final RuleData rule) { ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT); - assert Objects.nonNull(shenyuContext); + Objects.requireNonNull(shenyuContext); ContextMappingRuleHandle ruleHandle = buildRuleHandle(rule); if (Objects.isNull(ruleHandle)) { LOG.error("context path rule configuration is null :{}", rule); diff --git a/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/HystrixPlugin.java b/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/HystrixPlugin.java index c9bf6ffce4..00fcf0c789 100644 --- a/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/HystrixPlugin.java +++ b/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/HystrixPlugin.java @@ -55,7 +55,7 @@ public class HystrixPlugin extends AbstractShenyuPlugin { @Override protected Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPluginChain chain, final SelectorData selector, final RuleData rule) { final ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT); - assert Objects.nonNull(shenyuContext); + Objects.requireNonNull(shenyuContext); final HystrixHandle hystrixHandle = HystrixPluginDataHandler.CACHED_HANDLE.get().obtainHandle(CacheKeyUtils.INST.getKey(rule)); String groupKey = hystrixHandle.getGroupKey(); if (StringUtils.isBlank(hystrixHandle.getGroupKey())) { diff --git a/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-sentinel/src/main/java/org/apache/shenyu/plugin/sentinel/SentinelPlugin.java b/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-sentinel/src/main/java/org/apache/shenyu/plugin/sentinel/SentinelPlugin.java index a97eb20957..342587ac93 100644 --- a/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-sentinel/src/main/java/org/apache/shenyu/plugin/sentinel/SentinelPlugin.java +++ b/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-sentinel/src/main/java/org/apache/shenyu/plugin/sentinel/SentinelPlugin.java @@ -54,7 +54,7 @@ public class SentinelPlugin extends AbstractShenyuPlugin { @Override protected Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPluginChain chain, final SelectorData selector, final RuleData rule) { final ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT); - assert Objects.nonNull(shenyuContext); + Objects.requireNonNull(shenyuContext); String resourceName = CacheKeyUtils.INST.getKey(rule); SentinelHandle sentinelHandle = SentinelRuleHandle.CACHED_HANDLE.get().obtainHandle(resourceName); sentinelHandle.checkData(); diff --git a/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/AbstractHttpClientPlugin.java b/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/AbstractHttpClientPlugin.java index 0ce91691ee..a81ba42d92 100644 --- a/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/AbstractHttpClientPlugin.java +++ b/shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/AbstractHttpClientPlugin.java @@ -74,7 +74,7 @@ public abstract class AbstractHttpClientPlugin<R> implements ShenyuPlugin { @Override public final Mono<Void> execute(final ServerWebExchange exchange, final ShenyuPluginChain chain) { final ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT); - assert Objects.nonNull(shenyuContext); + Objects.requireNonNull(shenyuContext); final URI uri = exchange.getAttribute(Constants.HTTP_URI); if (Objects.isNull(uri)) { Object error = ShenyuResultWrap.error(exchange, ShenyuResultEnum.CANNOT_FIND_URL); diff --git a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/main/java/org/apache/shenyu/plugin/logging/common/body/LoggingServerHttpResponse.java b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/main/java/org/apache/shenyu/plugin/logging/common/body/LoggingServerHttpResponse.java index bb3d260236..43a733ec45 100644 --- a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/main/java/org/apache/shenyu/plugin/logging/common/body/LoggingServerHttpResponse.java +++ b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/main/java/org/apache/shenyu/plugin/logging/common/body/LoggingServerHttpResponse.java @@ -127,7 +127,7 @@ public class LoggingServerHttpResponse<L extends ShenyuRequestLog> extends Serve @NonNull private Flux<? extends DataBuffer> appendResponse(final Publisher<? extends DataBuffer> body) { ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT); - assert Objects.nonNull(shenyuContext); + Objects.requireNonNull(shenyuContext); if (Objects.nonNull(getStatusCode())) { logInfo.setStatus(getStatusCode().value()); } @@ -196,7 +196,7 @@ public class LoggingServerHttpResponse<L extends ShenyuRequestLog> extends Serve */ private String getUpstreamIp() { ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT); - assert Objects.nonNull(shenyuContext); + Objects.requireNonNull(shenyuContext); if (RpcTypeEnum.HTTP.getName().equals(shenyuContext.getRpcType())) { URI uri = exchange.getAttribute(Constants.HTTP_URI); if (Objects.nonNull(uri)) { @@ -242,13 +242,13 @@ public class LoggingServerHttpResponse<L extends ShenyuRequestLog> extends Serve final ShenyuResult<?> shenyuResult = ShenyuResultWrap.shenyuResult(); Object resultData = shenyuResult.format(exchange, result); final Object responseData = shenyuResult.result(exchange, resultData); - assert Objects.nonNull(responseData); + Objects.requireNonNull(responseData); final byte[] bytes = (responseData instanceof byte[]) ? (byte[]) responseData : responseData.toString().getBytes(StandardCharsets.UTF_8); logInfo.setResponseContentLength(bytes.length); ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT); - assert Objects.nonNull(shenyuContext); + Objects.requireNonNull(shenyuContext); logInfo.setTimeLocal(shenyuContext.getStartDateTime().format(DATE_TIME_FORMATTER)); logInfo.setModule(shenyuContext.getModule()); long costTime = DateUtils.acquireMillisBetween(shenyuContext.getStartDateTime(), LocalDateTime.now()); diff --git a/shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/MetricsPlugin.java b/shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/MetricsPlugin.java index 02418931c8..0eda902b3d 100644 --- a/shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/MetricsPlugin.java +++ b/shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/MetricsPlugin.java @@ -44,7 +44,7 @@ public class MetricsPlugin implements ShenyuPlugin { public Mono<Void> execute(final ServerWebExchange exchange, final ShenyuPluginChain chain) { MetricsReporter.counterIncrement(LabelNames.REQUEST_TOTAL); ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT); - assert Objects.nonNull(shenyuContext); + Objects.requireNonNull(shenyuContext); setMetricsCallbacks(exchange); MetricsReporter.counterIncrement(LabelNames.REQUEST_TYPE_TOTAL, new String[]{exchange.getRequest().getURI().getRawPath(), shenyuContext.getRpcType()}); LocalDateTime startDateTime = Optional.of(shenyuContext).map(ShenyuContext::getStartDateTime).orElseGet(LocalDateTime::now); diff --git a/shenyu-plugin/shenyu-plugin-modify-response/src/main/java/org/apache/shenyu/plugin/modify/response/ModifyResponsePlugin.java b/shenyu-plugin/shenyu-plugin-modify-response/src/main/java/org/apache/shenyu/plugin/modify/response/ModifyResponsePlugin.java index 66327d522f..b4698683ee 100644 --- a/shenyu-plugin/shenyu-plugin-modify-response/src/main/java/org/apache/shenyu/plugin/modify/response/ModifyResponsePlugin.java +++ b/shenyu-plugin/shenyu-plugin-modify-response/src/main/java/org/apache/shenyu/plugin/modify/response/ModifyResponsePlugin.java @@ -61,7 +61,7 @@ public class ModifyResponsePlugin extends AbstractShenyuPlugin { @Override protected Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPluginChain chain, final SelectorData selector, final RuleData rule) { ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT); - assert Objects.nonNull(shenyuContext); + Objects.requireNonNull(shenyuContext); ModifyResponseRuleHandle ruleHandle = ModifyResponsePluginDataHandler.CACHED_HANDLE.get().obtainHandle(CacheKeyUtils.INST.getKey(rule)); if (Objects.isNull(ruleHandle)) { return chain.execute(exchange); diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/main/java/org/apache/shenyu/plugin/divide/DividePlugin.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/main/java/org/apache/shenyu/plugin/divide/DividePlugin.java index 3a2c9be759..39a514b2ee 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/main/java/org/apache/shenyu/plugin/divide/DividePlugin.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/main/java/org/apache/shenyu/plugin/divide/DividePlugin.java @@ -70,7 +70,7 @@ public class DividePlugin extends AbstractShenyuPlugin { @Override protected Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPluginChain chain, final SelectorData selector, final RuleData rule) { ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT); - assert Objects.nonNull(shenyuContext); + Objects.requireNonNull(shenyuContext); DivideRuleHandle ruleHandle = buildRuleHandle(rule); if (ruleHandle.getHeaderMaxSize() > 0) { long headerSize = exchange.getRequest().getHeaders().values() diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/test/java/org/apache/shenyu/plugin/divide/DividePluginTest.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/test/java/org/apache/shenyu/plugin/divide/DividePluginTest.java index a269736768..70109d02dc 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/test/java/org/apache/shenyu/plugin/divide/DividePluginTest.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/test/java/org/apache/shenyu/plugin/divide/DividePluginTest.java @@ -163,9 +163,9 @@ public final class DividePluginTest { loadBalancerFactoryMockedStatic.when(() -> LoadBalancerFactory.selector(any(), any(), any())) .thenReturn(null); dividePlugin.doExecute(exchange, chain, selectorData, ruleData); - // hit `assert shenyuContext != null` + // hit `Objects.requireNonNull(shenyuContext)` exchange.getAttributes().remove(Constants.CONTEXT); - assertThrows(AssertionError.class, () -> dividePlugin.doExecute(exchange, chain, selectorData, ruleData)); + assertThrows(NullPointerException.class, () -> dividePlugin.doExecute(exchange, chain, selectorData, ruleData)); } /** diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-dubbo-common/src/main/java/org/apache/shenyu/plugin/dubbo/common/AbstractDubboPlugin.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-dubbo-common/src/main/java/org/apache/shenyu/plugin/dubbo/common/AbstractDubboPlugin.java index 00380c19fc..3062c4e113 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-dubbo-common/src/main/java/org/apache/shenyu/plugin/dubbo/common/AbstractDubboPlugin.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-dubbo-common/src/main/java/org/apache/shenyu/plugin/dubbo/common/AbstractDubboPlugin.java @@ -94,7 +94,7 @@ public abstract class AbstractDubboPlugin extends AbstractShenyuPlugin { final RuleData rule) { String param = exchange.getAttribute(Constants.PARAM_TRANSFORM); ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT); - assert Objects.nonNull(shenyuContext); + Objects.requireNonNull(shenyuContext); MetaData metaData = exchange.getAttribute(Constants.META_DATA); if (!checkMetaData(metaData)) { LOG.error(" path is : {}, meta data have error : {}", shenyuContext.getPath(), metaData); diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-grpc/src/main/java/org/apache/shenyu/plugin/grpc/GrpcPlugin.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-grpc/src/main/java/org/apache/shenyu/plugin/grpc/GrpcPlugin.java index ab88dcaebc..a1b6bc516c 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-grpc/src/main/java/org/apache/shenyu/plugin/grpc/GrpcPlugin.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-grpc/src/main/java/org/apache/shenyu/plugin/grpc/GrpcPlugin.java @@ -71,7 +71,7 @@ public class GrpcPlugin extends AbstractShenyuPlugin { protected Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPluginChain chain, final SelectorData selector, final RuleData rule) { final String param = exchange.getAttribute(Constants.PARAM_TRANSFORM); ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT); - assert Objects.nonNull(shenyuContext); + Objects.requireNonNull(shenyuContext); MetaData metaData = exchange.getAttribute(Constants.META_DATA); if (!checkMetaData(metaData)) { @@ -80,7 +80,7 @@ public class GrpcPlugin extends AbstractShenyuPlugin { Object error = ShenyuResultWrap.error(exchange, ShenyuResultEnum.META_DATA_ERROR); return WebFluxResultUtils.result(exchange, error); } - assert Objects.nonNull(metaData); + Objects.requireNonNull(metaData); if (StringUtils.isNoneBlank(metaData.getParameterTypes()) && StringUtils.isBlank(param)) { exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR); Object error = ShenyuResultWrap.error(exchange, ShenyuResultEnum.GRPC_HAVE_BODY_PARAM); diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-motan/src/main/java/org/apache/shenyu/plugin/motan/MotanPlugin.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-motan/src/main/java/org/apache/shenyu/plugin/motan/MotanPlugin.java index 35643d8ac7..e98680ce1c 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-motan/src/main/java/org/apache/shenyu/plugin/motan/MotanPlugin.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-motan/src/main/java/org/apache/shenyu/plugin/motan/MotanPlugin.java @@ -69,10 +69,10 @@ public class MotanPlugin extends AbstractShenyuPlugin { final SelectorData selector, final RuleData rule) { String param = exchange.getAttribute(Constants.PARAM_TRANSFORM); ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT); - assert Objects.nonNull(shenyuContext); + Objects.requireNonNull(shenyuContext); MetaData metaData = exchange.getAttribute(Constants.META_DATA); if (!checkMetaData(metaData)) { - assert Objects.nonNull(metaData); + Objects.requireNonNull(metaData); LOG.error("path is :{}, meta data have error.... {}", shenyuContext.getPath(), metaData); exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR); Object error = ShenyuResultWrap.error(exchange, ShenyuResultEnum.META_DATA_ERROR); diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-sofa/src/main/java/org/apache/shenyu/plugin/sofa/SofaPlugin.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-sofa/src/main/java/org/apache/shenyu/plugin/sofa/SofaPlugin.java index fca010881c..558a30a844 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-sofa/src/main/java/org/apache/shenyu/plugin/sofa/SofaPlugin.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-sofa/src/main/java/org/apache/shenyu/plugin/sofa/SofaPlugin.java @@ -70,10 +70,10 @@ public class SofaPlugin extends AbstractShenyuPlugin { protected Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPluginChain chain, final SelectorData selector, final RuleData rule) { String param = exchange.getAttribute(Constants.PARAM_TRANSFORM); ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT); - assert Objects.nonNull(shenyuContext); + Objects.requireNonNull(shenyuContext); MetaData metaData = exchange.getAttribute(Constants.META_DATA); if (!checkMetaData(metaData)) { - assert Objects.nonNull(metaData); + Objects.requireNonNull(metaData); LOG.error(" path is :{}, meta data have error.... {}", shenyuContext.getPath(), metaData); exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR); Object error = ShenyuResultWrap.error(exchange, ShenyuResultEnum.META_DATA_ERROR); diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-sofa/src/main/java/org/apache/shenyu/plugin/sofa/param/SofaParamResolveServiceImpl.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-sofa/src/main/java/org/apache/shenyu/plugin/sofa/param/SofaParamResolveServiceImpl.java index 4d5bf3a799..fc8981ed51 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-sofa/src/main/java/org/apache/shenyu/plugin/sofa/param/SofaParamResolveServiceImpl.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-sofa/src/main/java/org/apache/shenyu/plugin/sofa/param/SofaParamResolveServiceImpl.java @@ -28,6 +28,7 @@ import org.apache.commons.lang3.tuple.Pair; import org.apache.shenyu.common.utils.GsonUtils; import org.apache.shenyu.common.utils.ParamCheckUtils; import org.springframework.lang.NonNull; +import org.springframework.util.Assert; import java.util.ArrayList; import java.util.List; @@ -73,7 +74,7 @@ public class SofaParamResolveServiceImpl implements SofaParamResolveService { // no generic info return mapValue; } - assert parameterType.length == 3; + Assert.state(parameterType.length == 3, "parameterType length must be 3."); // generic map final GenericMap genericMap = new GenericMap(parameterType[2]); mapValue.replaceAll((k, v) -> convertToGenericObject(parameterType[2], mapValue.get(k))); diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-tars/src/main/java/org/apache/shenyu/plugin/tars/TarsPlugin.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-tars/src/main/java/org/apache/shenyu/plugin/tars/TarsPlugin.java index e6966a9a72..01593e07f7 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-tars/src/main/java/org/apache/shenyu/plugin/tars/TarsPlugin.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-tars/src/main/java/org/apache/shenyu/plugin/tars/TarsPlugin.java @@ -64,10 +64,10 @@ public class TarsPlugin extends AbstractShenyuPlugin { protected Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPluginChain chain, final SelectorData selector, final RuleData rule) { String body = exchange.getAttribute(Constants.PARAM_TRANSFORM); ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT); - assert Objects.nonNull(shenyuContext); + Objects.requireNonNull(shenyuContext); MetaData metaData = exchange.getAttribute(Constants.META_DATA); if (!checkMetaData(metaData)) { - assert Objects.nonNull(metaData); + Objects.requireNonNull(metaData); LOG.error(" path is :{}, meta data have error.... {}", shenyuContext.getPath(), metaData); exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR); Object error = ShenyuResultWrap.error(exchange, ShenyuResultEnum.META_DATA_ERROR); diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-tars/src/main/java/org/apache/shenyu/plugin/tars/cache/ApplicationConfigCache.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-tars/src/main/java/org/apache/shenyu/plugin/tars/cache/ApplicationConfigCache.java index 558787314c..1422da5d0c 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-tars/src/main/java/org/apache/shenyu/plugin/tars/cache/ApplicationConfigCache.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-tars/src/main/java/org/apache/shenyu/plugin/tars/cache/ApplicationConfigCache.java @@ -216,7 +216,7 @@ public final class ApplicationConfigCache { * @see ReentrantLock */ private void tryLockedLoadMetaData(final MetaData metaData) throws ClassNotFoundException { - assert Objects.nonNull(LOCK); + Objects.requireNonNull(LOCK); if (LOCK.tryLock()) { try { if (StringUtils.isEmpty(metaData.getRpcExt())) { diff --git a/shenyu-plugin/shenyu-plugin-response/src/main/java/org/apache/shenyu/plugin/response/ResponsePlugin.java b/shenyu-plugin/shenyu-plugin-response/src/main/java/org/apache/shenyu/plugin/response/ResponsePlugin.java index 2d0f78312a..e16747ea6d 100644 --- a/shenyu-plugin/shenyu-plugin-response/src/main/java/org/apache/shenyu/plugin/response/ResponsePlugin.java +++ b/shenyu-plugin/shenyu-plugin-response/src/main/java/org/apache/shenyu/plugin/response/ResponsePlugin.java @@ -48,7 +48,7 @@ public class ResponsePlugin implements ShenyuPlugin { @Override public Mono<Void> execute(final ServerWebExchange exchange, final ShenyuPluginChain chain) { ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT); - assert Objects.nonNull(shenyuContext); + Objects.requireNonNull(shenyuContext); return writerMap.get(shenyuContext.getRpcType()).writeWith(exchange, chain); } diff --git a/shenyu-plugin/shenyu-plugin-rewrite/src/main/java/org/apache/shenyu/plugin/rewrite/RewritePlugin.java b/shenyu-plugin/shenyu-plugin-rewrite/src/main/java/org/apache/shenyu/plugin/rewrite/RewritePlugin.java index 76563ce62d..3f0616fbcc 100644 --- a/shenyu-plugin/shenyu-plugin-rewrite/src/main/java/org/apache/shenyu/plugin/rewrite/RewritePlugin.java +++ b/shenyu-plugin/shenyu-plugin-rewrite/src/main/java/org/apache/shenyu/plugin/rewrite/RewritePlugin.java @@ -57,7 +57,7 @@ public class RewritePlugin extends AbstractShenyuPlugin { } // match the new selector/rule of RewritePlugin ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT); - assert Objects.nonNull(shenyuContext); + Objects.requireNonNull(shenyuContext); return rewriteContextPath + shenyuContext.getRealUrl(); } @@ -90,7 +90,7 @@ public class RewritePlugin extends AbstractShenyuPlugin { attributes.put(Constants.META_DATA, metaData); } ShenyuContext context = exchange.getAttribute(Constants.CONTEXT); - assert Objects.nonNull(context); + Objects.requireNonNull(context); if (Objects.nonNull(metaData) && Boolean.TRUE.equals(metaData.getEnabled())) { context.setRpcType(metaData.getRpcType()); } else { diff --git a/shenyu-plugin/shenyu-plugin-security/shenyu-plugin-sign/src/main/java/org/apache/shenyu/plugin/sign/service/ComposableSignService.java b/shenyu-plugin/shenyu-plugin-security/shenyu-plugin-sign/src/main/java/org/apache/shenyu/plugin/sign/service/ComposableSignService.java index f6934b9fd6..f4142c7604 100644 --- a/shenyu-plugin/shenyu-plugin-security/shenyu-plugin-sign/src/main/java/org/apache/shenyu/plugin/sign/service/ComposableSignService.java +++ b/shenyu-plugin/shenyu-plugin-security/shenyu-plugin-sign/src/main/java/org/apache/shenyu/plugin/sign/service/ComposableSignService.java @@ -104,7 +104,7 @@ public class ComposableSignService implements SignService { private VerifyResult signatureVerify(final ServerWebExchange exchange, final BiFunction<String, SignParameters, String> signFunction) { final ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT); - assert Objects.nonNull(shenyuContext); + Objects.requireNonNull(shenyuContext); SignParameters signParameters = extractor.extract(exchange.getRequest());