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

mybatis / ibatis-2 / 736

29 Dec 2025 12:23AM UTC coverage: 65.571% (-0.03%) from 65.597%
736

push

github

web-flow
Merge pull request #339 from hazendaz/master

Key set cleanup to use size directly increasing speed for same result

1598 of 2797 branches covered (57.13%)

4 of 4 new or added lines in 1 file covered. (100.0%)

149 existing lines in 7 files now uncovered.

5047 of 7697 relevant lines covered (65.57%)

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
      return ResultLoader.getResult(client, statementName, parameterObject, targetType);
1✔
89
    }
90
    InvocationHandler handler = new LazyResultLoader(client, statementName, parameterObject, targetType);
1✔
91
    ClassLoader cl = targetType.getClassLoader();
1✔
92
    if (Set.class.isAssignableFrom(targetType)) {
1!
UNCOV
93
      return Proxy.newProxyInstance(cl, SET_INTERFACES, handler);
×
94
    } else {
95
      return Proxy.newProxyInstance(cl, LIST_INTERFACES, handler);
1✔
96
    }
97
  }
98

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

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

130
}
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