| 1 | /* |
| 2 | * Copyright 2007 brunella ltd |
| 3 | * |
| 4 | * Licensed under the LGPL Version 3 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * |
| 7 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 8 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 9 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 10 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 11 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 12 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 13 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 14 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 15 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 16 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 17 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 18 | */ |
| 19 | package sf.qof.codegen; |
| 20 | |
| 21 | import static sf.qof.codegen.Constants.SIG_getBoolean; |
| 22 | import static sf.qof.codegen.Constants.SIG_getBooleanNamed; |
| 23 | import static sf.qof.codegen.Constants.SIG_getByte; |
| 24 | import static sf.qof.codegen.Constants.SIG_getByteNamed; |
| 25 | import static sf.qof.codegen.Constants.SIG_getDate; |
| 26 | import static sf.qof.codegen.Constants.SIG_getDateNamed; |
| 27 | import static sf.qof.codegen.Constants.SIG_getDouble; |
| 28 | import static sf.qof.codegen.Constants.SIG_getDoubleNamed; |
| 29 | import static sf.qof.codegen.Constants.SIG_getFloat; |
| 30 | import static sf.qof.codegen.Constants.SIG_getFloatNamed; |
| 31 | import static sf.qof.codegen.Constants.SIG_getInt; |
| 32 | import static sf.qof.codegen.Constants.SIG_getIntNamed; |
| 33 | import static sf.qof.codegen.Constants.SIG_getLong; |
| 34 | import static sf.qof.codegen.Constants.SIG_getLongNamed; |
| 35 | import static sf.qof.codegen.Constants.SIG_getShort; |
| 36 | import static sf.qof.codegen.Constants.SIG_getShortNamed; |
| 37 | import static sf.qof.codegen.Constants.SIG_getString; |
| 38 | import static sf.qof.codegen.Constants.SIG_getStringNamed; |
| 39 | import static sf.qof.codegen.Constants.SIG_getTime; |
| 40 | import static sf.qof.codegen.Constants.SIG_getTimeLong; |
| 41 | import static sf.qof.codegen.Constants.SIG_getTimeNamed; |
| 42 | import static sf.qof.codegen.Constants.SIG_getTimestamp; |
| 43 | import static sf.qof.codegen.Constants.SIG_getTimestampNamed; |
| 44 | import static sf.qof.codegen.Constants.SIG_wasNull; |
| 45 | import static sf.qof.codegen.Constants.TYPE_Character; |
| 46 | import static sf.qof.codegen.Constants.TYPE_Date; |
| 47 | import static sf.qof.codegen.Constants.TYPE_String; |
| 48 | import static sf.qof.codegen.Constants.TYPE_boolean; |
| 49 | import static sf.qof.codegen.Constants.TYPE_byte; |
| 50 | import static sf.qof.codegen.Constants.TYPE_double; |
| 51 | import static sf.qof.codegen.Constants.TYPE_float; |
| 52 | import static sf.qof.codegen.Constants.TYPE_int; |
| 53 | import static sf.qof.codegen.Constants.TYPE_long; |
| 54 | import static sf.qof.codegen.Constants.TYPE_short; |
| 55 | import static sf.qof.codegen.Constants.TYPE_sqlDate; |
| 56 | import static sf.qof.codegen.Constants.TYPE_sqlTime; |
| 57 | import static sf.qof.codegen.Constants.TYPE_sqlTimestamp; |
| 58 | |
| 59 | import java.lang.reflect.Method; |
| 60 | |
| 61 | import net.sf.cglib.core.CodeEmitter; |
| 62 | import net.sf.cglib.core.Local; |
| 63 | import net.sf.cglib.core.Signature; |
| 64 | |
| 65 | import org.objectweb.asm.Label; |
| 66 | import org.objectweb.asm.Type; |
| 67 | |
| 68 | import sf.qof.adapter.DynamicMappingAdapter; |
| 69 | import sf.qof.adapter.GeneratorMappingAdapter; |
| 70 | import sf.qof.mapping.AbstractCharacterMapping; |
| 71 | import sf.qof.mapping.AbstractDateTimeMapping; |
| 72 | import sf.qof.mapping.AbstractNumberMapping; |
| 73 | import sf.qof.mapping.AdapterMapping; |
| 74 | import sf.qof.mapping.CharacterMappingVisitor; |
| 75 | import sf.qof.mapping.DateTimeMappingVisitor; |
| 76 | import sf.qof.mapping.Mapper; |
| 77 | import sf.qof.mapping.MappingVisitor; |
| 78 | import sf.qof.mapping.NumberMappingVisitor; |
| 79 | import sf.qof.mapping.ResultMapping; |
| 80 | import sf.qof.mapping.AbstractCharacterMapping.CharacterMapping; |
| 81 | import sf.qof.mapping.AbstractCharacterMapping.StringMapping; |
| 82 | import sf.qof.mapping.AbstractDateTimeMapping.DateMapping; |
| 83 | import sf.qof.mapping.AbstractDateTimeMapping.TimeMapping; |
| 84 | import sf.qof.mapping.AbstractDateTimeMapping.TimestampMapping; |
| 85 | import sf.qof.mapping.AbstractNumberMapping.BooleanMapping; |
| 86 | import sf.qof.mapping.AbstractNumberMapping.ByteMapping; |
| 87 | import sf.qof.mapping.AbstractNumberMapping.DoubleMapping; |
| 88 | import sf.qof.mapping.AbstractNumberMapping.FloatMapping; |
| 89 | import sf.qof.mapping.AbstractNumberMapping.IntegerMapping; |
| 90 | import sf.qof.mapping.AbstractNumberMapping.LongMapping; |
| 91 | import sf.qof.mapping.AbstractNumberMapping.ShortMapping; |
| 92 | import sf.qof.util.ReflectionUtils; |
| 93 | |
| 94 | /** |
| 95 | * Internal - ResultMappingGenerator is the generator class for result mappings. |
| 96 | */ |
| 97 | public class ResultMappingGenerator implements MappingVisitor, NumberMappingVisitor, CharacterMappingVisitor, |
| 98 | DateTimeMappingVisitor { |
| 99 | |
| 100 | private CodeEmitter co; |
| 101 | private Local resultSetOrCallableStatement; |
| 102 | private Local result; |
| 103 | private Local mapKey; |
| 104 | private boolean constructorMappings; |
| 105 | private Local[] constructorParameters; |
| 106 | |
| 107 | public ResultMappingGenerator(CodeEmitter co, Local resultSetOrCallableStatement, Local result, Local mapKey, |
| 108 | boolean constructorMappings, Local[] constructorParameters) { |
| 109 | this.co = co; |
| 110 | this.resultSetOrCallableStatement = resultSetOrCallableStatement; |
| 111 | this.result = result; |
| 112 | this.mapKey = mapKey; |
| 113 | this.constructorMappings = constructorMappings; |
| 114 | this.constructorParameters = constructorParameters; |
| 115 | } |
| 116 | |
| 117 | // implementation of MappingVisitor |
| 118 | |
| 119 | public final void visit(Mapper mapper, AbstractNumberMapping mapping) { |
| 120 | mapping.accept(mapper, (NumberMappingVisitor) this); |
| 121 | } |
| 122 | |
| 123 | public final void visit(Mapper mapper, AbstractCharacterMapping mapping) { |
| 124 | mapping.accept(mapper, (CharacterMappingVisitor) this); |
| 125 | } |
| 126 | |
| 127 | public final void visit(Mapper mapper, AbstractDateTimeMapping mapping) { |
| 128 | mapping.accept(mapper, (DateTimeMappingVisitor) this); |
| 129 | } |
| 130 | |
| 131 | // implementation of NumberMappingVisitor |
| 132 | |
| 133 | public void visit(Mapper mapper, ByteMapping mapping) { |
| 134 | generateResultMapping(mapping, TYPE_byte, SIG_getByte, SIG_getByteNamed); |
| 135 | } |
| 136 | |
| 137 | public void visit(Mapper mapper, ShortMapping mapping) { |
| 138 | generateResultMapping(mapping, TYPE_short, SIG_getShort, SIG_getShortNamed); |
| 139 | } |
| 140 | |
| 141 | public void visit(Mapper mapper, IntegerMapping mapping) { |
| 142 | generateResultMapping(mapping, TYPE_int, SIG_getInt, SIG_getIntNamed); |
| 143 | } |
| 144 | |
| 145 | public void visit(Mapper mapper, LongMapping mapping) { |
| 146 | generateResultMapping(mapping, TYPE_long, SIG_getLong, SIG_getLongNamed); |
| 147 | } |
| 148 | |
| 149 | public void visit(Mapper mapper, FloatMapping mapping) { |
| 150 | generateResultMapping(mapping, TYPE_float, SIG_getFloat, SIG_getFloatNamed); |
| 151 | } |
| 152 | |
| 153 | public void visit(Mapper mapper, DoubleMapping mapping) { |
| 154 | generateResultMapping(mapping, TYPE_double, SIG_getDouble, SIG_getDoubleNamed); |
| 155 | } |
| 156 | |
| 157 | public void visit(Mapper mapper, BooleanMapping mapping) { |
| 158 | generateResultMapping(mapping, TYPE_boolean, SIG_getBoolean, SIG_getBooleanNamed); |
| 159 | } |
| 160 | |
| 161 | private void generateResultMapping(ResultMapping mapping, Type primitiveType, Signature signatureGet, |
| 162 | Signature signatureGetNamed) { |
| 163 | if ((constructorMappings && mapping.getConstructorParameter() == null) |
| 164 | ||(!constructorMappings && mapping.getConstructorParameter() != null)) { |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | int sqlIndex = getSqlIndex(mapping); |
| 169 | String sqlColumn = getSqlColumn(mapping); |
| 170 | Method setter = mapping.getSetter(); |
| 171 | Class<?> type = mapping.getType(); |
| 172 | Local localResult = null; |
| 173 | |
| 174 | if (constructorMappings) { |
| 175 | type = mapping.getConstructor().getParameterTypes()[mapping.getConstructorParameter() - 1]; |
| 176 | localResult = co.make_local(Type.getType(type)); |
| 177 | constructorParameters[mapping.getConstructorParameter() - 1] = localResult; |
| 178 | } else { |
| 179 | if (mapping.isMapKey()) { |
| 180 | localResult = mapKey; |
| 181 | } else { |
| 182 | localResult = result; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | if (!type.isPrimitive()) { |
| 187 | // -------- |
| 188 | Local localValue = co.make_local(primitiveType); |
| 189 | Label labelWasNull = co.make_label(); |
| 190 | Label labelEnd = co.make_label(); |
| 191 | co.load_local(resultSetOrCallableStatement); |
| 192 | if (sqlColumn == null) { |
| 193 | co.push(sqlIndex); |
| 194 | co.invoke_interface(resultSetOrCallableStatement.getType(), signatureGet); |
| 195 | } else { |
| 196 | co.push(sqlColumn); |
| 197 | co.invoke_interface(resultSetOrCallableStatement.getType(), signatureGetNamed); |
| 198 | } |
| 199 | co.store_local(localValue); |
| 200 | co.load_local(resultSetOrCallableStatement); |
| 201 | co.invoke_interface(resultSetOrCallableStatement.getType(), SIG_wasNull); |
| 202 | co.if_jump(CodeEmitter.NE, labelWasNull); |
| 203 | |
| 204 | if (setter != null) { |
| 205 | co.load_local(localResult); |
| 206 | co.load_local(localValue); |
| 207 | EmitUtils.boxUsingValueOf(co, primitiveType); |
| 208 | co.invoke_virtual(Type.getType(setter.getDeclaringClass()), ReflectionUtils.getMethodSignature(setter)); |
| 209 | co.goTo(labelEnd); |
| 210 | |
| 211 | co.mark(labelWasNull); |
| 212 | co.load_local(localResult); |
| 213 | co.aconst_null(); |
| 214 | co.invoke_virtual(Type.getType(setter.getDeclaringClass()), ReflectionUtils.getMethodSignature(setter)); |
| 215 | } else { |
| 216 | co.load_local(localValue); |
| 217 | EmitUtils.boxUsingValueOf(co, primitiveType); |
| 218 | co.store_local(localResult); |
| 219 | co.goTo(labelEnd); |
| 220 | |
| 221 | co.mark(labelWasNull); |
| 222 | co.aconst_null(); |
| 223 | co.store_local(localResult); |
| 224 | } |
| 225 | co.mark(labelEnd); |
| 226 | } else { |
| 227 | // it's an int, etc |
| 228 | // ... = getInt(index); |
| 229 | if (setter != null) { |
| 230 | co.load_local(localResult); |
| 231 | } |
| 232 | co.load_local(resultSetOrCallableStatement); |
| 233 | if (sqlColumn == null) { |
| 234 | co.push(sqlIndex); |
| 235 | co.invoke_interface(resultSetOrCallableStatement.getType(), signatureGet); |
| 236 | } else { |
| 237 | co.push(sqlColumn); |
| 238 | co.invoke_interface(resultSetOrCallableStatement.getType(), signatureGetNamed); |
| 239 | } |
| 240 | if (setter != null) { |
| 241 | co.invoke_virtual(Type.getType(setter.getDeclaringClass()), ReflectionUtils.getMethodSignature(setter)); |
| 242 | } else { |
| 243 | co.store_local(localResult); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // implementation of CharacterMappingVisitor |
| 249 | |
| 250 | public final void visit(Mapper mapper, StringMapping mapping) { |
| 251 | if ((constructorMappings && mapping.getConstructorParameter() == null) |
| 252 | ||(!constructorMappings && mapping.getConstructorParameter() != null)) { |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | generateGetString(mapping); |
| 257 | generateStoreString(mapping); |
| 258 | } |
| 259 | |
| 260 | public final void visit(Mapper mapper, CharacterMapping mapping) { |
| 261 | if ((constructorMappings && mapping.getConstructorParameter() == null) |
| 262 | ||(!constructorMappings && mapping.getConstructorParameter() != null)) { |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | generateGetString(mapping); |
| 267 | |
| 268 | Class<?> type; |
| 269 | if (constructorMappings) { |
| 270 | type = mapping.getConstructor().getParameterTypes()[mapping.getConstructorParameter() - 1]; |
| 271 | } else { |
| 272 | type = mapping.getType(); |
| 273 | } |
| 274 | // get first char: |
| 275 | // Character c = (s == null) || (s.length() == 0) ? null : |
| 276 | // Character.valueOf(s.charAt(0)); |
| 277 | // char c = (s == null) || (s.length() == 0) ? 0 : s.charAt(0); |
| 278 | Local localValue = co.make_local(TYPE_String); |
| 279 | co.store_local(localValue); |
| 280 | |
| 281 | Label labelNull = co.make_label(); |
| 282 | Label labelEnd = co.make_label(); |
| 283 | |
| 284 | co.load_local(localValue); |
| 285 | co.ifnull(labelNull); |
| 286 | co.load_local(localValue); |
| 287 | co.invoke_virtual(TYPE_String, new Signature("length", "()I")); |
| 288 | co.if_jump(CodeEmitter.EQ, labelNull); |
| 289 | co.load_local(localValue); |
| 290 | co.push(0); |
| 291 | co.invoke_virtual(TYPE_String, new Signature("charAt", "(I)C")); |
| 292 | if (!type.isPrimitive()) { |
| 293 | co.invoke_static(TYPE_Character, new Signature("valueOf", "(C)Ljava/lang/Character;")); |
| 294 | } |
| 295 | co.goTo(labelEnd); |
| 296 | |
| 297 | co.mark(labelNull); |
| 298 | if (type.isPrimitive()) { |
| 299 | co.push(0); |
| 300 | } else { |
| 301 | co.aconst_null(); |
| 302 | } |
| 303 | co.mark(labelEnd); |
| 304 | |
| 305 | generateStoreString(mapping); |
| 306 | } |
| 307 | |
| 308 | private void generateGetString(ResultMapping mapping) { |
| 309 | int sqlIndex = getSqlIndex(mapping); |
| 310 | String sqlColumn = getSqlColumn(mapping); |
| 311 | Method setter = mapping.getSetter(); |
| 312 | |
| 313 | // ... = getString(index); |
| 314 | if (setter != null) { |
| 315 | if (mapping.isMapKey()) { |
| 316 | co.load_local(mapKey); |
| 317 | } else { |
| 318 | co.load_local(result); |
| 319 | } |
| 320 | } |
| 321 | co.load_local(resultSetOrCallableStatement); |
| 322 | if (sqlColumn == null) { |
| 323 | co.push(sqlIndex); |
| 324 | co.invoke_interface(resultSetOrCallableStatement.getType(), SIG_getString); |
| 325 | } else { |
| 326 | co.push(sqlColumn); |
| 327 | co.invoke_interface(resultSetOrCallableStatement.getType(), SIG_getStringNamed); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | private void generateStoreString(ResultMapping mapping) { |
| 332 | Method setter = mapping.getSetter(); |
| 333 | Class<?> type = mapping.getType(); |
| 334 | Local localResult = null; |
| 335 | |
| 336 | if (constructorMappings) { |
| 337 | type = mapping.getConstructor().getParameterTypes()[mapping.getConstructorParameter() - 1]; |
| 338 | localResult = co.make_local(Type.getType(type)); |
| 339 | constructorParameters[mapping.getConstructorParameter() - 1] = localResult; |
| 340 | } else { |
| 341 | if (mapping.isMapKey()) { |
| 342 | localResult = mapKey; |
| 343 | } else { |
| 344 | localResult = result; |
| 345 | } |
| 346 | } |
| 347 | if (setter != null) { |
| 348 | co.invoke_virtual(Type.getType(setter.getDeclaringClass()), ReflectionUtils.getMethodSignature(setter)); |
| 349 | } else { |
| 350 | co.store_local(localResult); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | // implementation of DateTimeVisitor |
| 355 | |
| 356 | public void visit(Mapper mapper, DateMapping mapping) { |
| 357 | generateParameterMapping(mapping, TYPE_sqlDate, SIG_getDate, SIG_getDateNamed); |
| 358 | } |
| 359 | |
| 360 | public void visit(Mapper mapper, TimeMapping mapping) { |
| 361 | generateParameterMapping(mapping, TYPE_sqlTime, SIG_getTime, SIG_getTimeNamed); |
| 362 | } |
| 363 | |
| 364 | public void visit(Mapper mapper, TimestampMapping mapping) { |
| 365 | generateParameterMapping(mapping, TYPE_sqlTimestamp, SIG_getTimestamp, SIG_getTimestampNamed); |
| 366 | } |
| 367 | |
| 368 | public final void generateParameterMapping(AbstractDateTimeMapping mapping, Type sqlType, Signature sqlTypeGet, Signature sqlTypeGetNamed) { |
| 369 | if ((constructorMappings && mapping.getConstructorParameter() == null) |
| 370 | ||(!constructorMappings && mapping.getConstructorParameter() != null)) { |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | int sqlIndex = getSqlIndex(mapping); |
| 375 | String sqlColumn = getSqlColumn(mapping); |
| 376 | Method setter = mapping.getSetter(); |
| 377 | Class<?> type = mapping.getType(); |
| 378 | Local localResult = null; |
| 379 | |
| 380 | if (constructorMappings) { |
| 381 | type = mapping.getConstructor().getParameterTypes()[mapping.getConstructorParameter() - 1]; |
| 382 | localResult = co.make_local(Type.getType(type)); |
| 383 | constructorParameters[mapping.getConstructorParameter() - 1] = localResult; |
| 384 | } else { |
| 385 | if (mapping.isMapKey()) { |
| 386 | localResult = mapKey; |
| 387 | } else { |
| 388 | localResult = result; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | // ... = getDate(index); |
| 393 | if (setter != null) { |
| 394 | if (mapping.isMapKey()) { |
| 395 | co.load_local(mapKey); |
| 396 | } else { |
| 397 | co.load_local(localResult); |
| 398 | } |
| 399 | } |
| 400 | co.load_local(resultSetOrCallableStatement); |
| 401 | if (sqlColumn == null) { |
| 402 | co.push(sqlIndex); |
| 403 | co.invoke_interface(resultSetOrCallableStatement.getType(), sqlTypeGet); |
| 404 | } else { |
| 405 | co.push(sqlColumn); |
| 406 | co.invoke_interface(resultSetOrCallableStatement.getType(), sqlTypeGetNamed); |
| 407 | } |
| 408 | // convert java.sql.Date to java.util.Date |
| 409 | Local date = co.make_local(sqlType); |
| 410 | co.store_local(date); |
| 411 | Label labelNull = co.make_label(); |
| 412 | Label labelEnd = co.make_label(); |
| 413 | co.load_local(date); |
| 414 | co.ifnull(labelNull); |
| 415 | |
| 416 | co.new_instance(TYPE_Date); |
| 417 | co.dup(); |
| 418 | co.load_local(date); |
| 419 | co.invoke_virtual(sqlType, SIG_getTimeLong); |
| 420 | co.invoke_constructor(TYPE_Date, new Signature("<init>", "(J)V")); |
| 421 | co.goTo(labelEnd); |
| 422 | |
| 423 | co.mark(labelNull); |
| 424 | co.aconst_null(); |
| 425 | |
| 426 | co.mark(labelEnd); |
| 427 | if (setter != null) { |
| 428 | co.invoke_virtual(Type.getType(setter.getDeclaringClass()), ReflectionUtils.getMethodSignature(setter)); |
| 429 | } else { |
| 430 | if (mapping.isMapKey()) { |
| 431 | co.store_local(mapKey); |
| 432 | } else { |
| 433 | co.store_local(localResult); |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | public final void visit(Mapper mapper, AdapterMapping mapping) { |
| 439 | if ((constructorMappings && mapping.getConstructorParameter() == null) |
| 440 | ||(!constructorMappings && mapping.getConstructorParameter() != null)) { |
| 441 | return; |
| 442 | } |
| 443 | |
| 444 | int[] sqlIndexes = mapping.getSqlIndexes(); |
| 445 | String[] sqlColumns = mapping.getSqlColumns(); |
| 446 | Method setter = mapping.getSetter(); |
| 447 | Class<?> type = mapping.getType(); |
| 448 | Local localResult = null; |
| 449 | |
| 450 | if (constructorMappings) { |
| 451 | type = mapping.getConstructor().getParameterTypes()[mapping.getConstructorParameter() - 1]; |
| 452 | localResult = co.make_local(Type.getType(type)); |
| 453 | constructorParameters[mapping.getConstructorParameter() - 1] = localResult; |
| 454 | } else { |
| 455 | if (mapping.isMapKey()) { |
| 456 | localResult = mapKey; |
| 457 | } else { |
| 458 | localResult = result; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | if (setter != null) { |
| 463 | if (mapping.isMapKey()) { |
| 464 | co.load_local(mapKey); |
| 465 | } else { |
| 466 | co.load_local(localResult); |
| 467 | } |
| 468 | } |
| 469 | if (mapping.getAdapter() instanceof GeneratorMappingAdapter) { |
| 470 | if (sqlColumns != null) { |
| 471 | ((GeneratorMappingAdapter) mapping.getAdapter()).generateFromResultSet(mapping, co, resultSetOrCallableStatement, sqlColumns); |
| 472 | } else { |
| 473 | ((GeneratorMappingAdapter) mapping.getAdapter()).generateFromResult(mapping, co, resultSetOrCallableStatement, sqlIndexes); |
| 474 | } |
| 475 | // type check of the result |
| 476 | if (mapping.isMapKey()) { |
| 477 | if (!mapping.getMapKeyType().isPrimitive()) { |
| 478 | co.checkcast(Type.getType(mapping.getMapKeyType())); |
| 479 | } |
| 480 | } else { |
| 481 | if (!type.isPrimitive()) { |
| 482 | co.checkcast(Type.getType(type)); |
| 483 | } |
| 484 | } |
| 485 | } else if (mapping.getAdapter() instanceof DynamicMappingAdapter) { |
| 486 | co.getfield(QueryObjectGenerator.getAdapterFieldName(mapping.getAdapter().getClass())); |
| 487 | co.load_local(resultSetOrCallableStatement); |
| 488 | if (sqlColumns != null) { |
| 489 | // push columns |
| 490 | co.push(sqlColumns.length); |
| 491 | co.newarray(TYPE_String); |
| 492 | for (int i = 0; i < sqlColumns.length; i++) { |
| 493 | co.dup(); |
| 494 | co.push(i); |
| 495 | co.push(sqlColumns[i]); |
| 496 | co.array_store(TYPE_String); |
| 497 | } |
| 498 | co.invoke_interface(Type.getType(DynamicMappingAdapter.class), new Signature("get", |
| 499 | "(Ljava/sql/ResultSet;[Ljava/lang/String;)Ljava/lang/Object;")); |
| 500 | } else { |
| 501 | // push indexes |
| 502 | co.push(sqlIndexes.length); |
| 503 | co.newarray(TYPE_int); |
| 504 | for (int i = 0; i < sqlIndexes.length; i++) { |
| 505 | co.dup(); |
| 506 | co.push(i); |
| 507 | co.push(sqlIndexes[i]); |
| 508 | co.array_store(TYPE_int); |
| 509 | } |
| 510 | // either get from a ResultSet or CallableStatement |
| 511 | co.invoke_interface(Type.getType(DynamicMappingAdapter.class), new Signature("get", |
| 512 | "("+ resultSetOrCallableStatement.getType().getDescriptor() + "[I)Ljava/lang/Object;")); |
| 513 | } |
| 514 | if (mapping.isMapKey()) { |
| 515 | co.checkcast(Type.getType(mapping.getMapKeyType())); |
| 516 | } else { |
| 517 | co.checkcast(Type.getType(mapping.getType())); |
| 518 | } |
| 519 | } else { |
| 520 | throw new RuntimeException("Unsupported adapter type " + mapping.getAdapter()); |
| 521 | } |
| 522 | if (setter != null) { |
| 523 | co.invoke_virtual(Type.getType(setter.getDeclaringClass()), ReflectionUtils.getMethodSignature(setter)); |
| 524 | } else { |
| 525 | if (mapping.isMapKey()) { |
| 526 | co.store_local(mapKey); |
| 527 | } else { |
| 528 | co.store_local(localResult); |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | private String getSqlColumn(ResultMapping mapping) { |
| 534 | return mapping.getSqlColumns() != null && mapping.getSqlColumns().length == 1 ? mapping.getSqlColumns()[0] : null; |
| 535 | } |
| 536 | |
| 537 | private int getSqlIndex(ResultMapping mapping) { |
| 538 | return mapping.getSqlIndexes() != null && mapping.getSqlIndexes().length == 1 ? mapping.getSqlIndexes()[0] : -1; |
| 539 | } |
| 540 | } |