You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2021/04/11 12:23:33 UTC
[dubbo] branch master updated: fix Use @SPI's wrapper method to
report a null pointer exception bug (#7326)
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 a10d7f3 fix Use @SPI's wrapper method to report a null pointer exception bug (#7326)
a10d7f3 is described below
commit a10d7f343c4bde1ebdf06170f65a3846d697b2ee
Author: xiaoheng1 <20...@qq.com>
AuthorDate: Sun Apr 11 20:23:14 2021 +0800
fix Use @SPI's wrapper method to report a null pointer exception bug (#7326)
* fix Use @SPI's wrapper method to report a null pointer exception bug. see #7176.
(cherry picked from commit df6d6adf4806bd200a5ea7bafcfd1445322b0c08)
* fix #7176 Use @SPI's wrapper method to report a null pointer exception bug
* recover wildcard import
* delete issue link
* add new spi interface.
* fix testActivateComparator test case.
Co-authored-by: xiaoheng <xiaoyu>
---
.../dubbo/common/extension/ExtensionLoader.java | 9 ++++--
.../extension/support/ActivateComparator.java | 16 +++++-----
.../common/extension/ExtensionLoaderTest.java | 9 ++++++
.../extension/activate/ActivateWrapperExt1.java | 25 ++++++++++++++++
.../activate/impl/ActivateWrapperExt1Impl1.java | 28 ++++++++++++++++++
.../activate/impl/ActivateWrapperExt1Impl2.java | 28 ++++++++++++++++++
.../activate/impl/ActivateWrapperExt1Wrapper.java | 34 ++++++++++++++++++++++
.../extension/support/ActivateComparatorTest.java | 22 +++++++-------
...o.common.extension.activate.ActivateWrapperExt1 | 3 ++
9 files changed, 153 insertions(+), 21 deletions(-)
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java
index d0881cb..88c1281 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java
@@ -48,6 +48,7 @@ import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
+import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@@ -259,6 +260,8 @@ public class ExtensionLoader<T> {
*/
public List<T> getActivateExtension(URL url, String[] values, String group) {
List<T> activateExtensions = new ArrayList<>();
+ // solve the bug of using @SPI's wrapper method to report a null pointer exception.
+ TreeMap<Class, T> activateExtensionsMap = new TreeMap<>(ActivateComparator.COMPARATOR);
List<String> names = values == null ? new ArrayList<>(0) : asList(values);
if (!names.contains(REMOVE_VALUE_PREFIX + DEFAULT_KEY)) {
getExtensionClasses();
@@ -281,10 +284,12 @@ public class ExtensionLoader<T> {
&& !names.contains(name)
&& !names.contains(REMOVE_VALUE_PREFIX + name)
&& isActive(activateValue, url)) {
- activateExtensions.add(getExtension(name));
+ activateExtensionsMap.put(getExtensionClass(name), getExtension(name));
}
}
- activateExtensions.sort(ActivateComparator.COMPARATOR);
+ if(!activateExtensionsMap.isEmpty()){
+ activateExtensions.addAll(activateExtensionsMap.values());
+ }
}
List<T> loadedExtensions = new ArrayList<>();
for (int i = 0; i < names.size(); i++) {
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/support/ActivateComparator.java b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/support/ActivateComparator.java
index 9ad1aaf..6c1eacf 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/support/ActivateComparator.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/support/ActivateComparator.java
@@ -27,12 +27,12 @@ import java.util.Comparator;
/**
* OrderComparator
*/
-public class ActivateComparator implements Comparator<Object> {
+public class ActivateComparator implements Comparator<Class> {
- public static final Comparator<Object> COMPARATOR = new ActivateComparator();
+ public static final Comparator<Class> COMPARATOR = new ActivateComparator();
@Override
- public int compare(Object o1, Object o2) {
+ public int compare(Class o1, Class o2) {
if (o1 == null && o2 == null) {
return 0;
}
@@ -46,15 +46,15 @@ public class ActivateComparator implements Comparator<Object> {
return 0;
}
- Class<?> inf = findSpi(o1.getClass());
+ Class<?> inf = findSpi(o1);
- ActivateInfo a1 = parseActivate(o1.getClass());
- ActivateInfo a2 = parseActivate(o2.getClass());
+ ActivateInfo a1 = parseActivate(o1);
+ ActivateInfo a2 = parseActivate(o2);
if ((a1.applicableToCompare() || a2.applicableToCompare()) && inf != null) {
ExtensionLoader<?> extensionLoader = ExtensionLoader.getExtensionLoader(inf);
if (a1.applicableToCompare()) {
- String n2 = extensionLoader.getExtensionName(o2.getClass());
+ String n2 = extensionLoader.getExtensionName(o2);
if (a1.isLess(n2)) {
return -1;
}
@@ -65,7 +65,7 @@ public class ActivateComparator implements Comparator<Object> {
}
if (a2.applicableToCompare()) {
- String n1 = extensionLoader.getExtensionName(o1.getClass());
+ String n1 = extensionLoader.getExtensionName(o1);
if (a2.isLess(n1)) {
return 1;
}
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoaderTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoaderTest.java
index b861fc0..c3db7a8 100644
--- a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoaderTest.java
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/ExtensionLoaderTest.java
@@ -22,6 +22,7 @@ import org.apache.dubbo.common.convert.StringToBooleanConverter;
import org.apache.dubbo.common.convert.StringToDoubleConverter;
import org.apache.dubbo.common.convert.StringToIntegerConverter;
import org.apache.dubbo.common.extension.activate.ActivateExt1;
+import org.apache.dubbo.common.extension.activate.ActivateWrapperExt1;
import org.apache.dubbo.common.extension.activate.impl.ActivateExt1Impl1;
import org.apache.dubbo.common.extension.activate.impl.GroupActivateExtImpl;
import org.apache.dubbo.common.extension.activate.impl.OldActivateExt1Impl2;
@@ -159,6 +160,14 @@ public class ExtensionLoaderTest {
}
@Test
+ public void test_getActivateExtension_WithWrapper() throws Exception {
+ URL url = URL.valueOf("test://localhost/test");
+ List<ActivateWrapperExt1> list = getExtensionLoader(ActivateWrapperExt1.class)
+ .getActivateExtension(url, new String[]{}, "order");
+ assertEquals(2, list.size());
+ }
+
+ @Test
public void test_getExtension_ExceptionNoExtension() throws Exception {
try {
getExtensionLoader(SimpleExt.class).getExtension("XXX");
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/activate/ActivateWrapperExt1.java b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/activate/ActivateWrapperExt1.java
new file mode 100644
index 0000000..1ae2322
--- /dev/null
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/activate/ActivateWrapperExt1.java
@@ -0,0 +1,25 @@
+/*
+ * 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.extension.activate;
+
+import org.apache.dubbo.common.extension.SPI;
+
+@SPI("extImp1")
+public interface ActivateWrapperExt1 {
+ String echo(String msg);
+}
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/activate/impl/ActivateWrapperExt1Impl1.java b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/activate/impl/ActivateWrapperExt1Impl1.java
new file mode 100644
index 0000000..0ff2ef4
--- /dev/null
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/activate/impl/ActivateWrapperExt1Impl1.java
@@ -0,0 +1,28 @@
+/*
+ * 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.extension.activate.impl;
+
+import org.apache.dubbo.common.extension.Activate;
+import org.apache.dubbo.common.extension.activate.ActivateWrapperExt1;
+
+@Activate(order = 1, group = {"order"})
+public class ActivateWrapperExt1Impl1 implements ActivateWrapperExt1 {
+ public String echo(String msg) {
+ return msg;
+ }
+}
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/activate/impl/ActivateWrapperExt1Impl2.java b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/activate/impl/ActivateWrapperExt1Impl2.java
new file mode 100644
index 0000000..716d815
--- /dev/null
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/activate/impl/ActivateWrapperExt1Impl2.java
@@ -0,0 +1,28 @@
+/*
+ * 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.extension.activate.impl;
+
+import org.apache.dubbo.common.extension.Activate;
+import org.apache.dubbo.common.extension.activate.ActivateWrapperExt1;
+
+@Activate(order = 2, group = {"order"})
+public class ActivateWrapperExt1Impl2 implements ActivateWrapperExt1 {
+ public String echo(String msg) {
+ return msg;
+ }
+}
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/activate/impl/ActivateWrapperExt1Wrapper.java b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/activate/impl/ActivateWrapperExt1Wrapper.java
new file mode 100644
index 0000000..671a3b9
--- /dev/null
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/activate/impl/ActivateWrapperExt1Wrapper.java
@@ -0,0 +1,34 @@
+/*
+ * 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.extension.activate.impl;
+
+import org.apache.dubbo.common.extension.activate.ActivateWrapperExt1;
+
+
+public class ActivateWrapperExt1Wrapper implements ActivateWrapperExt1 {
+ private ActivateWrapperExt1 activateWrapperExt1;
+
+ public ActivateWrapperExt1Wrapper(ActivateWrapperExt1 activateWrapperExt1) {
+ this.activateWrapperExt1 = activateWrapperExt1;
+ }
+
+ @Override
+ public String echo(String msg) {
+ return activateWrapperExt1.echo(msg);
+ }
+}
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/support/ActivateComparatorTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/support/ActivateComparatorTest.java
index 8b9a1d6..e3f4159 100644
--- a/dubbo-common/src/test/java/org/apache/dubbo/common/extension/support/ActivateComparatorTest.java
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/extension/support/ActivateComparatorTest.java
@@ -32,19 +32,19 @@ public class ActivateComparatorTest {
Filter3 f3 = new Filter3();
Filter4 f4 = new Filter4();
OldFilter5 f5 = new OldFilter5();
- List<Filter0> filters = new ArrayList<>();
- filters.add(f1);
- filters.add(f2);
- filters.add(f3);
- filters.add(f4);
- filters.add(f5);
+ List<Class> filters = new ArrayList<>();
+ filters.add(f1.getClass());
+ filters.add(f2.getClass());
+ filters.add(f3.getClass());
+ filters.add(f4.getClass());
+ filters.add(f5.getClass());
Collections.sort(filters, ActivateComparator.COMPARATOR);
- Assertions.assertEquals(f4, filters.get(0));
- Assertions.assertEquals(f5, filters.get(1));
- Assertions.assertEquals(f3, filters.get(2));
- Assertions.assertEquals(f2, filters.get(3));
- Assertions.assertEquals(f1, filters.get(4));
+ Assertions.assertEquals(f4.getClass(), filters.get(0));
+ Assertions.assertEquals(f5.getClass(), filters.get(1));
+ Assertions.assertEquals(f3.getClass(), filters.get(2));
+ Assertions.assertEquals(f2.getClass(), filters.get(3));
+ Assertions.assertEquals(f1.getClass(), filters.get(4));
}
}
diff --git a/dubbo-common/src/test/resources/META-INF/dubbo/internal/org.apache.dubbo.common.extension.activate.ActivateWrapperExt1 b/dubbo-common/src/test/resources/META-INF/dubbo/internal/org.apache.dubbo.common.extension.activate.ActivateWrapperExt1
new file mode 100644
index 0000000..78e4606
--- /dev/null
+++ b/dubbo-common/src/test/resources/META-INF/dubbo/internal/org.apache.dubbo.common.extension.activate.ActivateWrapperExt1
@@ -0,0 +1,3 @@
+wrapper1=org.apache.dubbo.common.extension.activate.impl.ActivateWrapperExt1Wrapper
+extImp1=org.apache.dubbo.common.extension.activate.impl.ActivateWrapperExt1Impl1
+extImp2=org.apache.dubbo.common.extension.activate.impl.ActivateWrapperExt1Impl2
\ No newline at end of file