This commit is contained in:
@@ -76,7 +76,7 @@ public final class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T
|
||||
if (Modifier.isStatic(field.getModifiers())) continue;
|
||||
ref = factory.findRef(field);
|
||||
if (ref != null && ref.ignore()) continue;
|
||||
Type t = ObjectEncoder.createClassType(field.getGenericType(), this.type);
|
||||
Type t = TypeToken.createClassType(field.getGenericType(), this.type);
|
||||
list.add(new DeMember(ObjectEncoder.createAttribute(factory, clazz, field, null, null), factory.loadDecoder(t)));
|
||||
}
|
||||
final boolean reversible = factory.isReversible();
|
||||
@@ -99,7 +99,7 @@ public final class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T
|
||||
}
|
||||
ref = factory.findRef(method);
|
||||
if (ref != null && ref.ignore()) continue;
|
||||
Type t = ObjectEncoder.createClassType(method.getGenericParameterTypes()[0], this.type);
|
||||
Type t = TypeToken.createClassType(method.getGenericParameterTypes()[0], this.type);
|
||||
list.add(new DeMember(ObjectEncoder.createAttribute(factory, clazz, null, null, method), factory.loadDecoder(t)));
|
||||
}
|
||||
if (cps != null) { //可能存在某些构造函数中的字段名不存在setter方法
|
||||
@@ -115,7 +115,7 @@ public final class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T
|
||||
//不存在setter方法
|
||||
try {
|
||||
Field f = clazz.getDeclaredField(constructorField);
|
||||
Type t = ObjectEncoder.createClassType(f.getGenericType(), this.type);
|
||||
Type t = TypeToken.createClassType(f.getGenericType(), this.type);
|
||||
list.add(new DeMember(ObjectEncoder.createAttribute(factory, clazz, f, null, null), factory.loadDecoder(t)));
|
||||
} catch (NoSuchFieldException nsfe) { //不存在field, 可能存在getter方法
|
||||
char[] fs = constructorField.toCharArray();
|
||||
@@ -127,7 +127,7 @@ public final class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T
|
||||
} catch (NoSuchMethodException ex) {
|
||||
getter = clazz.getMethod("is" + mn);
|
||||
}
|
||||
Type t = ObjectEncoder.createClassType(getter.getGenericParameterTypes()[0], this.type);
|
||||
Type t = TypeToken.createClassType(getter.getGenericParameterTypes()[0], this.type);
|
||||
list.add(new DeMember(ObjectEncoder.createAttribute(factory, clazz, null, getter, null), factory.loadDecoder(t)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ public final class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T
|
||||
if (Modifier.isStatic(field.getModifiers())) continue;
|
||||
ref = factory.findRef(field);
|
||||
if (ref != null && ref.ignore()) continue;
|
||||
Type t = createClassType(field.getGenericType(), this.type);
|
||||
Type t = TypeToken.createClassType(field.getGenericType(), this.type);
|
||||
list.add(new EnMember(createAttribute(factory, clazz, field, null, null), factory.loadEncoder(t)));
|
||||
}
|
||||
for (final Method method : clazz.getMethods()) {
|
||||
@@ -91,7 +91,7 @@ public final class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T
|
||||
}
|
||||
ref = factory.findRef(method);
|
||||
if (ref != null && ref.ignore()) continue;
|
||||
Type t = createClassType(method.getGenericReturnType(), this.type);
|
||||
Type t = TypeToken.createClassType(method.getGenericReturnType(), this.type);
|
||||
list.add(new EnMember(createAttribute(factory, clazz, null, method, null), factory.loadEncoder(t)));
|
||||
}
|
||||
this.members = list.toArray(new EnMember[list.size()]);
|
||||
@@ -146,42 +146,6 @@ public final class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T
|
||||
return "ObjectEncoder{" + "type=" + type + ", members=" + Arrays.toString(members) + '}';
|
||||
}
|
||||
|
||||
static Type createClassType(final Type type, final Type declaringType0) {
|
||||
if (TypeToken.isClassType(type)) return type;
|
||||
if (type instanceof ParameterizedType) { // e.g. Map<String, String>
|
||||
final ParameterizedType pt = (ParameterizedType) type;
|
||||
final Type[] paramTypes = pt.getActualTypeArguments();
|
||||
for (int i = 0; i < paramTypes.length; i++) {
|
||||
paramTypes[i] = createClassType(paramTypes[i], declaringType0);
|
||||
}
|
||||
return TypeToken.createParameterizedType(pt.getOwnerType(), pt.getRawType(), paramTypes);
|
||||
}
|
||||
Type declaringType = declaringType0;
|
||||
if (declaringType instanceof Class) {
|
||||
do {
|
||||
declaringType = ((Class) declaringType).getGenericSuperclass();
|
||||
if (declaringType == Object.class) return Object.class;
|
||||
} while (declaringType instanceof Class);
|
||||
}
|
||||
//存在通配符则declaringType 必须是 ParameterizedType
|
||||
if (!(declaringType instanceof ParameterizedType)) return Object.class;
|
||||
final ParameterizedType declaringPType = (ParameterizedType) declaringType;
|
||||
final Type[] virTypes = ((Class) declaringPType.getRawType()).getTypeParameters();
|
||||
final Type[] desTypes = declaringPType.getActualTypeArguments();
|
||||
if (type instanceof WildcardType) { // e.g. <? extends Serializable>
|
||||
final WildcardType wt = (WildcardType) type;
|
||||
for (Type f : wt.getUpperBounds()) {
|
||||
for (int i = 0; i < virTypes.length; i++) {
|
||||
if (virTypes[i].equals(f)) return desTypes.length <= i ? Object.class : desTypes[i];
|
||||
}
|
||||
}
|
||||
} else if (type instanceof TypeVariable) { // e.g. <? extends E>
|
||||
for (int i = 0; i < virTypes.length; i++) {
|
||||
if (virTypes[i].equals(type)) return desTypes.length <= i ? Object.class : desTypes[i];
|
||||
}
|
||||
}
|
||||
return type;
|
||||
}
|
||||
//
|
||||
// static Type makeGenericType(final Type type, final Type[] virGenericTypes, final Type[] realGenericTypes) {
|
||||
// if (type instanceof Class) { //e.g. String
|
||||
@@ -230,7 +194,6 @@ public final class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T
|
||||
// }
|
||||
// return type;
|
||||
// }
|
||||
|
||||
static boolean contains(String[] values, String value) {
|
||||
for (String str : values) {
|
||||
if (str.equals(value)) return true;
|
||||
|
||||
@@ -38,6 +38,7 @@ public abstract class TypeToken<T> {
|
||||
* 例如: Map< String, String > 返回 ture; Map< ? extends Serializable, String > 返回false;
|
||||
*
|
||||
* @param type Type对象
|
||||
*
|
||||
* @return 是否可反解析
|
||||
*/
|
||||
public final static boolean isClassType(final Type type) {
|
||||
@@ -55,12 +56,58 @@ public abstract class TypeToken<T> {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 动态创建类型为ParameterizedType或Class的Type
|
||||
*
|
||||
* @param type 当前泛型
|
||||
* @param declaringType0 子类
|
||||
*
|
||||
* @return Type
|
||||
*/
|
||||
public static Type createClassType(final Type type, final Type declaringType0) {
|
||||
if (isClassType(type)) return type;
|
||||
if (type instanceof ParameterizedType) { // e.g. Map<String, String>
|
||||
final ParameterizedType pt = (ParameterizedType) type;
|
||||
final Type[] paramTypes = pt.getActualTypeArguments();
|
||||
for (int i = 0; i < paramTypes.length; i++) {
|
||||
paramTypes[i] = createClassType(paramTypes[i], declaringType0);
|
||||
}
|
||||
return createParameterizedType(pt.getOwnerType(), pt.getRawType(), paramTypes);
|
||||
}
|
||||
Type declaringType = declaringType0;
|
||||
if (declaringType instanceof Class) {
|
||||
do {
|
||||
declaringType = ((Class) declaringType).getGenericSuperclass();
|
||||
if (declaringType == Object.class) return Object.class;
|
||||
} while (declaringType instanceof Class);
|
||||
}
|
||||
//存在通配符则declaringType 必须是 ParameterizedType
|
||||
if (!(declaringType instanceof ParameterizedType)) return Object.class;
|
||||
final ParameterizedType declaringPType = (ParameterizedType) declaringType;
|
||||
final Type[] virTypes = ((Class) declaringPType.getRawType()).getTypeParameters();
|
||||
final Type[] desTypes = declaringPType.getActualTypeArguments();
|
||||
if (type instanceof WildcardType) { // e.g. <? extends Serializable>
|
||||
final WildcardType wt = (WildcardType) type;
|
||||
for (Type f : wt.getUpperBounds()) {
|
||||
for (int i = 0; i < virTypes.length; i++) {
|
||||
if (virTypes[i].equals(f)) return desTypes.length <= i ? Object.class : desTypes[i];
|
||||
}
|
||||
}
|
||||
} else if (type instanceof TypeVariable) { // e.g. <? extends E>
|
||||
for (int i = 0; i < virTypes.length; i++) {
|
||||
if (virTypes[i].equals(type)) return desTypes.length <= i ? Object.class : desTypes[i];
|
||||
}
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* 动态创建 ParameterizedType
|
||||
*
|
||||
* @param ownerType0 ParameterizedType 的 ownerType
|
||||
* @param rawType0 ParameterizedType 的 rawType
|
||||
* @param actualTypeArguments0 ParameterizedType 的 actualTypeArguments
|
||||
*
|
||||
* @return Type
|
||||
*/
|
||||
public static Type createParameterizedType(final Type ownerType0, final Type rawType0, final Type... actualTypeArguments0) {
|
||||
@@ -104,8 +151,8 @@ public abstract class TypeToken<T> {
|
||||
final ParameterizedType that = (ParameterizedType) o;
|
||||
if (this == that) return true;
|
||||
return Objects.equals(ownerType, that.getOwnerType())
|
||||
&& Objects.equals(rawType, that.getRawType())
|
||||
&& Arrays.equals(actualTypeArguments, that.getActualTypeArguments());
|
||||
&& Objects.equals(rawType, that.getRawType())
|
||||
&& Arrays.equals(actualTypeArguments, that.getActualTypeArguments());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user