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

zhonghongsheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 186361b661e Refactor TypedSPILoader's type input param to object 
(#26891)
186361b661e is described below

commit 186361b661ec3aa754687ba15fe56657fc65ce0c
Author: Liang Zhang <[email protected]>
AuthorDate: Mon Jul 10 19:55:25 2023 +0800

    Refactor TypedSPILoader's type input param to object (#26891)
---
 .../ServiceProviderNotFoundServerException.java        |  2 +-
 .../infra/util/spi/type/typed/TypedSPILoader.java      | 18 +++++++++---------
 2 files changed, 10 insertions(+), 10 deletions(-)

diff --git 
a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/spi/exception/ServiceProviderNotFoundServerException.java
 
b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/spi/exception/ServiceProviderNotFoundServerException.java
index 2b241685b5e..1c18793e260 100644
--- 
a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/spi/exception/ServiceProviderNotFoundServerException.java
+++ 
b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/spi/exception/ServiceProviderNotFoundServerException.java
@@ -30,7 +30,7 @@ public final class ServiceProviderNotFoundServerException 
extends ShardingSphere
     
     private static final int ERROR_CODE = 1;
     
-    public ServiceProviderNotFoundServerException(final Class<?> clazz, final 
String type) {
+    public ServiceProviderNotFoundServerException(final Class<?> clazz, final 
Object type) {
         super(ERROR_CATEGORY, ERROR_CODE, String.format("No implementation 
class load from SPI `%s` with type `%s`.", clazz.getName(), type));
     }
 }
diff --git 
a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/spi/type/typed/TypedSPILoader.java
 
b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/spi/type/typed/TypedSPILoader.java
index e338cb7ee6e..7fc6df89673 100644
--- 
a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/spi/type/typed/TypedSPILoader.java
+++ 
b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/spi/type/typed/TypedSPILoader.java
@@ -39,7 +39,7 @@ public final class TypedSPILoader {
      * @param <T> SPI class type
      * @return contains or not
      */
-    public static <T extends TypedSPI> boolean contains(final Class<T> 
spiClass, final String type) {
+    public static <T extends TypedSPI> boolean contains(final Class<T> 
spiClass, final Object type) {
         return 
ShardingSphereServiceLoader.getServiceInstances(spiClass).stream().anyMatch(each
 -> matchesType(type, each));
     }
     
@@ -51,7 +51,7 @@ public final class TypedSPILoader {
      * @param <T> SPI class type
      * @return service
      */
-    public static <T extends TypedSPI> Optional<T> findService(final Class<T> 
spiClass, final String type) {
+    public static <T extends TypedSPI> Optional<T> findService(final Class<T> 
spiClass, final Object type) {
         return findService(spiClass, type, new Properties());
     }
     
@@ -64,7 +64,7 @@ public final class TypedSPILoader {
      * @param <T> SPI class type
      * @return service
      */
-    public static <T extends TypedSPI> Optional<T> findService(final Class<T> 
spiClass, final String type, final Properties props) {
+    public static <T extends TypedSPI> Optional<T> findService(final Class<T> 
spiClass, final Object type, final Properties props) {
         if (null == type) {
             return findDefaultService(spiClass);
         }
@@ -105,7 +105,7 @@ public final class TypedSPILoader {
      * @param <T> SPI class type
      * @return service
      */
-    public static <T extends TypedSPI> T getService(final Class<T> spiClass, 
final String type) {
+    public static <T extends TypedSPI> T getService(final Class<T> spiClass, 
final Object type) {
         return getService(spiClass, type, new Properties());
     }
     
@@ -118,7 +118,7 @@ public final class TypedSPILoader {
      * @param <T> SPI class type
      * @return service
      */
-    public static <T extends TypedSPI> T getService(final Class<T> spiClass, 
final String type, final Properties props) {
+    public static <T extends TypedSPI> T getService(final Class<T> spiClass, 
final Object type, final Properties props) {
         return findService(spiClass, type, props).orElseGet(() -> 
findDefaultService(spiClass).orElseThrow(() -> new 
ServiceProviderNotFoundServerException(spiClass, type)));
     }
     
@@ -132,7 +132,7 @@ public final class TypedSPILoader {
      * @return is valid service or not
      * @throws ServiceProviderNotFoundServerException service provider not 
found server exception
      */
-    public static <T extends TypedSPI> boolean checkService(final Class<T> 
spiClass, final String type, final Properties props) {
+    public static <T extends TypedSPI> boolean checkService(final Class<T> 
spiClass, final Object type, final Properties props) {
         for (T each : 
ShardingSphereServiceLoader.getServiceInstances(spiClass)) {
             if (matchesType(type, each)) {
                 each.init(null == props ? new Properties() : 
convertToStringTypedProperties(props));
@@ -142,12 +142,12 @@ public final class TypedSPILoader {
         throw new ServiceProviderNotFoundServerException(spiClass, type);
     }
     
-    private static boolean matchesType(final String type, final TypedSPI 
instance) {
+    private static boolean matchesType(final Object type, final TypedSPI 
instance) {
         if (null == instance.getType()) {
             return false;
         }
-        if (instance.getType() instanceof String) {
-            return instance.getType().toString().equalsIgnoreCase(type) || 
instance.getTypeAliases().contains(type);
+        if (instance.getType() instanceof String && type instanceof String) {
+            return 
instance.getType().toString().equalsIgnoreCase(type.toString()) || 
instance.getTypeAliases().contains(type);
         }
         return instance.getType().equals(type) || 
instance.getTypeAliases().contains(type);
     }

Reply via email to