|
24 | 24 | import java.io.InputStream; |
25 | 25 | import java.lang.reflect.ParameterizedType; |
26 | 26 | import java.lang.reflect.Type; |
27 | | -import java.util.ArrayList; |
28 | | -import java.util.Collection; |
29 | | -import java.util.HashSet; |
30 | | -import java.util.LinkedHashSet; |
31 | | -import java.util.LinkedList; |
32 | | -import java.util.Map; |
33 | 27 | import java.util.Objects; |
34 | | -import java.util.Set; |
35 | | -import java.util.TreeSet; |
36 | 28 |
|
37 | 29 | import com.alibaba.com.caucho.hessian.io.Hessian2Input; |
38 | 30 |
|
@@ -136,88 +128,15 @@ public <T> T readObject(Class<T> cls, Type type) throws IOException, ClassNotFou |
136 | 128 | mH2i.setSerializerFactory(hessian2FactoryManager.getSerializerFactory( |
137 | 129 | Thread.currentThread().getContextClassLoader())); |
138 | 130 | } |
139 | | - T result = readObject(cls); |
140 | | - if (type instanceof ParameterizedType && result != null) { |
141 | | - result = (T) convertCollectionElementsIfNeeded(result, (ParameterizedType) type); |
142 | | - } |
143 | | - return result; |
144 | | - } |
145 | | - |
146 | | - private Object convertCollectionElementsIfNeeded(Object obj, ParameterizedType type) { |
147 | | - Type[] typeArgs = type.getActualTypeArguments(); |
148 | | - |
149 | | - if (obj instanceof Collection && typeArgs.length >= 1 && typeArgs[0] instanceof Class) { |
150 | | - Class<?> elementType = (Class<?>) typeArgs[0]; |
151 | | - if (isNarrowNumberType(elementType)) { |
152 | | - Collection<?> src = (Collection<?>) obj; |
153 | | - Collection<Object> converted = createCompatibleCollection(src, src.size()); |
154 | | - for (Object e : src) { |
155 | | - converted.add(convertNumber(e, elementType)); |
156 | | - } |
157 | | - return converted; |
| 131 | + if (type instanceof ParameterizedType) { |
| 132 | + Type[] typeArgs = ((ParameterizedType) type).getActualTypeArguments(); |
| 133 | + Class<?>[] expectedTypes = new Class<?>[typeArgs.length]; |
| 134 | + for (int i = 0; i < typeArgs.length; i++) { |
| 135 | + expectedTypes[i] = typeArgs[i] instanceof Class ? (Class<?>) typeArgs[i] : Object.class; |
158 | 136 | } |
| 137 | + return (T) mH2i.readObject(cls, expectedTypes); |
159 | 138 | } |
160 | | - |
161 | | - if (obj instanceof Map && typeArgs.length >= 2) { |
162 | | - Class<?> keyType = typeArgs[0] instanceof Class ? (Class<?>) typeArgs[0] : null; |
163 | | - Class<?> valType = typeArgs[1] instanceof Class ? (Class<?>) typeArgs[1] : null; |
164 | | - boolean convertKey = keyType != null && isNarrowNumberType(keyType); |
165 | | - boolean convertVal = valType != null && isNarrowNumberType(valType); |
166 | | - if (convertKey || convertVal) { |
167 | | - Map<Object, Object> src = (Map<Object, Object>) obj; |
168 | | - Map<Object, Object> converted = new java.util.LinkedHashMap<>(src.size()); |
169 | | - for (Map.Entry<Object, Object> entry : src.entrySet()) { |
170 | | - Object k = convertKey ? convertNumber(entry.getKey(), keyType) : entry.getKey(); |
171 | | - Object v = convertVal ? convertNumber(entry.getValue(), valType) : entry.getValue(); |
172 | | - converted.put(k, v); |
173 | | - } |
174 | | - return converted; |
175 | | - } |
176 | | - } |
177 | | - return obj; |
178 | | - } |
179 | | - |
180 | | - private static boolean isNarrowNumberType(Class<?> type) { |
181 | | - return type == Byte.class |
182 | | - || type == byte.class |
183 | | - || type == Short.class |
184 | | - || type == short.class |
185 | | - || type == Float.class |
186 | | - || type == float.class; |
187 | | - } |
188 | | - |
189 | | - private static Object convertNumber(Object value, Class<?> targetType) { |
190 | | - if (!(value instanceof Number)) { |
191 | | - return value; |
192 | | - } |
193 | | - Number num = (Number) value; |
194 | | - if (targetType == Byte.class || targetType == byte.class) { |
195 | | - return num.byteValue(); |
196 | | - } |
197 | | - if (targetType == Short.class || targetType == short.class) { |
198 | | - return num.shortValue(); |
199 | | - } |
200 | | - if (targetType == Float.class || targetType == float.class) { |
201 | | - return num.floatValue(); |
202 | | - } |
203 | | - return value; |
204 | | - } |
205 | | - |
206 | | - @SuppressWarnings({"unchecked", "rawtypes"}) |
207 | | - private static Collection<Object> createCompatibleCollection(Collection<?> source, int size) { |
208 | | - if (source instanceof LinkedList) { |
209 | | - return new LinkedList<>(); |
210 | | - } |
211 | | - if (source instanceof LinkedHashSet) { |
212 | | - return new LinkedHashSet<>(size); |
213 | | - } |
214 | | - if (source instanceof TreeSet) { |
215 | | - return new TreeSet(((TreeSet) source).comparator()); |
216 | | - } |
217 | | - if (source instanceof Set) { |
218 | | - return new HashSet<>(size); |
219 | | - } |
220 | | - return new ArrayList<>(size); |
| 139 | + return readObject(cls); |
221 | 140 | } |
222 | 141 |
|
223 | 142 | public InputStream readInputStream() throws IOException { |
|
0 commit comments