Skip to content

Commit

Permalink
optimize: some minor syntax optimization (#5869)
Browse files Browse the repository at this point in the history
Signed-off-by: slievrly <slievrly@163.com>
  • Loading branch information
slievrly committed Sep 24, 2023
1 parent 9fe8d3b commit 4c6d48e
Show file tree
Hide file tree
Showing 47 changed files with 120 additions and 118 deletions.
2 changes: 1 addition & 1 deletion changes/en-us/2.0.0.md
Expand Up @@ -118,7 +118,7 @@ The version is updated as follows:
- [[#5783](https://github.com/seata/seata/pull/5783)] support the nacos application name property
- [[#5524](https://github.com/seata/seata/pull/5524)] support for more operational commands in seata-server.sh
- [[#5836](https://github.com/seata/seata/pull/5836)] separate MySQL from Mariadb implementations

- [[#5869](https://github.com/seata/seata/pull/5869)] some minor syntax optimization

### security:
- [[#5642](https://github.com/seata/seata/pull/5642)] add Hessian Serializer WhiteDenyList
Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/2.0.0.md
Expand Up @@ -118,6 +118,7 @@ Seata 是一款开源的分布式事务解决方案,提供高性能和简单
- [[#5783](https://github.com/seata/seata/pull/5783)] 支持nacos上application name配置
- [[#5524](https://github.com/seata/seata/pull/5524)] 支持 seata-server.sh 中的更多操作命令
- [[#5836](https://github.com/seata/seata/pull/5836)] 分离mariadb和mysql的AT实现
- [[#5869](https://github.com/seata/seata/pull/5869)] 优化一些小的语法


### security:
Expand Down
4 changes: 2 additions & 2 deletions common/src/main/java/io/seata/common/util/BlobUtils.java
Expand Up @@ -64,7 +64,7 @@ public static String blob2string(Blob blob) {
}

try {
return new String(blob.getBytes((long) 1, (int) blob.length()), Constants.DEFAULT_CHARSET);
return new String(blob.getBytes(1, (int) blob.length()), Constants.DEFAULT_CHARSET);
} catch (Exception e) {
throw new ShouldNeverHappenException(e);
}
Expand Down Expand Up @@ -100,7 +100,7 @@ public static byte[] blob2Bytes(Blob blob) {
}

try {
return blob.getBytes((long) 1, (int) blob.length());
return blob.getBytes(1, (int) blob.length());
} catch (Exception e) {
throw new ShouldNeverHappenException(e);
}
Expand Down
Expand Up @@ -662,7 +662,7 @@ public static Object invokeStaticMethod(Class<?> targetClass, String staticMetho
*/
public static Map<String, Object> getAnnotationValues(Annotation annotation) throws NoSuchFieldException {
InvocationHandler h = Proxy.getInvocationHandler(annotation);
return (Map<String, Object>)getFieldValue(h, "memberValues");
return getFieldValue(h, "memberValues");
}

//endregion
Expand Down
Expand Up @@ -60,7 +60,7 @@ public static byte[] decompress(byte[] bytes) {
Inflater inflater = new Inflater();
inflater.setInput(bytes);
byte[] outputBytes = new byte[BUFFER_SIZE];
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();) {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
while (!inflater.finished()) {
length = inflater.inflate(outputBytes);
if (length == 0) {
Expand Down
Expand Up @@ -58,7 +58,7 @@ public void testChangeValue() throws Exception {
configuration.getLong("eee", 0);
ConfigurationCache.getInstance().onChangeEvent(new ConfigurationChangeEvent("eee", "1"));
long eee = configuration.getLong("eee", 0);
Assertions.assertEquals((long) 1, eee);
Assertions.assertEquals(1, eee);

// test null
configuration.getConfig("test", null);
Expand All @@ -71,7 +71,7 @@ public void testChangeValue() throws Exception {
Assertions.assertNull(test);
}

// FIXME: 2023/2/19 wait bugfix
// FIXME: 2023/2/19 wait bugfix
// @Test
public void testConfigListener() throws Exception {
Configuration configuration = new FileConfiguration("registry");
Expand Down
Expand Up @@ -35,8 +35,8 @@ void getInstance() {
ConfigurationFactory.reload();
Assertions.assertEquals(ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig("config.file.name"), "file-test-pro.conf");
Assertions.assertEquals(ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig("config.file.testBlank"), "");
Assertions.assertEquals(ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig("config.file.testNull"), null);
Assertions.assertEquals(ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig("config.file.testExist"), null);
Assertions.assertNull(ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig("config.file.testNull"));
Assertions.assertNull(ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig("config.file.testExist"));
Configuration instance = ConfigurationFactory.getInstance();
Assertions.assertEquals(instance.getConfig("service.disableGlobalTransaction"), "true");
Assertions.assertEquals(instance.getConfig("service.default.grouplist"), "127.0.0.1:8092");
Expand Down
Expand Up @@ -36,8 +36,8 @@ public void getInstance() {
ConfigurationFactory.reload();
Assertions.assertEquals(ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig("config.file.name"), "file-test-yaml.conf");
Assertions.assertEquals(ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig("config.file.testBlank"), "");
Assertions.assertEquals(ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig("config.file.testNull"), null);
Assertions.assertEquals(ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig("config.file.testExist"), null);
Assertions.assertNull(ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig("config.file.testNull"));
Assertions.assertNull(ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig("config.file.testExist"));
Configuration instance = ConfigurationFactory.getInstance();
Assertions.assertEquals(instance.getConfig("service.disableGlobalTransaction"), "true");
Assertions.assertEquals(instance.getConfig("service.default.grouplist"), "127.0.0.1:8093");
Expand Down
Expand Up @@ -49,7 +49,7 @@ public RemotingDesc getServiceDesc(Object bean, String beanName) throws Framewor
try {
RemotingDesc serviceBeanDesc = new RemotingDesc();
Class<?> interfaceClass = (Class<?>)ReflectionUtil.invokeMethod(bean, "getInterfaceClass");
String interfaceClassName = (String)ReflectionUtil.getFieldValue(bean, "interfaceName");
String interfaceClassName = ReflectionUtil.getFieldValue(bean, "interfaceName");
String version = (String)ReflectionUtil.invokeMethod(bean, "getVersion");
String group = (String)ReflectionUtil.invokeMethod(bean, "getGroup");
serviceBeanDesc.setServiceClass(interfaceClass);
Expand Down
Expand Up @@ -93,7 +93,7 @@ public RemotingDesc getServiceDesc(Object bean, String beanName) throws Framewor
Object metadata = ReflectionUtil.invokeMethod(consumerBean, "getMetadata");

String interfaceClassName = (String) ReflectionUtil.invokeMethod(metadata, "getInterfaceName");
Class<?> interfaceClass = (Class<?>) Class.forName(interfaceClassName);
Class<?> interfaceClass = Class.forName(interfaceClassName);
String uniqueId = (String) ReflectionUtil.invokeMethod(metadata, "getVersion");
String group = (String) ReflectionUtil.invokeMethod(metadata, "getGroup");
RemotingDesc serviceBeanDesc = new RemotingDesc();
Expand Down
Expand Up @@ -50,8 +50,8 @@ public RemotingDesc getServiceDesc(Object bean, String beanName) throws Framewor
try {
RemotingDesc serviceBeanDesc = new RemotingDesc();
Class<?> interfaceClass = (Class<?>)ReflectionUtil.invokeMethod(bean, "getInterfaceClass");
String interfaceClassName = (String)ReflectionUtil.getFieldValue(bean, "interfaceType");
String uniqueId = (String)ReflectionUtil.getFieldValue(bean, "uniqueId");
String interfaceClassName = ReflectionUtil.getFieldValue(bean, "interfaceType");
String uniqueId = ReflectionUtil.getFieldValue(bean, "uniqueId");
serviceBeanDesc.setServiceClass(interfaceClass);
serviceBeanDesc.setServiceClassName(interfaceClassName);
serviceBeanDesc.setUniqueId(uniqueId);
Expand Down
Expand Up @@ -212,7 +212,7 @@ public static String readStreamAsStr(InputStream is) throws IOException {

src.close();
dest.close();
return new String(bos.toByteArray(), "UTF-8");
return bos.toString("UTF-8");
}

@Test
Expand Down
Expand Up @@ -128,7 +128,7 @@ protected TableRecords afterImage(TableRecords beforeImage) throws SQLException
TableMeta tmeta = getTableMeta(sqlRecognizers.get(0).getTableName());
String selectSQL = buildAfterImageSQL(tmeta, beforeImage);
ResultSet rs = null;
try (PreparedStatement pst = statementProxy.getConnection().prepareStatement(selectSQL);) {
try (PreparedStatement pst = statementProxy.getConnection().prepareStatement(selectSQL)) {
SqlGenerateUtils.setParamForPk(beforeImage.pkRows(), getTableMeta().getPrimaryKeyOnlyName(), pst);
rs = pst.executeQuery();
return TableRecords.buildRecords(tmeta, rs);
Expand Down
Expand Up @@ -81,7 +81,7 @@ protected TableRecords beforeImage() throws SQLException {
SQLUpdateRecognizer recognizer = (SQLUpdateRecognizer) sqlRecognizer;
String tableNames = recognizer.getTableName();
// update join sql,like update t1 inner join t2 on t1.id = t2.id set t1.name = ?; tableItems = {"update t1 inner join t2","t1","t2"}
String[] tableItems = tableNames.split(recognizer.MULTI_TABLE_NAME_SEPERATOR);
String[] tableItems = tableNames.split(SQLUpdateRecognizer.MULTI_TABLE_NAME_SEPERATOR);
String joinTable = tableItems[0];
final int itemTableIndex = 1;
String suffixCommonCondition = buildBeforeImageSQLCommonConditionSuffix(paramAppenderList);
Expand Down Expand Up @@ -143,7 +143,7 @@ private String buildBeforeImageSQL(String joinTable, String itemTable,String suf
protected TableRecords afterImage(TableRecords beforeImage) throws SQLException {
SQLUpdateRecognizer recognizer = (SQLUpdateRecognizer) sqlRecognizer;
String tableNames = recognizer.getTableName();
String[] tableItems = tableNames.split(recognizer.MULTI_TABLE_NAME_SEPERATOR);
String[] tableItems = tableNames.split(SQLUpdateRecognizer.MULTI_TABLE_NAME_SEPERATOR);
String joinTable = tableItems[0];
final int itemTableIndex = 1;
ArrayList<List<Object>> joinConditionParams = new ArrayList<>();
Expand Down
Expand Up @@ -181,7 +181,7 @@ public void testRowListToMapWithSinglePk(){
rows.add(row3);

Map<String, Map<String, Field>> result =DataCompareUtils.rowListToMap(rows,primaryKeyList);
Assertions.assertTrue(result.size()==3);
Assertions.assertEquals(3, result.size());
Assertions.assertEquals(result.keySet().iterator().next(),"1");

}
Expand Down Expand Up @@ -216,7 +216,7 @@ public void testRowListToMapWithMultipPk(){
rows.add(row3);

Map<String, Map<String, Field>> result =DataCompareUtils.rowListToMap(rows,primaryKeyList);
Assertions.assertTrue(result.size()==3);
Assertions.assertEquals(3, result.size());
Assertions.assertEquals(result.keySet().iterator().next(),"1_2");

}
Expand Down
Expand Up @@ -734,7 +734,7 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa
if(metas.isEmpty()){
metas = Arrays.asList(columnsMetasReturnValue);
}
return new MockResultSet((MockStatementBase) this.connection.createStatement())
return new MockResultSet(this.connection.createStatement())
.mockResultSet(columnMetaColumnLabels, metas.toArray(new Object[0][]));
}

Expand Down Expand Up @@ -791,7 +791,7 @@ public ResultSet getTypeInfo() throws SQLException {
@Override
public ResultSet getIndexInfo(String catalog, String schema, String table, boolean unique, boolean approximate)
throws SQLException {
return new MockResultSet((MockStatementBase)this.connection.createStatement())
return new MockResultSet(this.connection.createStatement())
.mockResultSet(indexMetaColumnLabels, indexMetasReturnValue);
}

Expand Down
Expand Up @@ -119,7 +119,7 @@ public void testGetInsertRows() {
@Test
public void testNotPlaceholder_giveValidPkIndex() {
String sql = "insert into test(create_time) values(sysdate)";
List<SQLStatement> sqlStatements = SQLUtils.parseStatements(sql, DB_TYPE);;
List<SQLStatement> sqlStatements = SQLUtils.parseStatements(sql, DB_TYPE);

OracleInsertRecognizer oracle = new OracleInsertRecognizer(sql, sqlStatements.get(0));
List<List<Object>> insertRows = oracle.getInsertRows(Collections.singletonList(-1));
Expand Down
Expand Up @@ -104,7 +104,7 @@ public void testGetInsertRows() {

SQLInsertRecognizer recognizer = (SQLInsertRecognizer) SQLVisitorFactory.get(sql, DB_TYPE).get(0);
List<List<Object>> insertRows = recognizer.getInsertRows(Collections.singletonList(pkIndex));
Assertions.assertTrue(insertRows.size() == 1);
Assertions.assertEquals(1, insertRows.size());

//test for exception
Assertions.assertThrows(SQLParsingException.class, () -> {
Expand Down
Expand Up @@ -31,9 +31,6 @@ public void testColumnMeta() {
Assertions.assertEquals(columnMeta, new ColumnMeta());
columnMeta.setIsAutoincrement("Yes");
Assertions.assertTrue(columnMeta.isAutoincrement());
Assertions.assertEquals(columnMeta, columnMeta);
Assertions.assertEquals(columnMeta.hashCode(), columnMeta.hashCode());
Assertions.assertNotEquals(columnMeta, "");

ColumnMeta other = new ColumnMeta();
other.setTableCat("");
Expand Down
Expand Up @@ -32,9 +32,6 @@ public void testIndexMeta() {
IndexMeta indexMeta = new IndexMeta();
indexMeta.setValues(Lists.newArrayList());
Assertions.assertNotNull(indexMeta.toString());
Assertions.assertEquals(indexMeta, indexMeta);
Assertions.assertEquals(indexMeta.hashCode(), indexMeta.hashCode());
Assertions.assertNotEquals(indexMeta, "");

IndexMeta other = new IndexMeta();
other.setValues(Lists.newArrayList(new ColumnMeta()));
Expand Down
Expand Up @@ -37,10 +37,7 @@ public class TableMetaTest {
@Test
public void testTableMeta() {
TableMeta tableMeta = new TableMeta();
Assertions.assertEquals(tableMeta, tableMeta);
Assertions.assertEquals(tableMeta, new TableMeta());
Assertions.assertEquals(tableMeta.hashCode(), tableMeta.hashCode());
Assertions.assertNotEquals(tableMeta, "");

TableMeta other = new TableMeta();
other.setTableName("");
Expand Down
Expand Up @@ -160,7 +160,7 @@ public void dataValidationDelete() throws SQLException {
@Test
public void testParsePK() {
TableMeta tableMeta = Mockito.mock(TableMeta.class);
Mockito.when(tableMeta.getPrimaryKeyOnlyName()).thenReturn(Arrays.asList(new String[]{"id"}));
Mockito.when(tableMeta.getPrimaryKeyOnlyName()).thenReturn(Arrays.asList("id"));
Mockito.when(tableMeta.getTableName()).thenReturn("table_name");

TableRecords beforeImage = new TableRecords();
Expand Down
Expand Up @@ -52,9 +52,9 @@ public void testGetConnection() throws SQLException {

Assertions.assertTrue(connFromDataSourceProxyXANative instanceof ConnectionProxyXA);
XAConnection xaConnectionFromProxy = ((ConnectionProxyXA)connFromDataSourceProxyXANative).getWrappedXAConnection();
Assertions.assertTrue(xaConnection == xaConnectionFromProxy);
Assertions.assertSame(xaConnection, xaConnectionFromProxy);
Connection connectionFromProxy = ((ConnectionProxyXA)connFromDataSourceProxyXANative).getWrappedConnection();
Assertions.assertTrue(connection == connectionFromProxy);
Assertions.assertSame(connection, connectionFromProxy);

}
}
Expand Up @@ -83,7 +83,7 @@ public void testGetConnection() throws SQLException {
Assertions.assertTrue(wrappedConnection instanceof PooledConnection);

Connection wrappedPhysicalConn = ((PooledConnection)wrappedConnection).getConnection();
Assertions.assertTrue(wrappedPhysicalConn == connection);
Assertions.assertSame(wrappedPhysicalConn, connection);

XAConnection xaConnection = connectionProxyXA.getWrappedXAConnection();
Connection connectionInXA = xaConnection.getConnection();
Expand Down Expand Up @@ -117,7 +117,7 @@ public void testGetMariaXaConnection() throws SQLException {
Assertions.assertTrue(wrappedConnection instanceof PooledConnection);

Connection wrappedPhysicalConn = ((PooledConnection)wrappedConnection).getConnection();
Assertions.assertTrue(wrappedPhysicalConn == connection);
Assertions.assertSame(wrappedPhysicalConn, connection);

XAConnection xaConnection = connectionProxyXA.getWrappedXAConnection();
Connection connectionInXA = xaConnection.getConnection();
Expand Down
Expand Up @@ -407,7 +407,7 @@ private int getIdIndex(String stateInstanceId, String separator) {
if (StringUtils.hasLength(stateInstanceId)) {
int start = stateInstanceId.lastIndexOf(separator);
if (start > 0) {
String indexStr = stateInstanceId.substring(start + 1, stateInstanceId.length());
String indexStr = stateInstanceId.substring(start + 1);
try {
return Integer.parseInt(indexStr);
} catch (NumberFormatException e) {
Expand Down Expand Up @@ -635,7 +635,7 @@ public StateMachineInstance getStateMachineInstanceByBusinessKey(String business
private void deserializeParamsAndException(StateMachineInstance stateMachineInstance) {
byte[] serializedException = (byte[]) stateMachineInstance.getSerializedException();
if (serializedException != null) {
stateMachineInstance.setException((Exception) exceptionSerializer.deserialize(serializedException));
stateMachineInstance.setException(exceptionSerializer.deserialize(serializedException));
}

String serializedStartParams = (String) stateMachineInstance.getSerializedStartParams();
Expand Down Expand Up @@ -677,7 +677,7 @@ private void deserializeParamsAndException(StateInstance stateInstance) {
}
byte[] serializedException = (byte[]) stateInstance.getSerializedException();
if (serializedException != null) {
stateInstance.setException((Exception) exceptionSerializer.deserialize(serializedException));
stateInstance.setException(exceptionSerializer.deserialize(serializedException));
}
}
}
Expand Down
Expand Up @@ -44,7 +44,7 @@ public class ExceptionMatchEvaluator implements Evaluator {
public boolean evaluate(Map<String, Object> variables) {

Object eObj = variables.get(getRootObjectName());
if (eObj != null && (eObj instanceof Exception) && StringUtils.hasText(exceptionString)) {
if ((eObj instanceof Exception) && StringUtils.hasText(exceptionString)) {

Exception e = (Exception)eObj;

Expand Down
Expand Up @@ -295,8 +295,8 @@ protected StateMachineInstance forwardInternal(String stateMachineInstId, Map<St

String next = null;
State state = stateMachineInstance.getStateMachine().getState(EngineUtils.getOriginStateName(lastForwardState));
if (state != null && state instanceof AbstractTaskState) {
next = ((AbstractTaskState)state).getNext();
if (state instanceof AbstractTaskState) {
next = state.getNext();
}
if (StringUtils.isEmpty(next)) {
LOGGER.warn(
Expand Down
Expand Up @@ -139,7 +139,7 @@ private Instruction compensateRoute(ProcessContext context, State compensationTr

StateMachine stateMachine = (StateMachine)context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE);
State state = stateMachine.getState(EngineUtils.getOriginStateName(stateToBeCompensated));
if (state != null && state instanceof AbstractTaskState) {
if (state instanceof AbstractTaskState) {

AbstractTaskState taskState = (AbstractTaskState)state;

Expand Down
Expand Up @@ -30,5 +30,5 @@ public enum RecoverStrategy {
/**
* Forward
*/
Forward;
Forward
}

0 comments on commit 4c6d48e

Please sign in to comment.