You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2024/01/23 02:45:30 UTC
(shardingsphere) branch master updated: Fix sonar error (#29804)
This is an automated email from the ASF dual-hosted git repository.
jianglongtao 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 5aa2132dbfb Fix sonar error (#29804)
5aa2132dbfb is described below
commit 5aa2132dbfb113b34eb3ba28d87a01ae876ec6a5
Author: jiangML <10...@qq.com>
AuthorDate: Tue Jan 23 10:45:24 2024 +0800
Fix sonar error (#29804)
* Fix sonar error, optimize PluginPreconditions and AgentPreconditions
* Fix sonar error, optimize log
---
.../advisor/config/AdvisorConfigurationLoader.java | 7 +-----
.../executor/type/ConstructorAdviceExecutor.java | 3 ++-
.../type/InstanceMethodAdviceExecutor.java | 7 +++---
.../executor/type/StaticMethodAdviceExecutor.java | 7 +++---
.../agent/core/builder/AgentTransformer.java | 6 ++---
.../impl/MethodAdvisorBuilderInterceptor.java | 3 ++-
.../core/plugin/PluginLifecycleServiceManager.java | 7 +++---
.../agent/core/plugin/jar/PluginJarLoader.java | 3 ++-
.../agent/core/spi/AgentServiceLoader.java | 7 +-----
.../agent/core/util/AgentPreconditions.java | 13 -----------
.../agent/plugin/core/spi/PluginServiceLoader.java | 7 +-----
.../plugin/core/util/PluginPreconditions.java | 26 ----------------------
12 files changed, 24 insertions(+), 72 deletions(-)
diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/advisor/config/AdvisorConfigurationLoader.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/advisor/config/AdvisorConfigurationLoader.java
index 1e0a3cc06e6..98551e16ec5 100644
--- a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/advisor/config/AdvisorConfigurationLoader.java
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/advisor/config/AdvisorConfigurationLoader.java
@@ -29,7 +29,6 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.JarFile;
-import java.util.logging.Logger;
/**
* Advisor configuration loader.
@@ -37,8 +36,6 @@ import java.util.logging.Logger;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class AdvisorConfigurationLoader {
- private static final Logger LOGGER = Logger.getLogger(AdvisorConfigurationLoader.class.getName());
-
/**
* Load advisor configurations.
*
@@ -51,9 +48,7 @@ public final class AdvisorConfigurationLoader {
AgentPluginClassLoader agentPluginClassLoader = new AgentPluginClassLoader(Thread.currentThread().getContextClassLoader(), pluginJars);
for (String each : pluginTypes) {
InputStream advisorsResourceStream = getResourceStream(agentPluginClassLoader, each);
- if (null == advisorsResourceStream) {
- LOGGER.info(String.format("No configuration of advisor for type `%s`.", each));
- } else {
+ if (null != advisorsResourceStream) {
mergeConfigurations(result, YamlAdvisorsConfigurationSwapper.swap(YamlAdvisorsConfigurationLoader.load(advisorsResourceStream), each));
}
}
diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/advisor/executor/type/ConstructorAdviceExecutor.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/advisor/executor/type/ConstructorAdviceExecutor.java
index 3ebc0c50902..e27d220021b 100644
--- a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/advisor/executor/type/ConstructorAdviceExecutor.java
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/advisor/executor/type/ConstructorAdviceExecutor.java
@@ -34,6 +34,7 @@ import org.apache.shardingsphere.agent.core.advisor.executor.AdviceExecutor;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.logging.Level;
import java.util.logging.Logger;
/**
@@ -65,7 +66,7 @@ public final class ConstructorAdviceExecutor implements AdviceExecutor {
// CHECKSTYLE:OFF
} catch (final Throwable ex) {
// CHECKSTYLE:ON
- LOGGER.severe(String.format("Constructor advice execution error. class: %s, %s", target.getClass().getTypeName(), ex.getMessage()));
+ LOGGER.log(Level.SEVERE, "Constructor advice execution error. class: {0}, {1}", new String[]{target.getClass().getTypeName(), ex.getMessage()});
}
}
diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/advisor/executor/type/InstanceMethodAdviceExecutor.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/advisor/executor/type/InstanceMethodAdviceExecutor.java
index 051ba01bf83..9fff440ddfc 100644
--- a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/advisor/executor/type/InstanceMethodAdviceExecutor.java
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/advisor/executor/type/InstanceMethodAdviceExecutor.java
@@ -38,6 +38,7 @@ import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
+import java.util.logging.Level;
import java.util.logging.Logger;
/**
@@ -89,7 +90,7 @@ public final class InstanceMethodAdviceExecutor implements AdviceExecutor {
// CHECKSTYLE:OFF
} catch (final Throwable ex) {
// CHECKSTYLE:ON
- LOGGER.severe(String.format("Failed to execute the pre-method of method `%s` in class `%s`, %s.", method.getName(), target.getClass(), ex.getMessage()));
+ LOGGER.log(Level.SEVERE, "Failed to execute the pre-method of method `{0}` in class `{1}`, {2}.", new String[]{method.getName(), target.getClass().getName(), ex.getMessage()});
}
}
@@ -105,7 +106,7 @@ public final class InstanceMethodAdviceExecutor implements AdviceExecutor {
// CHECKSTYLE:OFF
} catch (final Throwable ignored) {
// CHECKSTYLE:ON
- LOGGER.severe(String.format("Failed to execute the error handler of method `%s` in class `%s`, %s.", method.getName(), target.getClass(), ex.getMessage()));
+ LOGGER.log(Level.SEVERE, "Failed to execute the error handler of method `{0}` in class `{1}`, {2}.", new String[]{method.getName(), target.getClass().getName(), ex.getMessage()});
}
}
@@ -121,7 +122,7 @@ public final class InstanceMethodAdviceExecutor implements AdviceExecutor {
// CHECKSTYLE:OFF
} catch (final Throwable ex) {
// CHECKSTYLE:ON
- LOGGER.severe(String.format("Failed to execute the post-method of method `%s` in class `%s`, %s.", method.getName(), target.getClass(), ex.getMessage()));
+ LOGGER.log(Level.SEVERE, "Failed to execute the post-method of method `{0}` in class `{1}`, {2}.", new String[]{method.getName(), target.getClass().getName(), ex.getMessage()});
}
}
diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/advisor/executor/type/StaticMethodAdviceExecutor.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/advisor/executor/type/StaticMethodAdviceExecutor.java
index 55a61a5e2b2..3e7182ed319 100644
--- a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/advisor/executor/type/StaticMethodAdviceExecutor.java
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/advisor/executor/type/StaticMethodAdviceExecutor.java
@@ -36,6 +36,7 @@ import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
+import java.util.logging.Level;
import java.util.logging.Logger;
/**
@@ -87,7 +88,7 @@ public final class StaticMethodAdviceExecutor implements AdviceExecutor {
// CHECKSTYLE:OFF
} catch (final Throwable ex) {
// CHECKSTYLE:ON
- LOGGER.severe(String.format("Failed to execute the pre-method of method `%s` in class `%s`, %s.", method.getName(), klass, ex.getMessage()));
+ LOGGER.log(Level.SEVERE, "Failed to execute the pre-method of method `{0}` in class `{1}`, {2}.", new String[]{method.getName(), klass.getClass().getName(), ex.getMessage()});
}
}
@@ -103,7 +104,7 @@ public final class StaticMethodAdviceExecutor implements AdviceExecutor {
// CHECKSTYLE:OFF
} catch (final Throwable ignored) {
// CHECKSTYLE:ON
- LOGGER.severe(String.format("Failed to execute the error handler of method `%s` in class `%s`, %s.", method.getName(), klass, ex.getMessage()));
+ LOGGER.log(Level.SEVERE, "Failed to execute the error handler of method `{0}` in class `{1}`, {2}.", new String[]{method.getName(), klass.getClass().getName(), ex.getMessage()});
}
}
@@ -119,7 +120,7 @@ public final class StaticMethodAdviceExecutor implements AdviceExecutor {
// CHECKSTYLE:OFF
} catch (final Throwable ex) {
// CHECKSTYLE:ON
- LOGGER.severe(String.format("Failed to execute the post-method of method `%s` in class `%s` %s.", method.getName(), klass, ex.getMessage()));
+ LOGGER.log(Level.SEVERE, "Failed to execute the post-method of method `{0}` in class `{1}` {2}.", new String[]{method.getName(), klass.getClass().getName(), ex.getMessage()});
}
}
diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/builder/AgentTransformer.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/builder/AgentTransformer.java
index bf64688cc93..dc2a36d314c 100644
--- a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/builder/AgentTransformer.java
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/builder/AgentTransformer.java
@@ -26,7 +26,6 @@ import net.bytebuddy.pool.TypePool.Default;
import net.bytebuddy.utility.JavaModule;
import org.apache.shardingsphere.agent.api.PluginConfiguration;
import org.apache.shardingsphere.agent.core.advisor.config.AdvisorConfiguration;
-import org.apache.shardingsphere.agent.core.advisor.config.AdvisorConfigurationLoader;
import org.apache.shardingsphere.agent.core.advisor.config.MethodAdvisorConfiguration;
import org.apache.shardingsphere.agent.core.builder.interceptor.AgentBuilderInterceptChainEngine;
import org.apache.shardingsphere.agent.core.builder.interceptor.impl.MethodAdvisorBuilderInterceptor;
@@ -40,6 +39,7 @@ import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.jar.JarFile;
+import java.util.logging.Level;
import java.util.logging.Logger;
/**
@@ -48,7 +48,7 @@ import java.util.logging.Logger;
@RequiredArgsConstructor
public final class AgentTransformer implements Transformer {
- private static final Logger LOGGER = Logger.getLogger(AdvisorConfigurationLoader.class.getName());
+ private static final Logger LOGGER = Logger.getLogger(AgentTransformer.class.getName());
private static final Map<AgentPluginClassLoader, TypePool> TYPE_POOL_MAP = new ConcurrentHashMap<>();
@@ -79,7 +79,7 @@ public final class AgentTransformer implements Transformer {
result.getAdvisors().add(each);
continue;
}
- LOGGER.severe(String.format("The advice class `%s` does not exist", each.getAdviceClassName()));
+ LOGGER.log(Level.SEVERE, "The advice class `{0}` does not exist", new String[]{each.getAdviceClassName()});
}
return result;
}
diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/builder/interceptor/impl/MethodAdvisorBuilderInterceptor.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/builder/interceptor/impl/MethodAdvisorBuilderInterceptor.java
index fa998efee32..f34969a3118 100644
--- a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/builder/interceptor/impl/MethodAdvisorBuilderInterceptor.java
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/builder/interceptor/impl/MethodAdvisorBuilderInterceptor.java
@@ -27,6 +27,7 @@ import org.apache.shardingsphere.agent.core.builder.interceptor.AgentBuilderInte
import org.apache.shardingsphere.agent.core.plugin.classloader.ClassLoaderContext;
import java.util.Optional;
+import java.util.logging.Level;
import java.util.logging.Logger;
/**
@@ -58,7 +59,7 @@ public final class MethodAdvisorBuilderInterceptor implements AgentBuilderInterc
// CHECKSTYLE:OFF
} catch (final Throwable ex) {
// CHECKSTYLE:ON
- LOGGER.severe(String.format("Failed to load advice class: %s, %s.", typePointcut.getTypeName(), ex.getMessage()));
+ LOGGER.log(Level.SEVERE, "Failed to load advice class: {0}, {1}", new String[]{typePointcut.getTypeName(), ex.getMessage()});
}
}
return result;
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 c36f5bbc050..bfbdbc796ca 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
@@ -29,6 +29,7 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.jar.JarFile;
+import java.util.logging.Level;
import java.util.logging.Logger;
/**
@@ -71,12 +72,12 @@ public final class PluginLifecycleServiceManager {
private static void start(final PluginConfiguration pluginConfig, final PluginLifecycleService pluginLifecycleService, final boolean isEnhancedForProxy) {
try {
- LOGGER.info(String.format("Start plugin: %s", pluginLifecycleService.getType()));
+ LOGGER.log(Level.INFO, "Start plugin: {0}", new String[]{pluginLifecycleService.getType()});
pluginLifecycleService.start(pluginConfig, isEnhancedForProxy);
// CHECKSTYLE:OFF
} catch (final Throwable ex) {
// CHECKSTYLE:ON
- LOGGER.severe(String.format("Failed to start service %s.", ex.getMessage()));
+ LOGGER.log(Level.SEVERE, "Failed to start service {0}.", new String[]{ex.getMessage()});
}
}
@@ -86,7 +87,7 @@ public final class PluginLifecycleServiceManager {
try {
each.close();
} catch (final IOException ex) {
- LOGGER.severe(String.format("Failed to close jar file %s.", ex.getMessage()));
+ LOGGER.log(Level.SEVERE, "Failed to close jar file {0}.", new String[]{ex.getMessage()});
}
});
}
diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/jar/PluginJarLoader.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/jar/PluginJarLoader.java
index 530752bc79c..197baf5e530 100644
--- a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/jar/PluginJarLoader.java
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/jar/PluginJarLoader.java
@@ -31,6 +31,7 @@ import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collection;
import java.util.LinkedList;
import java.util.jar.JarFile;
+import java.util.logging.Level;
import java.util.logging.Logger;
/**
@@ -53,7 +54,7 @@ public final class PluginJarLoader {
Collection<JarFile> result = new LinkedList<>();
for (File each : jarFiles) {
result.add(new JarFile(each, true));
- LOGGER.info(String.format("Loaded jar: %s", each.getName()));
+ LOGGER.log(Level.INFO, "Loaded jar: {0}", new String[]{each.getName()});
}
return result;
}
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 03eb3de6e3f..eddec9cca04 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
@@ -39,13 +39,8 @@ public final class AgentServiceLoader<T> {
private final Collection<T> services;
private AgentServiceLoader(final Class<T> service) {
- validate(service);
- this.services = load(service);
- }
-
- private void validate(final Class<T> service) {
- AgentPreconditions.checkNotNull(service, "SPI class is null.");
AgentPreconditions.checkArgument(service.isInterface(), String.format("SPI class `%s` is not interface.", service));
+ this.services = load(service);
}
private Collection<T> load(final Class<T> service) {
diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/util/AgentPreconditions.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/util/AgentPreconditions.java
index f6fe11baf59..e521e87174e 100644
--- a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/util/AgentPreconditions.java
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/util/AgentPreconditions.java
@@ -39,19 +39,6 @@ public final class AgentPreconditions {
}
}
- /**
- * Check not null.
- *
- * @param reference reference
- * @param errorMessage error message
- * @throws NullPointerException null pointer exception
- */
- public static void checkNotNull(final Object reference, final String errorMessage) {
- if (null == reference) {
- throw new NullPointerException(errorMessage);
- }
- }
-
/**
* Check argument.
*
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 867d6510197..392c3d1cada 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
@@ -37,13 +37,8 @@ public final class PluginServiceLoader<T> {
private final Collection<T> services;
private PluginServiceLoader(final Class<T> service) {
- validate(service);
- this.services = load(service);
- }
-
- private void validate(final Class<T> service) {
- PluginPreconditions.checkNotNull(service, "SPI class is null.");
PluginPreconditions.checkArgument(service.isInterface(), String.format("SPI class `%s` is not interface.", service));
+ this.services = load(service);
}
private Collection<T> load(final Class<T> service) {
diff --git a/agent/plugins/core/src/main/java/org/apache/shardingsphere/agent/plugin/core/util/PluginPreconditions.java b/agent/plugins/core/src/main/java/org/apache/shardingsphere/agent/plugin/core/util/PluginPreconditions.java
index 3b0a83cb45a..98e27220a49 100644
--- a/agent/plugins/core/src/main/java/org/apache/shardingsphere/agent/plugin/core/util/PluginPreconditions.java
+++ b/agent/plugins/core/src/main/java/org/apache/shardingsphere/agent/plugin/core/util/PluginPreconditions.java
@@ -26,32 +26,6 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class PluginPreconditions {
- /**
- * Check state.
- *
- * @param state state
- * @param errorMessage error message
- * @throws IllegalStateException illegal state exception
- */
- public static void checkState(final boolean state, final String errorMessage) {
- if (!state) {
- throw new IllegalStateException(errorMessage);
- }
- }
-
- /**
- * Check not null.
- *
- * @param reference reference
- * @param errorMessage error message
- * @throws NullPointerException null pointer exception
- */
- public static void checkNotNull(final Object reference, final String errorMessage) {
- if (null == reference) {
- throw new NullPointerException(errorMessage);
- }
- }
-
/**
* Check argument.
*