• 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

83.06
hessian2-codec/src/main/java/io/github/wuwen5/hessian/io/FieldDeserializer2FactoryUnsafe.java
1
/*
2
 * Copyright (c) 2001-2008 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 java.io.IOException;
52
import java.lang.reflect.Field;
53
import java.lang.reflect.Modifier;
54
import lombok.extern.slf4j.Slf4j;
55
import sun.misc.Unsafe;
56

57
/**
58
 * Serializing an object for known object types.
59
 */
60
@Slf4j
3✔
61
public class FieldDeserializer2FactoryUnsafe extends FieldDeserializer2Factory {
3✔
62

63
    @SuppressWarnings("restriction")
64
    private static Unsafe unsafe;
65

66
    /**
67
     * Creates a map of the classes fields.
68
     */
69
    @Override
70
    public FieldDeserializer create(Field field) {
71
        if (Modifier.isTransient(field.getModifiers()) || Modifier.isStatic(field.getModifiers())) {
8!
72
            return NullFieldDeserializer.DESER;
×
73
        }
74

75
        Class<?> type = field.getType();
3✔
76
        FieldDeserializer deser;
77

78
        if (String.class.equals(type)) {
4✔
79
            deser = new StringFieldDeserializer(field);
6✔
80
        } else if (byte.class.equals(type)) {
4✔
81
            deser = new ByteFieldDeserializer(field);
6✔
82
        } else if (char.class.equals(type)) {
4✔
83
            deser = new CharFieldDeserializer(field);
6✔
84
        } else if (short.class.equals(type)) {
4✔
85
            deser = new ShortFieldDeserializer(field);
6✔
86
        } else if (int.class.equals(type)) {
4✔
87
            deser = new IntFieldDeserializer(field);
6✔
88
        } else if (long.class.equals(type)) {
4✔
89
            deser = new LongFieldDeserializer(field);
6✔
90
        } else if (float.class.equals(type)) {
4✔
91
            deser = new FloatFieldDeserializer(field);
6✔
92
        } else if (double.class.equals(type)) {
4✔
93
            deser = new DoubleFieldDeserializer(field);
6✔
94
        } else if (boolean.class.equals(type)) {
4✔
95
            deser = new BooleanFieldDeserializer(field);
6✔
96
        } else if (java.sql.Date.class.equals(type)) {
4✔
97
            deser = new SqlDateFieldDeserializer(field);
6✔
98
        } else if (java.sql.Timestamp.class.equals(type)) {
4✔
99
            deser = new SqlTimestampFieldDeserializer(field);
6✔
100
        } else if (java.sql.Time.class.equals(type)) {
4✔
101
            deser = new SqlTimeFieldDeserializer(field);
6✔
102
        } else {
103
            deser = new ObjectFieldDeserializer(field);
5✔
104
        }
105

106
        return deser;
2✔
107
    }
108

109
    static class ObjectFieldDeserializer implements FieldDeserializer {
110
        private final Field field;
111
        private final long offset;
112

113
        @SuppressWarnings("restriction")
114
        ObjectFieldDeserializer(Field field) {
2✔
115
            this.field = field;
3✔
116
            offset = unsafe.objectFieldOffset(this.field);
6✔
117
        }
1✔
118

119
        @Override
120
        @SuppressWarnings("restriction")
121
        public void deserialize(AbstractHessianDecoder in, Object obj) throws IOException {
122
            Object value = null;
2✔
123

124
            try {
125
                value = in.readObject(field.getType());
6✔
126

127
                unsafe.putObject(obj, offset, value);
6✔
128
            } catch (Exception e) {
×
129
                logDeserializeError(field, value, e);
×
130
            }
1✔
131
        }
1✔
132
    }
133

134
    static class BooleanFieldDeserializer implements FieldDeserializer {
135
        private final Field field;
136
        private final long offset;
137

138
        @SuppressWarnings("restriction")
139
        BooleanFieldDeserializer(Field field) {
2✔
140
            this.field = field;
3✔
141
            offset = unsafe.objectFieldOffset(this.field);
6✔
142
        }
1✔
143

144
        @Override
145
        @SuppressWarnings("restriction")
146
        public void deserialize(AbstractHessianDecoder in, Object obj) throws IOException {
147
            boolean value = false;
2✔
148

149
            try {
150
                value = in.readBoolean();
3✔
151

152
                unsafe.putBoolean(obj, offset, value);
6✔
153
            } catch (Exception e) {
×
154
                logDeserializeError(field, value, e);
×
155
            }
1✔
156
        }
1✔
157
    }
158

159
    static class ByteFieldDeserializer implements FieldDeserializer {
160
        private final Field field;
161
        private final long offset;
162

163
        @SuppressWarnings("restriction")
164
        ByteFieldDeserializer(Field field) {
2✔
165
            this.field = field;
3✔
166
            offset = unsafe.objectFieldOffset(this.field);
6✔
167
        }
1✔
168

169
        @Override
170
        @SuppressWarnings("restriction")
171
        public void deserialize(AbstractHessianDecoder in, Object obj) throws IOException {
172
            int value = 0;
2✔
173

174
            try {
175
                value = in.readInt();
3✔
176

177
                unsafe.putByte(obj, offset, (byte) value);
7✔
178
            } catch (Exception e) {
×
179
                logDeserializeError(field, value, e);
×
180
            }
1✔
181
        }
1✔
182
    }
183

184
    static class CharFieldDeserializer implements FieldDeserializer {
185
        private final Field field;
186
        private final long offset;
187

188
        @SuppressWarnings("restriction")
189
        CharFieldDeserializer(Field field) {
2✔
190
            this.field = field;
3✔
191
            offset = unsafe.objectFieldOffset(this.field);
6✔
192
        }
1✔
193

194
        @Override
195
        @SuppressWarnings("restriction")
196
        public void deserialize(AbstractHessianDecoder in, Object obj) throws IOException {
197
            String value = null;
2✔
198

199
            try {
200
                value = in.readString();
3✔
201

202
                char ch;
203

204
                if (value != null && !value.isEmpty()) {
5!
205
                    ch = value.charAt(0);
5✔
206
                } else {
207
                    ch = 0;
×
208
                }
209

210
                unsafe.putChar(obj, offset, ch);
6✔
211
            } catch (Exception e) {
×
212
                logDeserializeError(field, value, e);
×
213
            }
1✔
214
        }
1✔
215
    }
216

217
    static class ShortFieldDeserializer implements FieldDeserializer {
218
        private final Field field;
219
        private final long offset;
220

221
        @SuppressWarnings("restriction")
222
        ShortFieldDeserializer(Field field) {
2✔
223
            this.field = field;
3✔
224
            offset = unsafe.objectFieldOffset(this.field);
6✔
225
        }
1✔
226

227
        @Override
228
        @SuppressWarnings("restriction")
229
        public void deserialize(AbstractHessianDecoder in, Object obj) throws IOException {
230
            int value = 0;
2✔
231

232
            try {
233
                value = in.readInt();
3✔
234

235
                unsafe.putShort(obj, offset, (short) value);
7✔
236
            } catch (Exception e) {
×
237
                logDeserializeError(field, value, e);
×
238
            }
1✔
239
        }
1✔
240
    }
241

242
    static class IntFieldDeserializer implements FieldDeserializer {
243
        private final Field field;
244
        private final long offset;
245

246
        @SuppressWarnings("restriction")
247
        IntFieldDeserializer(Field field) {
2✔
248
            this.field = field;
3✔
249
            offset = unsafe.objectFieldOffset(this.field);
6✔
250
        }
1✔
251

252
        @Override
253
        @SuppressWarnings("restriction")
254
        public void deserialize(AbstractHessianDecoder in, Object obj) throws IOException {
255
            int value = 0;
2✔
256

257
            try {
258
                value = in.readInt();
3✔
259

260
                unsafe.putInt(obj, offset, value);
6✔
261
            } catch (Exception e) {
×
262
                logDeserializeError(field, value, e);
×
263
            }
1✔
264
        }
1✔
265
    }
266

267
    static class LongFieldDeserializer implements FieldDeserializer {
268
        private final Field field;
269
        private final long offset;
270

271
        @SuppressWarnings("restriction")
272
        LongFieldDeserializer(Field field) {
2✔
273
            this.field = field;
3✔
274
            offset = unsafe.objectFieldOffset(this.field);
6✔
275
        }
1✔
276

277
        @Override
278
        @SuppressWarnings("restriction")
279
        public void deserialize(AbstractHessianDecoder in, Object obj) throws IOException {
280
            long value = 0;
2✔
281

282
            try {
283
                value = in.readLong();
3✔
284

285
                unsafe.putLong(obj, offset, value);
6✔
286
            } catch (Exception e) {
×
287
                logDeserializeError(field, value, e);
×
288
            }
1✔
289
        }
1✔
290
    }
291

292
    static class FloatFieldDeserializer implements FieldDeserializer {
293
        private final Field field;
294
        private final long aLong;
295

296
        @SuppressWarnings("restriction")
297
        FloatFieldDeserializer(Field field) {
2✔
298
            this.field = field;
3✔
299
            aLong = unsafe.objectFieldOffset(this.field);
6✔
300
        }
1✔
301

302
        @Override
303
        public void deserialize(AbstractHessianDecoder in, Object obj) throws IOException {
304
            double value = 0;
2✔
305

306
            try {
307
                value = in.readDouble();
3✔
308

309
                unsafe.putFloat(obj, aLong, (float) value);
7✔
310
            } catch (Exception e) {
×
311
                logDeserializeError(field, value, e);
×
312
            }
1✔
313
        }
1✔
314
    }
315

316
    static class DoubleFieldDeserializer implements FieldDeserializer {
317
        private final Field field;
318
        private final long offset;
319

320
        DoubleFieldDeserializer(Field field) {
2✔
321
            this.field = field;
3✔
322
            offset = unsafe.objectFieldOffset(this.field);
6✔
323
        }
1✔
324

325
        @Override
326
        @SuppressWarnings("restriction")
327
        public void deserialize(AbstractHessianDecoder in, Object obj) throws IOException {
328
            double value = 0;
2✔
329

330
            try {
331
                value = in.readDouble();
3✔
332

333
                unsafe.putDouble(obj, offset, value);
6✔
334
            } catch (Exception e) {
×
335
                logDeserializeError(field, value, e);
×
336
            }
1✔
337
        }
1✔
338
    }
339

340
    static class StringFieldDeserializer implements FieldDeserializer {
341
        private final Field field;
342
        private final long offset;
343

344
        @SuppressWarnings("restriction")
345
        StringFieldDeserializer(Field field) {
2✔
346
            this.field = field;
3✔
347
            offset = unsafe.objectFieldOffset(this.field);
6✔
348
        }
1✔
349

350
        @Override
351
        public void deserialize(AbstractHessianDecoder in, Object obj) throws IOException {
352
            String value = null;
2✔
353

354
            try {
355
                value = in.readString();
3✔
356

357
                unsafe.putObject(obj, offset, value);
6✔
358
            } catch (Exception e) {
×
359
                logDeserializeError(field, value, e);
×
360
            }
1✔
361
        }
1✔
362
    }
363

364
    static class SqlDateFieldDeserializer implements FieldDeserializer {
365
        private final Field field;
366
        private final long offset;
367

368
        @SuppressWarnings("restriction")
369
        SqlDateFieldDeserializer(Field field) {
2✔
370
            this.field = field;
3✔
371
            offset = unsafe.objectFieldOffset(this.field);
6✔
372
        }
1✔
373

374
        @Override
375
        @SuppressWarnings("restriction")
376
        public void deserialize(AbstractHessianDecoder in, Object obj) throws IOException {
377
            java.sql.Date value = null;
2✔
378

379
            try {
380
                java.util.Date date = (java.util.Date) in.readObject();
4✔
381

382
                if (date != null) {
2!
383
                    value = new java.sql.Date(date.getTime());
6✔
384

385
                    unsafe.putObject(obj, offset, value);
7✔
386
                } else {
387
                    unsafe.putObject(obj, offset, null);
×
388
                }
389
            } catch (Exception e) {
×
390
                logDeserializeError(field, value, e);
×
391
            }
1✔
392
        }
1✔
393
    }
394

395
    static class SqlTimestampFieldDeserializer implements FieldDeserializer {
396
        private final Field field;
397
        private final long offset;
398

399
        @SuppressWarnings("restriction")
400
        SqlTimestampFieldDeserializer(Field field) {
2✔
401
            this.field = field;
3✔
402
            offset = unsafe.objectFieldOffset(this.field);
6✔
403
        }
1✔
404

405
        @Override
406
        @SuppressWarnings("restriction")
407
        public void deserialize(AbstractHessianDecoder in, Object obj) throws IOException {
408
            java.sql.Timestamp value = null;
2✔
409

410
            try {
411
                java.util.Date date = (java.util.Date) in.readObject();
4✔
412

413
                if (date != null) {
2!
414
                    value = new java.sql.Timestamp(date.getTime());
6✔
415

416
                    unsafe.putObject(obj, offset, value);
7✔
417
                } else {
418
                    unsafe.putObject(obj, offset, null);
×
419
                }
420
            } catch (Exception e) {
×
421
                logDeserializeError(field, value, e);
×
422
            }
1✔
423
        }
1✔
424
    }
425

426
    static class SqlTimeFieldDeserializer implements FieldDeserializer {
427
        private final Field field;
428
        private final long offset;
429

430
        @SuppressWarnings("restriction")
431
        SqlTimeFieldDeserializer(Field field) {
2✔
432
            this.field = field;
3✔
433
            offset = unsafe.objectFieldOffset(this.field);
6✔
434
        }
1✔
435

436
        @Override
437
        public void deserialize(AbstractHessianDecoder in, Object obj) throws IOException {
438
            java.sql.Time value = null;
2✔
439

440
            try {
441
                java.util.Date date = (java.util.Date) in.readObject();
4✔
442

443
                if (date != null) {
2!
444
                    value = new java.sql.Time(date.getTime());
6✔
445

446
                    unsafe.putObject(obj, offset, value);
7✔
447
                } else {
448
                    unsafe.putObject(obj, offset, null);
×
449
                }
450
            } catch (Exception e) {
×
451
                logDeserializeError(field, value, e);
×
452
            }
1✔
453
        }
1✔
454
    }
455

456
    static {
457
        try {
458
            Class<?> unsafe = Class.forName("sun.misc.Unsafe");
3✔
459
            Field theUnsafe = null;
2✔
460
            for (Field field : unsafe.getDeclaredFields()) {
17✔
461
                if ("theUnsafe".equals(field.getName())) {
5✔
462
                    theUnsafe = field;
2✔
463
                }
464
            }
465

466
            if (theUnsafe != null) {
2!
467
                theUnsafe.setAccessible(true);
3✔
468
                FieldDeserializer2FactoryUnsafe.unsafe = (Unsafe) theUnsafe.get(null);
5✔
469
            }
470

471
        } catch (Throwable e) {
×
472
            log.trace(e.toString(), e);
×
473
        }
1✔
474
    }
1✔
475
}
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