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

mybatis / ibatis-2 / 678

27 Dec 2025 01:13AM UTC coverage: 65.584% (+0.05%) from 65.532%
678

push

github

hazendaz
Merge remote-tracking branch 'upstream/master' into support-javax

1606 of 2810 branches covered (57.15%)

167 of 305 new or added lines in 90 files covered. (54.75%)

6 existing lines in 4 files now uncovered.

5067 of 7726 relevant lines covered (65.58%)

0.66 hits per line

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

66.1
/src/main/java/com/ibatis/sqlmap/engine/exchange/JavaBeanDataExchange.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.exchange;
17

18
import com.ibatis.sqlmap.engine.accessplan.AccessPlan;
19
import com.ibatis.sqlmap.engine.accessplan.AccessPlanFactory;
20
import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap;
21
import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMapping;
22
import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
23
import com.ibatis.sqlmap.engine.mapping.result.ResultMapping;
24
import com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactoryUtil;
25
import com.ibatis.sqlmap.engine.scope.ErrorContext;
26
import com.ibatis.sqlmap.engine.scope.StatementScope;
27

28
import java.util.ArrayList;
29
import java.util.List;
30
import java.util.Map;
31

32
/**
33
 * DataExchange implementation for beans.
34
 */
35
public class JavaBeanDataExchange extends BaseDataExchange implements DataExchange {
36

37
  /** The Constant NO_DATA. */
38
  private static final Object[] NO_DATA = new Object[0];
1✔
39

40
  /** The result plan. */
41
  private AccessPlan resultPlan;
42

43
  /** The parameter plan. */
44
  private AccessPlan parameterPlan;
45

46
  /** The out param plan. */
47
  private AccessPlan outParamPlan;
48

49
  /**
50
   * Instantiates a new java bean data exchange.
51
   *
52
   * @param dataExchangeFactory
53
   *          the data exchange factory
54
   */
55
  protected JavaBeanDataExchange(DataExchangeFactory dataExchangeFactory) {
56
    super(dataExchangeFactory);
1✔
57
  }
1✔
58

59
  /**
60
   * Initializes the data exchange instance.
61
   *
62
   * @param properties
63
   *          the properties
64
   */
65
  @Override
66
  public void initialize(Map properties) {
67
    Object map = properties.get("map");
1✔
68
    if (map instanceof ParameterMap) {
1✔
69
      ParameterMap parameterMap = (ParameterMap) map;
1✔
70
      if (parameterMap != null) {
1!
71
        ParameterMapping[] parameterMappings = parameterMap.getParameterMappings();
1✔
72
        String[] parameterPropNames = new String[parameterMappings.length];
1✔
73
        for (int i = 0; i < parameterPropNames.length; i++) {
1✔
74
          parameterPropNames[i] = parameterMappings[i].getPropertyName();
1✔
75
        }
76
        parameterPlan = AccessPlanFactory.getAccessPlan(parameterMap.getParameterClass(), parameterPropNames);
1✔
77

78
        // OUTPUT PARAMS
79
        List outParamList = new ArrayList<>();
1✔
80
        for (int i = 0; i < parameterPropNames.length; i++) {
1✔
81
          if (parameterMappings[i].isOutputAllowed()) {
1✔
82
            outParamList.add(parameterMappings[i].getPropertyName());
1✔
83
          }
84
        }
85
        String[] outParams = (String[]) outParamList.toArray(new String[outParamList.size()]);
1✔
86
        outParamPlan = AccessPlanFactory.getAccessPlan(parameterMap.getParameterClass(), outParams);
1✔
87
      }
88

89
    } else if (map instanceof ResultMap) {
1!
90
      ResultMap resultMap = (ResultMap) map;
1✔
91
      if (resultMap != null) {
1!
92
        ResultMapping[] resultMappings = resultMap.getResultMappings();
1✔
93
        String[] resultPropNames = new String[resultMappings.length];
1✔
94
        for (int i = 0; i < resultPropNames.length; i++) {
1✔
95
          resultPropNames[i] = resultMappings[i].getPropertyName();
1✔
96
        }
97
        resultPlan = AccessPlanFactory.getAccessPlan(resultMap.getResultClass(), resultPropNames);
1✔
98
      }
99
    }
100
  }
1✔
101

102
  @Override
103
  public Object[] getData(StatementScope statementScope, ParameterMap parameterMap, Object parameterObject) {
104
    if (parameterPlan != null) {
1!
105
      return parameterPlan.getProperties(parameterObject);
1✔
106
    } else {
107
      return NO_DATA;
×
108
    }
109
  }
110

111
  @Override
112
  public Object setData(StatementScope statementScope, ResultMap resultMap, Object resultObject, Object[] values) {
113
    if (resultPlan != null) {
1!
114
      Object object = resultObject;
1✔
115

116
      ErrorContext errorContext = statementScope.getErrorContext();
1✔
117

118
      if (object == null) {
1✔
119
        errorContext.setMoreInfo("The error occured while instantiating the result object");
1✔
120
        try {
121
          object = ResultObjectFactoryUtil.createObjectThroughFactory(resultMap.getResultClass());
1✔
122
        } catch (Exception e) {
×
123
          throw new RuntimeException("JavaBeansDataExchange could not instantiate result class.  Cause: " + e, e);
×
124
        }
1✔
125
      }
126
      errorContext.setMoreInfo("The error happened while setting a property on the result object.");
1✔
127
      resultPlan.setProperties(object, values);
1✔
128
      return object;
1✔
129
    } else {
130
      return null;
×
131
    }
132
  }
133

134
  // Bug ibatis-12
135
  @Override
136
  public Object setData(StatementScope statementScope, ParameterMap parameterMap, Object parameterObject,
137
      Object[] values) {
138
    if (outParamPlan != null) {
×
139
      Object object = parameterObject;
×
140
      if (object == null) {
×
141
        try {
142
          object = ResultObjectFactoryUtil.createObjectThroughFactory(parameterMap.getParameterClass());
×
143
        } catch (Exception e) {
×
144
          throw new RuntimeException("JavaBeansDataExchange could not instantiate parameter class. Cause: " + e, e);
×
145
        }
×
146
      }
147
      values = getOutputParamValues(parameterMap.getParameterMappings(), values);
×
148
      outParamPlan.setProperties(object, values);
×
149
      return object;
×
150
    } else {
151
      return null;
×
152
    }
153
  }
154

155
  /**
156
   * Gets the output param values.
157
   *
158
   * @param mappings
159
   *          the mappings
160
   * @param values
161
   *          the values
162
   *
163
   * @return the output param values
164
   */
165
  private Object[] getOutputParamValues(ParameterMapping[] mappings, Object[] values) {
NEW
166
    List outParamValues = new ArrayList<>();
×
167
    for (int i = 0; i < mappings.length; i++) {
×
168
      if (mappings[i].isOutputAllowed()) {
×
169
        outParamValues.add(values[i]);
×
170
      }
171
    }
172
    return outParamValues.toArray();
×
173
  }
174

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

© 2026 Coveralls, Inc