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

wuwen5 / hessian / 17952260094

23 Sep 2025 07:16AM UTC coverage: 71.118% (+0.2%) from 70.892%
17952260094

push

github

web-flow
Refactor JavaDeserializer and UnsafeDeserializer to reduce code duplication (#52)

* Initial plan

* Refactor JavaDeserializer and UnsafeDeserializer to reduce code duplication

Co-authored-by: wuwen5 <5037807+wuwen5@users.noreply.github.com>

* Add @SuppressWarnings annotation for unused parameter in resolve method

Co-authored-by: wuwen5 <5037807+wuwen5@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: wuwen5 <5037807+wuwen5@users.noreply.github.com>

1750 of 2647 branches covered (66.11%)

Branch coverage included in aggregate %.

4177 of 5687 relevant lines covered (73.45%)

3.18 hits per line

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

91.89
hessian2-codec/src/main/java/io/github/wuwen5/hessian/io/JavaDeserializer.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.lang.reflect.Constructor;
52

53
/**
54
 * Serializing an object for known object types.
55
 */
56
public class JavaDeserializer extends AbstractFieldDeserializer {
57
    private final Constructor<?> constructor;
58
    private final Object[] constructorArgs;
59

60
    public JavaDeserializer(Class<?> cl, FieldDeserializer2Factory fieldFactory) {
61
        super(cl, fieldFactory);
4✔
62

63
        constructor = getConstructor(cl);
5✔
64
        constructorArgs = getConstructorArgs(constructor);
6✔
65
    }
1✔
66

67
    protected Constructor<?> getConstructor(Class<?> cl) {
68
        Constructor<?>[] constructors = cl.getDeclaredConstructors();
3✔
69
        long bestCost = Long.MAX_VALUE;
2✔
70

71
        Constructor<?> constructorVar = null;
2✔
72

73
        for (Constructor<?> value : constructors) {
16✔
74
            Class<?>[] param = value.getParameterTypes();
3✔
75
            long cost = getCost(param);
3✔
76

77
            if (cost < bestCost) {
4✔
78
                constructorVar = value;
2✔
79
                bestCost = cost;
2✔
80
            }
81
        }
82

83
        if (constructorVar != null) {
2✔
84
            constructorVar.setAccessible(true);
3✔
85
        }
86

87
        return constructorVar;
2✔
88
    }
89

90
    private static long getCost(Class<?>[] param) {
91
        long cost = 0;
2✔
92

93
        for (Class<?> aClass : param) {
16✔
94
            cost = 4 * cost;
4✔
95

96
            if (Object.class.equals(aClass)) {
4!
97
                cost += 1;
×
98
            } else if (String.class.equals(aClass)) {
4✔
99
                cost += 2;
5✔
100
            } else if (int.class.equals(aClass)) {
4✔
101
                cost += 3;
5✔
102
            } else if (long.class.equals(aClass)) {
4✔
103
                cost += 4;
5✔
104
            } else if (aClass.isPrimitive()) {
3✔
105
                cost += 5;
5✔
106
            } else {
107
                cost += 6;
4✔
108
            }
109
        }
110

111
        if (cost < 0 || cost > (1L << 48)) {
8!
112
            cost = 1L << 48;
×
113
        }
114

115
        cost += (long) param.length << 48;
8✔
116
        return cost;
2✔
117
    }
118

119
    protected Object[] getConstructorArgs(Constructor<?> constructor) {
120
        Object[] objects = null;
2✔
121

122
        if (constructor != null) {
2✔
123
            Class<?>[] params = constructor.getParameterTypes();
3✔
124
            objects = new Object[params.length];
4✔
125
            for (int i = 0; i < params.length; i++) {
8✔
126
                objects[i] = FieldDeserializer2Factory.getParamArg(params[i]);
7✔
127
            }
128
        }
129

130
        return objects;
2✔
131
    }
132

133
    @Override
134
    protected Object instantiate() throws HessianProtocolException {
135
        try {
136
            if (constructor != null) {
3✔
137
                return constructor.newInstance(constructorArgs);
6✔
138
            } else {
139
                return type.getDeclaredConstructor().newInstance();
×
140
            }
141
        } catch (Exception e) {
1✔
142
            throw new HessianProtocolException("'" + type.getName() + "' could not be instantiated", e);
9✔
143
        }
144
    }
145
}
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