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

mybatis / ibatis-2 / 733

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

Pull #338

github

web-flow
Merge abffa4d0e into adb99ac51
Pull Request #338: Various statement syntax cleanup

1598 of 2797 branches covered (57.13%)

207 of 278 new or added lines in 7 files covered. (74.46%)

3 existing lines in 3 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

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 = {};
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
    }
107
    return NO_DATA;
×
108
  }
109

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

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

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

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

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

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