移除ConvertEntity
This commit is contained in:
@@ -28,7 +28,6 @@ public final class AnyEncoder<W extends Writer, T> implements Encodeable<W, T> {
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public void convertTo(final W out, final T value) {
|
public void convertTo(final W out, final T value) {
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
out.writeClassName(null);
|
|
||||||
out.writeNull();
|
out.writeNull();
|
||||||
} else {
|
} else {
|
||||||
Class clazz = value.getClass();
|
Class clazz = value.getClass();
|
||||||
@@ -37,9 +36,6 @@ public final class AnyEncoder<W extends Writer, T> implements Encodeable<W, T> {
|
|||||||
out.writeObjectE(value);
|
out.writeObjectE(value);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (out.needWriteClassName()) {
|
|
||||||
out.writeClassName(factory.getEntityAlias(clazz));
|
|
||||||
}
|
|
||||||
factory.loadEncoder(clazz).convertTo(out, value);
|
factory.loadEncoder(clazz).convertTo(out, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,33 +0,0 @@
|
|||||||
/*
|
|
||||||
* To change this license header, choose License Headers in Project Properties.
|
|
||||||
* To change this template file, choose Tools | Templates
|
|
||||||
* and open the template in the editor.
|
|
||||||
*/
|
|
||||||
package org.redkale.convert;
|
|
||||||
|
|
||||||
import java.lang.annotation.*;
|
|
||||||
import static java.lang.annotation.ElementType.TYPE;
|
|
||||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 用于类名的别名, 该值必须是全局唯一 <br>
|
|
||||||
* 使用场景: 当自定义序列化为了不指定class可以使用@ConvertEntity来取个别名。 <br>
|
|
||||||
* 关联方法: {@link org.redkale.convert.Reader#readClassName()} 和 {@link org.redkale.convert.Writer#writeClassName(java.lang.String) } 。
|
|
||||||
*
|
|
||||||
* <p>详情见: https://redkale.org
|
|
||||||
*
|
|
||||||
* @author zhangjx
|
|
||||||
*/
|
|
||||||
@Inherited
|
|
||||||
@Documented
|
|
||||||
@Target({TYPE})
|
|
||||||
@Retention(RUNTIME)
|
|
||||||
public @interface ConvertEntity {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 别名值
|
|
||||||
*
|
|
||||||
* @return String
|
|
||||||
*/
|
|
||||||
String value();
|
|
||||||
}
|
|
||||||
@@ -53,8 +53,6 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
|||||||
// -----------------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------------
|
||||||
private final ConcurrentHashMap<Class, Creator> creators = new ConcurrentHashMap();
|
private final ConcurrentHashMap<Class, Creator> creators = new ConcurrentHashMap();
|
||||||
|
|
||||||
private final ConcurrentHashMap<String, Class> entitys = new ConcurrentHashMap();
|
|
||||||
|
|
||||||
private final ConcurrentHashMap<Type, Map<String, SimpledCoder<R, W, ?>>> fieldCoders = new ConcurrentHashMap();
|
private final ConcurrentHashMap<Type, Map<String, SimpledCoder<R, W, ?>>> fieldCoders = new ConcurrentHashMap();
|
||||||
|
|
||||||
private final ConcurrentHashMap<Type, Decodeable<R, ?>> decoders = new ConcurrentHashMap();
|
private final ConcurrentHashMap<Type, Decodeable<R, ?>> decoders = new ConcurrentHashMap();
|
||||||
@@ -765,185 +763,6 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
final String getEntityAlias(Class clazz) {
|
|
||||||
if (clazz == String.class) {
|
|
||||||
return "A";
|
|
||||||
}
|
|
||||||
if (clazz == int.class) {
|
|
||||||
return "I";
|
|
||||||
}
|
|
||||||
if (clazz == Integer.class) {
|
|
||||||
return "i";
|
|
||||||
}
|
|
||||||
if (clazz == long.class) {
|
|
||||||
return "J";
|
|
||||||
}
|
|
||||||
if (clazz == Long.class) {
|
|
||||||
return "j";
|
|
||||||
}
|
|
||||||
if (clazz == byte.class) {
|
|
||||||
return "B";
|
|
||||||
}
|
|
||||||
if (clazz == Byte.class) {
|
|
||||||
return "b";
|
|
||||||
}
|
|
||||||
if (clazz == boolean.class) {
|
|
||||||
return "Z";
|
|
||||||
}
|
|
||||||
if (clazz == Boolean.class) {
|
|
||||||
return "z";
|
|
||||||
}
|
|
||||||
if (clazz == short.class) {
|
|
||||||
return "S";
|
|
||||||
}
|
|
||||||
if (clazz == Short.class) {
|
|
||||||
return "s";
|
|
||||||
}
|
|
||||||
if (clazz == char.class) {
|
|
||||||
return "C";
|
|
||||||
}
|
|
||||||
if (clazz == Character.class) {
|
|
||||||
return "c";
|
|
||||||
}
|
|
||||||
if (clazz == float.class) {
|
|
||||||
return "F";
|
|
||||||
}
|
|
||||||
if (clazz == Float.class) {
|
|
||||||
return "f";
|
|
||||||
}
|
|
||||||
if (clazz == double.class) {
|
|
||||||
return "D";
|
|
||||||
}
|
|
||||||
if (clazz == Double.class) {
|
|
||||||
return "d";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (clazz == String[].class) {
|
|
||||||
return "[A";
|
|
||||||
}
|
|
||||||
if (clazz == int[].class) {
|
|
||||||
return "[I";
|
|
||||||
}
|
|
||||||
if (clazz == long[].class) {
|
|
||||||
return "[J";
|
|
||||||
}
|
|
||||||
if (clazz == byte[].class) {
|
|
||||||
return "[B";
|
|
||||||
}
|
|
||||||
if (clazz == boolean[].class) {
|
|
||||||
return "[Z";
|
|
||||||
}
|
|
||||||
if (clazz == short[].class) {
|
|
||||||
return "[S";
|
|
||||||
}
|
|
||||||
if (clazz == char[].class) {
|
|
||||||
return "[C";
|
|
||||||
}
|
|
||||||
if (clazz == float[].class) {
|
|
||||||
return "[F";
|
|
||||||
}
|
|
||||||
if (clazz == double[].class) {
|
|
||||||
return "[D";
|
|
||||||
}
|
|
||||||
|
|
||||||
ConvertEntity ce = (ConvertEntity) clazz.getAnnotation(ConvertEntity.class);
|
|
||||||
if (ce != null && findEntityAlias(ce.value()) == null) {
|
|
||||||
entitys.put(ce.value(), clazz);
|
|
||||||
}
|
|
||||||
return ce == null ? clazz.getName() : ce.value();
|
|
||||||
}
|
|
||||||
|
|
||||||
final Class getEntityAlias(String name) {
|
|
||||||
if ("A".equals(name)) {
|
|
||||||
return String.class;
|
|
||||||
}
|
|
||||||
if ("I".equals(name)) {
|
|
||||||
return int.class;
|
|
||||||
}
|
|
||||||
if ("i".equals(name)) {
|
|
||||||
return Integer.class;
|
|
||||||
}
|
|
||||||
if ("J".equals(name)) {
|
|
||||||
return long.class;
|
|
||||||
}
|
|
||||||
if ("j".equals(name)) {
|
|
||||||
return Long.class;
|
|
||||||
}
|
|
||||||
if ("B".equals(name)) {
|
|
||||||
return byte.class;
|
|
||||||
}
|
|
||||||
if ("b".equals(name)) {
|
|
||||||
return Byte.class;
|
|
||||||
}
|
|
||||||
if ("Z".equals(name)) {
|
|
||||||
return boolean.class;
|
|
||||||
}
|
|
||||||
if ("z".equals(name)) {
|
|
||||||
return Boolean.class;
|
|
||||||
}
|
|
||||||
if ("S".equals(name)) {
|
|
||||||
return short.class;
|
|
||||||
}
|
|
||||||
if ("s".equals(name)) {
|
|
||||||
return Short.class;
|
|
||||||
}
|
|
||||||
if ("C".equals(name)) {
|
|
||||||
return char.class;
|
|
||||||
}
|
|
||||||
if ("c".equals(name)) {
|
|
||||||
return Character.class;
|
|
||||||
}
|
|
||||||
if ("F".equals(name)) {
|
|
||||||
return float.class;
|
|
||||||
}
|
|
||||||
if ("f".equals(name)) {
|
|
||||||
return Float.class;
|
|
||||||
}
|
|
||||||
if ("D".equals(name)) {
|
|
||||||
return double.class;
|
|
||||||
}
|
|
||||||
if ("d".equals(name)) {
|
|
||||||
return Double.class;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ("[A".equals(name)) {
|
|
||||||
return String[].class;
|
|
||||||
}
|
|
||||||
if ("[I".equals(name)) {
|
|
||||||
return int[].class;
|
|
||||||
}
|
|
||||||
if ("[J".equals(name)) {
|
|
||||||
return long[].class;
|
|
||||||
}
|
|
||||||
if ("[B".equals(name)) {
|
|
||||||
return byte[].class;
|
|
||||||
}
|
|
||||||
if ("[Z".equals(name)) {
|
|
||||||
return boolean[].class;
|
|
||||||
}
|
|
||||||
if ("[S".equals(name)) {
|
|
||||||
return short[].class;
|
|
||||||
}
|
|
||||||
if ("[C".equals(name)) {
|
|
||||||
return char[].class;
|
|
||||||
}
|
|
||||||
if ("[F".equals(name)) {
|
|
||||||
return float[].class;
|
|
||||||
}
|
|
||||||
if ("[D".equals(name)) {
|
|
||||||
return double[].class;
|
|
||||||
}
|
|
||||||
|
|
||||||
Class clazz = findEntityAlias(name);
|
|
||||||
try {
|
|
||||||
return clazz == null
|
|
||||||
? Thread.currentThread().getContextClassLoader().loadClass(name)
|
|
||||||
: clazz;
|
|
||||||
} catch (Exception ex) {
|
|
||||||
throw new ConvertException("convert entity is " + name, ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ConvertFactory columnFactory(Type type, ConvertCoder[] coders, boolean encode) {
|
ConvertFactory columnFactory(Type type, ConvertCoder[] coders, boolean encode) {
|
||||||
if (Utility.isEmpty(coders)) {
|
if (Utility.isEmpty(coders)) {
|
||||||
return this;
|
return this;
|
||||||
@@ -1144,11 +963,6 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
|||||||
return parent == null ? null : parent.findFieldFuncConsumer();
|
return parent == null ? null : parent.findFieldFuncConsumer();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Class findEntityAlias(String name) {
|
|
||||||
Class clazz = entitys.get(name);
|
|
||||||
return parent == null ? clazz : parent.findEntityAlias(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置ColumnHandler初始化的处理函数
|
* 设置ColumnHandler初始化的处理函数
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -363,9 +363,6 @@ public class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T> {
|
|||||||
if (clazz == null) {
|
if (clazz == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (!clazz.isEmpty()) {
|
|
||||||
return (T) factory.loadDecoder(factory.getEntityAlias(clazz)).convertFrom(in);
|
|
||||||
}
|
|
||||||
if (this.creator == null) {
|
if (this.creator == null) {
|
||||||
if (typeClass.isInterface() || Modifier.isAbstract(typeClass.getModifiers())) {
|
if (typeClass.isInterface() || Modifier.isAbstract(typeClass.getModifiers())) {
|
||||||
throw new ConvertException(
|
throw new ConvertException(
|
||||||
|
|||||||
@@ -331,9 +331,6 @@ public class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T> {
|
|||||||
}
|
}
|
||||||
if (value.getClass() != this.typeClass && !this.type.equals(out.specificObjectType())) {
|
if (value.getClass() != this.typeClass && !this.type.equals(out.specificObjectType())) {
|
||||||
final Class clz = value.getClass();
|
final Class clz = value.getClass();
|
||||||
if (out.needWriteClassName()) {
|
|
||||||
out.writeClassName(factory.getEntityAlias(clz));
|
|
||||||
}
|
|
||||||
factory.loadEncoder(clz).convertTo(out, value);
|
factory.loadEncoder(clz).convertTo(out, value);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -191,13 +191,6 @@ public abstract class Reader {
|
|||||||
*/
|
*/
|
||||||
public abstract String readStandardString();
|
public abstract String readStandardString();
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取反解析对象的类名
|
|
||||||
*
|
|
||||||
* @return 类名
|
|
||||||
*/
|
|
||||||
public abstract String readClassName();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 读取一个String值
|
* 读取一个String值
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -95,20 +95,6 @@ public abstract class Writer {
|
|||||||
/** 输出null值 */
|
/** 输出null值 */
|
||||||
public abstract void writeNull();
|
public abstract void writeNull();
|
||||||
|
|
||||||
/**
|
|
||||||
* 是否需要写入类名, JSON不需要
|
|
||||||
*
|
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
public abstract boolean needWriteClassName();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 写入类名
|
|
||||||
*
|
|
||||||
* @param clazz 类名
|
|
||||||
*/
|
|
||||||
public abstract void writeClassName(String clazz);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 输出一个为null的对象
|
* 输出一个为null的对象
|
||||||
*
|
*
|
||||||
@@ -116,7 +102,6 @@ public abstract class Writer {
|
|||||||
*/
|
*/
|
||||||
@ClassDepends
|
@ClassDepends
|
||||||
public final void writeObjectNull(final Class clazz) {
|
public final void writeObjectNull(final Class clazz) {
|
||||||
writeClassName(null);
|
|
||||||
writeNull();
|
writeNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -384,11 +384,6 @@ public class JsonReader extends Reader {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public final String readClassName() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 读取一个int值
|
* 读取一个int值
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -215,14 +215,6 @@ public abstract class JsonWriter extends Writer {
|
|||||||
writeLatin1To(false, String.valueOf(value));
|
writeLatin1To(false, String.valueOf(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public final boolean needWriteClassName() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void writeClassName(String clazz) {}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void writeObjectB(Object obj) {
|
public final void writeObjectB(Object obj) {
|
||||||
super.writeObjectB(obj);
|
super.writeObjectB(obj);
|
||||||
|
|||||||
@@ -396,11 +396,6 @@ public class ProtobufReader extends Reader {
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public final String readClassName() {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final String readStandardString() {
|
public final String readStandardString() {
|
||||||
return readString();
|
return readString();
|
||||||
|
|||||||
@@ -193,16 +193,6 @@ public abstract class ProtobufWriter extends Writer {
|
|||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public final boolean needWriteClassName() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void writeClassName(String clazz) {
|
|
||||||
// do nothing
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ClassDepends
|
@ClassDepends
|
||||||
public final void writeObjectB(Object obj) {
|
public final void writeObjectB(Object obj) {
|
||||||
|
|||||||
@@ -7,10 +7,8 @@ package org.redkale.test.convert;
|
|||||||
|
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import org.redkale.convert.ConvertEntity;
|
|
||||||
|
|
||||||
/** @author zhangjx */
|
/** @author zhangjx */
|
||||||
@ConvertEntity("myname")
|
|
||||||
public class SimpleChildEntity extends SimpleEntity {
|
public class SimpleChildEntity extends SimpleEntity {
|
||||||
|
|
||||||
private short st = -1234;
|
private short st = -1234;
|
||||||
|
|||||||
Reference in New Issue
Block a user