You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/05/08 04:50:59 UTC
[shardingsphere] branch master updated: Remove useless exception thrown in ShardingSphereServiceLoader (#17441)
This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 5f693fa5793 Remove useless exception thrown in ShardingSphereServiceLoader (#17441)
5f693fa5793 is described below
commit 5f693fa57938b3390e53a307a3ed01a9cba8ba26
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun May 8 12:50:53 2022 +0800
Remove useless exception thrown in ShardingSphereServiceLoader (#17441)
* Refactor ShardingSphereServiceLoaderTest
* Remove useless exception thrown in ShardingSphereServiceLoader
* Remove useless exception thrown in ShardingSphereServiceLoader
---
.../spi/ShardingSphereServiceLoader.java | 33 ++------
.../ServiceLoaderInstantiationException.java | 32 -------
.../spi/exception/ShardingSphereSPIException.java | 4 -
.../spi/ShardingSphereServiceLoaderTest.java | 98 ++++++----------------
.../ServiceLoaderInstantiationExceptionTest.java | 32 -------
...tonFixtureService.java => EmptySPIFixture.java} | 2 +-
...ureServiceImpl.java => MultitonSPIFixture.java} | 2 +-
...erviceImpl.java => MultitonSPIFixtureImpl.java} | 2 +-
...ixtureService.java => SingletonSPIFixture.java} | 2 +-
...reService.java => SingletonSPIFixtureImpl.java} | 2 +-
....shardingsphere.spi.fixture.MultitonSPIFixture} | 2 +-
...shardingsphere.spi.fixture.SingletonSPIFixture} | 2 +-
12 files changed, 39 insertions(+), 174 deletions(-)
diff --git a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/ShardingSphereServiceLoader.java b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/ShardingSphereServiceLoader.java
index fca5d266a08..4c1a0864b9f 100644
--- a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/ShardingSphereServiceLoader.java
+++ b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/ShardingSphereServiceLoader.java
@@ -19,8 +19,8 @@ package org.apache.shardingsphere.spi;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
+import lombok.SneakyThrows;
import org.apache.shardingsphere.spi.annotation.SingletonSPI;
-import org.apache.shardingsphere.spi.exception.ServiceLoaderInstantiationException;
import java.util.Collection;
import java.util.Collections;
@@ -64,18 +64,12 @@ public final class ShardingSphereServiceLoader {
* @return service instances
*/
public static <T> Collection<T> getServiceInstances(final Class<T> serviceInterface) {
- return null == serviceInterface.getAnnotation(SingletonSPI.class) ? newServiceInstances(serviceInterface) : getSingletonServiceInstances(serviceInterface);
+ return null == serviceInterface.getAnnotation(SingletonSPI.class) ? createNewServiceInstances(serviceInterface) : getSingletonServiceInstances(serviceInterface);
}
- /**
- * New service instances.
- *
- * @param serviceInterface service interface
- * @param <T> type of service
- * @return service instances
- */
+ @SneakyThrows(ReflectiveOperationException.class)
@SuppressWarnings("unchecked")
- public static <T> Collection<T> newServiceInstances(final Class<T> serviceInterface) {
+ private static <T> Collection<T> createNewServiceInstances(final Class<T> serviceInterface) {
if (!SERVICES.containsKey(serviceInterface)) {
return Collections.emptyList();
}
@@ -85,28 +79,13 @@ public final class ShardingSphereServiceLoader {
}
Collection<T> result = new LinkedList<>();
for (Object each : services) {
- result.add((T) newServiceInstance(each.getClass()));
+ result.add((T) each.getClass().getDeclaredConstructor().newInstance());
}
return result;
}
- private static Object newServiceInstance(final Class<?> clazz) {
- try {
- return clazz.getDeclaredConstructor().newInstance();
- } catch (final ReflectiveOperationException ex) {
- throw new ServiceLoaderInstantiationException(clazz, ex);
- }
- }
-
- /**
- * Get singleton service instances.
- *
- * @param serviceInterface service interface
- * @param <T> type of service
- * @return service instances
- */
@SuppressWarnings("unchecked")
- public static <T> Collection<T> getSingletonServiceInstances(final Class<T> serviceInterface) {
+ private static <T> Collection<T> getSingletonServiceInstances(final Class<T> serviceInterface) {
return (Collection<T>) SERVICES.getOrDefault(serviceInterface, Collections.emptyList());
}
}
diff --git a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ServiceLoaderInstantiationException.java b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ServiceLoaderInstantiationException.java
deleted file mode 100644
index 3c3365d5134..00000000000
--- a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ServiceLoaderInstantiationException.java
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * 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.shardingsphere.spi.exception;
-
-/**
- * Service loader instantiation exception.
- */
-public final class ServiceLoaderInstantiationException extends ShardingSphereSPIException {
-
- private static final long serialVersionUID = 6261274443437676201L;
-
- private static final int ERROR_CODE = 2;
-
- public ServiceLoaderInstantiationException(final Class<?> clazz, final Exception cause) {
- super(ERROR_CODE, String.format("Can not find public default constructor for SPI class `%s`.", clazz.getName()), cause);
- }
-}
diff --git a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ShardingSphereSPIException.java b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ShardingSphereSPIException.java
index bf82427fd2f..d33c6f64d67 100644
--- a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ShardingSphereSPIException.java
+++ b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ShardingSphereSPIException.java
@@ -28,10 +28,6 @@ public abstract class ShardingSphereSPIException extends RuntimeException {
super(createErrorMessage(errorCode, message));
}
- public ShardingSphereSPIException(final int errorCode, final String message, final Exception cause) {
- super(createErrorMessage(errorCode, message), cause);
- }
-
private static String createErrorMessage(final int errorCode, final String message) {
return String.format("SPI-%05d: %s", errorCode, message);
}
diff --git a/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/ShardingSphereServiceLoaderTest.java b/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/ShardingSphereServiceLoaderTest.java
index 815d10995b9..4b64bda4d35 100644
--- a/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/ShardingSphereServiceLoaderTest.java
+++ b/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/ShardingSphereServiceLoaderTest.java
@@ -17,19 +17,16 @@
package org.apache.shardingsphere.spi;
-import lombok.SneakyThrows;
-import org.apache.shardingsphere.spi.exception.ServiceLoaderInstantiationException;
-import org.apache.shardingsphere.spi.fixture.NonSingletonFixtureService;
-import org.apache.shardingsphere.spi.fixture.SingletonFixtureService;
-import org.apache.shardingsphere.spi.type.typed.fixture.TypedSPIFixture;
+import org.apache.shardingsphere.spi.fixture.EmptySPIFixture;
+import org.apache.shardingsphere.spi.fixture.MultitonSPIFixture;
+import org.apache.shardingsphere.spi.fixture.MultitonSPIFixtureImpl;
+import org.apache.shardingsphere.spi.fixture.SingletonSPIFixture;
+import org.apache.shardingsphere.spi.fixture.SingletonSPIFixtureImpl;
import org.junit.Test;
-import java.lang.reflect.Field;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
import java.util.Collection;
-import java.util.Map;
+import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
@@ -37,80 +34,37 @@ import static org.junit.Assert.assertTrue;
public final class ShardingSphereServiceLoaderTest {
- @Test
- public void assertGetSingletonServiceInstanceWhenIsNotExist() {
- ShardingSphereServiceLoader.register(Collection.class);
- Collection<?> collection = ShardingSphereServiceLoader.getSingletonServiceInstances(Collection.class);
- assertTrue(collection.isEmpty());
- }
-
- @Test
- public void assertGetSingletonServiceInstanceWhenIsExist() {
- ShardingSphereServiceLoader.register(TypedSPIFixture.class);
- Collection<?> collection = ShardingSphereServiceLoader.getSingletonServiceInstances(TypedSPIFixture.class);
- assertThat(collection.size(), is(1));
- assertThat(collection.iterator().next(), is(ShardingSphereServiceLoader.getSingletonServiceInstances(TypedSPIFixture.class).iterator().next()));
- }
-
- @Test
- public void assertNewServiceInstanceWhenIsNotExist() {
- ShardingSphereServiceLoader.register(Collection.class);
- Collection<?> collection = ShardingSphereServiceLoader.newServiceInstances(Collection.class);
- assertTrue(collection.isEmpty());
- }
-
- @Test
- public void assertNewServiceInstanceWhenIsExist() {
- ShardingSphereServiceLoader.register(TypedSPIFixture.class);
- Collection<?> collection = ShardingSphereServiceLoader.newServiceInstances(TypedSPIFixture.class);
- assertThat(collection.size(), is(1));
- assertThat(collection.iterator().next(), not(ShardingSphereServiceLoader.getSingletonServiceInstances(TypedSPIFixture.class).iterator().next()));
+ static {
+ ShardingSphereServiceLoader.register(EmptySPIFixture.class);
+ ShardingSphereServiceLoader.register(SingletonSPIFixture.class);
+ ShardingSphereServiceLoader.register(MultitonSPIFixture.class);
}
@Test
- public void assertRegisterTwice() {
- ShardingSphereServiceLoader.register(TypedSPIFixture.class);
- Collection<?> actualFirstRegister = ShardingSphereServiceLoader.newServiceInstances(TypedSPIFixture.class);
- assertThat(actualFirstRegister.size(), is(1));
- ShardingSphereServiceLoader.register(TypedSPIFixture.class);
- Collection<?> actualSecondRegister = ShardingSphereServiceLoader.newServiceInstances(TypedSPIFixture.class);
- assertThat(actualSecondRegister.size(), is(actualFirstRegister.size()));
+ public void assertGetServiceInstancesWithUnregisteredSPI() {
+ assertTrue(ShardingSphereServiceLoader.getServiceInstances(Object.class).isEmpty());
}
@Test
- public void assertNewInstanceError() throws NoSuchMethodException, IllegalAccessException {
- Method method = ShardingSphereServiceLoader.class.getDeclaredMethod("newServiceInstance", Class.class);
- method.setAccessible(true);
- Throwable targetException = null;
- try {
- method.invoke(null, TypedSPIFixture.class);
- } catch (final InvocationTargetException ex) {
- targetException = ex.getTargetException();
- }
- assertTrue(targetException instanceof ServiceLoaderInstantiationException);
+ public void assertGetServiceInstancesWithEmptyInstances() {
+ assertTrue(ShardingSphereServiceLoader.getServiceInstances(EmptySPIFixture.class).isEmpty());
}
@Test
- public void assertGetSingletonServiceInstance() {
- ShardingSphereServiceLoader.register(SingletonFixtureService.class);
- Collection<SingletonFixtureService> actual = ShardingSphereServiceLoader.getServiceInstances(SingletonFixtureService.class);
- Collection<Object> expected = getRegisteredServices(SingletonFixtureService.class);
- assertThat(actual, is(expected));
+ public void assertGetServiceInstancesWithSingletonSPI() {
+ Collection<SingletonSPIFixture> actual = ShardingSphereServiceLoader.getServiceInstances(SingletonSPIFixture.class);
+ assertThat(actual.size(), is(1));
+ SingletonSPIFixture actualInstance = actual.iterator().next();
+ assertThat(actualInstance, instanceOf(SingletonSPIFixtureImpl.class));
+ assertThat(actualInstance, is(ShardingSphereServiceLoader.getServiceInstances(SingletonSPIFixture.class).iterator().next()));
}
@Test
- public void assertGetNonSingletonServiceInstance() {
- ShardingSphereServiceLoader.register(NonSingletonFixtureService.class);
- Collection<NonSingletonFixtureService> actual = ShardingSphereServiceLoader.getServiceInstances(NonSingletonFixtureService.class);
- Collection<Object> expected = getRegisteredServices(NonSingletonFixtureService.class);
- assertThat(actual, not(expected));
- }
-
- @SuppressWarnings("unchecked")
- @SneakyThrows({NoSuchFieldException.class, IllegalAccessException.class})
- private Collection<Object> getRegisteredServices(final Class<?> serviceClass) {
- Field field = ShardingSphereServiceLoader.class.getDeclaredField("SERVICES");
- field.setAccessible(true);
- return ((Map<Class<?>, Collection<Object>>) field.get(null)).get(serviceClass);
+ public void assertGetServiceInstancesWithMultitonSPI() {
+ Collection<MultitonSPIFixture> actual = ShardingSphereServiceLoader.getServiceInstances(MultitonSPIFixture.class);
+ assertThat(actual.size(), is(1));
+ MultitonSPIFixture actualInstance = actual.iterator().next();
+ assertThat(actualInstance, instanceOf(MultitonSPIFixtureImpl.class));
+ assertThat(actualInstance, not(ShardingSphereServiceLoader.getServiceInstances(MultitonSPIFixture.class).iterator().next()));
}
}
diff --git a/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/exception/ServiceLoaderInstantiationExceptionTest.java b/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/exception/ServiceLoaderInstantiationExceptionTest.java
deleted file mode 100644
index fa7b1bf1618..00000000000
--- a/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/exception/ServiceLoaderInstantiationExceptionTest.java
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * 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.shardingsphere.spi.exception;
-
-import org.junit.Test;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-
-public final class ServiceLoaderInstantiationExceptionTest {
-
- @Test
- public void assertGetMessage() {
- assertThat(new ServiceLoaderInstantiationException(Object.class, new RuntimeException()).getMessage(),
- is("SPI-00002: Can not find public default constructor for SPI class `java.lang.Object`."));
- }
-}
diff --git a/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/NonSingletonFixtureService.java b/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/EmptySPIFixture.java
similarity index 94%
copy from shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/NonSingletonFixtureService.java
copy to shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/EmptySPIFixture.java
index e715124705c..1d464015a75 100644
--- a/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/NonSingletonFixtureService.java
+++ b/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/EmptySPIFixture.java
@@ -17,5 +17,5 @@
package org.apache.shardingsphere.spi.fixture;
-public interface NonSingletonFixtureService {
+public interface EmptySPIFixture {
}
diff --git a/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/SingletonFixtureServiceImpl.java b/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/MultitonSPIFixture.java
similarity index 91%
rename from shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/SingletonFixtureServiceImpl.java
rename to shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/MultitonSPIFixture.java
index e0cbdcfcc39..63d8aac6075 100644
--- a/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/SingletonFixtureServiceImpl.java
+++ b/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/MultitonSPIFixture.java
@@ -17,5 +17,5 @@
package org.apache.shardingsphere.spi.fixture;
-public final class SingletonFixtureServiceImpl implements SingletonFixtureService {
+public interface MultitonSPIFixture {
}
diff --git a/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/NonSingletonFixtureServiceImpl.java b/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/MultitonSPIFixtureImpl.java
similarity index 90%
rename from shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/NonSingletonFixtureServiceImpl.java
rename to shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/MultitonSPIFixtureImpl.java
index a0d7cb7168b..48e401dd534 100644
--- a/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/NonSingletonFixtureServiceImpl.java
+++ b/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/MultitonSPIFixtureImpl.java
@@ -17,5 +17,5 @@
package org.apache.shardingsphere.spi.fixture;
-public final class NonSingletonFixtureServiceImpl implements NonSingletonFixtureService {
+public final class MultitonSPIFixtureImpl implements MultitonSPIFixture {
}
diff --git a/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/SingletonFixtureService.java b/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/SingletonSPIFixture.java
similarity index 95%
rename from shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/SingletonFixtureService.java
rename to shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/SingletonSPIFixture.java
index 954bd181acc..3cfdb1f095d 100644
--- a/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/SingletonFixtureService.java
+++ b/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/SingletonSPIFixture.java
@@ -20,5 +20,5 @@ package org.apache.shardingsphere.spi.fixture;
import org.apache.shardingsphere.spi.annotation.SingletonSPI;
@SingletonSPI
-public interface SingletonFixtureService {
+public interface SingletonSPIFixture {
}
diff --git a/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/NonSingletonFixtureService.java b/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/SingletonSPIFixtureImpl.java
similarity index 91%
rename from shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/NonSingletonFixtureService.java
rename to shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/SingletonSPIFixtureImpl.java
index e715124705c..414fe0420a1 100644
--- a/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/NonSingletonFixtureService.java
+++ b/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/fixture/SingletonSPIFixtureImpl.java
@@ -17,5 +17,5 @@
package org.apache.shardingsphere.spi.fixture;
-public interface NonSingletonFixtureService {
+public final class SingletonSPIFixtureImpl implements SingletonSPIFixture {
}
diff --git a/shardingsphere-spi/src/test/resources/META-INF/services/org.apache.shardingsphere.spi.fixture.SingletonFixtureService b/shardingsphere-spi/src/test/resources/META-INF/services/org.apache.shardingsphere.spi.fixture.MultitonSPIFixture
similarity index 92%
rename from shardingsphere-spi/src/test/resources/META-INF/services/org.apache.shardingsphere.spi.fixture.SingletonFixtureService
rename to shardingsphere-spi/src/test/resources/META-INF/services/org.apache.shardingsphere.spi.fixture.MultitonSPIFixture
index 270e75c5792..ead9587e33f 100644
--- a/shardingsphere-spi/src/test/resources/META-INF/services/org.apache.shardingsphere.spi.fixture.SingletonFixtureService
+++ b/shardingsphere-spi/src/test/resources/META-INF/services/org.apache.shardingsphere.spi.fixture.MultitonSPIFixture
@@ -15,4 +15,4 @@
# limitations under the License.
#
-org.apache.shardingsphere.spi.fixture.SingletonFixtureServiceImpl
+org.apache.shardingsphere.spi.fixture.MultitonSPIFixtureImpl
diff --git a/shardingsphere-spi/src/test/resources/META-INF/services/org.apache.shardingsphere.spi.fixture.NonSingletonFixtureService b/shardingsphere-spi/src/test/resources/META-INF/services/org.apache.shardingsphere.spi.fixture.SingletonSPIFixture
similarity index 91%
rename from shardingsphere-spi/src/test/resources/META-INF/services/org.apache.shardingsphere.spi.fixture.NonSingletonFixtureService
rename to shardingsphere-spi/src/test/resources/META-INF/services/org.apache.shardingsphere.spi.fixture.SingletonSPIFixture
index 1e087233487..0f87b236d23 100644
--- a/shardingsphere-spi/src/test/resources/META-INF/services/org.apache.shardingsphere.spi.fixture.NonSingletonFixtureService
+++ b/shardingsphere-spi/src/test/resources/META-INF/services/org.apache.shardingsphere.spi.fixture.SingletonSPIFixture
@@ -15,4 +15,4 @@
# limitations under the License.
#
-org.apache.shardingsphere.spi.fixture.NonSingletonFixtureServiceImpl
+org.apache.shardingsphere.spi.fixture.SingletonSPIFixtureImpl