Convert优化

This commit is contained in:
Redkale
2018-07-21 10:50:01 +08:00
parent 099b3fb7f3
commit 5de52ed6e8
15 changed files with 174 additions and 96 deletions

View File

@@ -30,8 +30,43 @@ public class ServiceWatchService extends AbstractWatchService {
protected Application application; protected Application application;
@RestConvert(type = void.class) @RestConvert(type = void.class)
@RestMapping(name = "findfield", auth = false, comment = "查询Service中指定字段的内容") @RestMapping(name = "setfield", auth = false, comment = "设置Service中指定字段的内容")
public RetResult findfield(@RestParam(name = "name", comment = "Service的资源名") String name, public RetResult setfield(@RestParam(name = "name", comment = "Service的资源名") String name,
@RestParam(name = "type", comment = "Service的类名") String type,
@RestParam(name = "field", comment = "字段名") String field,
@RestParam(name = "value", comment = "字段值") String value) {
if (name == null) name = "";
if (type == null) type = "";
if (field == null) field = "";
type = type.trim();
field = field.trim();
if (type.isEmpty()) return new RetResult(RET_WATCH_PARAMS_ILLEGAL, "not found param `type`");
if (field.isEmpty()) return new RetResult(RET_WATCH_PARAMS_ILLEGAL, "not found param `field`");
Object dest = findService(name, type);
Class clazz = dest.getClass();
Throwable t = null;
try {
Field fieldObj = null;
do {
try {
fieldObj = clazz.getDeclaredField(field);
break;
} catch (Exception e) {
if (t == null) t = e;
}
} while ((clazz = clazz.getSuperclass()) != Object.class);
if (fieldObj == null) return new RetResult(RET_WATCH_RUN_EXCEPTION, "run exception (" + String.valueOf(t) + ")");
fieldObj.setAccessible(true);
fieldObj.set(dest, JsonConvert.root().convertFrom(fieldObj.getGenericType(), value));
return RetResult.success();
} catch (Throwable t2) {
return new RetResult(RET_WATCH_RUN_EXCEPTION, "run exception (" + t2.toString() + ")");
}
}
@RestConvert(type = void.class)
@RestMapping(name = "getfield", auth = false, comment = "查询Service中指定字段的内容")
public RetResult getfield(@RestParam(name = "name", comment = "Service的资源名") String name,
@RestParam(name = "type", comment = "Service的类名") String type, @RestParam(name = "type", comment = "Service的类名") String type,
@RestParam(name = "field", comment = "字段名") String field) { @RestParam(name = "field", comment = "字段名") String field) {
if (name == null) name = ""; if (name == null) name = "";
@@ -133,16 +168,28 @@ public class ServiceWatchService extends AbstractWatchService {
if (dest == null) return new RetResult(RET_SERVICE_DEST_NOT_EXISTS, "not found servie (name=" + name + ", type=" + type + ")"); if (dest == null) return new RetResult(RET_SERVICE_DEST_NOT_EXISTS, "not found servie (name=" + name + ", type=" + type + ")");
return dest; return dest;
} }
//
// @RestMapping(name = "load", auth = false, comment = "动态增加Service") @RestMapping(name = "load", auth = false, comment = "动态增加Service")
// public RetResult loadService(String type, @RestUploadFile(maxLength = 10 * 1024 * 1024, fileNameReg = "\\.jar$") byte[] jar) { public RetResult loadService(String type, @RestUploadFile(maxLength = 10 * 1024 * 1024, fileNameReg = "\\.jar$") byte[] jar) {
// //待开发 //待开发
// return RetResult.success(); return RetResult.success();
// } }
//
// @RestMapping(name = "stop", auth = false, comment = "动态停止Service") @RestMapping(name = "reload", auth = false, comment = "重新加载Service")
// public RetResult stopService(String name, String type) { public RetResult reloadService(String name, String type) {
// //待开发 //待开发
// return RetResult.success(); return RetResult.success();
// } }
@RestMapping(name = "stop", auth = false, comment = "动态停止Service")
public RetResult stopService(String name, String type) {
//待开发
return RetResult.success();
}
@RestMapping(name = "find", auth = false, comment = "查找Service")
public RetResult find(String name, String type) {
//待开发
return RetResult.success();
}
} }

View File

