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

wuwen5 / hessian / 17882005206

20 Sep 2025 01:06PM UTC coverage: 70.892% (+1.5%) from 69.377%
17882005206

push

github

web-flow
refactor: code clean (#50)

* refactor: code clean

* refactor: code clean,remove hessian1 code

* refactor: fix HessianDebugState

1780 of 2691 branches covered (66.15%)

Branch coverage included in aggregate %.

4226 of 5781 relevant lines covered (73.1%)

3.17 hits per line

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

40.0
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 HessianSerializer {
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
            writeDefinition20(cl, out);
×
99

100
            out.writeObjectBegin(cl.getName());
×
101
        }
102

103
        writeInstance(obj, out);
×
104
    }
×
105

106
    @SuppressWarnings("unused")
107
    protected Object writeReplace(Object obj) {
108
        return null;
×
109
    }
110

111
    protected Class<?> getClass(Object obj) {
112
        return obj.getClass();
×
113
    }
114

115
    protected void writeObject10(Object obj, AbstractHessianEncoder out) throws IOException {
116
        throw new UnsupportedOperationException(getClass().getName());
×
117
    }
118

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

123
    protected void writeInstance(Object obj, AbstractHessianEncoder out) throws IOException {
124
        throw new UnsupportedOperationException(getClass().getName());
×
125
    }
126

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

138
    enum MethodSerializer {
32✔
139

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

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

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

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

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

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

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

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

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

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

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

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

257
                if (value == null) {
×
258
                    out.writeNull();
×
259
                } else {
260
                    out.writeUTCDate(value.getTime());
×
261
                }
262
            }
×
263
        };
264

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

274
        static RuntimeException error(Method method, Throwable cause) {
275
            String msg = (method.getDeclaringClass().getSimpleName() + "." + method.getName() + "(): " + cause);
×
276

277
            return new HessianMethodSerializationException(msg, cause);
×
278
        }
279
    }
280

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