• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / ibatis-2 / 730

28 Dec 2025 10:16PM UTC coverage: 65.615% (+0.5%) from 65.146%
730

push

github

web-flow
Update README.md

1602 of 2802 branches covered (57.17%)

5053 of 7701 relevant lines covered (65.61%)

0.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.67
/src/main/java/com/ibatis/sqlmap/engine/mapping/result/loader/LazyResultLoader.java
1
/*
2
 * Copyright 2004-2025 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
 *    https://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
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

21
import java.lang.reflect.InvocationHandler;
22
import java.lang.reflect.InvocationTargetException;
23
import java.lang.reflect.Method;
24
import java.lang.reflect.Proxy;
25
import java.sql.SQLException;
26
import java.util.Collection;
27
import java.util.List;
28
import java.util.Set;
29

30
/**
31
 * Class to lazily load results into objects.
32
 */
33
public class LazyResultLoader implements InvocationHandler {
34

35
  /** The Constant SET_INTERFACES. */
36
  private static final Class[] SET_INTERFACES = { Set.class };
1✔
37

38
  /** The Constant LIST_INTERFACES. */
39
  private static final Class[] LIST_INTERFACES = { List.class };
1✔
40

41
  /** The client. */
42
  protected SqlMapClientImpl client;
43

44
  /** The statement name. */
45
  protected String statementName;
46

47
  /** The parameter object. */
48
  protected Object parameterObject;
49

50
  /** The target type. */
51
  protected Class targetType;
52

53
  /** The loaded. */
54
  protected boolean loaded;
55

56
  /** The result object. */
57
  protected Object resultObject;
58

59
  /**
60
   * Constructor for a lazy list loader.
61
   *
62
   * @param client
63
   *          - the client that is creating the lazy list
64
   * @param statementName
65
   *          - the statement to be used to build the list
66
   * @param parameterObject
67
   *          - the parameter object to be used to build the list
68
   * @param targetType
69
   *          - the type we are putting data into
70
   */
71
  public LazyResultLoader(SqlMapClientImpl client, String statementName, Object parameterObject, Class targetType) {
1✔
72
    this.client = client;
1✔
73
    this.statementName = statementName;
1✔
74
    this.parameterObject = parameterObject;
1✔
75
    this.targetType = targetType;
1✔
76
  }
1✔
77

78
  /**
79
   * Loads the result.
80
   *
81
   * @return the results - a list or object
82
   *
83
   * @throws SQLException
84
   *           if there is a problem
85
   */
86
  public Object loadResult() throws SQLException {
87
    if (Collection.class.isAssignableFrom(targetType)) {
1✔
88
      InvocationHandler handler = new LazyResultLoader(client, statementName, parameterObject, targetType);
1✔
89
      ClassLoader cl = targetType.getClassLoader();
1✔
90
      if (Set.class.isAssignableFrom(targetType)) {
1!
91
        return Proxy.newProxyInstance(cl, SET_INTERFACES, handler);
×
92
      } else {
93
        return Proxy.newProxyInstance(cl, LIST_INTERFACES, handler);
1✔
94
      }
95
    } else {
96
      return ResultLoader.getResult(client, statementName, parameterObject, targetType);
1✔
97
    }
98
  }
99

100
  public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
101
    if ("finalize".hashCode() == method.getName().hashCode() && "finalize".equals(method.getName())) {
1!
102
      return null;
×
103
    } else {
104
      loadObject();
1✔
105
      if (resultObject != null) {
1!
106
        try {
107
          return method.invoke(resultObject, objects);
1✔
108
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
×
109
          throw ClassInfo.unwrapThrowable(e);
×
110
        }
111
      } else {
112
        return null;
×
113
      }
114
    }
115
  }
116

117
  /**
118
   * Load object.
119
   */
120
  private synchronized void loadObject() {
121
    if (!loaded) {
1!
122
      try {
123
        loaded = true;
1✔
124
        resultObject = ResultLoader.getResult(client, statementName, parameterObject, targetType);
1✔
125
      } catch (SQLException e) {
×
126
        throw new RuntimeException("Error lazy loading result. Cause: " + e, e);
×
127
      }
1✔
128
    }
129
  }
1✔
130

131
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc