This commit is contained in:
@@ -32,6 +32,10 @@ public final class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T
|
|||||||
|
|
||||||
protected Factory factory;
|
protected Factory factory;
|
||||||
|
|
||||||
|
private boolean inited = false;
|
||||||
|
|
||||||
|
private 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;
|
||||||
if (type instanceof ParameterizedType) {
|
if (type instanceof ParameterizedType) {
|
||||||
@@ -45,56 +49,63 @@ public final class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T
|
|||||||
|
|
||||||
public void init(final Factory factory) {
|
public void init(final Factory factory) {
|
||||||
this.factory = factory;
|
this.factory = factory;
|
||||||
if (type == Object.class) return;
|
|
||||||
|
|
||||||
Class clazz = null;
|
|
||||||
if (type instanceof ParameterizedType) {
|
|
||||||
final ParameterizedType pts = (ParameterizedType) type;
|
|
||||||
clazz = (Class) (pts).getRawType();
|
|
||||||
} else if (!(type instanceof Class)) {
|
|
||||||
throw new ConvertException("[" + type + "] is no a class");
|
|
||||||
} else {
|
|
||||||
clazz = (Class) type;
|
|
||||||
}
|
|
||||||
final Type[] virGenericTypes = this.typeClass.getTypeParameters();
|
|
||||||
final Type[] realGenericTypes = (type instanceof ParameterizedType) ? ((ParameterizedType) type).getActualTypeArguments() : TYPEZERO;
|
|
||||||
this.creator = factory.loadCreator(clazz);
|
|
||||||
final Set<DeMember> list = new HashSet<>();
|
|
||||||
try {
|
try {
|
||||||
ConvertColumnEntry ref;
|
if (type == Object.class) return;
|
||||||
for (final Field field : clazz.getFields()) {
|
|
||||||
if (Modifier.isStatic(field.getModifiers())) continue;
|
Class clazz = null;
|
||||||
ref = factory.findRef(field);
|
if (type instanceof ParameterizedType) {
|
||||||
if (ref != null && ref.ignore()) continue;
|
final ParameterizedType pts = (ParameterizedType) type;
|
||||||
Type t = ObjectEncoder.makeGenericType(field.getGenericType(), virGenericTypes, realGenericTypes);
|
clazz = (Class) (pts).getRawType();
|
||||||
list.add(new DeMember(ObjectEncoder.createAttribute(factory, clazz, field, null, null), factory.loadDecoder(t)));
|
} else if (!(type instanceof Class)) {
|
||||||
|
throw new ConvertException("[" + type + "] is no a class");
|
||||||
|
} else {
|
||||||
|
clazz = (Class) type;
|
||||||
}
|
}
|
||||||
final boolean reversible = factory.isReversible();
|
final Type[] virGenericTypes = this.typeClass.getTypeParameters();
|
||||||
for (final Method method : clazz.getMethods()) {
|
final Type[] realGenericTypes = (type instanceof ParameterizedType) ? ((ParameterizedType) type).getActualTypeArguments() : TYPEZERO;
|
||||||
if (Modifier.isStatic(method.getModifiers())) continue;
|
this.creator = factory.loadCreator(clazz);
|
||||||
if (Modifier.isAbstract(method.getModifiers())) continue;
|
final Set<DeMember> list = new HashSet<>();
|
||||||
if (method.isSynthetic()) continue;
|
try {
|
||||||
if (method.getName().length() < 4) continue;
|
ConvertColumnEntry ref;
|
||||||
if (!method.getName().startsWith("set")) continue;
|
for (final Field field : clazz.getFields()) {
|
||||||
if (method.getParameterTypes().length != 1) continue;
|
if (Modifier.isStatic(field.getModifiers())) continue;
|
||||||
if (method.getReturnType() != void.class) continue;
|
ref = factory.findRef(field);
|
||||||
if (reversible) {
|
if (ref != null && ref.ignore()) continue;
|
||||||
boolean is = method.getParameterTypes()[0] == boolean.class || method.getParameterTypes()[0] == Boolean.class;
|
Type t = ObjectEncoder.makeGenericType(field.getGenericType(), virGenericTypes, realGenericTypes);
|
||||||
try {
|
list.add(new DeMember(ObjectEncoder.createAttribute(factory, clazz, field, null, null), factory.loadDecoder(t)));
|
||||||
clazz.getMethod(method.getName().replaceFirst("set", is ? "is" : "get"));
|
|
||||||
} catch (Exception e) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ref = factory.findRef(method);
|
final boolean reversible = factory.isReversible();
|
||||||
if (ref != null && ref.ignore()) continue;
|
for (final Method method : clazz.getMethods()) {
|
||||||
Type t = ObjectEncoder.makeGenericType(method.getGenericParameterTypes()[0], virGenericTypes, realGenericTypes);
|
if (Modifier.isStatic(method.getModifiers())) continue;
|
||||||
list.add(new DeMember(ObjectEncoder.createAttribute(factory, clazz, null, null, method), factory.loadDecoder(t)));
|
if (Modifier.isAbstract(method.getModifiers())) continue;
|
||||||
|
if (method.isSynthetic()) continue;
|
||||||
|
if (method.getName().length() < 4) continue;
|
||||||
|
if (!method.getName().startsWith("set")) continue;
|
||||||
|
if (method.getParameterTypes().length != 1) continue;
|
||||||
|
if (method.getReturnType() != void.class) continue;
|
||||||
|
if (reversible) {
|
||||||
|
boolean is = method.getParameterTypes()[0] == boolean.class || method.getParameterTypes()[0] == Boolean.class;
|
||||||
|
try {
|
||||||
|
clazz.getMethod(method.getName().replaceFirst("set", is ? "is" : "get"));
|
||||||
|
} catch (Exception e) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ref = factory.findRef(method);
|
||||||
|
if (ref != null && ref.ignore()) continue;
|
||||||
|
Type t = ObjectEncoder.makeGenericType(method.getGenericParameterTypes()[0], virGenericTypes, realGenericTypes);
|
||||||
|
list.add(new DeMember(ObjectEncoder.createAttribute(factory, clazz, null, null, method), factory.loadDecoder(t)));
|
||||||
|
}
|
||||||
|
this.members = list.toArray(new DeMember[list.size()]);
|
||||||
|
Arrays.sort(this.members);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
throw new ConvertException(ex);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
inited = true;
|
||||||
|
synchronized (lock) {
|
||||||
|
lock.notifyAll();
|
||||||
}
|
}
|
||||||
this.members = list.toArray(new DeMember[list.size()]);
|
|
||||||
Arrays.sort(this.members);
|
|
||||||
} catch (Exception ex) {
|
|
||||||
throw new ConvertException(ex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,6 +120,16 @@ public final class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T
|
|||||||
final String clazz = in.readClassName();
|
final String clazz = in.readClassName();
|
||||||
if (clazz != null && !clazz.isEmpty()) return (T) factory.loadDecoder(factory.getEntity(clazz)).convertFrom(in);
|
if (clazz != null && !clazz.isEmpty()) return (T) factory.loadDecoder(factory.getEntity(clazz)).convertFrom(in);
|
||||||
if (in.readObjectB() == Reader.SIGN_NULL) return null;
|
if (in.readObjectB() == Reader.SIGN_NULL) return null;
|
||||||
|
if (!this.inited) {
|
||||||
|
synchronized (lock) {
|
||||||
|
try {
|
||||||
|
lock.wait();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
final T result = this.creator.create();
|
final T result = this.creator.create();
|
||||||
final AtomicInteger index = new AtomicInteger();
|
final AtomicInteger index = new AtomicInteger();
|
||||||
while (in.hasNext()) {
|
while (in.hasNext()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user