You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by to...@apache.org on 2022/04/04 16:05:04 UTC
[shardingsphere] branch master updated: Use class.getDeclaredConstructor().newInstance() to replace deprecated class.newInstance() (#16592)
This is an automated email from the ASF dual-hosted git repository.
totalo 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 930af9b7878 Use class.getDeclaredConstructor().newInstance() to replace deprecated class.newInstance() (#16592)
930af9b7878 is described below
commit 930af9b7878175fd4db7bfa6e5e80900943f3a08
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Tue Apr 5 00:04:45 2022 +0800
Use class.getDeclaredConstructor().newInstance() to replace deprecated class.newInstance() (#16592)
---
.../org/apache/shardingsphere/agent/core/plugin/ApmPluginLoader.java | 4 ++--
.../core/plugin/interceptor/InstanceMethodAroundInterceptorTest.java | 2 +-
.../tracing/opentracing/service/OpenTracingPluginBootService.java | 2 +-
.../parser/core/featured/FeaturedDistSQLStatementParserEngine.java | 2 +-
.../sharding/classbased/ClassBasedShardingAlgorithmFactory.java | 4 ++--
.../core/check/consistency/SingleTableDataCalculatorRegistry.java | 4 ++--
.../transaction/xa/jta/datasource/swapper/DataSourceSwapper.java | 4 ++--
.../proxy/backend/text/distsql/ral/RALBackendHandlerFactory.java | 4 ++--
.../communication/jdbc/connection/JDBCBackendConnectionTest.java | 2 +-
.../org/apache/shardingsphere/spi/ShardingSphereServiceLoader.java | 4 ++--
10 files changed, 16 insertions(+), 16 deletions(-)
diff --git a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/ApmPluginLoader.java b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/ApmPluginLoader.java
index 43983af0fc9..6e0dcab4836 100644
--- a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/ApmPluginLoader.java
+++ b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/ApmPluginLoader.java
@@ -157,7 +157,7 @@ public final class ApmPluginLoader extends ClassLoader implements Closeable, Plu
}
@Override
- @SneakyThrows({ClassNotFoundException.class, IllegalAccessException.class, InstantiationException.class})
+ @SneakyThrows(ReflectiveOperationException.class)
@SuppressWarnings("unchecked")
public <T> T getOrCreateInstance(final String adviceClassName) {
if (objectPool.containsKey(adviceClassName)) {
@@ -167,7 +167,7 @@ public final class ApmPluginLoader extends ClassLoader implements Closeable, Plu
try {
Object inst = objectPool.get(adviceClassName);
if (Objects.isNull(inst)) {
- inst = Class.forName(adviceClassName, true, this).newInstance();
+ inst = Class.forName(adviceClassName, true, this).getDeclaredConstructor().newInstance();
objectPool.put(adviceClassName, inst);
}
return (T) inst;
diff --git a/shardingsphere-agent/shardingsphere-agent-core/src/test/java/org/apache/shardingsphere/agent/core/plugin/interceptor/InstanceMethodAroundInterceptorTest.java b/shardingsphere-agent/shardingsphere-agent-core/src/test/java/org/apache/shardingsphere/agent/core/plugin/interceptor/InstanceMethodAroundInterceptorTest.java
index 6424855edba..c0359f4e1b5 100644
--- a/shardingsphere-agent/shardingsphere-agent-core/src/test/java/org/apache/shardingsphere/agent/core/plugin/interceptor/InstanceMethodAroundInterceptorTest.java
+++ b/shardingsphere-agent/shardingsphere-agent-core/src/test/java/org/apache/shardingsphere/agent/core/plugin/interceptor/InstanceMethodAroundInterceptorTest.java
@@ -101,7 +101,7 @@ public final class InstanceMethodAroundInterceptorTest {
.make()
.load(new MockClassLoader())
.getLoaded()
- .newInstance();
+ .getDeclaredConstructor().newInstance();
List<String> queues = new LinkedList<>();
if ("mockWithException".equals(methodName)) {
try {
diff --git a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-tracing/shardingsphere-agent-tracing-opentracing/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/opentracing/service/OpenTracingPluginBootService.java b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-tracing/shardingsphere-agent-tracing-opentracing/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/opentracing/service/OpenTracingPluginBootService.java
index ad4895ef2ca..081bfbaf7bc 100644
--- a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-tracing/shardingsphere-agent-tracing-opentracing/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/opentracing/service/OpenTracingPluginBootService.java
+++ b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-tracing/shardingsphere-agent-tracing-opentracing/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/opentracing/service/OpenTracingPluginBootService.java
@@ -33,7 +33,7 @@ public final class OpenTracingPluginBootService implements PluginBootService {
String tracerClassName = pluginConfig.getProps().getProperty("OPENTRACING_TRACER_CLASS_NAME");
Preconditions.checkNotNull(tracerClassName, "Can not find opentracing tracer implementation in you config");
try {
- init((Tracer) Class.forName(tracerClassName).newInstance());
+ init((Tracer) Class.forName(tracerClassName).getDeclaredConstructor().newInstance());
} catch (final ReflectiveOperationException ex) {
throw new RuntimeException("Initialize opentracing tracer class failure.", ex);
}
diff --git a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/featured/FeaturedDistSQLStatementParserEngine.java b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/featured/FeaturedDistSQLStatementParserEngine.java
index e3390622cf5..9a4bf1d5908 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/featured/FeaturedDistSQLStatementParserEngine.java
+++ b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/featured/FeaturedDistSQLStatementParserEngine.java
@@ -70,7 +70,7 @@ public final class FeaturedDistSQLStatementParserEngine {
@SneakyThrows(ReflectiveOperationException.class)
@SuppressWarnings("rawtypes")
private SQLStatement getSQLStatement(final String sql, final String featureType, final ParseASTNode parseASTNode) {
- SQLVisitor visitor = FACADES.get(featureType).getVisitorClass().newInstance();
+ SQLVisitor visitor = FACADES.get(featureType).getVisitorClass().getDeclaredConstructor().newInstance();
if (parseASTNode.getRootNode() instanceof ErrorNode) {
throw new SQLParsingException("Unsupported SQL of `%s`", sql);
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmFactory.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmFactory.java
index 30c1223d724..57ce1536c40 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmFactory.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmFactory.java
@@ -41,14 +41,14 @@ public final class ClassBasedShardingAlgorithmFactory {
* @param <T> class generic type
* @return sharding algorithm instance
*/
- @SneakyThrows
+ @SneakyThrows(ReflectiveOperationException.class)
@SuppressWarnings("unchecked")
public static <T extends ShardingAlgorithm> T newInstance(final String shardingAlgorithmClassName, final Class<T> superShardingAlgorithmClass, final Properties props) {
Class<?> result = Class.forName(shardingAlgorithmClassName);
if (!superShardingAlgorithmClass.isAssignableFrom(result)) {
throw new ShardingSphereException("Class %s should be implement %s", shardingAlgorithmClassName, superShardingAlgorithmClass.getName());
}
- T instance = (T) result.newInstance();
+ T instance = (T) result.getDeclaredConstructor().newInstance();
setProperties(instance, props);
instance.init();
return instance;
diff --git a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/check/consistency/SingleTableDataCalculatorRegistry.java b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/check/consistency/SingleTableDataCalculatorRegistry.java
index cf0dcc69690..08d63c3ca1f 100644
--- a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/check/consistency/SingleTableDataCalculatorRegistry.java
+++ b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/check/consistency/SingleTableDataCalculatorRegistry.java
@@ -63,8 +63,8 @@ public final class SingleTableDataCalculatorRegistry {
SingleTableDataCalculator calculator = calculatorMap.get(databaseType);
Preconditions.checkNotNull(calculator, String.format("calculator not found for algorithmType '%s' databaseType '%s'", algorithmType, databaseType));
try {
- return calculator.getClass().newInstance();
- } catch (final InstantiationException | IllegalAccessException ex) {
+ return calculator.getClass().getDeclaredConstructor().newInstance();
+ } catch (final ReflectiveOperationException ex) {
throw new ServiceLoaderInstantiationException(calculator.getClass(), ex);
}
}
diff --git a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/DataSourceSwapper.java b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/DataSou [...]
index 72b7435bcc3..d2415e3b7a9 100644
--- a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/DataSourceSwapper.java
+++ b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/DataSourceSwapper.java
@@ -90,8 +90,8 @@ public final class DataSourceSwapper {
}
}
try {
- return (XADataSource) xaDataSourceClass.newInstance();
- } catch (final InstantiationException | IllegalAccessException ex) {
+ return (XADataSource) xaDataSourceClass.getDeclaredConstructor().newInstance();
+ } catch (final ReflectiveOperationException ex) {
throw new ShardingSphereException("Failed to instance [%s]", xaDataSourceClassName);
}
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/RALBackendHandlerFactory.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/RALBackendHandlerFactory.java
index 9010fda545c..1cd925aee50 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/RALBackendHandlerFactory.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/RALBackendHandlerFactory.java
@@ -175,8 +175,8 @@ public final class RALBackendHandlerFactory {
private static RALBackendHandler newInstance(final Class<? extends RALBackendHandler> clazz) {
try {
- return clazz.newInstance();
- } catch (final InstantiationException | IllegalAccessException ex) {
+ return clazz.getDeclaredConstructor().newInstance();
+ } catch (final ReflectiveOperationException ex) {
throw new UnsupportedOperationException(String.format("Can not find public constructor for class `%s`", clazz.getName()));
}
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/JDBCBackendConnectionTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/JDBCBackendConnectionTest.java
index 472d6db210b..53794f31133 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/JDBCBackendConnectionTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/JDBCBackendConnectionTest.java
@@ -145,7 +145,7 @@ public final class JDBCBackendConnectionTest {
Field field = ProxyContext.getInstance().getClass().getDeclaredField("backendDataSource");
field.setAccessible(true);
Class<?> clazz = field.getType();
- Object datasource = clazz.getDeclaredConstructors()[0].newInstance();
+ Object datasource = clazz.getDeclaredConstructor().newInstance();
field.set(ProxyContext.getInstance(), datasource);
}
diff --git a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/ShardingSphereServiceLoader.java b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/ShardingSphereServiceLoader.java
index f8283472b9d..28fc09e3bad 100644
--- a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/ShardingSphereServiceLoader.java
+++ b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/ShardingSphereServiceLoader.java
@@ -93,8 +93,8 @@ public final class ShardingSphereServiceLoader {
private static Object newServiceInstance(final Class<?> clazz) {
try {
- return clazz.newInstance();
- } catch (final InstantiationException | IllegalAccessException ex) {
+ return clazz.getDeclaredConstructor().newInstance();
+ } catch (final ReflectiveOperationException ex) {
throw new ServiceLoaderInstantiationException(clazz, ex);
}
}