From 3901caf2e258bba533c1f2f1a7935e43b741fd50 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Sat, 11 May 2024 00:03:08 +0800 Subject: [PATCH] Fix code inspection for agent module (#31200) --- .../plugin/PluginLifecycleServiceManager.java | 4 +-- .../agent/core/spi/AgentServiceLoader.java | 2 +- .../yaml/fixture/YamlAdviceFixture.java | 26 ----------------- .../config/PluginConfigurationLoaderTest.java | 6 ++-- .../YamlPluginsConfigurationSwapperTest.java | 6 ++-- .../fixture/targeted/TargetObjectFixture.java | 4 +-- .../yaml/AgentYamlEngineTest.java | 6 ++-- .../plugin/core/spi/PluginServiceLoader.java | 2 +- .../PrometheusPluginLifecycleService.java | 4 +-- ...lemetryJDBCExecutorCallbackAdviceTest.java | 2 +- .../OpenTelemetryRootSpanAdviceTest.java | 2 +- ...penTelemetrySQLParserEngineAdviceTest.java | 2 +- .../batch/BatchPreparedStatementExecutor.java | 2 +- .../jdbc/core/ShardingSphereSavepoint.java | 2 +- .../DriverDatabaseConnectionManagerTest.java | 2 +- .../driver/jdbc/util/JDBCTestSQL.java | 29 ------------------- 16 files changed, 23 insertions(+), 78 deletions(-) delete mode 100644 jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/util/JDBCTestSQL.java diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/PluginLifecycleServiceManager.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/PluginLifecycleServiceManager.java index bfbdbc796ca6f..ea415850eb7a1 100644 --- a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/PluginLifecycleServiceManager.java +++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/PluginLifecycleServiceManager.java @@ -52,8 +52,8 @@ public final class PluginLifecycleServiceManager { */ public static void init(final Map pluginConfigs, final Collection pluginJars, final ClassLoader pluginClassLoader, final boolean isEnhancedForProxy) { if (STARTED_FLAG.compareAndSet(false, true)) { - PluginLifecycleServiceManager.start(pluginConfigs, pluginClassLoader, isEnhancedForProxy); - Runtime.getRuntime().addShutdownHook(new Thread(() -> PluginLifecycleServiceManager.close(pluginJars))); + start(pluginConfigs, pluginClassLoader, isEnhancedForProxy); + Runtime.getRuntime().addShutdownHook(new Thread(() -> close(pluginJars))); } } diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/spi/AgentServiceLoader.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/spi/AgentServiceLoader.java index 23834ea855a36..5cc39ff7fd8c0 100644 --- a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/spi/AgentServiceLoader.java +++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/spi/AgentServiceLoader.java @@ -40,7 +40,7 @@ public final class AgentServiceLoader { private AgentServiceLoader(final Class service) { AgentPreconditions.checkArgument(service.isInterface(), String.format("SPI class `%s` is not interface.", service)); - this.services = load(service); + services = load(service); } private Collection load(final Class service) { diff --git a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/advisor/config/yaml/fixture/YamlAdviceFixture.java b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/advisor/config/yaml/fixture/YamlAdviceFixture.java index 292d2ed724d13..6f2fb97757d3a 100644 --- a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/advisor/config/yaml/fixture/YamlAdviceFixture.java +++ b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/advisor/config/yaml/fixture/YamlAdviceFixture.java @@ -22,35 +22,9 @@ import org.apache.shardingsphere.agent.api.advice.type.InstanceMethodAdvice; import org.apache.shardingsphere.agent.api.advice.type.StaticMethodAdvice; -import java.lang.reflect.Method; - public final class YamlAdviceFixture implements ConstructorAdvice, InstanceMethodAdvice, StaticMethodAdvice { @Override public void onConstructor(final TargetAdviceObject target, final Object[] args, final String pluginType) { } - - @Override - public void beforeMethod(final TargetAdviceObject target, final Method method, final Object[] args, final String pluginType) { - } - - @Override - public void beforeMethod(final Class clazz, final Method method, final Object[] args, final String pluginType) { - } - - @Override - public void afterMethod(final TargetAdviceObject target, final Method method, final Object[] args, final Object result, final String pluginType) { - } - - @Override - public void afterMethod(final Class clazz, final Method method, final Object[] args, final Object result, final String pluginType) { - } - - @Override - public void onThrowing(final TargetAdviceObject target, final Method method, final Object[] args, final Throwable throwable, final String pluginType) { - } - - @Override - public void onThrowing(final Class clazz, final Method method, final Object[] args, final Throwable throwable, final String pluginType) { - } } diff --git a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/config/PluginConfigurationLoaderTest.java b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/config/PluginConfigurationLoaderTest.java index 3e7ec100c51af..457139941deb8 100644 --- a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/config/PluginConfigurationLoaderTest.java +++ b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/config/PluginConfigurationLoaderTest.java @@ -51,7 +51,7 @@ private void assertLoggingPluginConfiguration(final PluginConfiguration actual) assertNull(actual.getPassword()); assertThat(actual.getPort(), is(8080)); assertThat(actual.getProps().size(), is(1)); - assertThat(actual.getProps().get("key"), is("value")); + assertThat(actual.getProps().getProperty("key"), is("value")); } private void assertMetricsPluginConfiguration(final PluginConfiguration actual) { @@ -59,7 +59,7 @@ private void assertMetricsPluginConfiguration(final PluginConfiguration actual) assertThat(actual.getPassword(), is("random")); assertThat(actual.getPort(), is(8081)); assertThat(actual.getProps().size(), is(1)); - assertThat(actual.getProps().get("key"), is("value")); + assertThat(actual.getProps().getProperty("key"), is("value")); } private void assertTracingPluginConfiguration(final PluginConfiguration actual) { @@ -67,6 +67,6 @@ private void assertTracingPluginConfiguration(final PluginConfiguration actual) assertThat(actual.getPassword(), is("random")); assertThat(actual.getPort(), is(8082)); assertThat(actual.getProps().size(), is(1)); - assertThat(actual.getProps().get("key"), is("value")); + assertThat(actual.getProps().getProperty("key"), is("value")); } } diff --git a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/config/yaml/swapper/YamlPluginsConfigurationSwapperTest.java b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/config/yaml/swapper/YamlPluginsConfigurationSwapperTest.java index 0117776320668..6a9bc2332bfd5 100644 --- a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/config/yaml/swapper/YamlPluginsConfigurationSwapperTest.java +++ b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/config/yaml/swapper/YamlPluginsConfigurationSwapperTest.java @@ -102,7 +102,7 @@ private void assertLogFixturePluginConfiguration(final PluginConfiguration actua assertNull(actual.getPassword()); assertThat(actual.getPort(), is(8080)); assertThat(actual.getProps().size(), is(1)); - assertThat(actual.getProps().get("key"), is("value")); + assertThat(actual.getProps().getProperty("key"), is("value")); } private void assertMetricsPluginConfiguration(final PluginConfiguration actual) { @@ -110,7 +110,7 @@ private void assertMetricsPluginConfiguration(final PluginConfiguration actual) assertThat(actual.getPassword(), is("random")); assertThat(actual.getPort(), is(8081)); assertThat(actual.getProps().size(), is(1)); - assertThat(actual.getProps().get("key"), is("value")); + assertThat(actual.getProps().getProperty("key"), is("value")); } private void assertTracingPluginConfiguration(final PluginConfiguration actual) { @@ -118,7 +118,7 @@ private void assertTracingPluginConfiguration(final PluginConfiguration actual) assertThat(actual.getPassword(), is("random")); assertThat(actual.getPort(), is(8082)); assertThat(actual.getProps().size(), is(1)); - assertThat(actual.getProps().get("key"), is("value")); + assertThat(actual.getProps().getProperty("key"), is("value")); } private Properties createProperties() { diff --git a/agent/core/src/test/java/org/apache/shardingsphere/fixture/targeted/TargetObjectFixture.java b/agent/core/src/test/java/org/apache/shardingsphere/fixture/targeted/TargetObjectFixture.java index e707a5e2f1a03..a3e6d029fa4a9 100644 --- a/agent/core/src/test/java/org/apache/shardingsphere/fixture/targeted/TargetObjectFixture.java +++ b/agent/core/src/test/java/org/apache/shardingsphere/fixture/targeted/TargetObjectFixture.java @@ -41,7 +41,7 @@ public void call(final List queue) { * @throws UnsupportedOperationException unsupported operation exception */ public void callWhenExceptionThrown(final List queue) { - throw new UnsupportedOperationException(); + throw new UnsupportedOperationException(""); } /** @@ -60,6 +60,6 @@ public static void staticCall(final List queue) { * @throws UnsupportedOperationException unsupported operation exception */ public static void staticCallWhenExceptionThrown(final List queue) { - throw new UnsupportedOperationException(); + throw new UnsupportedOperationException(""); } } diff --git a/agent/core/src/test/java/org/apache/shardingsphere/yaml/AgentYamlEngineTest.java b/agent/core/src/test/java/org/apache/shardingsphere/yaml/AgentYamlEngineTest.java index 7a2d22bd50c3a..42e911fc420b0 100644 --- a/agent/core/src/test/java/org/apache/shardingsphere/yaml/AgentYamlEngineTest.java +++ b/agent/core/src/test/java/org/apache/shardingsphere/yaml/AgentYamlEngineTest.java @@ -76,7 +76,7 @@ private void assertLogFixturePluginConfiguration(final PluginConfiguration actua assertNull(actual.getPassword()); assertThat(actual.getPort(), is(8080)); assertThat(actual.getProps().size(), is(1)); - assertThat(actual.getProps().get("key"), is("value")); + assertThat(actual.getProps().getProperty("key"), is("value")); } private void assertMetricsPluginConfiguration(final PluginConfiguration actual) { @@ -84,7 +84,7 @@ private void assertMetricsPluginConfiguration(final PluginConfiguration actual) assertThat(actual.getPassword(), is("random")); assertThat(actual.getPort(), is(8081)); assertThat(actual.getProps().size(), is(1)); - assertThat(actual.getProps().get("key"), is("value")); + assertThat(actual.getProps().getProperty("key"), is("value")); } private void assertTracingPluginConfiguration(final PluginConfiguration actual) { @@ -92,7 +92,7 @@ private void assertTracingPluginConfiguration(final PluginConfiguration actual) assertThat(actual.getPassword(), is("random")); assertThat(actual.getPort(), is(8082)); assertThat(actual.getProps().size(), is(1)); - assertThat(actual.getProps().get("key"), is("value")); + assertThat(actual.getProps().getProperty("key"), is("value")); } private void assertYamlAdvisorConfiguration(final YamlAdvisorConfiguration actual) { diff --git a/agent/plugins/core/src/main/java/org/apache/shardingsphere/agent/plugin/core/spi/PluginServiceLoader.java b/agent/plugins/core/src/main/java/org/apache/shardingsphere/agent/plugin/core/spi/PluginServiceLoader.java index 683db6ffecefc..8b10280006858 100644 --- a/agent/plugins/core/src/main/java/org/apache/shardingsphere/agent/plugin/core/spi/PluginServiceLoader.java +++ b/agent/plugins/core/src/main/java/org/apache/shardingsphere/agent/plugin/core/spi/PluginServiceLoader.java @@ -38,7 +38,7 @@ public final class PluginServiceLoader { private PluginServiceLoader(final Class service) { PluginPreconditions.checkArgument(service.isInterface(), String.format("SPI class `%s` is not interface.", service)); - this.services = load(service); + services = load(service); } private Collection load(final Class service) { diff --git a/agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/PrometheusPluginLifecycleService.java b/agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/PrometheusPluginLifecycleService.java index 1da1832be02c4..b8a4630389d38 100644 --- a/agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/PrometheusPluginLifecycleService.java +++ b/agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/PrometheusPluginLifecycleService.java @@ -89,8 +89,8 @@ private InetSocketAddress getSocketAddress(final PluginConfiguration pluginConfi return isNullOrEmpty(pluginConfig.getHost()) ? new InetSocketAddress(pluginConfig.getPort()) : new InetSocketAddress(pluginConfig.getHost(), pluginConfig.getPort()); } - private boolean isNullOrEmpty(final String string) { - return null == string || string.isEmpty(); + private boolean isNullOrEmpty(final String value) { + return null == value || value.isEmpty(); } @Override diff --git a/agent/plugins/tracing/type/opentelemetry/src/test/java/org/apache/shardingsphere/agent/plugin/tracing/opentelemetry/advice/OpenTelemetryJDBCExecutorCallbackAdviceTest.java b/agent/plugins/tracing/type/opentelemetry/src/test/java/org/apache/shardingsphere/agent/plugin/tracing/opentelemetry/advice/OpenTelemetryJDBCExecutorCallbackAdviceTest.java index e5712ab6317ae..82f8f8fae0c9c 100644 --- a/agent/plugins/tracing/type/opentelemetry/src/test/java/org/apache/shardingsphere/agent/plugin/tracing/opentelemetry/advice/OpenTelemetryJDBCExecutorCallbackAdviceTest.java +++ b/agent/plugins/tracing/type/opentelemetry/src/test/java/org/apache/shardingsphere/agent/plugin/tracing/opentelemetry/advice/OpenTelemetryJDBCExecutorCallbackAdviceTest.java @@ -125,7 +125,7 @@ void assertMethod() { void assertExceptionHandle() { OpenTelemetryJDBCExecutorCallbackAdvice advice = new OpenTelemetryJDBCExecutorCallbackAdvice(); advice.beforeMethod(targetObject, null, new Object[]{executionUnit, false}, "OpenTelemetry"); - advice.onThrowing(targetObject, null, new Object[]{executionUnit, false}, new IOException(), "OpenTelemetry"); + advice.onThrowing(targetObject, null, new Object[]{executionUnit, false}, new IOException(""), "OpenTelemetry"); List spanItems = testExporter.getFinishedSpanItems(); assertCommonData(spanItems); assertThat(spanItems.iterator().next().getStatus().getStatusCode(), is(StatusCode.ERROR)); diff --git a/agent/plugins/tracing/type/opentelemetry/src/test/java/org/apache/shardingsphere/agent/plugin/tracing/opentelemetry/advice/OpenTelemetryRootSpanAdviceTest.java b/agent/plugins/tracing/type/opentelemetry/src/test/java/org/apache/shardingsphere/agent/plugin/tracing/opentelemetry/advice/OpenTelemetryRootSpanAdviceTest.java index ad4c6582ed174..3ef2d8f122478 100644 --- a/agent/plugins/tracing/type/opentelemetry/src/test/java/org/apache/shardingsphere/agent/plugin/tracing/opentelemetry/advice/OpenTelemetryRootSpanAdviceTest.java +++ b/agent/plugins/tracing/type/opentelemetry/src/test/java/org/apache/shardingsphere/agent/plugin/tracing/opentelemetry/advice/OpenTelemetryRootSpanAdviceTest.java @@ -79,7 +79,7 @@ void assertMethod() { void assertExceptionHandle() { OpenTelemetryRootSpanAdvice advice = new OpenTelemetryRootSpanAdvice(); advice.beforeMethod(new TargetAdviceObjectFixture(), null, new Object[]{}, "OpenTelemetry"); - advice.onThrowing(new TargetAdviceObjectFixture(), null, new Object[]{}, new IOException(), "OpenTelemetry"); + advice.onThrowing(new TargetAdviceObjectFixture(), null, new Object[]{}, new IOException(""), "OpenTelemetry"); List spanItems = testExporter.getFinishedSpanItems(); assertCommonData(spanItems); assertThat(spanItems.iterator().next().getStatus().getStatusCode(), is(StatusCode.ERROR)); diff --git a/agent/plugins/tracing/type/opentelemetry/src/test/java/org/apache/shardingsphere/agent/plugin/tracing/opentelemetry/advice/OpenTelemetrySQLParserEngineAdviceTest.java b/agent/plugins/tracing/type/opentelemetry/src/test/java/org/apache/shardingsphere/agent/plugin/tracing/opentelemetry/advice/OpenTelemetrySQLParserEngineAdviceTest.java index 7a1ececc36a5c..526e014ba3c50 100644 --- a/agent/plugins/tracing/type/opentelemetry/src/test/java/org/apache/shardingsphere/agent/plugin/tracing/opentelemetry/advice/OpenTelemetrySQLParserEngineAdviceTest.java +++ b/agent/plugins/tracing/type/opentelemetry/src/test/java/org/apache/shardingsphere/agent/plugin/tracing/opentelemetry/advice/OpenTelemetrySQLParserEngineAdviceTest.java @@ -80,7 +80,7 @@ void assertExceptionHandle() { TargetAdviceObjectFixture adviceObjectFixture = new TargetAdviceObjectFixture(); OpenTelemetrySQLParserEngineAdvice advice = new OpenTelemetrySQLParserEngineAdvice(); advice.beforeMethod(adviceObjectFixture, null, new Object[]{SQL, true}, "OpenTelemetry"); - advice.onThrowing(adviceObjectFixture, null, new Object[]{SQL, true}, new IOException(), "OpenTelemetry"); + advice.onThrowing(adviceObjectFixture, null, new Object[]{SQL, true}, new IOException(""), "OpenTelemetry"); List spanItems = testExporter.getFinishedSpanItems(); assertCommonData(spanItems); assertThat(spanItems.iterator().next().getStatus().getStatusCode(), is(StatusCode.ERROR)); diff --git a/jdbc/src/main/java/org/apache/shardingsphere/driver/executor/batch/BatchPreparedStatementExecutor.java b/jdbc/src/main/java/org/apache/shardingsphere/driver/executor/batch/BatchPreparedStatementExecutor.java index dfca1b1469bb7..69a63292e8f07 100644 --- a/jdbc/src/main/java/org/apache/shardingsphere/driver/executor/batch/BatchPreparedStatementExecutor.java +++ b/jdbc/src/main/java/org/apache/shardingsphere/driver/executor/batch/BatchPreparedStatementExecutor.java @@ -245,7 +245,7 @@ private List> getParameterSets(final JDBCExecutionUnit executionUni return each.getParameterSets(); } } - throw new IllegalStateException(); + throw new IllegalStateException("Can not get value from parameter sets."); } /** diff --git a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/ShardingSphereSavepoint.java b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/ShardingSphereSavepoint.java index 416ad67c17a40..4c16cfffda8f4 100644 --- a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/ShardingSphereSavepoint.java +++ b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/ShardingSphereSavepoint.java @@ -36,7 +36,7 @@ public ShardingSphereSavepoint() { } public ShardingSphereSavepoint(final String name) throws SQLException { - ShardingSpherePreconditions.checkState(null != name && 0 != name.length(), () -> new SQLFeatureNotSupportedException("Savepoint name can not be NULL or empty")); + ShardingSpherePreconditions.checkNotEmpty(name, () -> new SQLFeatureNotSupportedException("Savepoint name can not be NULL or empty")); savepointName = name; } diff --git a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java index 1256fe37b7871..e1d7251ac4fc5 100644 --- a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java +++ b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java @@ -93,7 +93,7 @@ private Map mockStorageUnits() throws SQLException { Map result = new HashMap<>(2, 1F); result.put("ds", mockStorageUnit(new MockedDataSource())); DataSource invalidDataSource = mock(DataSource.class); - when(invalidDataSource.getConnection()).thenThrow(new SQLException()); + when(invalidDataSource.getConnection()).thenThrow(new SQLException("")); result.put("invalid_ds", mockStorageUnit(invalidDataSource)); return result; } diff --git a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/util/JDBCTestSQL.java b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/util/JDBCTestSQL.java deleted file mode 100644 index c67d1e208c399..0000000000000 --- a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/util/JDBCTestSQL.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.shardingsphere.driver.jdbc.util; - -import lombok.AccessLevel; -import lombok.NoArgsConstructor; - -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class JDBCTestSQL { - - public static final String SELECT_GROUP_BY_USER_ID_SQL = "SELECT user_id FROM t_order GROUP BY user_id"; - - public static final String SELECT_ORDER_BY_USER_ID_SQL = "SELECT user_id FROM t_order WHERE status = 'init' ORDER BY user_id"; -}