This commit is contained in:
wentch
2016-01-06 15:57:20 +08:00
parent b4fadaf412
commit 9d7678200a
5 changed files with 40 additions and 10 deletions

View File

@@ -5,6 +5,7 @@
*/
package org.redkale.convert;
import java.lang.reflect.*;
import org.redkale.util.Attribute;
/**
@@ -33,6 +34,19 @@ public final class DeMember<R extends Reader, T, F> implements Comparable<DeMemb
this.decoder = decoder;
}
public static <R extends Reader, T, F> DeMember<R, T, F> create(final Factory factory, final Class<T> clazz, final String fieldname) {
try {
Field field = clazz.getDeclaredField(fieldname);
return new DeMember<>(Attribute.create(field), factory.loadDecoder(field.getGenericType()));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public final boolean match(String name) {
return attribute.field().equals(name);
}
public final void read(R in, T obj) {
this.attribute.set(obj, decoder.convertFrom(in));
}

View File

@@ -5,6 +5,7 @@
*/
package org.redkale.convert;
import java.lang.reflect.*;
import org.redkale.util.Attribute;
/**
@@ -38,6 +39,19 @@ public final class EnMember<W extends Writer, T, F> implements Comparable<EnMemb
//this.isnumber = Number.class.isAssignableFrom(t) || (!this.isbool && t.isPrimitive());
}
public static <W extends Writer, T, F> EnMember<W, T, F> create(final Factory factory, final Class<T> clazz, final String fieldname) {
try {
Field field = clazz.getDeclaredField(fieldname);
return new EnMember<>(Attribute.create(field), factory.loadEncoder(field.getGenericType()));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public final boolean match(String name) {
return attribute.field().equals(name);
}
public boolean write(final W out, final boolean comma, final T obj) {
F value = attribute.get(obj);
if (value == null) return comma;

View File

@@ -349,14 +349,15 @@ public abstract class Factory<R extends Reader, W extends Writer> {
od = new ObjectDecoder(type);
decoder = od;
} else if (!clazz.getName().startsWith("java.")) {
SimpledCoder simpleCoder = null;
Decodeable simpleCoder = null;
for (final Method method : clazz.getDeclaredMethods()) {
if (!Modifier.isStatic(method.getModifiers())) continue;
if (method.getParameterTypes().length != 0) continue;
if (!SimpledCoder.class.isAssignableFrom(method.getReturnType())) continue;
Class[] paramTypes = method.getParameterTypes();
if (paramTypes.length != 1 || paramTypes[0] != Factory.class) continue;
if (!Decodeable.class.isAssignableFrom(method.getReturnType())) continue;
try {
method.setAccessible(true);
simpleCoder = (SimpledCoder) method.invoke(null);
simpleCoder = (Decodeable) method.invoke(null, this);
break;
} catch (Exception e) {
}
@@ -427,14 +428,15 @@ public abstract class Factory<R extends Reader, W extends Writer> {
} else if (clazz == Object.class) {
return (Encodeable<W, E>) this.anyEncoder;
} else if (!clazz.getName().startsWith("java.")) {
SimpledCoder simpleCoder = null;
Encodeable simpleCoder = null;
for (final Method method : clazz.getDeclaredMethods()) {
if (!Modifier.isStatic(method.getModifiers())) continue;
if (method.getParameterTypes().length != 0) continue;
if (!SimpledCoder.class.isAssignableFrom(method.getReturnType())) continue;
Class[] paramTypes = method.getParameterTypes();
if (paramTypes.length != 1 || paramTypes[0] != Factory.class) continue;
if (!Encodeable.class.isAssignableFrom(method.getReturnType())) continue;
try {
method.setAccessible(true);
simpleCoder = (SimpledCoder) method.invoke(null);
simpleCoder = (Encodeable) method.invoke(null, this);
break;
} catch (Exception e) {
}

View File

@@ -155,7 +155,7 @@ public class JsonByteBufferReader extends JsonReader {
char ch = nextGoodChar();
if (ch == ',') return true;
if (ch == '}' || ch == ']') return false;
backChar(ch);
backChar(ch); // { [ 交由 readObjectB 或 readMapB 或 readArrayB 读取
return true;
}

View File

@@ -267,7 +267,7 @@ public class JsonReader implements Reader {
if (ch == ',') return true;
if (ch == '}' || ch == ']') return false;
}
this.position--;
this.position--; // { [ 交由 readObjectB 或 readMapB 或 readArrayB 读取
return true;
}