This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/master by this push:
new 2062cdb [ISSUE-#6904] Fix reflect invoke error in
org.apache.dubbo.common.utils.PojoUtils#generalize (#6905)
2062cdb is described below
commit 2062cdb2c537000c86555570c8d6c894f88bb7b7
Author: 赵延 <[email protected]>
AuthorDate: Sat Mar 6 21:20:02 2021 +0800
[ISSUE-#6904] Fix reflect invoke error in
org.apache.dubbo.common.utils.PojoUtils#generalize (#6905)
* fix when the class is not public static, reflect invoke failed
* add doc
---
.../org/apache/dubbo/common/utils/PojoUtils.java | 1 +
.../apache/dubbo/common/utils/ReflectUtils.java | 19 ++++++++-
.../common/PojoUtilsForNonPublicStaticTest.java | 49 ++++++++++++++++++++++
3 files changed, 68 insertions(+), 1 deletion(-)
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 4514dc8..302c578 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
@@ -173,6 +173,7 @@ public class PojoUtils {
}
for (Method method : pojo.getClass().getMethods()) {
if (ReflectUtils.isBeanPropertyReadMethod(method)) {
+ ReflectUtils.makeAccessible(method);
try {
map.put(ReflectUtils.getPropertyNameFromBeanReadMethod(method),
generalize(method.invoke(pojo), history));
} catch (Exception e) {
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 5007287..7b57764 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
@@ -1332,4 +1332,21 @@ public final class ReflectUtils {
return types;
}
-}
\ No newline at end of file
+
+ /**
+ * Copy from org.springframework.util.ReflectionUtils.
+ * Make the given method 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 method the method to make accessible
+ * @see java.lang.reflect.Method#setAccessible
+ */
+ @SuppressWarnings("deprecation") // on JDK 9
+ public static void makeAccessible(Method method) {
+ if ((!Modifier.isPublic(method.getModifiers()) ||
+ !Modifier.isPublic(method.getDeclaringClass().getModifiers()))
&& !method.isAccessible()) {
+ method.setAccessible(true);
+ }
+ }
+}
diff --git
a/dubbo-common/src/test/java/org/apache/dubbo/common/PojoUtilsForNonPublicStaticTest.java
b/dubbo-common/src/test/java/org/apache/dubbo/common/PojoUtilsForNonPublicStaticTest.java
new file mode 100644
index 0000000..5fe175b
--- /dev/null
+++
b/dubbo-common/src/test/java/org/apache/dubbo/common/PojoUtilsForNonPublicStaticTest.java
@@ -0,0 +1,49 @@
+/*
+ * 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;
+
+import org.apache.dubbo.common.utils.PojoUtils;
+import org.junit.jupiter.api.Test;
+
+public class PojoUtilsForNonPublicStaticTest {
+
+ @Test
+ public void testNonPublicStaticClass() {
+ NonPublicStaticData nonPublicStaticData = new
NonPublicStaticData("horizon");
+ PojoUtils.generalize(nonPublicStaticData);
+ }
+
+ /**
+ * the static class need is not same package with PojoUtils, so define it
here.
+ */
+ static class NonPublicStaticData {
+
+ private String name;
+
+ public NonPublicStaticData(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+ }
+}