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

mybatis / mybatis-3 / 4017

02 Dec 2026 01:21PM UTC coverage: 87.564% (+0.1%) from 87.454%
4017

Pull #3809

github

web-flow
Merge de6182685 into 19b82e6ae
Pull Request #3809: Fix JDBC array cleanup when binding or extraction fails

3907 of 4711 branches covered (82.93%)

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

152 existing lines in 10 files now uncovered.

10034 of 11459 relevant lines covered (87.56%)

0.88 hits per line

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

96.36
/src/main/java/org/apache/ibatis/executor/loader/AbstractSerialStateHolder.java
1
/*
2
 *    Copyright 2009-2026 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 org.apache.ibatis.executor.loader;
17

18
import java.io.ByteArrayInputStream;
19
import java.io.ByteArrayOutputStream;
20
import java.io.Externalizable;
21
import java.io.IOException;
22
import java.io.InvalidClassException;
23
import java.io.ObjectInput;
24
import java.io.ObjectInputFilter;
25
import java.io.ObjectInputStream;
26
import java.io.ObjectOutput;
27
import java.io.ObjectOutputStream;
28
import java.io.ObjectStreamException;
29
import java.io.StreamCorruptedException;
30
import java.util.Arrays;
31
import java.util.HashMap;
32
import java.util.List;
33
import java.util.Map;
34

35
import org.apache.ibatis.io.SerialFilterChecker;
36
import org.apache.ibatis.reflection.factory.ObjectFactory;
37

38
/**
39
 * Serialized form of a lazy-loading proxy that still has unloaded properties.
40
 * <p>
41
 * The proxy state is written as a nested byte array and read back with a separate {@link ObjectInputStream} in
42
 * {@link #readResolve()}. If the outer stream has an {@link ObjectInputFilter} set via
43
 * {@link ObjectInputStream#setObjectInputFilter(ObjectInputFilter)}, that filter is applied to the nested stream as
44
 * well. An allowlist filter therefore has to accept not only this holder class and {@code byte[]}, but also the classes
45
 * read from the nested stream: the target bean and the classes it references, {@code java.util.HashMap},
46
 * {@code java.util.Map$Entry[]}, {@link ResultLoaderMap.LoadPair}, the {@link ObjectFactory} implementation,
47
 * {@code java.lang.Class[]}, {@code java.lang.Object[]} and the types of any constructor arguments and mapped
48
 * parameters.
49
 *
50
 * @author Eduardo Macarron
51
 * @author Franta Mejta
52
 */
53
public abstract class AbstractSerialStateHolder implements Externalizable {
54

55
  private static final long serialVersionUID = 8940388717901644661L;
56
  private static final ThreadLocal<ObjectOutputStream> stream = new ThreadLocal<>();
1 ✔
57
  private byte[] userBeanBytes = {};
1 ✔
58
  private Object userBean;
59
  private Map<String, ResultLoaderMap.LoadPair> unloadedProperties;
60
  private ObjectFactory objectFactory;
61
  private Class<?>[] constructorArgTypes;
62
  private Object[] constructorArgs;
63
  private transient ObjectInputFilter objectInputFilter;
64

65
  public AbstractSerialStateHolder() {
1 ✔
66
  }
1 ✔
67

68
  public AbstractSerialStateHolder(final Object userBean,
69
      final Map<String, ResultLoaderMap.LoadPair> unloadedProperties, final ObjectFactory objectFactory,
70
      List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
1 ✔
71
    this.userBean = userBean;
1 ✔
72
    this.unloadedProperties = new HashMap<>(unloadedProperties);
1 ✔
73
    this.objectFactory = objectFactory;
1 ✔
74
    this.constructorArgTypes = constructorArgTypes.toArray(new Class<?>[0]);
1 ✔
75
    this.constructorArgs = constructorArgs.toArray(new Object[0]);
1 ✔
76
  }
1 ✔
77

78
  @Override
79
  public final void writeExternal(final ObjectOutput out) throws IOException {
80
    boolean firstRound = false;
1 ✔
81
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
1 ✔
82
    ObjectOutputStream os = stream.get();
1 ✔
83
    if (os == null) {
1 ✔
84
      os = new ObjectOutputStream(baos);
1 ✔
85
      firstRound = true;
1 ✔
86
      stream.set(os);
1 ✔
87
    }
88

89
    os.writeObject(this.userBean);
1 ✔
90
    os.writeObject(this.unloadedProperties);
1 ✔
91
    os.writeObject(this.objectFactory);
1 ✔
92
    os.writeObject(this.constructorArgTypes);
1 ✔
93
    os.writeObject(this.constructorArgs);
1 ✔
94

95
    final byte[] bytes = baos.toByteArray();
1 ✔
96
    out.writeObject(bytes);
1 ✔
97

98
    if (firstRound) {
1 ✔
99
      stream.remove();
1 ✔
100
    }
101
  }
1 ✔
102

103
  @Override
104
  public final void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
105
    final Object data = in.readObject();
1 ✔
106
    if (data.getClass().isArray()) {
1 ✔
107
      this.userBeanBytes = (byte[]) data;
1 ✔
108
    } else {
109
      this.userBean = data;
1 ✔
110
    }
111
    if (in instanceof ObjectInputStream) {
1 !
112
      this.objectInputFilter = ((ObjectInputStream) in).getObjectInputFilter();
1 ✔
113
    }
114
  }
1 ✔
115

116
  @SuppressWarnings("unchecked")
117
  protected final Object readResolve() throws ObjectStreamException {
118
    /* Second run */
119
    if (this.userBean != null && this.userBeanBytes.length == 0) {
1 !
120
      return this.userBean;
1 ✔
121
    }
122

123
    SerialFilterChecker.check();
1 ✔
124

125
    /* First run */
126
    try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(this.userBeanBytes))) {
1 ✔
127
      if (this.objectInputFilter != null && this.objectInputFilter != in.getObjectInputFilter()) {
1 !
128
        in.setObjectInputFilter(this.objectInputFilter);
1 ✔
129
      }
130
      this.userBean = in.readObject();
1 ✔
131
      this.unloadedProperties = (Map<String, ResultLoaderMap.LoadPair>) in.readObject();
1 ✔
132
      this.objectFactory = (ObjectFactory) in.readObject();
1 ✔
133
      this.constructorArgTypes = (Class<?>[]) in.readObject();
1 ✔
134
      this.constructorArgs = (Object[]) in.readObject();
1 ✔
135
    } catch (final IOException ex) {
1 ✔
136
      throw (ObjectStreamException) new StreamCorruptedException().initCause(ex);
1 ✔
UNCOV
137
    } catch (final ClassNotFoundException ex) {
×
UNCOV
138
      throw (ObjectStreamException) new InvalidClassException(ex.getLocalizedMessage()).initCause(ex);
×
139
    }
1 ✔
140

141
    final Map<String, ResultLoaderMap.LoadPair> arrayProps = new HashMap<>(this.unloadedProperties);
1 ✔
142
    final List<Class<?>> arrayTypes = Arrays.asList(this.constructorArgTypes);
1 ✔
143
    final List<Object> arrayValues = Arrays.asList(this.constructorArgs);
1 ✔
144

145
    return this.createDeserializationProxy(userBean, arrayProps, objectFactory, arrayTypes, arrayValues);
1 ✔
146
  }
147

148
  protected abstract Object createDeserializationProxy(Object target,
149
      Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory,
150
      List<Class<?>> constructorArgTypes, List<Object> constructorArgs);
151
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc