Skip to content

Commit ad7101e

Browse files
committed
Remove reflection.
1 parent 93beec0 commit ad7101e

File tree

3 files changed

+53
-34
lines changed

3 files changed

+53
-34
lines changed

src/main/java/org/apache/ibatis/binding/MapperMethod.java

+6-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2017 the original author or authors.
2+
* Copyright 2009-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,11 +18,12 @@
1818
import org.apache.ibatis.annotations.Flush;
1919
import org.apache.ibatis.annotations.MapKey;
2020
import org.apache.ibatis.cursor.Cursor;
21-
import org.apache.ibatis.io.Resources;
2221
import org.apache.ibatis.mapping.MappedStatement;
2322
import org.apache.ibatis.mapping.SqlCommandType;
2423
import org.apache.ibatis.mapping.StatementType;
24+
import org.apache.ibatis.reflection.Jdk;
2525
import org.apache.ibatis.reflection.MetaObject;
26+
import org.apache.ibatis.reflection.OptionalUtil;
2627
import org.apache.ibatis.reflection.ParamNameResolver;
2728
import org.apache.ibatis.reflection.TypeParameterResolver;
2829
import org.apache.ibatis.session.Configuration;
@@ -44,18 +45,6 @@
4445
*/
4546
public class MapperMethod {
4647

47-
private static Method optionalFactoryMethod = null;
48-
49-
static {
50-
try {
51-
optionalFactoryMethod = Resources.classForName("java.util.Optional").getMethod("ofNullable", Object.class);
52-
} catch (ClassNotFoundException e) {
53-
// Ignore
54-
} catch (NoSuchMethodException e) {
55-
// Ignore
56-
}
57-
}
58-
5948
private final SqlCommand command;
6049
private final MethodSignature method;
6150

@@ -95,9 +84,8 @@ public Object execute(SqlSession sqlSession, Object[] args) {
9584
} else {
9685
Object param = method.convertArgsToSqlCommandParam(args);
9786
result = sqlSession.selectOne(command.getName(), param);
98-
if (method.returnsOptional() &&
99-
(result == null || !method.getReturnType().equals(result.getClass()))) {
100-
result = wrapWithOptional(result);
87+
if (method.returnsOptional()) {
88+
result = OptionalUtil.ofNullable(result);
10189
}
10290
}
10391
break;
@@ -114,20 +102,6 @@ public Object execute(SqlSession sqlSession, Object[] args) {
114102
return result;
115103
}
116104

117-
private Object wrapWithOptional(Object result) {
118-
if (optionalFactoryMethod == null) {
119-
throw new BindingException("Can't use the java.util.Optional");
120-
}
121-
try {
122-
return optionalFactoryMethod.invoke(null, result);
123-
} catch (IllegalAccessException e) {
124-
throw new BindingException("Can't create a java.util.Optional instance.", e);
125-
} catch (InvocationTargetException e) {
126-
throw new BindingException("Can't create a java.util.Optional instance.", e);
127-
}
128-
}
129-
130-
131105
private Object rowCountResult(int rowCount) {
132106
final Object result;
133107
if (method.returnsVoid()) {
@@ -321,7 +295,7 @@ public MethodSignature(Configuration configuration, Class<?> mapperInterface, Me
321295
this.returnsVoid = void.class.equals(this.returnType);
322296
this.returnsMany = configuration.getObjectFactory().isCollection(this.returnType) || this.returnType.isArray();
323297
this.returnsCursor = Cursor.class.equals(this.returnType);
324-
this.returnsOptional = "java.util.Optional".equals(this.returnType.getName());
298+
this.returnsOptional = Jdk.optionalExists && Optional.class.equals(this.returnType);
325299
this.mapKey = getMapKey(method);
326300
this.returnsMap = this.mapKey != null;
327301
this.rowBoundsIndex = getUniqueParamIndex(method, RowBounds.class);

src/main/java/org/apache/ibatis/reflection/Jdk.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2017 the original author or authors.
2+
* Copyright 2009-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,7 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.apache.ibatis.reflection;
1817

1918
import org.apache.ibatis.io.Resources;
@@ -52,6 +51,19 @@ public class Jdk {
5251
dateAndTimeApiExists = available;
5352
}
5453

54+
public static final boolean optionalExists;
55+
56+
static {
57+
boolean available = false;
58+
try {
59+
Resources.classForName("java.util.Optional");
60+
available = true;
61+
} catch (ClassNotFoundException e) {
62+
// ignore
63+
}
64+
optionalExists = available;
65+
}
66+
5567
private Jdk() {
5668
super();
5769
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright 2009-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.apache.ibatis.reflection;
18+
19+
import java.util.Optional;
20+
21+
import org.apache.ibatis.lang.UsesJava8;
22+
23+
public abstract class OptionalUtil {
24+
25+
@UsesJava8
26+
public static Object ofNullable(Object value) {
27+
return Optional.ofNullable(value);
28+
}
29+
30+
private OptionalUtil() {
31+
super();
32+
}
33+
}

0 commit comments

Comments
 (0)