Skip to content

Commit

Permalink
use @SneakyThrow to instead of never occurred exception
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Sep 28, 2018
1 parent b4ea029 commit 0623d39
Show file tree
Hide file tree
Showing 50 changed files with 436 additions and 462 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

package io.shardingsphere.spi;

import io.shardingsphere.core.exception.ShardingException;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;

import java.util.Collection;
import java.util.LinkedList;
Expand Down Expand Up @@ -57,14 +57,11 @@ public static <T> NewInstanceServiceLoader<T> load(final Class<T> service) {
*
* @return service instances
*/
@SneakyThrows
public Collection<T> newServiceInstances() {
Collection<T> result = new LinkedList<>();
for (Class<T> each : serviceClasses) {
try {
result.add(each.newInstance());
} catch (final ReflectiveOperationException ex) {
throw new ShardingException(ex);
}
result.add(each.newInstance());
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.shardingsphere.core.executor.sql.execute.threadlocal.ExecutorExceptionHandler;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;

import java.lang.reflect.Field;

Expand All @@ -32,7 +33,7 @@ public final class ExecutorTestUtil {
* Listen event.
*
* @param eventCaller event caller
* @param event SQL execution event
* @param event SQL execution event
*/
public static void listen(final EventCaller eventCaller, final SQLExecutionEvent event) {
eventCaller.verifyDataSource(event.getRouteUnit().getDataSourceName());
Expand All @@ -46,10 +47,9 @@ public static void listen(final EventCaller eventCaller, final SQLExecutionEvent

/**
* Clear thread local.
*
* @throws ReflectiveOperationException reflective operation exception
*/
public static void clear() throws ReflectiveOperationException {
@SneakyThrows
public static void clear() {
Field field = ExecutorExceptionHandler.class.getDeclaredField("IS_EXCEPTION_THROWN");
field.setAccessible(true);
((ThreadLocal) field.get(ExecutorExceptionHandler.class)).remove();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
public class ExecutorExceptionHandlerTest {

@After
public void tearDown() throws ReflectiveOperationException {
public void tearDown() {
ExecutorTestUtil.clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package io.shardingsphere.core.keygen;

import io.shardingsphere.core.keygen.fixture.FixedTimeService;
import lombok.SneakyThrows;
import org.junit.Test;

import java.lang.reflect.Field;
Expand Down Expand Up @@ -80,7 +81,8 @@ public void assertSetWorkerIdFailureWhenTooMuch() {
}

@Test
public void assertSetWorkerIdSuccess() throws NoSuchFieldException, IllegalAccessException {
@SneakyThrows
public void assertSetWorkerIdSuccess() {
DefaultKeyGenerator.setWorkerId(1L);
Field workerIdField = DefaultKeyGenerator.class.getDeclaredField("workerId");
workerIdField.setAccessible(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import com.google.common.base.Preconditions;
import io.shardingsphere.core.parsing.integrate.jaxb.root.ParserResult;
import io.shardingsphere.core.parsing.integrate.jaxb.root.ParserResultSet;
import lombok.SneakyThrows;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import java.io.File;
import java.net.URL;
import java.util.HashMap;
Expand Down Expand Up @@ -64,14 +64,11 @@ private Map<String, ParserResult> loadParserResultSet() {
return result;
}

@SneakyThrows
private Map<String, ParserResult> loadParserResultSet(final File file) {
Map<String, ParserResult> result = new HashMap<>(Short.MAX_VALUE, 1);
try {
for (ParserResult each : ((ParserResultSet) JAXBContext.newInstance(ParserResultSet.class).createUnmarshaller().unmarshal(file)).getParserResults()) {
result.put(each.getSqlCaseId(), each);
}
} catch (JAXBException ex) {
throw new RuntimeException(ex);
for (ParserResult each : ((ParserResultSet) JAXBContext.newInstance(ParserResultSet.class).createUnmarshaller().unmarshal(file)).getParserResults()) {
result.put(each.getSqlCaseId(), each);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.shardingsphere.core.parsing.parser.context.condition.Condition;
import io.shardingsphere.core.parsing.parser.context.condition.OrCondition;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;

import java.lang.reflect.Field;
import java.util.Map;
Expand Down Expand Up @@ -85,13 +86,10 @@ private void assertCondition(final Condition actual, final ExpectedCondition exp
}
}

@SneakyThrows
private Object getField(final Object actual, final String fieldName) {
try {
Field field = actual.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(actual);
} catch (final ReflectiveOperationException ex) {
throw new RuntimeException(ex);
}
Field field = actual.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(actual);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

package io.shardingsphere.shardingjdbc.jdbc.adapter;

import io.shardingsphere.core.exception.ShardingException;
import io.shardingsphere.shardingjdbc.jdbc.adapter.invocation.SetParameterMethodInvocation;
import io.shardingsphere.shardingjdbc.jdbc.unsupported.AbstractUnsupportedOperationPreparedStatement;
import lombok.Getter;
import lombok.SneakyThrows;

import java.io.InputStream;
import java.io.Reader;
Expand Down Expand Up @@ -257,14 +257,6 @@ private void setParameter(final int parameterIndex, final Object value) {
parameters.set(parameterIndex - 1, value);
}

private void recordSetParameter(final String methodName, final Class[] argumentTypes, final Object... arguments) {
try {
setParameterMethodInvocations.add(new SetParameterMethodInvocation(PreparedStatement.class.getMethod(methodName, argumentTypes), arguments, arguments[1]));
} catch (final NoSuchMethodException ex) {
throw new ShardingException(ex);
}
}

protected final void replaySetParameter(final PreparedStatement preparedStatement, final List<Object> parameters) {
setParameterMethodInvocations.clear();
addParameters(parameters);
Expand All @@ -275,10 +267,15 @@ protected final void replaySetParameter(final PreparedStatement preparedStatemen

private void addParameters(final List<Object> parameters) {
for (int i = 0; i < parameters.size(); i++) {
recordSetParameter("setObject", new Class[]{int.class, Object.class}, i + 1, parameters.get(i));
setParameter(new Class[]{int.class, Object.class}, i + 1, parameters.get(i));
}
}

@SneakyThrows
private void setParameter(final Class[] argumentTypes, final Object... arguments) {
setParameterMethodInvocations.add(new SetParameterMethodInvocation(PreparedStatement.class.getMethod("setObject", argumentTypes), arguments, arguments[1]));
}

@Override
public final void clearParameters() {
parameters.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

package io.shardingsphere.shardingjdbc.jdbc.adapter;

import io.shardingsphere.core.exception.ShardingException;
import io.shardingsphere.shardingjdbc.jdbc.adapter.invocation.JdbcMethodInvocation;
import lombok.SneakyThrows;

import java.sql.SQLException;
import java.sql.Wrapper;
Expand Down Expand Up @@ -56,12 +56,9 @@ public final boolean isWrapperFor(final Class<?> iface) {
* @param argumentTypes argument types
* @param arguments arguments
*/
@SneakyThrows
public final void recordMethodInvocation(final Class<?> targetClass, final String methodName, final Class<?>[] argumentTypes, final Object[] arguments) {
try {
jdbcMethodInvocations.add(new JdbcMethodInvocation(targetClass.getMethod(methodName, argumentTypes), arguments));
} catch (final NoSuchMethodException ex) {
throw new ShardingException(ex);
}
jdbcMethodInvocations.add(new JdbcMethodInvocation(targetClass.getMethod(methodName, argumentTypes), arguments));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@

package io.shardingsphere.shardingjdbc.jdbc.adapter.invocation;

import io.shardingsphere.core.exception.ShardingException;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
Expand All @@ -43,11 +42,8 @@ public class JdbcMethodInvocation {
*
* @param target target object
*/
@SneakyThrows
public void invoke(final Object target) {
try {
method.invoke(target, arguments);
} catch (final IllegalAccessException | InvocationTargetException ex) {
throw new ShardingException("Invoke jdbc method exception", ex);
}
method.invoke(target, arguments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import lombok.Getter;

import javax.sql.DataSource;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.Collection;
Expand Down Expand Up @@ -101,7 +100,7 @@ private void closeOriginalDataSources() {
try {
Method closeMethod = each.getClass().getDeclaredMethod("close");
closeMethod.invoke(each);
} catch (final NoSuchMethodException | InvocationTargetException | IllegalAccessException ignored) {
} catch (final ReflectiveOperationException ignored) {
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.shardingsphere.dbtest.cases.assertion.dql.DQLIntegrateTestCases;
import io.shardingsphere.dbtest.cases.assertion.root.IntegrateTestCase;
import io.shardingsphere.dbtest.cases.assertion.root.IntegrateTestCases;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import javax.xml.bind.JAXBContext;
Expand Down Expand Up @@ -74,15 +75,12 @@ public final class IntegrateTestCasesLoader {

private final Map<String, IntegrateTestCase> dclIntegrateTestCaseMap;

@SneakyThrows
private IntegrateTestCasesLoader() {
try {
dqlIntegrateTestCaseMap = loadIntegrateTestCases(DQL_INTEGRATE_TEST_CASES_FILE_PREFIX);
dmlIntegrateTestCaseMap = loadIntegrateTestCases(DML_INTEGRATE_TEST_CASES_FILE_PREFIX);
ddlIntegrateTestCaseMap = loadIntegrateTestCases(DDL_INTEGRATE_TEST_CASES_FILE_PREFIX);
dclIntegrateTestCaseMap = loadIntegrateTestCases(DCL_INTEGRATE_TEST_CASES_FILE_PREFIX);
} catch (final IOException | URISyntaxException | JAXBException ex) {
throw new RuntimeException(ex);
}
dqlIntegrateTestCaseMap = loadIntegrateTestCases(DQL_INTEGRATE_TEST_CASES_FILE_PREFIX);
dmlIntegrateTestCaseMap = loadIntegrateTestCases(DML_INTEGRATE_TEST_CASES_FILE_PREFIX);
ddlIntegrateTestCaseMap = loadIntegrateTestCases(DDL_INTEGRATE_TEST_CASES_FILE_PREFIX);
dclIntegrateTestCaseMap = loadIntegrateTestCases(DCL_INTEGRATE_TEST_CASES_FILE_PREFIX);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.shardingsphere.api.config.TableRuleConfiguration;
import io.shardingsphere.core.rule.ShardingRule;
import io.shardingsphere.shardingjdbc.jdbc.core.ShardingContext;
import lombok.SneakyThrows;
import org.junit.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
Expand All @@ -47,7 +48,7 @@
public final class ShardingDataSourceFactoryTest {

@Test
public void assertCreateDataSourceWithShardingRuleAndConfigMapAndProperties() throws SQLException, NoSuchFieldException, IllegalAccessException {
public void assertCreateDataSourceWithShardingRuleAndConfigMapAndProperties() throws SQLException {
ShardingRuleConfiguration shardingRuleConfig = createShardingRuleConfig();
Properties props = new Properties();
Map<String, Object> configMap = new ConcurrentHashMap<>();
Expand All @@ -72,8 +73,8 @@ private Map<String, DataSource> getDataSourceMap() throws SQLException {
when(connection.createStatement()).thenReturn(statement);
when(statement.executeQuery(Mockito.anyString())).thenReturn(resultSet);
when(statement.getConnection()).thenReturn(connection);
when(statement.getConnection().getMetaData().getTables(ArgumentMatchers.<String>any(), ArgumentMatchers.<String>any(),
ArgumentMatchers.<String>any(), ArgumentMatchers.<String[]>any())).thenReturn(resultSet);
when(statement.getConnection().getMetaData().getTables(
ArgumentMatchers.<String>any(), ArgumentMatchers.<String>any(), ArgumentMatchers.<String>any(), ArgumentMatchers.<String[]>any())).thenReturn(resultSet);
when(resultSet.next()).thenReturn(false);
Map<String, DataSource> result = new HashMap<>(1);
when(statement.getConnection().getMetaData().getURL()).thenReturn("jdbc:h2:mem:demo_ds;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
Expand All @@ -90,13 +91,15 @@ private ShardingRuleConfiguration createShardingRuleConfig() {
return result;
}

private ShardingRule getShardingRule(final DataSource dataSource) throws NoSuchFieldException, IllegalAccessException {
@SneakyThrows
private ShardingRule getShardingRule(final DataSource dataSource) {
Field field = dataSource.getClass().getDeclaredField("shardingContext");
field.setAccessible(true);
return ((ShardingContext) field.get(dataSource)).getShardingRule();
}

private Properties getShardingProperties(final DataSource dataSource) throws NoSuchFieldException, IllegalAccessException {
@SneakyThrows
private Properties getShardingProperties(final DataSource dataSource) {
Field shardingPropertiesField = dataSource.getClass().getDeclaredField("shardingProperties");
shardingPropertiesField.setAccessible(true);
Field propsField = shardingPropertiesField.get(dataSource).getClass().getDeclaredField("props");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public abstract class AbstractBaseExecutorTest {
private TestDMLExecutionEventListener dmlExecutionEventListener;

@Before
public void setUp() throws SQLException, ReflectiveOperationException {
public void setUp() throws SQLException {
MockitoAnnotations.initMocks(this);
ExecutorExceptionHandler.setExceptionThrown(false);
executeEngine = new ShardingExecuteEngine(Runtime.getRuntime().availableProcessors());
Expand Down Expand Up @@ -87,7 +87,7 @@ private void setConnection() throws SQLException {
}

@After
public void tearDown() throws ReflectiveOperationException {
public void tearDown() {
ExecutorTestUtil.clear();
ShardingEventBusInstance.getInstance().unregister(dqlExecutionEventListener);
ShardingEventBusInstance.getInstance().unregister(dmlExecutionEventListener);
Expand Down
Loading

0 comments on commit 0623d39

Please sign in to comment.