Skip to content

Commit

Permalink
[ci] Cleanup if statement breaks / return logic
Browse files Browse the repository at this point in the history
  • Loading branch information
hazendaz committed Feb 21, 2023
1 parent abe66e1 commit 7f16cc3
Show file tree
Hide file tree
Showing 63 changed files with 471 additions and 538 deletions.
29 changes: 13 additions & 16 deletions src/main/java/org/apache/ibatis/binding/MapperMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,8 @@ private <E> Object executeForMany(SqlSession sqlSession, Object[] args) {
if (!method.getReturnType().isAssignableFrom(result.getClass())) {
if (method.getReturnType().isArray()) {
return convertToArray(result);
} else {
return convertToDeclaredCollection(sqlSession.getConfiguration(), result);
}
return convertToDeclaredCollection(sqlSession.getConfiguration(), result);
}
return result;
}
Expand Down Expand Up @@ -180,14 +179,13 @@ private <E> Object convertToDeclaredCollection(Configuration config, List<E> lis
private <E> Object convertToArray(List<E> list) {
Class<?> arrayComponentType = method.getReturnType().getComponentType();
Object array = Array.newInstance(arrayComponentType, list.size());
if (arrayComponentType.isPrimitive()) {
for (int i = 0; i < list.size(); i++) {
Array.set(array, i, list.get(i));
}
return array;
} else {
if (!arrayComponentType.isPrimitive()) {
return list.toArray((E[]) array);
}
for (int i = 0; i < list.size(); i++) {
Array.set(array, i, list.get(i));
}
return array;
}

private <K, V> Map<K, V> executeForMap(SqlSession sqlSession, Object[] args) {
Expand Down Expand Up @@ -226,13 +224,12 @@ public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method
final Class<?> declaringClass = method.getDeclaringClass();
MappedStatement ms = resolveMappedStatement(mapperInterface, methodName, declaringClass, configuration);
if (ms == null) {
if (method.getAnnotation(Flush.class) != null) {
name = null;
type = SqlCommandType.FLUSH;
} else {
if (method.getAnnotation(Flush.class) == null) {
throw new BindingException(
"Invalid bound statement (not found): " + mapperInterface.getName() + "." + methodName);
}
name = null;
type = SqlCommandType.FLUSH;
} else {
name = ms.getId();
type = ms.getSqlCommandType();
Expand All @@ -255,7 +252,8 @@ private MappedStatement resolveMappedStatement(Class<?> mapperInterface, String
String statementId = mapperInterface.getName() + "." + methodName;
if (configuration.hasStatement(statementId)) {
return configuration.getMappedStatement(statementId);
} else if (mapperInterface.equals(declaringClass)) {
}
if (mapperInterface.equals(declaringClass)) {
return null;
}
for (Class<?> superInterface : mapperInterface.getInterfaces()) {
Expand Down Expand Up @@ -359,12 +357,11 @@ private Integer getUniqueParamIndex(Method method, Class<?> paramType) {
final Class<?>[] argTypes = method.getParameterTypes();
for (int i = 0; i < argTypes.length; i++) {
if (paramType.isAssignableFrom(argTypes[i])) {
if (index == null) {
index = i;
} else {
if (index != null) {
throw new BindingException(
method.getName() + " cannot have multiple " + paramType.getSimpleName() + " parameters");
}
index = i;
}
}
return index;
Expand Down
28 changes: 13 additions & 15 deletions src/main/java/org/apache/ibatis/binding/MapperProxy.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2022 the original author or authors.
* Copyright 2009-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -82,9 +82,8 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
try {
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(this, args);
} else {
return cachedInvoker(method).invoke(proxy, method, args, sqlSession);
}
return cachedInvoker(method).invoke(proxy, method, args, sqlSession);
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
Expand All @@ -93,20 +92,19 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
private MapperMethodInvoker cachedInvoker(Method method) throws Throwable {
try {
return MapUtil.computeIfAbsent(methodCache, method, m -> {
if (m.isDefault()) {
try {
if (privateLookupInMethod == null) {
return new DefaultMethodInvoker(getMethodHandleJava8(method));
} else {
return new DefaultMethodInvoker(getMethodHandleJava9(method));
}
} catch (IllegalAccessException | InstantiationException | InvocationTargetException
| NoSuchMethodException e) {
throw new RuntimeException(e);
}
} else {
if (!m.isDefault()) {
return new PlainMethodInvoker(new MapperMethod(mapperInterface, method, sqlSession.getConfiguration()));
}
try {
if (privateLookupInMethod == null) {
return new DefaultMethodInvoker(getMethodHandleJava8(method));
} else {
return new DefaultMethodInvoker(getMethodHandleJava9(method));
}
} catch (IllegalAccessException | InstantiationException | InvocationTargetException
| NoSuchMethodException e) {
throw new RuntimeException(e);
}
});
} catch (RuntimeException re) {
Throwable cause = re.getCause();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2022 the original author or authors.
* Copyright 2009-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -106,11 +106,10 @@ private void jdbcTypeOpt(String expression, int p) {
private void jdbcType(String expression, int p) {
int left = skipWS(expression, p);
int right = skipUntil(expression, left, ",");
if (right > left) {
put("jdbcType", trimmedStr(expression, left, right));
} else {
if (right <= left) {
throw new BuilderException("Parsing error in {" + expression + "} in position " + p);
}
put("jdbcType", trimmedStr(expression, left, right));
option(expression, right + 1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,8 @@ private SqlSource buildSqlSource(Annotation annotation, Class<?> parameterType,
Method method) {
if (annotation instanceof Select) {
return buildSqlSourceFromStrings(((Select) annotation).value(), parameterType, languageDriver);
} else if (annotation instanceof Update) {
}
if (annotation instanceof Update) {
return buildSqlSourceFromStrings(((Update) annotation).value(), parameterType, languageDriver);
} else if (annotation instanceof Insert) {
return buildSqlSourceFromStrings(((Insert) annotation).value(), parameterType, languageDriver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ default Method resolveMethod(ProviderContext context) {
if (targetMethods.isEmpty()) {
throw new BuilderException("Cannot resolve the provider method because '" + context.getMapperMethod().getName()
+ "' does not return the CharSequence or its subclass in SqlProvider '" + getClass().getName() + "'.");
} else {
throw new BuilderException("Cannot resolve the provider method because '" + context.getMapperMethod().getName()
+ "' is found multiple in SqlProvider '" + getClass().getName() + "'.");
}
throw new BuilderException("Cannot resolve the provider method because '" + context.getMapperMethod().getName()
+ "' is found multiple in SqlProvider '" + getClass().getName() + "'.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -180,21 +180,26 @@ private SqlSource createSqlSource(Object parameterObject) {
Map<String, Object> params = (Map<String, Object>) parameterObject;
sql = invokeProviderMethod(extractProviderMethodArguments(params, providerMethodArgumentNames));
}
} else if (providerMethodParameterTypes.length == 0) {
sql = invokeProviderMethod();
} else if (providerMethodParameterTypes.length == 1) {
if (providerContext == null) {
sql = invokeProviderMethod(parameterObject);
} else {
sql = invokeProviderMethod(providerContext);
} else
switch (providerMethodParameterTypes.length) {
case 0:
sql = invokeProviderMethod();
break;
case 1:
if (providerContext == null) {
sql = invokeProviderMethod(parameterObject);
} else {
sql = invokeProviderMethod(providerContext);
}
break;
case 2:
sql = invokeProviderMethod(extractProviderMethodArguments(parameterObject));
break;
default:
throw new BuilderException("Cannot invoke SqlProvider method '" + providerMethod
+ "' with specify parameter '" + (parameterObject == null ? null : parameterObject.getClass())
+ "' because SqlProvider method arguments for '" + mapperMethod + "' is an invalid combination.");
}
} else if (providerMethodParameterTypes.length == 2) {
sql = invokeProviderMethod(extractProviderMethodArguments(parameterObject));
} else {
throw new BuilderException("Cannot invoke SqlProvider method '" + providerMethod + "' with specify parameter '"
+ (parameterObject == null ? null : parameterObject.getClass())
+ "' because SqlProvider method arguments for '" + mapperMethod + "' is an invalid combination.");
}
Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();
return languageDriver.createSqlSource(configuration, sql, parameterType);
} catch (BuilderException e) {
Expand All @@ -219,9 +224,8 @@ private Object[] extractProviderMethodArguments(Object parameterObject) {
args[providerContextIndex == 0 ? 1 : 0] = parameterObject;
args[providerContextIndex] = providerContext;
return args;
} else {
return new Object[] { parameterObject };
}
return new Object[] { parameterObject };
}

private Object[] extractProviderMethodArguments(Map<String, Object> params, String[] argumentNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,10 @@ private Properties getVariablesContext(Node node, Properties inheritedVariablesC
}
if (declaredProperties == null) {
return inheritedVariablesContext;
} else {
Properties newProperties = new Properties();
newProperties.putAll(inheritedVariablesContext);
newProperties.putAll(declaredProperties);
return newProperties;
}
Properties newProperties = new Properties();
newProperties.putAll(inheritedVariablesContext);
newProperties.putAll(declaredProperties);
return newProperties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public InputSource resolveEntity(String publicId, String systemId) throws SAXExc
String lowerCaseSystemId = systemId.toLowerCase(Locale.ENGLISH);
if (lowerCaseSystemId.contains(MYBATIS_CONFIG_SYSTEM) || lowerCaseSystemId.contains(IBATIS_CONFIG_SYSTEM)) {
return getInputSource(MYBATIS_CONFIG_DTD, publicId, systemId);
} else if (lowerCaseSystemId.contains(MYBATIS_MAPPER_SYSTEM)
|| lowerCaseSystemId.contains(IBATIS_MAPPER_SYSTEM)) {
}
if (lowerCaseSystemId.contains(MYBATIS_MAPPER_SYSTEM) || lowerCaseSystemId.contains(IBATIS_MAPPER_SYSTEM)) {
return getInputSource(MYBATIS_MAPPER_DTD, publicId, systemId);
}
}
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/org/apache/ibatis/cache/CacheKey.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2022 the original author or authors.
* Copyright 2009-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -98,13 +98,7 @@ public boolean equals(Object object) {

final CacheKey cacheKey = (CacheKey) object;

if (hashcode != cacheKey.hashcode) {
return false;
}
if (checksum != cacheKey.checksum) {
return false;
}
if (count != cacheKey.count) {
if ((hashcode != cacheKey.hashcode) || (checksum != cacheKey.checksum) || (count != cacheKey.count)) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2022 the original author or authors.
* Copyright 2009-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -52,11 +52,10 @@ public int getSize() {

@Override
public void putObject(Object key, Object object) {
if (object == null || object instanceof Serializable) {
delegate.putObject(key, serialize((Serializable) object));
} else {
if ((object != null) && !(object instanceof Serializable)) {
throw new CacheException("SharedCache failed to make a copy of a non-serializable object: " + object);
}
delegate.putObject(key, serialize((Serializable) object));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ public Object getObject(Object key) {
// issue #146
if (clearOnCommit) {
return null;
} else {
return object;
}
return object;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ public int hashCode() {
public boolean equals(Object obj) {
if (obj instanceof PooledConnection) {
return realConnection.hashCode() == ((PooledConnection) obj).realConnection.hashCode();
} else if (obj instanceof Connection) {
}
if (obj instanceof Connection) {
return hashCode == obj.hashCode();
} else {
return false;
Expand Down
Loading

0 comments on commit 7f16cc3

Please sign in to comment.