Skip to content

Commit 7006f38

Browse files
committed
refactor: use Hessian2Input.readObject(Class, Class...) for generic type handling
Use hessian2's built-in expectedTypes support instead of post-processing conversion, as suggested in review. Change-Id: I1cbb65a684f2f03aa7219826f1fa866b6d7d79cc Co-developed-by: Cursor <noreply@cursor.com>
1 parent 5d58c5f commit 7006f38

1 file changed

Lines changed: 7 additions & 88 deletions

File tree

  • dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2

dubbo-serialization/dubbo-serialization-hessian2/src/main/java/org/apache/dubbo/common/serialize/hessian2/Hessian2ObjectInput.java

Lines changed: 7 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,7 @@
2424
import java.io.InputStream;
2525
import java.lang.reflect.ParameterizedType;
2626
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;
3327
import java.util.Objects;
34-
import java.util.Set;
35-
import java.util.TreeSet;
3628

3729
import com.alibaba.com.caucho.hessian.io.Hessian2Input;
3830

@@ -136,88 +128,15 @@ public <T> T readObject(Class<T> cls, Type type) throws IOException, ClassNotFou
136128
mH2i.setSerializerFactory(hessian2FactoryManager.getSerializerFactory(
137129
Thread.currentThread().getContextClassLoader()));
138130
}
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;
158136
}
137+
return (T) mH2i.readObject(cls, expectedTypes);
159138
}
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);
221140
}
222141

223142
public InputStream readInputStream() throws IOException {

0 commit comments

Comments
 (0)