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

wuwen5 / hessian / 17803828826

15 Sep 2025 01:09AM UTC coverage: 69.283% (+0.2%) from 69.121%
17803828826

push

github

web-flow
perf(calendar): performance optimization of calendar serialization and deserialization (#44)

1830 of 2837 branches covered (64.5%)

Branch coverage included in aggregate %.

4226 of 5904 relevant lines covered (71.58%)

3.12 hits per line

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

38.99
hessian2-codec/src/main/java/io/github/wuwen5/hessian/io/AbstractSerializer.java
1
/*
2
 * Copyright (c) 2001-2004 Caucho Technology, Inc.  All rights reserved.
3
 *
4
 * The Apache Software License, Version 1.1
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 *
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in
15
 *    the documentation and/or other materials provided with the
16
 *    distribution.
17
 *
18
 * 3. The end-user documentation included with the redistribution, if
19
 *    any, must include the following acknowlegement:
20
 *       "This product includes software developed by the
21
 *        Caucho Technology (http://www.caucho.com/)."
22
 *    Alternately, this acknowlegement may appear in the software itself,
23
 *    if and wherever such third-party acknowlegements normally appear.
24
 *
25
 * 4. The names "Burlap", "Resin", and "Caucho" must not be used to
26
 *    endorse or promote products derived from this software without prior
27
 *    written permission. For written permission, please contact
28
 *    info@caucho.com.
29
 *
30
 * 5. Products derived from this software may not be called "Resin"
31
 *    nor may "Resin" appear in their names without prior written
32
 *    permission of Caucho Technology.
33
 *
34
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37
 * DISCLAIMED.  IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45
 *
46
 * @author Scott Ferguson
47
 */
48

49
package io.github.wuwen5.hessian.io;
50

51
import io.github.wuwen5.hessian.HessianException;
52
import java.io.IOException;
53
import java.lang.reflect.InvocationTargetException;
54
import java.lang.reflect.Method;
55
import lombok.extern.slf4j.Slf4j;
56

57
/**
58
 * Serializing an object.
59
 */
60
@Slf4j
3✔
61
public abstract class AbstractSerializer implements Serializer {
3✔
62
    public static final NullSerializer NULL = new NullSerializer();
5✔
63

64
    /**
65
     * Writes the object.
66
     * @param obj the object to serialize
67
     * @param out hessian encoder
68
     * @throws IOException if an error occurs
69
     */
70
    @Override
71
    public void writeObject(Object obj, AbstractHessianEncoder out) throws IOException {
72
        if (out.addRef(obj)) {
×
73
            return;
×
74
        }
75

76
        try {
77
            Object replace = writeReplace(obj);
×
78

79
            if (replace != null) {
×
80

81
                out.writeObject(replace);
×
82

83
                out.replaceRef(replace, obj);
×
84

85
                return;
×
86
            }
87
        } catch (RuntimeException e) {
×
88
            throw e;
×
89
        } catch (Exception e) {
×
90
            throw new HessianException(e);
×
91
        }
×
92

93
        Class<?> cl = getClass(obj);
×
94

95
        int ref = out.writeObjectBegin(cl.getName());
×
96

97
        if (ref < -1) {
×
98
            writeObject10(obj, out);
×
99
        } else {
100
            if (ref == -1) {
×
101
                writeDefinition20(cl, out);
×
102

103
                out.writeObjectBegin(cl.getName());
×
104
            }
105

106
            writeInstance(obj, out);
×
107
        }
108
    }
×
109

110
    protected Object writeReplace(Object obj) {
111
        return null;
×
112
    }
113

114
    protected Class<?> getClass(Object obj) {
115
        return obj.getClass();
×
116
    }
117

118
    protected void writeObject10(Object obj, AbstractHessianEncoder out) throws IOException {
119
        throw new UnsupportedOperationException(getClass().getName());
×
120
    }
121

122
    protected void writeDefinition20(Class<?> cl, AbstractHessianEncoder out) throws IOException {
123
        throw new UnsupportedOperationException(getClass().getName());
×
124
    }
125

126
    protected void writeInstance(Object obj, AbstractHessianEncoder out) throws IOException {
127
        throw new UnsupportedOperationException(getClass().getName());
×
128
    }
129

130
    /**
131
     * The NullSerializer exists as a marker for the factory classes so
132
     * they save a null result.
133
     */
134
    static final class NullSerializer extends AbstractSerializer {
3✔
135
        @Override
136
        public void writeObject(Object obj, AbstractHessianEncoder out) throws IOException {
137
            throw new IllegalStateException(getClass().getName());
×
138
        }
139
    }
140

141
    enum MethodSerializer {
32✔
142

143
        /**
144
         * Default method serializer
145
         */
146
        DEFAULT {
11✔
147
            @Override
148
            void serialize(AbstractHessianEncoder out, Object obj, Method method) {
149
                Object value = null;
×
150

151
                try {
152
                    value = method.invoke(obj);
×
153
                } catch (InvocationTargetException e) {
×
154
                    throw error(method, e.getCause());
×
155
                } catch (IllegalAccessException e) {
×
156
                    log.debug(e.toString(), e);
×
157
                }
×
158

159
                try {
160
                    out.writeObject(value);
×
161
                } catch (Exception e) {
×
162
                    throw error(method, e);
×
163
                }
×
164
            }
×
165
        },
166
        /**
167
         * Boolean method serializer
168
         */
169
        BOOLEAN {
11✔
170
            @Override
171
            void serialize(AbstractHessianEncoder out, Object obj, Method method) throws IOException {
172
                boolean value = false;
2✔
173
                try {
174
                    value = (Boolean) method.invoke(obj);
8✔
175
                } catch (InvocationTargetException e) {
×
176
                    throw error(method, e.getCause());
×
177
                } catch (IllegalAccessException e) {
×
178
                    log.debug(e.toString(), e);
×
179
                }
1✔
180
                out.writeBoolean(value);
3✔
181
            }
1✔
182
        },
183
        INT {
11✔
184
            @Override
185
            void serialize(AbstractHessianEncoder out, Object obj, Method method) throws IOException {
186
                int value = 0;
2✔
187

188
                try {
189
                    value = Integer.parseInt(method.invoke(obj).toString());
8✔
190
                } catch (InvocationTargetException e) {
×
191
                    throw error(method, e.getCause());
×
192
                } catch (IllegalAccessException e) {
×
193
                    log.debug(e.toString(), e);
×
194
                }
1✔
195

196
                out.writeInt(value);
3✔
197
            }
1✔
198
        },
199
        LONG {
11✔
200
            @Override
201
            void serialize(AbstractHessianEncoder out, Object obj, Method method) throws IOException {
202
                long value = 0;
2✔
203

204
                try {
205
                    value = (Long) method.invoke(obj);
8✔
206
                } catch (InvocationTargetException e) {
×
207
                    throw error(method, e.getCause());
×
208
                } catch (IllegalAccessException e) {
×
209
                    log.debug(e.toString(), e);
×
210
                }
1✔
211

212
                out.writeLong(value);
3✔
213
            }
1✔
214
        },
215
        DOUBLE {
11✔
216
            @Override
217
            void serialize(AbstractHessianEncoder out, Object obj, Method method) throws IOException {
218
                double value = 0;
2✔
219

220
                try {
221
                    value = Double.parseDouble(method.invoke(obj).toString());
8✔
222
                } catch (InvocationTargetException e) {
×
223
                    throw error(method, e.getCause());
×
224
                } catch (IllegalAccessException e) {
×
225
                    log.debug(e.toString(), e);
×
226
                }
1✔
227

228
                out.writeDouble(value);
3✔
229
            }
1✔
230
        },
231
        STRING {
11✔
232
            @Override
233
            void serialize(AbstractHessianEncoder out, Object obj, Method method) throws IOException {
234
                String value = null;
2✔
235

236
                try {
237
                    value = (String) method.invoke(obj);
7✔
238
                } catch (InvocationTargetException e) {
×
239
                    throw error(method, e.getCause());
×
240
                } catch (IllegalAccessException e) {
×
241
                    log.debug(e.toString(), e);
×
242
                }
1✔
243

244
                out.writeString(value);
3✔
245
            }
1✔
246
        },
247
        DATE {
11✔
248
            @Override
249
            void serialize(AbstractHessianEncoder out, Object obj, Method method) throws IOException {
250
                java.util.Date value = null;
×
251

252
                try {
253
                    value = (java.util.Date) method.invoke(obj);
×
254
                } catch (InvocationTargetException e) {
×
255
                    throw error(method, e.getCause());
×
256
                } catch (IllegalAccessException e) {
×
257
                    log.debug(e.toString(), e);
×
258
                }
×
259

260
                if (value == null) {
×
261
                    out.writeNull();
×
262
                } else {
263
                    out.writeUTCDate(value.getTime());
×
264
                }
265
            }
×
266
        };
267

268
        /**
269
         * serialize
270
         * @param out hessian encoder
271
         * @param obj the object to serialize
272
         * @param method the read method to be called
273
         * @throws IOException if an error occurs
274
         */
275
        abstract void serialize(AbstractHessianEncoder out, Object obj, Method method) throws IOException;
276

277
        static RuntimeException error(Method method, Throwable cause) {
278
            String msg = (method.getDeclaringClass().getSimpleName() + "." + method.getName() + "(): " + cause);
×
279

280
            return new HessianMethodSerializationException(msg, cause);
×
281
        }
282
    }
283

284
    static MethodSerializer getMethodSerializer(Class<?> type) {
285
        if (int.class.equals(type) || byte.class.equals(type) || short.class.equals(type)) {
12✔
286
            return MethodSerializer.INT;
2✔
287
        } else if (long.class.equals(type)) {
4✔
288
            return MethodSerializer.LONG;
2✔
289
        } else if (double.class.equals(type) || float.class.equals(type)) {
8✔
290
            return MethodSerializer.DOUBLE;
2✔
291
        } else if (boolean.class.equals(type)) {
4✔
292
            return MethodSerializer.BOOLEAN;
2✔
293
        } else if (String.class.equals(type)) {
4!
294
            return MethodSerializer.STRING;
2✔
295
        } else if (java.util.Date.class.equals(type)
×
296
                || java.sql.Date.class.equals(type)
×
297
                || java.sql.Timestamp.class.equals(type)
×
298
                || java.sql.Time.class.equals(type)) {
×
299
            return MethodSerializer.DATE;
×
300
        } else {
301
            return MethodSerializer.DEFAULT;
×
302
        }
303
    }
304
}
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