1
1
/**
2
- * Copyright 2009-2017 the original author or authors.
2
+ * Copyright 2009-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
18
18
import org .apache .ibatis .annotations .Flush ;
19
19
import org .apache .ibatis .annotations .MapKey ;
20
20
import org .apache .ibatis .cursor .Cursor ;
21
- import org .apache .ibatis .io .Resources ;
22
21
import org .apache .ibatis .mapping .MappedStatement ;
23
22
import org .apache .ibatis .mapping .SqlCommandType ;
24
23
import org .apache .ibatis .mapping .StatementType ;
24
+ import org .apache .ibatis .reflection .Jdk ;
25
25
import org .apache .ibatis .reflection .MetaObject ;
26
+ import org .apache .ibatis .reflection .OptionalUtil ;
26
27
import org .apache .ibatis .reflection .ParamNameResolver ;
27
28
import org .apache .ibatis .reflection .TypeParameterResolver ;
28
29
import org .apache .ibatis .session .Configuration ;
44
45
*/
45
46
public class MapperMethod {
46
47
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
-
59
48
private final SqlCommand command ;
60
49
private final MethodSignature method ;
61
50
@@ -95,9 +84,8 @@ public Object execute(SqlSession sqlSession, Object[] args) {
95
84
} else {
96
85
Object param = method .convertArgsToSqlCommandParam (args );
97
86
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 );
101
89
}
102
90
}
103
91
break ;
@@ -114,20 +102,6 @@ public Object execute(SqlSession sqlSession, Object[] args) {
114
102
return result ;
115
103
}
116
104
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
-
131
105
private Object rowCountResult (int rowCount ) {
132
106
final Object result ;
133
107
if (method .returnsVoid ()) {
@@ -321,7 +295,7 @@ public MethodSignature(Configuration configuration, Class<?> mapperInterface, Me
321
295
this .returnsVoid = void .class .equals (this .returnType );
322
296
this .returnsMany = configuration .getObjectFactory ().isCollection (this .returnType ) || this .returnType .isArray ();
323
297
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 );
325
299
this .mapKey = getMapKey (method );
326
300
this .returnsMap = this .mapKey != null ;
327
301
this .rowBoundsIndex = getUniqueParamIndex (method , RowBounds .class );
0 commit comments