This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch 3.1
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.1 by this push:
     new b21baffa8c Fix config absent when refresh (#11505)
b21baffa8c is described below

commit b21baffa8c792716e33b6e6a5d0d38b48b7be06c
Author: Albumen Kevin <[email protected]>
AuthorDate: Tue Feb 14 15:15:00 2023 +0800

    Fix config absent when refresh (#11505)
---
 .../org/apache/dubbo/config/AbstractConfig.java    | 53 +++++++++++-----------
 .../org/apache/dubbo/config/ServiceConfigTest.java | 12 +++++
 2 files changed, 38 insertions(+), 27 deletions(-)

diff --git 
a/dubbo-common/src/main/java/org/apache/dubbo/config/AbstractConfig.java 
b/dubbo-common/src/main/java/org/apache/dubbo/config/AbstractConfig.java
index e13935b99f..a46c033d5d 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/config/AbstractConfig.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/config/AbstractConfig.java
@@ -16,6 +16,27 @@
  */
 package org.apache.dubbo.config;
 
+import java.beans.BeanInfo;
+import java.beans.IntrospectionException;
+import java.beans.Introspector;
+import java.beans.MethodDescriptor;
+import java.beans.PropertyDescriptor;
+import java.io.Serializable;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicBoolean;
+
 import org.apache.dubbo.common.URL;
 import org.apache.dubbo.common.config.ConfigurationUtils;
 import org.apache.dubbo.common.config.Environment;
@@ -39,27 +60,6 @@ import org.apache.dubbo.rpc.model.ModuleModel;
 import org.apache.dubbo.rpc.model.ScopeModel;
 import org.apache.dubbo.rpc.model.ScopeModelUtil;
 
-import java.beans.BeanInfo;
-import java.beans.IntrospectionException;
-import java.beans.Introspector;
-import java.beans.MethodDescriptor;
-import java.beans.PropertyDescriptor;
-import java.io.Serializable;
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.atomic.AtomicBoolean;
-
 import static 
org.apache.dubbo.common.constants.LoggerCodeConstants.COMMON_FAILED_OVERRIDE_FIELD;
 import static 
org.apache.dubbo.common.constants.LoggerCodeConstants.COMMON_REFLECTIVE_OPERATION_FAILED;
 import static 
org.apache.dubbo.common.constants.LoggerCodeConstants.COMMON_UNEXPECTED_EXCEPTION;
@@ -606,7 +606,7 @@ public abstract class AbstractConfig implements 
Serializable {
                 try {
                     String propertyName = 
extractPropertyName(method.getName());
                     String getterName = 
calculatePropertyToGetter(propertyName);
-                    getterMethod = 
this.getClass().getDeclaredMethod(getterName);
+                    getterMethod = this.getClass().getMethod(getterName);
                 } catch (Exception ignore) {
                     continue;
                 }
@@ -734,13 +734,12 @@ public abstract class AbstractConfig implements 
Serializable {
 
         // loop methods, get override value and set the new value back to 
method
         List<Method> methods = MethodUtils.getMethods(obj.getClass(), method 
-> method.getDeclaringClass() != Object.class);
-        Method[] methodsList = this.getClass().getDeclaredMethods();
         for (Method method : methods) {
             if (MethodUtils.isSetter(method)) {
                 String propertyName = extractPropertyName(method.getName());
 
                 // if config mode is OVERRIDE_IF_ABSENT and property has set, 
skip
-                if (overrideIfAbsent && isPropertySet(methodsList, 
propertyName)) {
+                if (overrideIfAbsent && isPropertySet(methods, propertyName)) {
                     continue;
                 }
 
@@ -786,7 +785,7 @@ public abstract class AbstractConfig implements 
Serializable {
                 Map<String, String> oldMap = null;
                 try {
                     String getterName = 
calculatePropertyToGetter(propertyName);
-                    Method getterMethod = 
this.getClass().getDeclaredMethod(getterName);
+                    Method getterMethod = 
this.getClass().getMethod(getterName);
                     Object oldOne = getterMethod.invoke(this);
                     if (oldOne instanceof Map) {
                         oldMap = (Map) oldOne;
@@ -827,7 +826,7 @@ public abstract class AbstractConfig implements 
Serializable {
         }
     }
 
-    private boolean isPropertySet(Method[] methods, String propertyName) {
+    private boolean isPropertySet(List<Method> methods, String propertyName) {
         try {
             String getterName = calculatePropertyToGetter(propertyName);
             Method getterMethod = findGetMethod(methods, getterName);
@@ -844,7 +843,7 @@ public abstract class AbstractConfig implements 
Serializable {
         return false;
     }
 
-    private Method findGetMethod(Method[] methods, String methodName) {
+    private Method findGetMethod(List<Method> methods, String methodName) {
         for (Method method : methods) {
             if (method.getName().equals(methodName) && 
method.getParameterCount() == 0) {
                 return method;
diff --git 
a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ServiceConfigTest.java
 
b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ServiceConfigTest.java
index 8adbc61a15..0bf2ad5660 100644
--- 
a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ServiceConfigTest.java
+++ 
b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ServiceConfigTest.java
@@ -539,6 +539,18 @@ class ServiceConfigTest {
         });
     }
 
+    @Test
+    void testOverride() {
+        System.setProperty("dubbo.service.version", "TEST");
+        ServiceConfig<DemoService> serviceConfig = new ServiceConfig<>();
+        serviceConfig.setInterface(DemoService.class);
+        serviceConfig.setRef(new DemoServiceImpl());
+        serviceConfig.setVersion("1.0.0");
+        serviceConfig.refresh();
+        Assertions.assertEquals("1.0.0", serviceConfig.getVersion());
+        System.clearProperty("dubbo.service.version");
+    }
+
     @Test
     void testMappingRetry() {
         FrameworkModel frameworkModel = new FrameworkModel();

Reply via email to