diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/MergeableClusterInvoker.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/MergeableClusterInvoker.java index 3b815cea39..37dbbcaace 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/MergeableClusterInvoker.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/MergeableClusterInvoker.java @@ -20,6 +20,7 @@ import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.ConfigUtils; +import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.rpc.AsyncRpcResult; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; @@ -33,7 +34,6 @@ import org.apache.dubbo.rpc.cluster.merger.MergerFactory; import java.lang.reflect.Array; import java.lang.reflect.Method; -import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -130,9 +130,7 @@ public class MergeableClusterInvoker extends AbstractClusterInvoker { throw new RpcException("Can not merge result because missing method [ " + merger + " ] in class [ " + returnType.getName() + " ]"); } - if (!Modifier.isPublic(method.getModifiers())) { - method.setAccessible(true); - } + ReflectUtils.makeAccessible(method); result = resultList.remove(0).getValue(); try { if (method.getReturnType() != void.class diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalanceTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalanceTest.java index bb6260c967..61e5fa42fe 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalanceTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalanceTest.java @@ -16,6 +16,7 @@ */ package org.apache.dubbo.rpc.cluster.loadbalance; +import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.rpc.Invoker; import org.junit.jupiter.api.Assertions; @@ -129,7 +130,7 @@ public class RoundRobinLoadBalanceTest extends LoadBalanceBaseTest { try { //change recycle time to 1 ms recycleTimeField = RoundRobinLoadBalance.class.getDeclaredField("RECYCLE_PERIOD"); - recycleTimeField.setAccessible(true); + ReflectUtils.makeAccessible(recycleTimeField); recycleTimeField.setInt(RoundRobinLoadBalance.class, 10); } catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException | SecurityException e) { Assertions.assertTrue(true, "getField failed"); diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/beanutil/JavaBeanSerializeUtil.java b/dubbo-common/src/main/java/org/apache/dubbo/common/beanutil/JavaBeanSerializeUtil.java index d37335eb64..63fec6446a 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/beanutil/JavaBeanSerializeUtil.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/beanutil/JavaBeanSerializeUtil.java @@ -318,7 +318,7 @@ public final class JavaBeanSerializeUtil { } } if (method != null) { - method.setAccessible(true); + ReflectUtils.makeAccessible(method); } return method; } @@ -341,7 +341,7 @@ public final class JavaBeanSerializeUtil { constructorArgs[i] = getConstructorArg(paramTypes[i]); } try { - constructor.setAccessible(true); + ReflectUtils.makeAccessible(constructor); return constructor.newInstance(constructorArgs); } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { LogHelper.warn(logger, e.getMessage(), e); diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/json/J2oVisitor.java b/dubbo-common/src/main/java/org/apache/dubbo/common/json/J2oVisitor.java index bc6e4e39bb..63762aebad 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/json/J2oVisitor.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/json/J2oVisitor.java @@ -17,6 +17,7 @@ package org.apache.dubbo.common.json; import org.apache.dubbo.common.bytecode.Wrapper; +import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.common.utils.Stack; import org.apache.dubbo.common.utils.StringUtils; @@ -298,9 +299,7 @@ class J2oVisitor implements JSONVisitor { if (mValue instanceof Throwable && "message".equals(name)) { try { Field field = Throwable.class.getDeclaredField("detailMessage"); - if (!field.isAccessible()) { - field.setAccessible(true); - } + ReflectUtils.makeAccessible(field); field.set(mValue, obj); } catch (NoSuchFieldException | IllegalAccessException e) { throw new ParseException(StringUtils.toString(e)); diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/FieldUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/FieldUtils.java index a6192390a9..a6f32f9d4c 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/FieldUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/FieldUtils.java @@ -95,16 +95,13 @@ public interface FieldUtils { * @return the value of the specified {@link Field} */ static T getFieldValue(Object object, Field field) { - boolean accessible = field.isAccessible(); Object value = null; try { - if (!accessible) { - field.setAccessible(true); - } + ReflectUtils.makeAccessible(field); value = field.get(object); } catch (IllegalAccessException ignored) { } finally { - field.setAccessible(accessible); + ReflectUtils.makeAccessible(field); } return (T) value; } @@ -130,17 +127,14 @@ public interface FieldUtils { * @return the previous value of the specified {@link Field} */ static T setFieldValue(Object object, Field field, T value) { - boolean accessible = field.isAccessible(); Object previousValue = null; try { - if (!accessible) { - field.setAccessible(true); - } + ReflectUtils.makeAccessible(field); previousValue = field.get(object); field.set(object, value); } catch (IllegalAccessException ignored) { } finally { - field.setAccessible(accessible); + ReflectUtils.makeAccessible(field); } return (T) previousValue; } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/MethodUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/MethodUtils.java index a24501f2d3..1938eba340 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/MethodUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/MethodUtils.java @@ -277,13 +277,8 @@ public interface MethodUtils { T value = null; try { - final boolean isAccessible = method.isAccessible(); - - if (!isAccessible) { - method.setAccessible(true); - } + ReflectUtils.makeAccessible(method); value = (T) method.invoke(object, methodParameters); - method.setAccessible(isAccessible); } catch (Exception e) { throw new IllegalArgumentException(e); } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java index 302c578998..80e4ad3b74 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java @@ -475,9 +475,7 @@ public class PojoUtils { Method method = getSetterMethod(dest.getClass(), name, value.getClass()); Field field = getField(dest.getClass(), name); if (method != null) { - if (!method.isAccessible()) { - method.setAccessible(true); - } + ReflectUtils.makeAccessible(method); Type ptype = method.getGenericParameterTypes()[0]; value = realize0(value, method.getParameterTypes()[0], ptype, history); try { @@ -504,9 +502,7 @@ public class PojoUtils { if (message instanceof String) { try { Field field = Throwable.class.getDeclaredField("detailMessage"); - if (!field.isAccessible()) { - field.setAccessible(true); - } + ReflectUtils.makeAccessible(field); field.set(dest, message); } catch (Exception e) { } @@ -585,7 +581,7 @@ public class PojoUtils { } } } - constructor.setAccessible(true); + ReflectUtils.makeAccessible(constructor); Object[] parameters = Arrays.stream(constructor.getParameterTypes()).map(PojoUtils::getDefaultValue).toArray(); return constructor.newInstance(parameters); } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) { @@ -644,7 +640,7 @@ public class PojoUtils { } try { result = cls.getDeclaredField(fieldName); - result.setAccessible(true); + ReflectUtils.makeAccessible(result); } catch (NoSuchFieldException e) { for (Field field : cls.getFields()) { if (fieldName.equals(field.getName()) && ReflectUtils.isPublicInstanceField(field)) { diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ReflectUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ReflectUtils.java index 7b57764136..d7026b02ed 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ReflectUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ReflectUtils.java @@ -1077,9 +1077,7 @@ public final class ReflectUtils { Object property = getEmptyObject(field.getType(), emptyInstances, level + 1); if (property != null) { try { - if (!field.isAccessible()) { - field.setAccessible(true); - } + ReflectUtils.makeAccessible(field); field.set(value, property); } catch (Throwable ignored) { } @@ -1168,8 +1166,7 @@ public final class ReflectUtils { || Modifier.isStatic(field.getModifiers())) { continue; } - - field.setAccessible(true); + ReflectUtils.makeAccessible(field); properties.put(field.getName(), field); } @@ -1184,7 +1181,7 @@ public final class ReflectUtils { Method[] methods = cl.getDeclaredMethods(); for (Method method : methods) { if (isBeanPropertyReadMethod(method)) { - method.setAccessible(true); + ReflectUtils.makeAccessible(method); String property = getPropertyNameFromBeanReadMethod(method); properties.put(property, method); } @@ -1349,4 +1346,52 @@ public final class ReflectUtils { method.setAccessible(true); } } + + /** + * Copy from org.springframework.util.ReflectionUtils. + * Make the given field accessible, explicitly setting it accessible if + * necessary. The {@code setAccessible(true)} method is only called + * when actually necessary, to avoid unnecessary conflicts with a JVM + * SecurityManager (if active). + * @param field the field to make accessible + * @see java.lang.reflect.Field#setAccessible + */ + @SuppressWarnings("deprecation") // on JDK 9 + public static void makeAccessible(Field field) { + if ((!Modifier.isPublic(field.getModifiers()) || + !Modifier.isPublic(field.getDeclaringClass().getModifiers()) || + Modifier.isFinal(field.getModifiers())) && !field.isAccessible()) { + field.setAccessible(true); + } + } + + /** + * Copy from org.springframework.util.ReflectionUtils. + * Make the given constructor accessible, explicitly setting it accessible + * if necessary. The {@code setAccessible(true)} method is only called + * when actually necessary, to avoid unnecessary conflicts with a JVM + * SecurityManager (if active). + * @param ctor the constructor to make accessible + * @see java.lang.reflect.Constructor#setAccessible + */ + @SuppressWarnings("deprecation") // on JDK 9 + public static void makeAccessible(Constructor ctor) { + if ((!Modifier.isPublic(ctor.getModifiers()) || + !Modifier.isPublic(ctor.getDeclaringClass().getModifiers())) && !ctor.isAccessible()) { + ctor.setAccessible(true); + } + } + + public static boolean checkZeroArgConstructor(Class clazz) { + try { + clazz.getDeclaredConstructor(); + return true; + } catch (NoSuchMethodException e) { + return false; + } + } + + public static boolean isJdk(Class clazz) { + return clazz.getName().startsWith("java.") || clazz.getName().startsWith("javax."); + } } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ProviderModel.java b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ProviderModel.java index a64020413a..8fd7a52141 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ProviderModel.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ProviderModel.java @@ -18,6 +18,7 @@ package org.apache.dubbo.rpc.model; import org.apache.dubbo.common.BaseServiceMetadata; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.config.ServiceConfigBase; import java.lang.reflect.Method; @@ -184,7 +185,7 @@ public class ProviderModel { methodsToExport = serviceInterfaceClass.getMethods(); for (Method method : methodsToExport) { - method.setAccessible(true); + ReflectUtils.makeAccessible(method); List methodModels = methods.get(method.getName()); if (methodModels == null) { diff --git a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ServiceDescriptor.java b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ServiceDescriptor.java index 1c12672761..445142c299 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ServiceDescriptor.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ServiceDescriptor.java @@ -17,6 +17,7 @@ package org.apache.dubbo.rpc.model; import org.apache.dubbo.common.utils.CollectionUtils; +import org.apache.dubbo.common.utils.ReflectUtils; import java.lang.reflect.Method; import java.util.Arrays; @@ -47,7 +48,7 @@ public class ServiceDescriptor { private void initMethods() { Method[] methodsToExport = this.serviceInterfaceClass.getMethods(); for (Method method : methodsToExport) { - method.setAccessible(true); + ReflectUtils.makeAccessible(method); List methodModels = methods.computeIfAbsent(method.getName(), (k) -> new ArrayList<>(1)); methodModels.add(new MethodDescriptor(method)); diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/ClassGeneratorTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/ClassGeneratorTest.java index c49554623e..1db84905e5 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/ClassGeneratorTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/ClassGeneratorTest.java @@ -16,6 +16,8 @@ */ package org.apache.dubbo.common.bytecode; +import org.apache.dubbo.common.utils.ReflectUtils; + import org.junit.jupiter.api.Test; import java.lang.reflect.Field; @@ -35,7 +37,7 @@ public class ClassGeneratorTest { Bean b = new Bean(); Field fname = null, fs[] = Bean.class.getDeclaredFields(); for (Field f : fs) { - f.setAccessible(true); + ReflectUtils.makeAccessible(f); if (f.getName().equals("name")) fname = f; } @@ -65,7 +67,7 @@ public class ClassGeneratorTest { Bean b = new Bean(); Field fname = null, fs[] = Bean.class.getDeclaredFields(); for (Field f : fs) { - f.setAccessible(true); + ReflectUtils.makeAccessible(f); if (f.getName().equals("name")) fname = f; } @@ -105,4 +107,4 @@ class Bean { } public static volatile String abc = "df"; -} \ No newline at end of file +} diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/config/EnvironmentConfigurationTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/config/EnvironmentConfigurationTest.java index 974e07c8a7..2987fcd482 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/config/EnvironmentConfigurationTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/config/EnvironmentConfigurationTest.java @@ -16,6 +16,8 @@ */ package org.apache.dubbo.common.config; +import org.apache.dubbo.common.utils.ReflectUtils; + import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -64,11 +66,11 @@ class EnvironmentConfigurationTest { try { Class processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment"); Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment"); - theEnvironmentField.setAccessible(true); + ReflectUtils.makeAccessible(theEnvironmentField); Map env = (Map) theEnvironmentField.get(null); env.putAll(newenv); Field theCaseInsensitiveEnvironmentField = processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment"); - theCaseInsensitiveEnvironmentField.setAccessible(true); + ReflectUtils.makeAccessible(theCaseInsensitiveEnvironmentField); Map cienv = (Map) theCaseInsensitiveEnvironmentField.get(null); cienv.putAll(newenv); } catch (NoSuchFieldException e) { @@ -77,7 +79,7 @@ class EnvironmentConfigurationTest { for (Class cl : classes) { if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) { Field field = cl.getDeclaredField("m"); - field.setAccessible(true); + ReflectUtils.makeAccessible(field); Object obj = field.get(env); Map map = (Map) obj; map.clear(); @@ -90,7 +92,7 @@ class EnvironmentConfigurationTest { private static void updateEnv(String name, String val) throws ReflectiveOperationException { Map env = System.getenv(); Field field = env.getClass().getDeclaredField("m"); - field.setAccessible(true); + ReflectUtils.makeAccessible(field); ((Map) field.get(env)).put(name, val); } /** @@ -101,4 +103,4 @@ class EnvironmentConfigurationTest { } -} \ No newline at end of file +} diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ReflectUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ReflectUtilsTest.java index d9adde8443..0132d3d05a 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ReflectUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/ReflectUtilsTest.java @@ -43,6 +43,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; public class ReflectUtilsTest { @Test @@ -256,27 +257,25 @@ public class ReflectUtilsTest { @Test public void testGetBeanPropertyFields() throws Exception { - Map map = ReflectUtils.getBeanPropertyFields(EmptyClass.class); + EmptyClass emptyClass = new EmptyClass(); + Map map = ReflectUtils.getBeanPropertyFields(emptyClass.getClass()); assertThat(map.size(), is(2)); assertThat(map, hasKey("set")); assertThat(map, hasKey("property")); for (Field f : map.values()) { - if (!f.isAccessible()) { - fail(); - } + assertDoesNotThrow(() -> f.get(emptyClass)); } } @Test public void testGetBeanPropertyReadMethods() throws Exception { - Map map = ReflectUtils.getBeanPropertyReadMethods(EmptyClass.class); + EmptyClass emptyClass = new EmptyClass(); + Map map = ReflectUtils.getBeanPropertyReadMethods(emptyClass.getClass()); assertThat(map.size(), is(2)); assertThat(map, hasKey("set")); assertThat(map, hasKey("property")); for (Method m : map.values()) { - if (!m.isAccessible()) { - fail(); - } + assertDoesNotThrow(() -> m.invoke(emptyClass)); } } @@ -439,6 +438,13 @@ public class ReflectUtilsTest { Assertions.assertEquals("S", types6[1].getTypeName()); } + @Test + public void testCheckZeroArgConstructor() { + assertTrue(ReflectUtils.checkZeroArgConstructor(String.class)); + assertTrue(ReflectUtils.checkZeroArgConstructor(Bar.class)); + assertFalse(ReflectUtils.checkZeroArgConstructor(Foo4.class)); + } + public interface TypeClass { CompletableFuture getFuture(); @@ -544,4 +550,17 @@ public class ReflectUtilsTest { } -} \ No newline at end of file + static class Foo4 { + public Foo4(int i) { + + } + } + + static class Bar { + private Bar() { + + } + } + + +} diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractConfigTest.java index 041cd75073..d82eadd650 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/AbstractConfigTest.java @@ -17,6 +17,7 @@ package org.apache.dubbo.config; import org.apache.dubbo.common.utils.ConfigUtils; +import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.config.api.Greeting; import org.apache.dubbo.config.support.Parameter; import org.apache.dubbo.config.utils.ConfigValidationUtils; @@ -902,11 +903,11 @@ public class AbstractConfigTest { try { Class processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment"); Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment"); - theEnvironmentField.setAccessible(true); + ReflectUtils.makeAccessible(theEnvironmentField); Map env = (Map) theEnvironmentField.get(null); env.putAll(newenv); Field theCaseInsensitiveEnvironmentField = processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment"); - theCaseInsensitiveEnvironmentField.setAccessible(true); + ReflectUtils.makeAccessible(theCaseInsensitiveEnvironmentField); Map cienv = (Map) theCaseInsensitiveEnvironmentField.get(null); cienv.putAll(newenv); } catch (NoSuchFieldException e) { @@ -915,7 +916,7 @@ public class AbstractConfigTest { for (Class cl : classes) { if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) { Field field = cl.getDeclaredField("m"); - field.setAccessible(true); + ReflectUtils.makeAccessible(field); Object obj = field.get(env); Map map = (Map) obj; map.clear(); diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/status/SpringStatusChecker.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/status/SpringStatusChecker.java index ee6a2e47ad..18be13c4f6 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/status/SpringStatusChecker.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/status/SpringStatusChecker.java @@ -21,6 +21,7 @@ import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.status.Status; import org.apache.dubbo.common.status.StatusChecker; +import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.config.spring.extension.SpringExtensionFactory; import org.springframework.context.ApplicationContext; @@ -79,9 +80,7 @@ public class SpringStatusChecker implements StatusChecker { } } if (method != null) { - if (!method.isAccessible()) { - method.setAccessible(true); - } + ReflectUtils.makeAccessible(method); String[] configs = (String[]) method.invoke(context, new Object[0]); if (configs != null && configs.length > 0) { for (String config : configs) { diff --git a/dubbo-configcenter/dubbo-configcenter-nacos/src/main/java/org/apache/dubbo/configcenter/support/nacos/NacosDynamicConfiguration.java b/dubbo-configcenter/dubbo-configcenter-nacos/src/main/java/org/apache/dubbo/configcenter/support/nacos/NacosDynamicConfiguration.java index cca467f7bf..cc7f0e0d2e 100644 --- a/dubbo-configcenter/dubbo-configcenter-nacos/src/main/java/org/apache/dubbo/configcenter/support/nacos/NacosDynamicConfiguration.java +++ b/dubbo-configcenter/dubbo-configcenter-nacos/src/main/java/org/apache/dubbo/configcenter/support/nacos/NacosDynamicConfiguration.java @@ -24,6 +24,7 @@ import org.apache.dubbo.common.config.configcenter.ConfigurationListener; import org.apache.dubbo.common.config.configcenter.DynamicConfiguration; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.common.utils.StringUtils; import com.alibaba.fastjson.JSON; @@ -114,7 +115,7 @@ public class NacosDynamicConfiguration implements DynamicConfiguration { HttpAgent agent = null; try { Field field = configService.getClass().getDeclaredField("agent"); - field.setAccessible(true); + ReflectUtils.makeAccessible(field); agent = (HttpAgent) field.get(configService); } catch (Exception e) { throw new IllegalStateException(e); diff --git a/dubbo-metadata/dubbo-metadata-report-failover/src/test/java/org/apache/dubbo/metadata/store/failover/FailoverMetadataReportTest.java b/dubbo-metadata/dubbo-metadata-report-failover/src/test/java/org/apache/dubbo/metadata/store/failover/FailoverMetadataReportTest.java index c7e50a84ef..b57bd23b58 100644 --- a/dubbo-metadata/dubbo-metadata-report-failover/src/test/java/org/apache/dubbo/metadata/store/failover/FailoverMetadataReportTest.java +++ b/dubbo-metadata/dubbo-metadata-report-failover/src/test/java/org/apache/dubbo/metadata/store/failover/FailoverMetadataReportTest.java @@ -18,6 +18,7 @@ package org.apache.dubbo.metadata.store.failover; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.extension.ExtensionLoader; +import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.metadata.MetadataInfo; import org.apache.dubbo.metadata.definition.model.ServiceDefinition; import org.apache.dubbo.metadata.report.MetadataReport; @@ -204,7 +205,7 @@ public class FailoverMetadataReportTest { try { Field reportCache = AbstractMetadataReportFactory.class.getDeclaredField("SERVICE_STORE_MAP"); if (!reportCache.isAccessible()) { - reportCache.setAccessible(true); + ReflectUtils.makeAccessible(reportCache); } Map serviceStore = (Map) reportCache.get(factory); if (serviceStore != null) { @@ -219,4 +220,4 @@ public class FailoverMetadataReportTest { } } -} \ No newline at end of file +} diff --git a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryDirectoryTest.java b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryDirectoryTest.java index bb1f9252d4..53e429f8c2 100644 --- a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryDirectoryTest.java +++ b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryDirectoryTest.java @@ -20,6 +20,7 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.utils.LogUtil; import org.apache.dubbo.common.utils.NetUtils; +import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.registry.NotifyListener; import org.apache.dubbo.registry.Registry; @@ -150,7 +151,7 @@ public class RegistryDirectoryTest { "foo=bar&" + REGISTER_IP_KEY + "=10.20.30.40&" + INTERFACE_KEY + "=" + service); RegistryDirectory reg = getRegistryDirectory(url); Field field = reg.getClass().getSuperclass().getSuperclass().getDeclaredField("queryMap"); - field.setAccessible(true); + ReflectUtils.makeAccessible(field); Map queryMap = (Map) field.get(reg); Assertions.assertEquals("bar", queryMap.get("foo")); URL expected = url.setProtocol(DUBBO_PROTOCOL).clearParameters() diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/handler/WrappedChannelHandlerTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/handler/WrappedChannelHandlerTest.java index 5d8c78b82b..269bc18f3c 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/handler/WrappedChannelHandlerTest.java +++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/handler/WrappedChannelHandlerTest.java @@ -18,6 +18,7 @@ package org.apache.dubbo.remoting.handler; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.RemotingException; import org.apache.dubbo.remoting.transport.dispatcher.WrappedChannelHandler; @@ -60,7 +61,7 @@ public class WrappedChannelHandlerTest { clazz = clazz.getSuperclass(); } if (field != null) { - field.setAccessible(true); + ReflectUtils.makeAccessible(field); return field.get(obj); } else { throw new NoSuchFieldException(); @@ -143,4 +144,4 @@ public class WrappedChannelHandlerTest { class BizException extends RuntimeException { private static final long serialVersionUID = -7541893754900723624L; } -} \ No newline at end of file +} diff --git a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClientWrapper.java b/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClientWrapper.java index 0de6703579..7324d8a8bf 100644 --- a/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClientWrapper.java +++ b/dubbo-remoting/dubbo-remoting-etcd3/src/main/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClientWrapper.java @@ -23,6 +23,7 @@ import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.ConcurrentHashSet; import org.apache.dubbo.common.utils.NamedThreadFactory; +import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.remoting.etcd.RetryPolicy; import org.apache.dubbo.remoting.etcd.StateListener; @@ -604,14 +605,10 @@ public class JEtcdClientWrapper { private ManagedChannel newChannel(Client client) { try { Field connectionField = client.getClass().getDeclaredField("connectionManager"); - if (!connectionField.isAccessible()) { - connectionField.setAccessible(true); - } + ReflectUtils.makeAccessible(connectionField); Object connection = connectionField.get(client); Method channel = connection.getClass().getDeclaredMethod("getChannel"); - if (!channel.isAccessible()) { - channel.setAccessible(true); - } + ReflectUtils.makeAccessible(channel); return (ManagedChannel) channel.invoke(connection); } catch (Exception e) { throw new RuntimeException("Failed to obtain connection channel from " + url.getBackupAddress(), e); diff --git a/dubbo-remoting/dubbo-remoting-etcd3/src/test/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClientTest.java b/dubbo-remoting/dubbo-remoting-etcd3/src/test/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClientTest.java index 9f91a82144..5b222d8bbb 100644 --- a/dubbo-remoting/dubbo-remoting-etcd3/src/test/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClientTest.java +++ b/dubbo-remoting/dubbo-remoting-etcd3/src/test/java/org/apache/dubbo/remoting/etcd/jetcd/JEtcdClientTest.java @@ -34,6 +34,7 @@ package org.apache.dubbo.remoting.etcd.jetcd; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.remoting.etcd.ChildListener; import com.google.protobuf.ByteString; @@ -414,10 +415,10 @@ public class JEtcdClientTest { try { // hack, use reflection to get the shared channel. Field connectionField = client.getClass().getDeclaredField("connectionManager"); - connectionField.setAccessible(true); + ReflectUtils.makeAccessible(connectionField); Object connection = connectionField.get(client); Method channelMethod = connection.getClass().getDeclaredMethod("getChannel"); - channelMethod.setAccessible(true); + ReflectUtils.makeAccessible(channelMethod); ManagedChannel channel = (ManagedChannel) channelMethod.invoke(connection); return channel; } catch (Exception e) { diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AppResponse.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AppResponse.java index 118e70151d..e49a65ffbf 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AppResponse.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AppResponse.java @@ -16,6 +16,7 @@ */ package org.apache.dubbo.rpc; + import org.apache.dubbo.rpc.proxy.InvokerInvocationHandler; import java.util.HashMap; diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericImplFilter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericImplFilter.java index f841f3fe7f..aa55ebd431 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericImplFilter.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericImplFilter.java @@ -190,9 +190,7 @@ public class GenericImplFilter implements Filter, Filter.Listener { if (targetException != null) { try { Field field = Throwable.class.getDeclaredField("detailMessage"); - if (!field.isAccessible()) { - field.setAccessible(true); - } + ReflectUtils.makeAccessible(field); field.set(targetException, exception.getExceptionMessage()); } catch (Throwable e) { logger.warn(e.getMessage(), e); diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/InvokerInvocationHandler.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/InvokerInvocationHandler.java index 6e43c52e43..e6cfafe0c2 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/InvokerInvocationHandler.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/InvokerInvocationHandler.java @@ -19,6 +19,7 @@ package org.apache.dubbo.rpc.proxy; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.rpc.Constants; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.RpcContext; @@ -45,7 +46,7 @@ public class InvokerInvocationHandler implements InvocationHandler { static { try { stackTraceField = Throwable.class.getDeclaredField("stackTrace"); - stackTraceField.setAccessible(true); + ReflectUtils.makeAccessible(stackTraceField); } catch (NoSuchFieldException e) { // ignore } diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/AccessLogFilterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/AccessLogFilterTest.java index d02971c182..97184cd3ce 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/AccessLogFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/AccessLogFilterTest.java @@ -19,6 +19,7 @@ package org.apache.dubbo.rpc.filter; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.utils.DubboAppender; import org.apache.dubbo.common.utils.LogUtil; +import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.rpc.Filter; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; @@ -64,7 +65,7 @@ public class AccessLogFilterTest { Invocation invocation = new MockInvocation(); Field field = AccessLogFilter.class.getDeclaredField("LOG_ENTRIES"); - field.setAccessible(true); + ReflectUtils.makeAccessible(field); assertTrue(((Map) field.get(AccessLogFilter.class)).isEmpty()); accessLogFilter.invoke(invoker, invocation); @@ -84,4 +85,4 @@ public class AccessLogFilterTest { accessLogFilter.invoke(invoker, invocation); } -} \ No newline at end of file +} diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/FutureFilter.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/FutureFilter.java index ee922afed3..955043e587 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/FutureFilter.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/FutureFilter.java @@ -20,6 +20,7 @@ import org.apache.dubbo.common.constants.CommonConstants; import org.apache.dubbo.common.extension.Activate; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.rpc.Filter; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; @@ -79,10 +80,8 @@ public class FutureFilter implements Filter, Filter.Listener { if (onInvokeMethod == null || onInvokeInst == null) { throw new IllegalStateException("service:" + invoker.getUrl().getServiceKey() + " has a oninvoke callback config , but no such " + (onInvokeMethod == null ? "method" : "instance") + " found. url:" + invoker.getUrl()); } - if (!onInvokeMethod.isAccessible()) { - onInvokeMethod.setAccessible(true); - } + ReflectUtils.makeAccessible(onInvokeMethod); Object[] params = invocation.getArguments(); try { onInvokeMethod.invoke(onInvokeInst, params); @@ -110,9 +109,7 @@ public class FutureFilter implements Filter, Filter.Listener { if (onReturnMethod == null || onReturnInst == null) { throw new IllegalStateException("service:" + invoker.getUrl().getServiceKey() + " has a onreturn callback config , but no such " + (onReturnMethod == null ? "method" : "instance") + " found. url:" + invoker.getUrl()); } - if (!onReturnMethod.isAccessible()) { - onReturnMethod.setAccessible(true); - } + ReflectUtils.makeAccessible(onReturnMethod); Object[] args = invocation.getArguments(); Object[] params; @@ -155,9 +152,7 @@ public class FutureFilter implements Filter, Filter.Listener { if (onthrowMethod == null || onthrowInst == null) { throw new IllegalStateException("service:" + invoker.getUrl().getServiceKey() + " has a onthrow callback config , but no such " + (onthrowMethod == null ? "method" : "instance") + " found. url:" + invoker.getUrl()); } - if (!onthrowMethod.isAccessible()) { - onthrowMethod.setAccessible(true); - } + ReflectUtils.makeAccessible(onthrowMethod); Class[] rParaTypes = onthrowMethod.getParameterTypes(); if (rParaTypes[0].isAssignableFrom(exception.getClass())) { try { diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvokerAvilableTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvokerAvilableTest.java index 6eaaf863f1..dc5fa77ff8 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvokerAvilableTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvokerAvilableTest.java @@ -20,6 +20,7 @@ package org.apache.dubbo.rpc.protocol.dubbo; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.utils.NetUtils; +import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.remoting.Constants; import org.apache.dubbo.remoting.exchange.ExchangeClient; import org.apache.dubbo.rpc.Exporter; @@ -156,7 +157,7 @@ public class DubboInvokerAvilableTest { private ExchangeClient[] getClients(DubboInvoker invoker) throws Exception { Field field = DubboInvoker.class.getDeclaredField("clients"); - field.setAccessible(true); + ReflectUtils.makeAccessible(field); ExchangeClient[] clients = (ExchangeClient[]) field.get(invoker); Assertions.assertEquals(1, clients.length); return clients; diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClientTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClientTest.java index ce82068137..b5e8066c2f 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClientTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/ReferenceCountExchangeClientTest.java @@ -21,6 +21,7 @@ import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.utils.DubboAppender; import org.apache.dubbo.common.utils.LogUtil; import org.apache.dubbo.common.utils.NetUtils; +import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.remoting.exchange.ExchangeClient; import org.apache.dubbo.rpc.Exporter; import org.apache.dubbo.rpc.Invoker; @@ -244,7 +245,7 @@ public class ReferenceCountExchangeClientTest { ReferenceCountExchangeClient client = getReferenceClient(invoker); try { Field clientField = ReferenceCountExchangeClient.class.getDeclaredField("client"); - clientField.setAccessible(true); + ReflectUtils.makeAccessible(clientField); return (ExchangeClient) clientField.get(client); } catch (Exception e) { e.printStackTrace(); @@ -278,7 +279,7 @@ public class ReferenceCountExchangeClientTest { @SuppressWarnings("rawtypes") DubboInvoker dInvoker = (DubboInvoker) ((AsyncToSyncInvoker) invoker).getInvoker(); try { Field clientField = DubboInvoker.class.getDeclaredField("clients"); - clientField.setAccessible(true); + ReflectUtils.makeAccessible(clientField); ExchangeClient[] clients = (ExchangeClient[]) clientField.get(dInvoker); List clientList = new ArrayList(clients.length); diff --git a/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftCodec.java b/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftCodec.java index 5f58ae7e22..dad896cc03 100644 --- a/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftCodec.java +++ b/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/ThriftCodec.java @@ -18,6 +18,7 @@ package org.apache.dubbo.rpc.protocol.thrift; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.utils.ClassUtils; +import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.Codec2; @@ -350,7 +351,7 @@ public class ThriftCodec implements Codec2 { try { field = clazz.getDeclaredField(fieldIdEnum.getFieldName()); - field.setAccessible(true); + ReflectUtils.makeAccessible(field); } catch (NoSuchFieldException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } diff --git a/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ServiceMethodNotFoundTest.java b/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ServiceMethodNotFoundTest.java index bb6857603e..e58623a570 100644 --- a/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ServiceMethodNotFoundTest.java +++ b/dubbo-rpc/dubbo-rpc-thrift/src/test/java/org/apache/dubbo/rpc/protocol/thrift/ServiceMethodNotFoundTest.java @@ -17,6 +17,7 @@ package org.apache.dubbo.rpc.protocol.thrift; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.rpc.gen.dubbo.$__DemoStub; import org.apache.dubbo.rpc.gen.dubbo.Demo; import org.apache.dubbo.rpc.protocol.thrift.ext.MultiServiceProcessor; @@ -46,7 +47,7 @@ public class ServiceMethodNotFoundTest extends AbstractTest { // for test Field field = processor.getClass().getSuperclass().getDeclaredField("processMap"); - field.setAccessible(true); + ReflectUtils.makeAccessible(field); Object obj = field.get(processor); @@ -132,7 +133,7 @@ public class ServiceMethodNotFoundTest extends AbstractTest { String arg = "Hello, World!"; invocation.setArguments( new Object[] { arg } ); - + invocation.setAttachment(Constants.INTERFACE_KEY, DemoImpl.class.getName()); Result result = invoker.invoke( invocation ); diff --git a/dubbo-serialization/dubbo-serialization-kryo/src/main/java/org/apache/dubbo/common/serialize/kryo/CompatibleKryo.java b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/org/apache/dubbo/common/serialize/kryo/CompatibleKryo.java index 69e4b35b16..f030186690 100644 --- a/dubbo-serialization/dubbo-serialization-kryo/src/main/java/org/apache/dubbo/common/serialize/kryo/CompatibleKryo.java +++ b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/org/apache/dubbo/common/serialize/kryo/CompatibleKryo.java @@ -18,7 +18,7 @@ package org.apache.dubbo.common.serialize.kryo; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.serialize.kryo.utils.ReflectionUtils; +import org.apache.dubbo.common.utils.ReflectUtils; import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryo.Serializer; @@ -43,7 +43,7 @@ public class CompatibleKryo extends Kryo { * default to the default serializer. * It is the responsibility of kryo to handle with every standard jdk classes, so we will just escape these classes. */ - if (!ReflectionUtils.isJdk(type) && !type.isArray() && !type.isEnum() && !ReflectionUtils.checkZeroArgConstructor(type)) { + if (!ReflectUtils.isJdk(type) && !type.isArray() && !type.isEnum() && !ReflectUtils.checkZeroArgConstructor(type)) { if (logger.isWarnEnabled()) { logger.warn(type + " has no zero-arg constructor and this will affect the serialization performance"); } diff --git a/dubbo-serialization/dubbo-serialization-kryo/src/main/java/org/apache/dubbo/common/serialize/kryo/utils/ReflectionUtils.java b/dubbo-serialization/dubbo-serialization-kryo/src/main/java/org/apache/dubbo/common/serialize/kryo/utils/ReflectionUtils.java deleted file mode 100644 index 7c3b45a816..0000000000 --- a/dubbo-serialization/dubbo-serialization-kryo/src/main/java/org/apache/dubbo/common/serialize/kryo/utils/ReflectionUtils.java +++ /dev/null @@ -1,33 +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.dubbo.common.serialize.kryo.utils; - -public abstract class ReflectionUtils { - - public static boolean checkZeroArgConstructor(Class clazz) { - try { - clazz.getDeclaredConstructor(); - return true; - } catch (NoSuchMethodException e) { - return false; - } - } - - public static boolean isJdk(Class clazz) { - return clazz.getName().startsWith("java.") || clazz.getName().startsWith("javax."); - } -} diff --git a/dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/kryo/ReflectionUtilsTest.java b/dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/kryo/ReflectionUtilsTest.java deleted file mode 100644 index ff77d879f9..0000000000 --- a/dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/kryo/ReflectionUtilsTest.java +++ /dev/null @@ -1,46 +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.dubbo.common.serialize.kryo; - -import org.apache.dubbo.common.serialize.kryo.utils.ReflectionUtils; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - - -public class ReflectionUtilsTest { - - @Test - public void test() { - assertTrue(ReflectionUtils.checkZeroArgConstructor(String.class)); - assertTrue(ReflectionUtils.checkZeroArgConstructor(Bar.class)); - assertFalse(ReflectionUtils.checkZeroArgConstructor(Foo.class)); - } - - static class Foo { - public Foo(int i) { - - } - } - - static class Bar { - private Bar() { - - } - } -}