From e2941a5d294d3ffd95f2798e9a344e63caa89e75 Mon Sep 17 00:00:00 2001 From: duxd Date: Thu, 31 Jan 2019 19:40:00 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../extension/support/ActivateComparator.java | 180 +++++++++--------- 1 file changed, 94 insertions(+), 86 deletions(-) diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/support/ActivateComparator.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/support/ActivateComparator.java index 7cf7a1b3716..778170c0387 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/support/ActivateComparator.java +++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/support/ActivateComparator.java @@ -1,86 +1,94 @@ -/* - * 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 com.alibaba.dubbo.common.extension.support; - -import com.alibaba.dubbo.common.extension.Activate; -import com.alibaba.dubbo.common.extension.ExtensionLoader; -import com.alibaba.dubbo.common.extension.SPI; - -import java.util.Comparator; - -/** - * OrderComparetor - */ -public class ActivateComparator implements Comparator { - - public static final Comparator COMPARATOR = new ActivateComparator(); - - @Override - public int compare(Object o1, Object o2) { - if (o1 == null && o2 == null) { - return 0; - } - if (o1 == null) { - return -1; - } - if (o2 == null) { - return 1; - } - if (o1.equals(o2)) { - return 0; - } - Activate a1 = o1.getClass().getAnnotation(Activate.class); - Activate a2 = o2.getClass().getAnnotation(Activate.class); - if ((a1.before().length > 0 || a1.after().length > 0 - || a2.before().length > 0 || a2.after().length > 0) - && o1.getClass().getInterfaces().length > 0 - && o1.getClass().getInterfaces()[0].isAnnotationPresent(SPI.class)) { - ExtensionLoader extensionLoader = ExtensionLoader.getExtensionLoader(o1.getClass().getInterfaces()[0]); - if (a1.before().length > 0 || a1.after().length > 0) { - String n2 = extensionLoader.getExtensionName(o2.getClass()); - for (String before : a1.before()) { - if (before.equals(n2)) { - return -1; - } - } - for (String after : a1.after()) { - if (after.equals(n2)) { - return 1; - } - } - } - if (a2.before().length > 0 || a2.after().length > 0) { - String n1 = extensionLoader.getExtensionName(o1.getClass()); - for (String before : a2.before()) { - if (before.equals(n1)) { - return 1; - } - } - for (String after : a2.after()) { - if (after.equals(n1)) { - return -1; - } - } - } - } - int n1 = a1 == null ? 0 : a1.order(); - int n2 = a2 == null ? 0 : a2.order(); - // never return 0 even if n1 equals n2, otherwise, o1 and o2 will override each other in collection like HashSet - return n1 > n2 ? 1 : -1; - } - -} +/* + * 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 com.alibaba.dubbo.common.extension.support; + +import com.alibaba.dubbo.common.extension.Activate; +import com.alibaba.dubbo.common.extension.ExtensionLoader; +import com.alibaba.dubbo.common.extension.SPI; + +import java.util.Comparator; + +/** + * OrderComparetor + */ +public class ActivateComparator implements Comparator { + + public static final Comparator COMPARATOR = new ActivateComparator(); + + @Override + public int compare(Object o1, Object o2) { + if (o1 == null && o2 == null) { + return 0; + } + if (o1 == null) { + return -1; + } + if (o2 == null) { + return 1; + } + if (o1.equals(o2)) { + return 0; + } + Activate a1 = o1.getClass().getAnnotation(Activate.class); + Activate a2 = o2.getClass().getAnnotation(Activate.class); + Class spiClass = null; + if(o1.getClass().getInterfaces().length > 0){ + for (Class item : o1.getClass().getInterfaces()) { + if (item.isAnnotationPresent(SPI.class)) { + spiClass = item; + break; + } + } + } + if ((a1.before().length > 0 || a1.after().length > 0 + || a2.before().length > 0 || a2.after().length > 0) + && spiClass != null) { + ExtensionLoader extensionLoader = ExtensionLoader.getExtensionLoader(spiClass); + if (a1.before().length > 0 || a1.after().length > 0) { + String n2 = extensionLoader.getExtensionName(o2.getClass()); + for (String before : a1.before()) { + if (before.equals(n2)) { + return -1; + } + } + for (String after : a1.after()) { + if (after.equals(n2)) { + return 1; + } + } + } + if (a2.before().length > 0 || a2.after().length > 0) { + String n1 = extensionLoader.getExtensionName(o1.getClass()); + for (String before : a2.before()) { + if (before.equals(n1)) { + return 1; + } + } + for (String after : a2.after()) { + if (after.equals(n1)) { + return -1; + } + } + } + } + int n1 = a1 == null ? 0 : a1.order(); + int n2 = a2 == null ? 0 : a2.order(); + // never return 0 even if n1 equals n2, otherwise, o1 and o2 will override each other in collection like HashSet + return n1 > n2 ? 1 : -1; + } + +} From c9c086f7a50d167cd795b4e7fe2b7657ecd175e6 Mon Sep 17 00:00:00 2001 From: Ian Luo Date: Fri, 1 Feb 2019 12:30:00 +0800 Subject: [PATCH 2/2] Update dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/support/ActivateComparator.java code style Co-Authored-By: dongYES <30755351+dongYES@users.noreply.github.com> --- .../dubbo/common/extension/support/ActivateComparator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/support/ActivateComparator.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/support/ActivateComparator.java index 778170c0387..1386efa6aed 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/support/ActivateComparator.java +++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/extension/support/ActivateComparator.java @@ -46,7 +46,7 @@ public int compare(Object o1, Object o2) { Activate a1 = o1.getClass().getAnnotation(Activate.class); Activate a2 = o2.getClass().getAnnotation(Activate.class); Class spiClass = null; - if(o1.getClass().getInterfaces().length > 0){ + if (o1.getClass().getInterfaces().length > 0) { for (Class item : o1.getClass().getInterfaces()) { if (item.isAnnotationPresent(SPI.class)) { spiClass = item;