1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.ibatis.sqlmap.engine.mapping.result.loader;
17
18 import com.ibatis.common.beans.ClassInfo;
19 import com.ibatis.sqlmap.engine.impl.SqlMapClientImpl;
20 import com.ibatis.sqlmap.engine.type.DomTypeMarker;
21
22 import java.sql.SQLException;
23 import java.util.Collection;
24 import java.util.List;
25 import java.util.Set;
26
27 import net.sf.cglib.proxy.Enhancer;
28 import net.sf.cglib.proxy.LazyLoader;
29 import net.sf.cglib.proxy.NoOp;
30
31
32
33
34 public class EnhancedLazyResultLoader {
35
36
37 private static final Class[] SET_INTERFACES = { Set.class };
38
39
40 private static final Class[] LIST_INTERFACES = { List.class };
41
42
43 private Object loader;
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public EnhancedLazyResultLoader(SqlMapClientImpl client, String statementName, Object parameterObject,
58 Class targetType) {
59 loader = new EnhancedLazyResultLoaderImpl(client, statementName, parameterObject, targetType);
60 }
61
62
63
64
65
66
67
68
69
70 public Object loadResult() throws SQLException {
71 return ((EnhancedLazyResultLoaderImpl) loader).loadResult();
72 }
73
74
75
76
77 private static class EnhancedLazyResultLoaderImpl implements LazyLoader {
78
79
80 protected SqlMapClientImpl client;
81
82
83 protected String statementName;
84
85
86 protected Object parameterObject;
87
88
89 protected Class targetType;
90
91
92
93
94
95
96
97
98
99
100
101
102
103 public EnhancedLazyResultLoaderImpl(SqlMapClientImpl client, String statementName, Object parameterObject,
104 Class targetType) {
105 this.client = client;
106 this.statementName = statementName;
107 this.parameterObject = parameterObject;
108 this.targetType = targetType;
109 }
110
111
112
113
114
115
116
117
118
119 public Object loadResult() throws SQLException {
120 if (DomTypeMarker.class.isAssignableFrom(targetType)) {
121 return ResultLoader.getResult(client, statementName, parameterObject, targetType);
122 }
123 if (Collection.class.isAssignableFrom(targetType)) {
124 if (Set.class.isAssignableFrom(targetType)) {
125 return Enhancer.create(Object.class, SET_INTERFACES, this);
126 }
127 return Enhancer.create(Object.class, LIST_INTERFACES, this);
128 }
129 if (targetType.isArray() || ClassInfo.isKnownType(targetType)) {
130 return ResultLoader.getResult(client, statementName, parameterObject, targetType);
131 }
132 return Enhancer.create(targetType, this);
133 }
134
135 @Override
136 public Object loadObject() throws Exception {
137 try {
138 Object result = ResultLoader.getResult(client, statementName, parameterObject, targetType);
139 if (result == null) {
140
141
142 result = Enhancer.create(targetType, NoOp.INSTANCE);
143 }
144 return result;
145 } catch (SQLException e) {
146 throw new RuntimeException("Error lazy loading result. Cause: " + e, e);
147 }
148 }
149 }
150
151 }