@@ -20,19 +20,19 @@ import java.util.*;
* @param <T> 反解析的数组元素类型 * @param <T> 反解析的数组元素类型
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public final class ArrayDecoder<T> implements Decodeable<Reader, T[]> { public class ArrayDecoder<T> implements Decodeable<Reader, T[]> {
private final Type type; protected final Type type;
private final Type componentType; protected final Type componentType;
private final Class componentClass; protected final Class componentClass;
protected final Decodeable<Reader, T> decoder; protected final Decodeable<Reader, T> decoder;
private boolean inited = false; protected boolean inited = false;
private final Object lock = new Object(); protected final Object lock = new Object();
public ArrayDecoder(final ConvertFactory factory, final Type type) { public ArrayDecoder(final ConvertFactory factory, final Type type) {
this.type = type; this.type = type;

View File

@@ -19,19 +19,19 @@ import java.lang.reflect.*;
* @param <T> 序列化的数组元素类型 * @param <T> 序列化的数组元素类型
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public final class ArrayEncoder<T> implements Encodeable<Writer, T[]> { public class ArrayEncoder<T> implements Encodeable<Writer, T[]> {
private final Type type; protected final Type type;
private final Type componentType; protected final Type componentType;
private final Encodeable anyEncoder; protected final Encodeable anyEncoder;
private final Encodeable<Writer, Object> encoder; protected final Encodeable<Writer, Object> encoder;
private boolean inited = false; protected boolean inited = false;
private final Object lock = new Object(); protected final Object lock = new Object();
public ArrayEncoder(final ConvertFactory factory, final Type type) { public ArrayEncoder(final ConvertFactory factory, final Type type) {
this.type = type; this.type = type;

View File

@@ -21,19 +21,19 @@ import java.util.Collection;
* @param <T> 反解析的集合元素类型 * @param <T> 反解析的集合元素类型
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public final class CollectionDecoder<T> implements Decodeable<Reader, Collection<T>> { public class CollectionDecoder<T> implements Decodeable<Reader, Collection<T>> {
private final Type type; protected final Type type;
private final Type componentType; protected final Type componentType;
protected Creator<Collection<T>> creator; protected Creator<Collection<T>> creator;
protected final Decodeable<Reader, T> decoder; protected final Decodeable<Reader, T> decoder;
private boolean inited = false; protected boolean inited = false;
private final Object lock = new Object(); protected final Object lock = new Object();
public CollectionDecoder(final ConvertFactory factory, final Type type) { public CollectionDecoder(final ConvertFactory factory, final Type type) {
this.type = type; this.type = type;

View File

@@ -19,15 +19,15 @@ import java.util.Collection;
* @param <T> 序列化的集合元素类型 * @param <T> 序列化的集合元素类型
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public final class CollectionEncoder<T> implements Encodeable<Writer, Collection<T>> { public class CollectionEncoder<T> implements Encodeable<Writer, Collection<T>> {
private final Type type; protected final Type type;
private final Encodeable<Writer, Object> encoder; protected final Encodeable<Writer, Object> encoder;
private boolean inited = false; protected boolean inited = false;
private final Object lock = new Object(); protected final Object lock = new Object();
public CollectionEncoder(final ConvertFactory factory, final Type type) { public CollectionEncoder(final ConvertFactory factory, final Type type) {
this.type = type; this.type = type;

View File

@@ -141,12 +141,54 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
public abstract boolean isFieldSort(); //当ConvertColumn.index相同时是否按字段名称排序 public abstract boolean isFieldSort(); //当ConvertColumn.index相同时是否按字段名称排序
public abstract SimpledCoder createEnumSimpledCoder(Class enumClass);
public abstract ConvertFactory createChild(); public abstract ConvertFactory createChild();
public abstract ConvertFactory createChild(boolean tiny); public abstract ConvertFactory createChild(boolean tiny);
protected SimpledCoder createEnumSimpledCoder(Class enumClass) {
return new EnumSimpledCoder(enumClass);
}
protected ObjectDecoder createObjectDecoder(Type type) {
return new ObjectDecoder(type);
}
protected ObjectEncoder createObjectEncoder(Type type) {
return new ObjectEncoder(type);
}
protected <E> Decodeable<R, E> createMapDecoder(Type type) {
return new MapDecoder(this, type);
}
protected <E> Encodeable<W, E> createMapEncoder(Type type) {
return new MapEncoder(this, type);
}
protected <E> Decodeable<R, E> createArrayDecoder(Type type) {
return new ArrayDecoder(this, type);
}
protected <E> Encodeable<W, E> createArrayEncoder(Type type) {
return new ArrayEncoder(this, type);
}
protected <E> Decodeable<R, E> createCollectionDecoder(Type type) {
return new CollectionDecoder(this, type);
}
protected <E> Encodeable<W, E> createCollectionEncoder(Type type) {
return new CollectionEncoder(this, type);
}
protected <E> Decodeable<R, E> createStreamDecoder(Type type) {
return new StreamDecoder(this, type);
}
protected <E> Encodeable<W, E> createStreamEncoder(Type type) {
return new StreamEncoder(this, type);
}
public Convert getConvert() { public Convert getConvert() {
return convert; return convert;
} }
@@ -468,7 +510,7 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
public final <E> Decodeable<R, E> loadDecoder(final Type type) { public final <E> Decodeable<R, E> loadDecoder(final Type type) {
Decodeable<R, E> decoder = findDecoder(type); Decodeable<R, E> decoder = findDecoder(type);
if (decoder != null) return decoder; if (decoder != null) return decoder;
if (type instanceof GenericArrayType) return new ArrayDecoder(this, type); if (type instanceof GenericArrayType) return createArrayDecoder(type);
Class clazz; Class clazz;
if (type instanceof ParameterizedType) { if (type instanceof ParameterizedType) {
final ParameterizedType pts = (ParameterizedType) type; final ParameterizedType pts = (ParameterizedType) type;
@@ -523,17 +565,17 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
if (clazz.isEnum()) { if (clazz.isEnum()) {
decoder = createEnumSimpledCoder(clazz); decoder = createEnumSimpledCoder(clazz);
} else if (clazz.isArray()) { } else if (clazz.isArray()) {
decoder = new ArrayDecoder(this, type); decoder = createArrayDecoder(type);
} else if (Collection.class.isAssignableFrom(clazz)) { } else if (Collection.class.isAssignableFrom(clazz)) {
decoder = new CollectionDecoder(this, type); decoder = createCollectionDecoder(type);
} else if (Stream.class.isAssignableFrom(clazz)) { } else if (Stream.class.isAssignableFrom(clazz)) {
decoder = new StreamDecoder(this, type); decoder = createStreamDecoder(type);
} else if (Map.class.isAssignableFrom(clazz)) { } else if (Map.class.isAssignableFrom(clazz)) {
decoder = new MapDecoder(this, type); decoder = createMapDecoder(type);
} else if (Optional.class == clazz) { } else if (Optional.class == clazz) {
decoder = new OptionalCoder(this, type); decoder = new OptionalCoder(this, type);
} else if (clazz == Object.class) { } else if (clazz == Object.class) {
od = new ObjectDecoder(type); od = createObjectDecoder(type);
decoder = od; decoder = od;
} else if (!clazz.getName().startsWith("java.") } else if (!clazz.getName().startsWith("java.")
|| java.net.HttpCookie.class == clazz || java.net.HttpCookie.class == clazz
@@ -553,7 +595,7 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
} }
} }
if (simpleCoder == null) { if (simpleCoder == null) {
od = new ObjectDecoder(type); od = createObjectDecoder(type);
decoder = od; decoder = od;
} else { } else {
decoder = simpleCoder; decoder = simpleCoder;
@@ -568,7 +610,7 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
public final <E> Encodeable<W, E> loadEncoder(final Type type) { public final <E> Encodeable<W, E> loadEncoder(final Type type) {
Encodeable<W, E> encoder = findEncoder(type); Encodeable<W, E> encoder = findEncoder(type);
if (encoder != null) return encoder; if (encoder != null) return encoder;
if (type instanceof GenericArrayType) return new ArrayEncoder(this, type); if (type instanceof GenericArrayType) return createArrayEncoder(type);
Class clazz; Class clazz;
if (type instanceof ParameterizedType) { if (type instanceof ParameterizedType) {
final ParameterizedType pts = (ParameterizedType) type; final ParameterizedType pts = (ParameterizedType) type;
@@ -609,13 +651,13 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
if (clazz.isEnum()) { if (clazz.isEnum()) {
encoder = createEnumSimpledCoder(clazz); encoder = createEnumSimpledCoder(clazz);
} else if (clazz.isArray()) { } else if (clazz.isArray()) {
encoder = new ArrayEncoder(this, type); encoder = createArrayEncoder(type);
} else if (Collection.class.isAssignableFrom(clazz)) { } else if (Collection.class.isAssignableFrom(clazz)) {
encoder = new CollectionEncoder(this, type); encoder = createCollectionEncoder(type);
} else if (Stream.class.isAssignableFrom(clazz)) { } else if (Stream.class.isAssignableFrom(clazz)) {
encoder = new StreamEncoder(this, type); encoder = createStreamEncoder(type);
} else if (Map.class.isAssignableFrom(clazz)) { } else if (Map.class.isAssignableFrom(clazz)) {
encoder = new MapEncoder(this, type); encoder = createMapEncoder(type);
} else if (Optional.class == clazz) { } else if (Optional.class == clazz) {
encoder = new OptionalCoder(this, type); encoder = new OptionalCoder(this, type);
} else if (clazz == Object.class) { } else if (clazz == Object.class) {
@@ -636,7 +678,7 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
} }
} }
if (simpleCoder == null) { if (simpleCoder == null) {
oe = new ObjectEncoder(type); oe = createObjectEncoder(type);
encoder = oe; encoder = oe;
} else { } else {
encoder = simpleCoder; encoder = simpleCoder;

View File

@@ -21,13 +21,13 @@ import java.util.Map;
* @param <V> Map value的数据类型 * @param <V> Map value的数据类型
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public final class MapDecoder<K, V> implements Decodeable<Reader, Map<K, V>> { public class MapDecoder<K, V> implements Decodeable<Reader, Map<K, V>> {
private final Type type; protected final Type type;
private final Type keyType; protected final Type keyType;
private final Type valueType; protected final Type valueType;
protected Creator<Map<K, V>> creator; protected Creator<Map<K, V>> creator;
@@ -35,9 +35,9 @@ public final class MapDecoder<K, V> implements Decodeable<Reader, Map<K, V>> {
protected final Decodeable<Reader, V> valueDecoder; protected final Decodeable<Reader, V> valueDecoder;
private boolean inited = false; protected boolean inited = false;
private final Object lock = new Object(); protected final Object lock = new Object();
public MapDecoder(final ConvertFactory factory, final Type type) { public MapDecoder(final ConvertFactory factory, final Type type) {
this.type = type; this.type = type;

View File

@@ -20,17 +20,17 @@ import java.util.Map;
* @param <V> Map value的数据类型 * @param <V> Map value的数据类型
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public final class MapEncoder<K, V> implements Encodeable<Writer, Map<K, V>> { public class MapEncoder<K, V> implements Encodeable<Writer, Map<K, V>> {
private final Type type; protected final Type type;
private final Encodeable<Writer, K> keyencoder; protected final Encodeable<Writer, K> keyencoder;
private final Encodeable<Writer, V> valencoder; protected final Encodeable<Writer, V> valencoder;
private boolean inited = false; protected boolean inited = false;
private final Object lock = new Object(); protected final Object lock = new Object();
public MapEncoder(final ConvertFactory factory, final Type type) { public MapEncoder(final ConvertFactory factory, final Type type) {
this.type = type; this.type = type;

View File

@@ -21,7 +21,7 @@ import org.redkale.util.*;
* @param <T> 反解析的数据类型 * @param <T> 反解析的数据类型
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public final class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T> { public class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T> {
protected final Type type; protected final Type type;
@@ -35,9 +35,9 @@ public final class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T
protected ConvertFactory factory; protected ConvertFactory factory;
private boolean inited = false; protected boolean inited = false;
private final Object lock = new Object(); protected final Object lock = new Object();
protected ObjectDecoder(Type type) { protected ObjectDecoder(Type type) {
this.type = ((type instanceof Class) && ((Class) type).isInterface()) ? Object.class : type; this.type = ((type instanceof Class) && ((Class) type).isInterface()) ? Object.class : type;
@@ -191,7 +191,7 @@ public final class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T
* @return 反解析后的对象结果 * @return 反解析后的对象结果
*/ */
@Override @Override
public final T convertFrom(final R in) { public T convertFrom(final R in) {
final String clazz = in.readObjectB(typeClass); final String clazz = in.readObjectB(typeClass);
if (clazz == null) return null; if (clazz == null) return null;
if (!clazz.isEmpty()) return (T) factory.loadDecoder(factory.getEntityAlias(clazz)).convertFrom(in); if (!clazz.isEmpty()) return (T) factory.loadDecoder(factory.getEntityAlias(clazz)).convertFrom(in);
@@ -255,7 +255,7 @@ public final class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T
} }
@Override @Override
public final Type getType() { public Type getType() {
return this.type; return this.type;
} }

View File

@@ -20,7 +20,7 @@ import org.redkale.util.*;
* @param <T> 序列化的数据类型 * @param <T> 序列化的数据类型
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public final class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T> { public class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T> {
static final Type[] TYPEZERO = new Type[0]; static final Type[] TYPEZERO = new Type[0];
@@ -32,9 +32,9 @@ public final class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T
protected ConvertFactory factory; protected ConvertFactory factory;
private boolean inited = false; protected boolean inited = false;
private final Object lock = new Object(); protected final Object lock = new Object();
protected ObjectEncoder(Type type) { protected ObjectEncoder(Type type) {
this.type = type; this.type = type;
@@ -125,7 +125,7 @@ public final class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T
} }
@Override @Override
public final void convertTo(W out, T value) { public void convertTo(W out, T value) {
if (value == null) { if (value == null) {
out.writeObjectNull(null); out.writeObjectNull(null);
return; return;
@@ -154,7 +154,7 @@ public final class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T
} }
@Override @Override
public final Type getType() { public Type getType() {
return this.type; return this.type;
} }

View File

@@ -22,7 +22,7 @@ import java.lang.reflect.Type;
*/ */
public abstract class SimpledCoder<R extends Reader, W extends Writer, T> implements Decodeable<R, T>, Encodeable<W, T> { public abstract class SimpledCoder<R extends Reader, W extends Writer, T> implements Decodeable<R, T>, Encodeable<W, T> {
private Type type; protected Type type;
@Override @Override
public abstract void convertTo(final W out, final T value); public abstract void convertTo(final W out, final T value);

View File

@@ -22,19 +22,19 @@ import java.util.stream.Stream;
* @param <T> 反解析的集合元素类型 * @param <T> 反解析的集合元素类型
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public final class StreamDecoder<T> implements Decodeable<Reader, Stream<T>> { public class StreamDecoder<T> implements Decodeable<Reader, Stream<T>> {
private final Type type; protected final Type type;
private final Type componentType; protected final Type componentType;
protected Creator<Stream<T>> creator; protected Creator<Stream<T>> creator;
protected final Decodeable<Reader, T> decoder; protected final Decodeable<Reader, T> decoder;
private boolean inited = false; protected boolean inited = false;
private final Object lock = new Object(); protected final Object lock = new Object();
public StreamDecoder(final ConvertFactory factory, final Type type) { public StreamDecoder(final ConvertFactory factory, final Type type) {
this.type = type; this.type = type;

View File

@@ -19,15 +19,15 @@ import java.util.stream.Stream;
* @param <T> 序列化的集合元素类型 * @param <T> 序列化的集合元素类型
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public final class StreamEncoder<T> implements Encodeable<Writer, Stream<T>> { public class StreamEncoder<T> implements Encodeable<Writer, Stream<T>> {
private final Type type; protected final Type type;
private final Encodeable<Writer, Object> encoder; protected final Encodeable<Writer, Object> encoder;
private boolean inited = false; protected boolean inited = false;
private final Object lock = new Object(); protected final Object lock = new Object();
public StreamEncoder(final ConvertFactory factory, final Type type) { public StreamEncoder(final ConvertFactory factory, final Type type) {
this.type = type; this.type = type;

View File

@@ -7,7 +7,6 @@ package org.redkale.convert.bson;
import java.io.Serializable; import java.io.Serializable;
import org.redkale.convert.*; import org.redkale.convert.*;
import org.redkale.convert.ext.EnumSimpledCoder;
import org.redkale.util.AnyValue; import org.redkale.util.AnyValue;
/** /**
@@ -51,11 +50,6 @@ public final class BsonFactory extends ConvertFactory<BsonReader, BsonWriter> {
return this; return this;
} }
@Override
public SimpledCoder createEnumSimpledCoder(Class enumClass) {
return new EnumSimpledCoder(enumClass);
}
public static BsonFactory root() { public static BsonFactory root() {
return instance; return instance;
} }

View File

@@ -52,11 +52,6 @@ public final class JsonFactory extends ConvertFactory<JsonReader, JsonWriter> {
return this; return this;
} }
@Override
public SimpledCoder createEnumSimpledCoder(Class enumClass) {
return new EnumSimpledCoder(enumClass);
}
public static JsonFactory root() { public static JsonFactory root() {
return instance; return instance;
} }