This commit is contained in:
redkale
2024-09-30 09:45:30 +08:00
parent 8cf8a2c037
commit dad7d38202
5 changed files with 76 additions and 94 deletions

View File

@@ -285,6 +285,37 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
return type;
}
protected boolean isCollectionType(final Type type) {
if (type == null) {
return false;
}
if (type instanceof Class) {
return Collection.class.isAssignableFrom((Class) type);
}
ParameterizedType ptype = (ParameterizedType) type;
if (!(ptype.getRawType() instanceof Class)) {
return false;
}
Type[] ptargs = ptype.getActualTypeArguments();
if (ptargs == null || ptargs.length != 1) {
return false;
}
Class ownerType = (Class) ptype.getRawType();
return Collection.class.isAssignableFrom(ownerType);
}
// 返回null表示type不是Collection类型
protected Type getCollectionComponentType(final Type type) {
if (!isCollectionType(type)) {
return null;
}
if (type instanceof Class) {
return Object.class;
}
ParameterizedType ptype = ((ParameterizedType) type);
return ptype.getActualTypeArguments()[0];
}
protected <E> Encodeable<W, E> createDyncEncoder(Type type) {
return null;
}

View File

@@ -36,13 +36,17 @@ public final class BsonFactory extends ConvertFactory<BsonReader, BsonWriter> {
static final Encodeable objectEncoder = instance.loadEncoder(Object.class);
static final Decodeable arrayDecoder = new BsonArrayDecoder(instance, Object[].class);
// only for BsonRead.skipValue
static final Decodeable skipArrayDecoder = new BsonArrayDecoder(instance, Object[].class);
static final Decodeable collectionDecoder = new BsonCollectionDecoder(instance, Collection.class);
// only for BsonRead.skipValue
static final Decodeable skipCollectionDecoder = new BsonCollectionDecoder(instance, Collection.class);
static final Decodeable streamDecoder = new BsonStreamDecoder(instance, Stream.class);
// only for BsonRead.skipValue
static final Decodeable skipStreamDecoder = new BsonStreamDecoder(instance, Stream.class);
static final Decodeable mapDecoder = new BsonMapDecoder(instance, Map.class);
// only for BsonRead.skipValue
static final Decodeable skipMapDecoder = new BsonMapDecoder(instance, Map.class);
static {
instance.register(Serializable.class, objectDecoder);
@@ -154,55 +158,52 @@ public final class BsonFactory extends ConvertFactory<BsonReader, BsonWriter> {
}
protected static byte typeEnum(final Type type) {
return typeEnum(TypeToken.typeToClass(type));
}
protected static byte typeEnum(final Class type) {
Objects.requireNonNull(type);
Class clazz = TypeToken.typeToClass(type);
byte typeval = 127; // 字段的类型值
if (type == boolean.class || type == Boolean.class) {
if (clazz == boolean.class || clazz == Boolean.class) {
typeval = 11;
} else if (type == byte.class || type == Byte.class) {
} else if (clazz == byte.class || clazz == Byte.class) {
typeval = 12;
} else if (type == short.class || type == Short.class) {
} else if (clazz == short.class || clazz == Short.class) {
typeval = 13;
} else if (type == char.class || type == Character.class) {
} else if (clazz == char.class || clazz == Character.class) {
typeval = 14;
} else if (type == int.class || type == Integer.class) {
} else if (clazz == int.class || clazz == Integer.class) {
typeval = 15;
} else if (type == long.class || type == Long.class) {
} else if (clazz == long.class || clazz == Long.class) {
typeval = 16;
} else if (type == float.class || type == Float.class) {
} else if (clazz == float.class || clazz == Float.class) {
typeval = 17;
} else if (type == double.class || type == Double.class) {
} else if (clazz == double.class || clazz == Double.class) {
typeval = 18;
} else if (type == String.class) {
} else if (clazz == String.class) {
typeval = 19;
} else if (type == boolean[].class || type == Boolean[].class) {
} else if (clazz == boolean[].class || clazz == Boolean[].class) {
typeval = 21;
} else if (type == byte[].class || type == Byte[].class) {
} else if (clazz == byte[].class || clazz == Byte[].class) {
typeval = 22;
} else if (type == short[].class || type == Short[].class) {
} else if (clazz == short[].class || clazz == Short[].class) {
typeval = 23;
} else if (type == char[].class || type == Character[].class) {
} else if (clazz == char[].class || clazz == Character[].class) {
typeval = 24;
} else if (type == int[].class || type == Integer[].class) {
} else if (clazz == int[].class || clazz == Integer[].class) {
typeval = 25;
} else if (type == long[].class || type == Long[].class) {
} else if (clazz == long[].class || clazz == Long[].class) {
typeval = 26;
} else if (type == float[].class || type == Float[].class) {
} else if (clazz == float[].class || clazz == Float[].class) {
typeval = 27;
} else if (type == double[].class || type == Double[].class) {
} else if (clazz == double[].class || clazz == Double[].class) {
typeval = 28;
} else if (type == String[].class) {
} else if (clazz == String[].class) {
typeval = 29;
} else if (type.isArray()) {
} else if (clazz.isArray()) {
typeval = 81;
} else if (Collection.class.isAssignableFrom(type)) {
} else if (Collection.class.isAssignableFrom(clazz)) {
typeval = 82;
} else if (Stream.class.isAssignableFrom(type)) {
} else if (Stream.class.isAssignableFrom(clazz)) {
typeval = 83;
} else if (Map.class.isAssignableFrom(type)) {
} else if (Map.class.isAssignableFrom(clazz)) {
typeval = 84;
}
return typeval;
@@ -247,15 +248,15 @@ public final class BsonFactory extends ConvertFactory<BsonReader, BsonWriter> {
case 29:
return StringArraySimpledCoder.instance;
case 81:
return arrayDecoder;
return skipArrayDecoder;
case 82:
return collectionDecoder;
return skipCollectionDecoder;
case 83:
return streamDecoder;
return skipStreamDecoder;
case 84:
return mapDecoder;
return skipMapDecoder;
case 127:
return BsonFactory.objectDecoder;
return objectDecoder;
default:
return null;
}

View File

@@ -104,41 +104,8 @@ public class BsonReader extends Reader {
return;
}
this.fieldTypeEnum = 0;
switch (val) {
case 11:
readBoolean();
break;
case 12:
readByte();
break;
case 13:
readShort();
break;
case 14:
readChar();
break;
case 15:
readInt();
break;
case 16:
readLong();
break;
case 17:
readFloat();
break;
case 18:
readDouble();
break;
case 19:
readString();
break;
default:
Decodeable decoder = BsonFactory.typeEnum(val);
if (decoder != null) {
decoder.convertFrom(this);
}
break;
}
Decodeable decoder = BsonFactory.typeEnum(val);
decoder.convertFrom(this);
}
@Override

View File

@@ -60,7 +60,7 @@ public abstract class ProtobufDynEncoder<T> extends ProtobufObjectEncoder<T> {
elementb.append(fieldName).append(',');
if (!ProtobufFactory.isSimpleType(fieldClass)
&& !fieldClass.isEnum()
&& !ProtobufFactory.supportSimpleCollectionType(fieldType)) {
&& !factory.supportSimpleCollectionType(fieldType)) {
if ((member.getEncoder() instanceof SimpledCoder)) {
simpledCoders.put(fieldName, (SimpledCoder) member.getEncoder());
} else {
@@ -206,7 +206,7 @@ public abstract class ProtobufDynEncoder<T> extends ProtobufObjectEncoder<T> {
mv.visitFieldInsn(GETFIELD, valtypeName, fname, fdesc);
}
mv.visitMethodInsn(INVOKEVIRTUAL, pbwriterName, "writeFieldValue", "(ILjava/lang/Enum;)V", false);
} else if (ProtobufFactory.supportSimpleCollectionType(fieldType)) {
} else if (factory.supportSimpleCollectionType(fieldType)) {
mv.visitVarInsn(ALOAD, 3); // out
Asms.visitInsn(mv, member.getTag()); // tag
mv.visitVarInsn(ALOAD, 2); // value
@@ -220,7 +220,7 @@ public abstract class ProtobufDynEncoder<T> extends ProtobufObjectEncoder<T> {
String fdesc = org.redkale.asm.Type.getDescriptor(field.getType());
mv.visitFieldInsn(GETFIELD, valtypeName, fname, fdesc);
}
Class componentType = ProtobufFactory.getSimpleCollectionComponentType(fieldType);
Class componentType = factory.getSimpleCollectionComponentType(fieldType);
String wmethodName = null;
if (componentType == Boolean.class) {
wmethodName = "writeFieldBoolsValue";

View File

@@ -388,23 +388,8 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|| fieldClass == String[].class;
}
protected static boolean supportSimpleCollectionType(Type type) {
if (!(type instanceof ParameterizedType)) {
return false;
}
ParameterizedType ptype = (ParameterizedType) type;
if (!(ptype.getRawType() instanceof Class)) {
return false;
}
Type[] ptargs = ptype.getActualTypeArguments();
if (ptargs == null || ptargs.length != 1) {
return false;
}
Class ownerType = (Class) ptype.getRawType();
if (!Collection.class.isAssignableFrom(ownerType)) {
return false;
}
Type componentType = ptargs[0];
protected boolean supportSimpleCollectionType(Type type) {
Type componentType = getCollectionComponentType(type);
return componentType == Boolean.class
|| componentType == Byte.class
|| componentType == Character.class
@@ -494,10 +479,8 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
return (n << 1) ^ (n >> 63);
}
public static Class getSimpleCollectionComponentType(Type type) {
return supportSimpleCollectionType(type)
? (Class) ((ParameterizedType) type).getActualTypeArguments()[0]
: null;
protected Class getSimpleCollectionComponentType(Type type) {
return supportSimpleCollectionType(type) ? TypeToken.typeToClass(getCollectionComponentType(type)) : null;
}
public static int getTag(String fieldName, Type fieldType, int fieldPos, boolean enumtostring) {