Skip to content

Commit

Permalink
fix Use @SPI's wrapper method to report a null pointer exception bug (#…
Browse files Browse the repository at this point in the history
…7326)

* fix Use @SPI's wrapper method to report a null pointer exception bug. see #7176.

(cherry picked from commit df6d6ad)

* 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>
  • Loading branch information
xiaoheng1 authored Apr 11, 2021
1 parent 3a0aeb5 commit a10d7f3
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
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;
Expand Down Expand Up @@ -259,6 +260,8 @@ public List<T> getActivateExtension(URL url, String key, String group) {
*/
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();
Expand All @@ -281,10 +284,12 @@ public List<T> getActivateExtension(URL url, String[] values, String group) {
&& !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++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
/**
* 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;
}
Expand All @@ -46,15 +46,15 @@ public int compare(Object o1, Object o2) {
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;
}
Expand All @@ -65,7 +65,7 @@ public int compare(Object o1, Object o2) {
}

if (a2.applicableToCompare()) {
String n1 = extensionLoader.getExtensionName(o1.getClass());
String n1 = extensionLoader.getExtensionName(o1);
if (a2.isLess(n1)) {
return 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand Down Expand Up @@ -158,6 +159,14 @@ public void test_getExtension_WithWrapper() throws Exception {
assertEquals(echoCount2 + 1, Ext5Wrapper2.echoCount.get());
}

@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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ public void testActivateComparator(){
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));
}
}
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a10d7f3

Please sign in to comment.