Reactor some newInstance method (#11686)

This commit is contained in:
尔等同学 2023-03-03 06:43:37 +08:00 committed by GitHub
parent ef29a41e58
commit c272af38c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 58 additions and 56 deletions

View File

@ -75,7 +75,7 @@ public class LoggerFactory {
// try to use the first available adapter
for (Class<? extends LoggerAdapter> clazz : candidates) {
try {
LoggerAdapter loggerAdapter = clazz.getConstructor().newInstance();
LoggerAdapter loggerAdapter = clazz.getDeclaredConstructor().newInstance();
loggerAdapter.getLogger(LoggerFactory.class);
if (loggerAdapter.isConfigured()) {
setLoggerAdapter(loggerAdapter);
@ -93,7 +93,7 @@ public class LoggerFactory {
System.err.println("Dubbo: Unable to find a proper configured logger to log out.");
for (Class<? extends LoggerAdapter> clazz : candidates) {
try {
LoggerAdapter loggerAdapter = clazz.getConstructor().newInstance();
LoggerAdapter loggerAdapter = clazz.getDeclaredConstructor().newInstance();
loggerAdapter.getLogger(LoggerFactory.class);
setLoggerAdapter(loggerAdapter);
found = true;
@ -221,7 +221,7 @@ public class LoggerFactory {
List<String> result = new LinkedList<>();
for (Map.Entry<Class<? extends LoggerAdapter>, String> entry : candidates.entrySet()) {
try {
LoggerAdapter loggerAdapter = entry.getKey().getConstructor().newInstance();
LoggerAdapter loggerAdapter = entry.getKey().getDeclaredConstructor().newInstance();
loggerAdapter.getLogger(LoggerFactory.class);
result.add(entry.getValue());
} catch (Exception ignored) {

View File

@ -204,7 +204,7 @@ public class CompatibleTypeUtils {
}
if (!type.isInterface()) {
try {
Collection result = (Collection) type.newInstance();
Collection result = (Collection) type.getDeclaredConstructor().newInstance();
result.addAll(collection);
return result;
} catch (Throwable ignored) {
@ -222,8 +222,8 @@ public class CompatibleTypeUtils {
Collection collection;
if (!type.isInterface()) {
try {
collection = (Collection) type.newInstance();
} catch (Throwable e) {
collection = (Collection) type.getDeclaredConstructor().newInstance();
} catch (Exception e) {
collection = new ArrayList<Object>(length);
}
} else if (type == Set.class) {

View File

@ -275,7 +275,7 @@ public class PojoUtils {
}
if (!type.isInterface() && !Modifier.isAbstract(type.getModifiers())) {
try {
return (Collection<Object>) type.newInstance();
return (Collection<Object>) type.getDeclaredConstructor().newInstance();
} catch (Exception e) {
// ignore
}
@ -306,7 +306,7 @@ public class PojoUtils {
result = new ConcurrentSkipListMap();
} else {
try {
result = cl.newInstance();
result = cl.getDeclaredConstructor().newInstance();
} catch (Exception e) { /* ignore */ }
if (result == null) {
@ -434,7 +434,7 @@ public class PojoUtils {
// when return type is not the subclass of return type from the signature and not an interface
if (!type.isInterface() && !type.isAssignableFrom(pojo.getClass())) {
try {
map = (Map<Object, Object>) type.newInstance();
map = (Map<Object, Object>) type.getDeclaredConstructor().newInstance();
Map<Object, Object> mapPojo = (Map<Object, Object>) pojo;
map.putAll(mapPojo);
if (GENERIC_WITH_CLZ) {
@ -596,7 +596,7 @@ public class PojoUtils {
private static Object newInstance(Class<?> cls) {
try {
return cls.newInstance();
return cls.getDeclaredConstructor().newInstance();
} catch (Throwable t) {
Constructor<?>[] constructors = cls.getDeclaredConstructors();
/*

View File

@ -538,7 +538,7 @@ public abstract class AbstractConfigManager extends LifecycleAdapter {
}
private <T extends AbstractConfig> T createConfig(Class<T> cls, ScopeModel scopeModel) throws ReflectiveOperationException {
T config = cls.newInstance();
T config = cls.getDeclaredConstructor().newInstance();
config.setScopeModel(scopeModel);
return config;
}

View File

@ -166,7 +166,7 @@ class ClassGeneratorTest {
cl.getField("FNAME").set(null, fname);
System.out.println(cl.getName());
Builder<String> builder = (Builder<String>) cl.newInstance();
Builder<String> builder = (Builder<String>) cl.getDeclaredConstructor().newInstance();
System.out.println(b.getName());
builder.setName(b, "ok");
System.out.println(b.getName());
@ -192,7 +192,7 @@ class ClassGeneratorTest {
cl.getField("FNAME").set(null, fname);
System.out.println(cl.getName());
Builder<String> builder = (Builder<String>) cl.newInstance();
Builder<String> builder = (Builder<String>) cl.getDeclaredConstructor().newInstance();
System.out.println(b.getName());
builder.setName(b, "ok");
System.out.println(b.getName());
@ -213,4 +213,4 @@ class Bean {
}
public static volatile String abc = "df";
}
}

View File

@ -29,8 +29,8 @@ class AdaptiveCompilerTest extends JavaCodeTest {
AdaptiveCompiler compiler = new AdaptiveCompiler();
compiler.setFrameworkModel(FrameworkModel.defaultModel());
Class<?> clazz = compiler.compile(JavaCodeTest.class, getSimpleCode(), AdaptiveCompiler.class.getClassLoader());
HelloService helloService = (HelloService) clazz.newInstance();
HelloService helloService = (HelloService) clazz.getDeclaredConstructor().newInstance();
Assertions.assertEquals("Hello world!", helloService.sayHello());
}
}
}

View File

@ -30,7 +30,7 @@ class JavassistCompilerTest extends JavaCodeTest {
Class<?> clazz = compiler.compile(JavaCodeTest.class, getSimpleCode(), JavassistCompiler.class.getClassLoader());
// Because javassist compiles using the caller class loader, we should't use HelloService directly
Object instance = clazz.newInstance();
Object instance = clazz.getDeclaredConstructor().newInstance();
Method sayHello = instance.getClass().getMethod("sayHello");
Assertions.assertEquals("Hello world!", sayHello.invoke(instance));
}
@ -48,7 +48,7 @@ class JavassistCompilerTest extends JavaCodeTest {
Assertions.assertThrows(RuntimeException.class, () -> compiler.compile(null, getSimpleCodeWithoutPackage(), JavassistCompiler.class.getClassLoader()));
} else {
Class<?> clazz = compiler.compile(null, getSimpleCodeWithoutPackage(), JavassistCompiler.class.getClassLoader());
Object instance = clazz.newInstance();
Object instance = clazz.getDeclaredConstructor().newInstance();
Method sayHello = instance.getClass().getMethod("sayHello");
Assertions.assertEquals("Hello world!", sayHello.invoke(instance));
}
@ -59,7 +59,7 @@ class JavassistCompilerTest extends JavaCodeTest {
Assertions.assertThrows(IllegalStateException.class, () -> {
JavassistCompiler compiler = new JavassistCompiler();
Class<?> clazz = compiler.compile(JavaCodeTest.class, getSimpleCodeWithSyntax0(), JavassistCompiler.class.getClassLoader());
Object instance = clazz.newInstance();
Object instance = clazz.getDeclaredConstructor().newInstance();
Method sayHello = instance.getClass().getMethod("sayHello");
Assertions.assertEquals("Hello world!", sayHello.invoke(instance));
});
@ -69,7 +69,7 @@ class JavassistCompilerTest extends JavaCodeTest {
void testCompileJavaClassWithImport() throws Exception {
JavassistCompiler compiler = new JavassistCompiler();
Class<?> clazz = compiler.compile(JavaCodeTest.class, getSimpleCodeWithImports(), JavassistCompiler.class.getClassLoader());
Object instance = clazz.newInstance();
Object instance = clazz.getDeclaredConstructor().newInstance();
Method sayHello = instance.getClass().getMethod("sayHello");
Assertions.assertEquals("Hello world!", sayHello.invoke(instance));
}
@ -78,7 +78,7 @@ class JavassistCompilerTest extends JavaCodeTest {
void testCompileJavaClassWithExtends() throws Exception {
JavassistCompiler compiler = new JavassistCompiler();
Class<?> clazz = compiler.compile(JavaCodeTest.class, getSimpleCodeWithWithExtends(), JavassistCompiler.class.getClassLoader());
Object instance = clazz.newInstance();
Object instance = clazz.getDeclaredConstructor().newInstance();
Method sayHello = instance.getClass().getMethod("sayHello");
Assertions.assertEquals("Hello world3!", sayHello.invoke(instance));
}

View File

@ -27,7 +27,7 @@ class JdkCompilerTest extends JavaCodeTest {
void test_compileJavaClass() throws Exception {
JdkCompiler compiler = new JdkCompiler();
Class<?> clazz = compiler.compile(JavaCodeTest.class, getSimpleCode(), JdkCompiler.class.getClassLoader());
Object instance = clazz.newInstance();
Object instance = clazz.getDeclaredConstructor().newInstance();
Method sayHello = instance.getClass().getMethod("sayHello");
Assertions.assertEquals("Hello world!", sayHello.invoke(instance));
}
@ -37,7 +37,7 @@ class JdkCompilerTest extends JavaCodeTest {
Assertions.assertThrows(IllegalStateException.class, () -> {
JdkCompiler compiler = new JdkCompiler();
Class<?> clazz = compiler.compile(JavaCodeTest.class, getSimpleCodeWithoutPackage(), JdkCompiler.class.getClassLoader());
Object instance = clazz.newInstance();
Object instance = clazz.getDeclaredConstructor().newInstance();
Method sayHello = instance.getClass().getMethod("sayHello");
Assertions.assertEquals("Hello world!", sayHello.invoke(instance));
});
@ -48,7 +48,7 @@ class JdkCompilerTest extends JavaCodeTest {
Assertions.assertThrows(IllegalStateException.class, () -> {
JdkCompiler compiler = new JdkCompiler();
Class<?> clazz = compiler.compile(JavaCodeTest.class, getSimpleCodeWithSyntax(), JdkCompiler.class.getClassLoader());
Object instance = clazz.newInstance();
Object instance = clazz.getDeclaredConstructor().newInstance();
Method sayHello = instance.getClass().getMethod("sayHello");
Assertions.assertEquals("Hello world!", sayHello.invoke(instance));
});
@ -58,7 +58,7 @@ class JdkCompilerTest extends JavaCodeTest {
void test_compileJavaClass_java8() throws Exception {
JdkCompiler compiler = new JdkCompiler("1.8");
Class<?> clazz = compiler.compile(JavaCodeTest.class, getSimpleCode(), JdkCompiler.class.getClassLoader());
Object instance = clazz.newInstance();
Object instance = clazz.getDeclaredConstructor().newInstance();
Method sayHello = instance.getClass().getMethod("sayHello");
Assertions.assertEquals("Hello world!", sayHello.invoke(instance));
}
@ -68,7 +68,7 @@ class JdkCompilerTest extends JavaCodeTest {
Assertions.assertThrows(IllegalStateException.class, () -> {
JdkCompiler compiler = new JdkCompiler("1.8");
Class<?> clazz = compiler.compile(JavaCodeTest.class, getSimpleCodeWithoutPackage(), JdkCompiler.class.getClassLoader());
Object instance = clazz.newInstance();
Object instance = clazz.getDeclaredConstructor().newInstance();
Method sayHello = instance.getClass().getMethod("sayHello");
Assertions.assertEquals("Hello world!", sayHello.invoke(instance));
});
@ -79,7 +79,7 @@ class JdkCompilerTest extends JavaCodeTest {
Assertions.assertThrows(IllegalStateException.class, () -> {
JdkCompiler compiler = new JdkCompiler("1.8");
Class<?> clazz = compiler.compile(JavaCodeTest.class, getSimpleCodeWithSyntax(), JdkCompiler.class.getClassLoader());
Object instance = clazz.newInstance();
Object instance = clazz.getDeclaredConstructor().newInstance();
Method sayHello = instance.getClass().getMethod("sayHello");
Assertions.assertEquals("Hello world!", sayHello.invoke(instance));
});

View File

@ -31,6 +31,7 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.lang.reflect.InvocationTargetException;
import java.util.stream.Stream;
import static org.hamcrest.CoreMatchers.is;
@ -49,8 +50,8 @@ class LoggerAdapterTest {
@ParameterizedTest
@MethodSource("data")
public void testGetLogger(Class<? extends LoggerAdapter> loggerAdapterClass, Class<? extends Logger> loggerClass) throws IllegalAccessException, InstantiationException {
LoggerAdapter loggerAdapter = loggerAdapterClass.newInstance();
void testGetLogger(Class<? extends LoggerAdapter> loggerAdapterClass, Class<? extends Logger> loggerClass) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
LoggerAdapter loggerAdapter = loggerAdapterClass.getDeclaredConstructor().newInstance();
Logger logger = loggerAdapter.getLogger(this.getClass());
assertThat(logger.getClass().isAssignableFrom(loggerClass), is(true));
@ -61,11 +62,11 @@ class LoggerAdapterTest {
@ParameterizedTest
@MethodSource("data")
public void testLevel(Class<? extends LoggerAdapter> loggerAdapterClass) throws IllegalAccessException, InstantiationException {
LoggerAdapter loggerAdapter = loggerAdapterClass.newInstance();
void testLevel(Class<? extends LoggerAdapter> loggerAdapterClass) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
LoggerAdapter loggerAdapter = loggerAdapterClass.getDeclaredConstructor().newInstance();
for (Level targetLevel : Level.values()) {
loggerAdapter.setLevel(targetLevel);
assertThat(loggerAdapter.getLevel(), is(targetLevel));
}
}
}
}

View File

@ -26,6 +26,7 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.lang.reflect.InvocationTargetException;
import java.util.stream.Stream;
import static org.hamcrest.CoreMatchers.not;
@ -47,8 +48,8 @@ class LoggerTest {
@ParameterizedTest
@MethodSource("data")
public void testAllLogMethod(Class<? extends LoggerAdapter> loggerAdapter) throws Exception {
LoggerAdapter adapter = loggerAdapter.newInstance();
void testAllLogMethod(Class<? extends LoggerAdapter> loggerAdapter) throws Exception {
LoggerAdapter adapter = loggerAdapter.getDeclaredConstructor().newInstance();
adapter.setLevel(Level.ALL);
Logger logger = adapter.getLogger(this.getClass());
logger.error("error");
@ -72,8 +73,8 @@ class LoggerTest {
@ParameterizedTest
@MethodSource("data")
public void testLevelEnable(Class<? extends LoggerAdapter> loggerAdapter) throws IllegalAccessException, InstantiationException {
LoggerAdapter adapter = loggerAdapter.newInstance();
void testLevelEnable(Class<? extends LoggerAdapter> loggerAdapter) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
LoggerAdapter adapter = loggerAdapter.getDeclaredConstructor().newInstance();
adapter.setLevel(Level.ALL);
Logger logger = adapter.getLogger(this.getClass());
assertThat(logger.isWarnEnabled(), not(nullValue()));
@ -82,4 +83,4 @@ class LoggerTest {
assertThat(logger.isInfoEnabled(), not(nullValue()));
assertThat(logger.isDebugEnabled(), not(nullValue()));
}
}
}

View File

@ -46,14 +46,14 @@ import static org.junit.jupiter.api.Assertions.fail;
class ReflectUtilsTest {
@Test
void testIsPrimitives() throws Exception {
void testIsPrimitives() {
assertTrue(ReflectUtils.isPrimitives(boolean[].class));
assertTrue(ReflectUtils.isPrimitives(byte.class));
assertFalse(ReflectUtils.isPrimitive(Map[].class));
}
@Test
void testIsPrimitive() throws Exception {
void testIsPrimitive() {
assertTrue(ReflectUtils.isPrimitive(boolean.class));
assertTrue(ReflectUtils.isPrimitive(String.class));
assertTrue(ReflectUtils.isPrimitive(Boolean.class));
@ -64,7 +64,7 @@ class ReflectUtilsTest {
}
@Test
void testGetBoxedClass() throws Exception {
void testGetBoxedClass() {
assertThat(ReflectUtils.getBoxedClass(int.class), sameInstance(Integer.class));
assertThat(ReflectUtils.getBoxedClass(boolean.class), sameInstance(Boolean.class));
assertThat(ReflectUtils.getBoxedClass(long.class), sameInstance(Long.class));
@ -544,4 +544,4 @@ class ReflectUtilsTest {
}
}
}

View File

@ -1005,7 +1005,7 @@ class AbstractConfigTest {
ModuleConfig.class, SslConfig.class, MetricsConfig.class, MonitorConfig.class, MethodConfig.class);
for (Class<? extends AbstractConfig> configClass : configClasses) {
AbstractConfig config = configClass.newInstance();
AbstractConfig config = configClass.getDeclaredConstructor().newInstance();
Map<String, String> metaData = config.getMetaData();
Assertions.assertEquals(0, metaData.size(), "Expect empty metadata for new instance but found: "+metaData +" of "+configClass.getSimpleName());
System.out.println(configClass.getSimpleName() + " metadata is checked.");

View File

@ -1070,7 +1070,7 @@ class ReferenceConfigTest {
classLoader1.loadedClass.put(resultClazzCustom1.getName(), resultClazzCustom1);
AtomicReference innerRequestReference = new AtomicReference();
AtomicReference innerResultReference = new AtomicReference();
innerResultReference.set(resultClazzCustom1.newInstance());
innerResultReference.set(resultClazzCustom1.getDeclaredConstructor().newInstance());
Constructor<?> declaredConstructor = clazz1impl.getDeclaredConstructor(AtomicReference.class, AtomicReference.class);
ServiceConfig serviceConfig = new ServiceConfig<>();
@ -1098,14 +1098,14 @@ class ReferenceConfigTest {
java.lang.reflect.Method callBean1 = object1.getClass().getDeclaredMethod("call", requestClazzOrigin);
callBean1.setAccessible(true);
Object result1 = callBean1.invoke(object1, requestClazzCustom2.newInstance());
Object result1 = callBean1.invoke(object1, requestClazzCustom2.getDeclaredConstructor().newInstance());
Assertions.assertEquals(resultClazzCustom3, result1.getClass());
Assertions.assertNotEquals(classLoader2, result1.getClass().getClassLoader());
Assertions.assertEquals(classLoader1, innerRequestReference.get().getClass().getClassLoader());
Thread.currentThread().setContextClassLoader(classLoader1);
callBean1.invoke(object1, requestClazzCustom2.newInstance());
callBean1.invoke(object1, requestClazzCustom2.getDeclaredConstructor().newInstance());
Assertions.assertEquals(classLoader1, Thread.currentThread().getContextClassLoader());
applicationModel.destroy();

View File

@ -111,7 +111,7 @@ public class JValidator implements Validator {
} catch (ClassNotFoundException e) {
parameterClass = generateMethodParameterClass(clazz, method, parameterClassName);
}
Object parameterBean = parameterClass.newInstance();
Object parameterBean = parameterClass.getDeclaredConstructor().newInstance();
for (int i = 0; i < args.length; i++) {
Field field = parameterClass.getField(method.getName() + "Argument" + i);
field.set(parameterBean, args[i]);

View File

@ -111,7 +111,7 @@ public class JValidatorNew implements Validator {
} catch (ClassNotFoundException e) {
parameterClass = generateMethodParameterClass(clazz, method, parameterClassName);
}
Object parameterBean = parameterClass.newInstance();
Object parameterBean = parameterClass.getDeclaredConstructor().newInstance();
for (int i = 0; i < args.length; i++) {
Field field = parameterClass.getField(method.getName() + "Argument" + i);
field.set(parameterBean, args[i]);

View File

@ -103,7 +103,7 @@ public class ObjectMapperCodec {
for (String moduleClassName : jacksonModuleClassNameList) {
try {
SimpleModule objectMapperModule = (SimpleModule) ClassUtils.forName(moduleClassName,
ObjectMapperCodec.class.getClassLoader()).newInstance();
ObjectMapperCodec.class.getClassLoader()).getDeclaredConstructor().newInstance();
mapper.registerModule(objectMapperModule);
} catch (Throwable ex) {

View File

@ -67,7 +67,7 @@ class JettyLoggerAdapterTest {
void testSuccessLogger() throws Exception{
Logger successLogger = mock(Logger.class);
Class<?> clazz = Class.forName("org.apache.dubbo.remoting.http.jetty.JettyLoggerAdapter");
JettyLoggerAdapter jettyLoggerAdapter = (JettyLoggerAdapter) clazz.newInstance();
JettyLoggerAdapter jettyLoggerAdapter = (JettyLoggerAdapter) clazz.getDeclaredConstructor().newInstance();
Field loggerField = clazz.getDeclaredField("logger");
loggerField.setAccessible(true);
@ -116,7 +116,7 @@ class JettyLoggerAdapterTest {
@Test
void testLoggerFormat() throws Exception{
Class<?> clazz = Class.forName("org.apache.dubbo.remoting.http.jetty.JettyLoggerAdapter");
Object newInstance = clazz.newInstance();
Object newInstance = clazz.getDeclaredConstructor().newInstance();
Method method = clazz.getDeclaredMethod("format", String.class, Object[].class);
method.setAccessible(true);

View File

@ -34,7 +34,7 @@ public class RpcMessageHandler implements Replier<RpcMessage> {
String impl = service + "Impl";
try {
Class<?> cl = Thread.currentThread().getContextClassLoader().loadClass(impl);
return cl.newInstance();
return cl.getDeclaredConstructor().newInstance();
} catch (Exception e) {
e.printStackTrace();
}

View File

@ -155,7 +155,7 @@ public class GenericImplFilter implements Filter, Filter.Listener {
// find the real interface from url
String realInterface = invoker.getUrl().getParameter(Constants.INTERFACE);
invokerInterface = ReflectUtils.forName(realInterface);
} catch (Throwable e) {
} catch (Exception e) {
// ignore
}
}
@ -186,7 +186,7 @@ public class GenericImplFilter implements Filter, Filter.Listener {
Throwable targetException = null;
Throwable lastException = null;
try {
targetException = (Throwable) clazz.newInstance();
targetException = (Throwable) clazz.getDeclaredConstructor().newInstance();
} catch (Throwable e) {
lastException = e;
for (Constructor<?> constructor : clazz.getConstructors()) {

View File

@ -75,7 +75,7 @@ class InjvmClassLoaderTest {
// AtomicReference to cache request/response of provider
AtomicReference innerRequestReference = new AtomicReference();
AtomicReference innerResultReference = new AtomicReference();
innerResultReference.set(resultClazzCustom1.newInstance());
innerResultReference.set(resultClazzCustom1.getDeclaredConstructor().newInstance());
Constructor<?> declaredConstructor = clazz1impl.getDeclaredConstructor(AtomicReference.class, AtomicReference.class);
@ -116,7 +116,7 @@ class InjvmClassLoaderTest {
java.lang.reflect.Method callBean1 = object1.getClass().getDeclaredMethod("call", requestClazzOrigin);
callBean1.setAccessible(true);
Object result1 = callBean1.invoke(object1, requestClazzCustom2.newInstance());
Object result1 = callBean1.invoke(object1, requestClazzCustom2.getDeclaredConstructor().newInstance());
// invoke result should load from classLoader3 ( sub classLoader of classLoader2 --> consumer side classLoader)
Assertions.assertEquals(resultClazzCustom3, result1.getClass());

View File

@ -35,7 +35,7 @@ public class MultiValueCreator {
public static Object createMultiValueMap() {
try {
return multiValueMapClass.newInstance();
return multiValueMapClass.getDeclaredConstructor().newInstance();
} catch (Exception e) {
}