java: disabled ObjectArrayTempalte - it doesn't work

This commit is contained in:
FURUHASHI Sadayuki 2010-12-06 00:13:05 +09:00
parent 05d9d22d9e
commit 2daa08b0e7
8 changed files with 90 additions and 62 deletions

View File

@ -372,8 +372,7 @@ public class Unpacker implements Iterable<MessagePackObject> {
* @return offset position that is parsed. * @return offset position that is parsed.
*/ */
public int execute(byte[] buffer, int offset, int length) throws UnpackException { public int execute(byte[] buffer, int offset, int length) throws UnpackException {
int noffset = impl.execute(buffer, offset + impl.offset, length); int noffset = impl.execute(buffer, offset, length);
impl.offset = noffset - offset;
if(impl.isFinished()) { if(impl.isFinished()) {
impl.resetState(); impl.resetState();
} }

View File

@ -331,7 +331,7 @@ public class VectoredByteBuffer implements GatheringByteChannel, ScatteringByteC
} }
public synchronized long read(GatheringByteChannel to) throws IOException { public synchronized long read(GatheringByteChannel to) throws IOException {
long total = to.write((ByteBuffer[])vec.toArray()); long total = to.write(vec.toArray(new ByteBuffer[0]));
while(!vec.isEmpty()) { while(!vec.isEmpty()) {
ByteBuffer r = vec.get(0); ByteBuffer r = vec.get(0);
if(r.remaining() == 0) { if(r.remaining() == 0) {

View File

@ -24,7 +24,7 @@ public class FieldList {
public static class Entry { public static class Entry {
public Entry() { public Entry() {
this.name = null; this.name = null;
this.option = null; this.option = FieldOption.IGNORE;
} }
public Entry(String name, FieldOption option) { public Entry(String name, FieldOption option) {
@ -44,7 +44,7 @@ public class FieldList {
} }
boolean isAvailable() { boolean isAvailable() {
return this.name != null; return this.option != FieldOption.IGNORE;
} }
boolean isRequired() { boolean isRequired() {

View File

@ -521,11 +521,24 @@ public class JavassistTemplateBuilder extends TemplateBuilder {
} }
public Template buildTemplate(Class<?> targetClass, FieldEntry[] entries) { public Template buildTemplate(Class<?> targetClass, FieldEntry[] entries) {
// FIXME private / packagefields
//for(FieldEntry e : entries) {
// Field f = e.getField();
// int mod = f.getModifiers();
// if(!Modifier.isPublic(mod)) {
// f.setAccessible(true);
// }
//}
Template[] tmpls = new Template[entries.length]; Template[] tmpls = new Template[entries.length];
for(int i=0; i < entries.length; i++) { for(int i=0; i < entries.length; i++) {
FieldEntry e = entries[i]; FieldEntry e = entries[i];
Template tmpl = TemplateRegistry.lookup(e.getGenericType(), true); if(!e.isAvailable()) {
tmpls[i] = tmpl; tmpls[i] = null;
} else {
Template tmpl = TemplateRegistry.lookup(e.getGenericType(), true);
tmpls[i] = tmpl;
}
} }
BuildContext bc = new BuildContext(this); BuildContext bc = new BuildContext(this);

View File

@ -22,6 +22,7 @@ import java.util.ArrayList;
import java.io.IOException; import java.io.IOException;
import org.msgpack.*; import org.msgpack.*;
// FIXME
public class ObjectArrayTemplate implements Template { public class ObjectArrayTemplate implements Template {
static void load() { } static void load() { }
@ -40,7 +41,7 @@ public class ObjectArrayTemplate implements Template {
if(!(target instanceof Object[])) { if(!(target instanceof Object[])) {
throw new MessageTypeException(); throw new MessageTypeException();
} }
Object[] array = (Object[])target; Object[] array = (Object[])target; // FIXME
pk.packArray(array.length); pk.packArray(array.length);
for(Object a : array) { for(Object a : array) {
componentTemplate.pack(pk, a); componentTemplate.pack(pk, a);
@ -49,7 +50,7 @@ public class ObjectArrayTemplate implements Template {
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
int length = pac.unpackArray(); int length = pac.unpackArray();
Object[] array; Object[] array; // FIXME
if(to != null && to instanceof Object[] && ((Object[])to).length == length) { if(to != null && to instanceof Object[] && ((Object[])to).length == length) {
array = (Object[])to; array = (Object[])to;
} else { } else {
@ -63,7 +64,7 @@ public class ObjectArrayTemplate implements Template {
public Object convert(MessagePackObject from, Object to) throws MessageTypeException { public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
MessagePackObject[] src = from.asArray(); MessagePackObject[] src = from.asArray();
Object[] array; Object[] array; // FIXME
if(to != null && to instanceof Object[] && ((Object[])to).length == src.length) { if(to != null && to instanceof Object[] && ((Object[])to).length == src.length) {
array = (Object[])to; array = (Object[])to;
} else { } else {

View File

@ -36,7 +36,7 @@ public class ReflectionTemplateBuilder extends TemplateBuilder {
} }
static abstract class ReflectionFieldEntry extends FieldEntry { static abstract class ReflectionFieldEntry extends FieldEntry {
public ReflectionFieldEntry(FieldEntry e) { ReflectionFieldEntry(FieldEntry e) {
super(e.getField(), e.getOption()); super(e.getField(), e.getOption());
} }
@ -51,6 +51,15 @@ public class ReflectionTemplateBuilder extends TemplateBuilder {
} }
} }
static class NullFieldEntry extends ReflectionFieldEntry {
NullFieldEntry(FieldEntry e) {
super(e);
}
public void pack(Object target, Packer pac) throws IOException { }
public void convert(Object target, MessagePackObject obj) throws MessageTypeException, IllegalAccessException { }
public void unpack(Object target, Unpacker pac) throws IOException, MessageTypeException, IllegalAccessException { }
}
static class ObjectFieldEntry extends ReflectionFieldEntry { static class ObjectFieldEntry extends ReflectionFieldEntry {
private Template template; private Template template;
@ -377,7 +386,9 @@ public class ReflectionTemplateBuilder extends TemplateBuilder {
for(int i=0; i < entries.length; i++) { for(int i=0; i < entries.length; i++) {
FieldEntry e = entries[i]; FieldEntry e = entries[i];
Class<?> type = e.getType(); Class<?> type = e.getType();
if(type.equals(boolean.class)) { if(!e.isAvailable()) {
res[i] = new NullFieldEntry(e);
} else if(type.equals(boolean.class)) {
res[i] = new BooleanFieldEntry(e); res[i] = new BooleanFieldEntry(e);
} else if(type.equals(byte.class)) { } else if(type.equals(byte.class)) {
res[i] = new ByteFieldEntry(e); res[i] = new ByteFieldEntry(e);

View File

@ -28,10 +28,17 @@ import org.msgpack.annotation.*;
public abstract class TemplateBuilder { public abstract class TemplateBuilder {
public static class FieldEntry { public static class FieldEntry {
private Field field = null; private Field field;
private FieldOption option = FieldOption.IGNORE; private FieldOption option;
public FieldEntry() { public FieldEntry() {
this.field = null;
this.option = FieldOption.IGNORE;
}
public FieldEntry(FieldEntry e) {
this.field = e.field;
this.option = e.option;
} }
public FieldEntry(Field field, FieldOption option) { public FieldEntry(Field field, FieldOption option) {
@ -84,10 +91,6 @@ public abstract class TemplateBuilder {
return option == FieldOption.NULLABLE; return option == FieldOption.NULLABLE;
} }
public boolean isAnnotated(Class<? extends Annotation> with) {
return field.getAnnotation(with) != null;
}
static String arrayTypeToString(Class<?> type) { static String arrayTypeToString(Class<?> type) {
int dim = 1; int dim = 1;
Class<?> baseType = type.getComponentType(); Class<?> baseType = type.getComponentType();
@ -111,19 +114,19 @@ public abstract class TemplateBuilder {
public abstract Template buildOrdinalEnumTemplate(Class<?> targetClass, Enum<?>[] entries); public abstract Template buildOrdinalEnumTemplate(Class<?> targetClass, Enum<?>[] entries);
public Template buildTemplate(Class<?> targetClass) { public Template buildTemplate(Class<?> targetClass, FieldList flist) throws NoSuchFieldException {
checkTypeValidation(targetClass); checkValidation(targetClass);
return buildTemplate(targetClass, readFieldEntries(targetClass)); return buildTemplate(targetClass, convertFieldEntries(targetClass, flist));
} }
public Template buildTemplate(Class<?> targetClass, FieldOption implicitOption) { public Template buildTemplate(Class<?> targetClass, FieldOption implicitOption) {
checkTypeValidation(targetClass); checkValidation(targetClass);
return buildTemplate(targetClass, readFieldEntries(targetClass, implicitOption)); return buildTemplate(targetClass, readFieldEntries(targetClass, implicitOption));
} }
public Template buildTemplate(Class<?> targetClass, FieldList flist) throws NoSuchFieldException { public Template buildTemplate(Class<?> targetClass) {
checkTypeValidation(targetClass); FieldOption implicitOption = readImplicitFieldOption(targetClass);
return buildTemplate(targetClass, convertFieldEntries(targetClass, flist)); return buildTemplate(targetClass, implicitOption);
} }
public Template buildOrdinalEnumTemplate(Class<?> targetClass) { public Template buildOrdinalEnumTemplate(Class<?> targetClass) {
@ -170,7 +173,7 @@ public abstract class TemplateBuilder {
} }
protected void checkTypeValidation(Class<?> targetClass) { private static void checkValidation(Class<?> targetClass) {
if(targetClass.isInterface()) { if(targetClass.isInterface()) {
throw new TemplateBuildException("cannot build template of interface"); throw new TemplateBuildException("cannot build template of interface");
} }
@ -182,14 +185,14 @@ public abstract class TemplateBuilder {
} }
} }
protected void checkOrdinalEnumValidation(Class<?> targetClass) { private static void checkOrdinalEnumValidation(Class<?> targetClass) {
if(!targetClass.isEnum()) { if(!targetClass.isEnum()) {
throw new TemplateBuildException("tried to build ordinal enum template of non-enum class"); throw new TemplateBuildException("tried to build ordinal enum template of non-enum class");
} }
} }
protected FieldEntry[] convertFieldEntries(Class<?> targetClass, FieldList flist) throws NoSuchFieldException { static FieldEntry[] convertFieldEntries(Class<?> targetClass, FieldList flist) throws NoSuchFieldException {
List<FieldList.Entry> src = flist.getList(); List<FieldList.Entry> src = flist.getList();
FieldEntry[] result = new FieldEntry[src.size()]; FieldEntry[] result = new FieldEntry[src.size()];
for(int i=0; i < src.size(); i++) { for(int i=0; i < src.size(); i++) {
@ -203,12 +206,7 @@ public abstract class TemplateBuilder {
return result; return result;
} }
protected FieldEntry[] readFieldEntries(Class<?> targetClass) { static FieldEntry[] readFieldEntries(Class<?> targetClass, FieldOption implicitOption) {
FieldOption implicitOption = readImplicitFieldOption(targetClass);
return readFieldEntries(targetClass, implicitOption);
}
protected FieldEntry[] readFieldEntries(Class<?> targetClass, FieldOption implicitOption) {
Field[] allFields = readAllFields(targetClass); Field[] allFields = readAllFields(targetClass);
/* index: /* index:
@ -253,14 +251,14 @@ public abstract class TemplateBuilder {
if(e == null) { if(e == null) {
result[i] = new FieldEntry(); result[i] = new FieldEntry();
} else { } else {
result[i] = new FieldEntry(e.getField(), e.getOption()); result[i] = e;
} }
} }
return result; return result;
} }
private Field[] readAllFields(Class<?> targetClass) { private static Field[] readAllFields(Class<?> targetClass) {
// order: [fields of super class, ..., fields of this class] // order: [fields of super class, ..., fields of this class]
List<Field[]> succ = new ArrayList<Field[]>(); List<Field[]> succ = new ArrayList<Field[]>();
int total = 0; int total = 0;
@ -279,7 +277,7 @@ public abstract class TemplateBuilder {
return result; return result;
} }
private FieldOption readImplicitFieldOption(Class<?> targetClass) { private static FieldOption readImplicitFieldOption(Class<?> targetClass) {
MessagePackMessage a = targetClass.getAnnotation(MessagePackMessage.class); MessagePackMessage a = targetClass.getAnnotation(MessagePackMessage.class);
if(a == null) { if(a == null) {
return FieldOption.DEFAULT; return FieldOption.DEFAULT;
@ -287,7 +285,7 @@ public abstract class TemplateBuilder {
return a.value(); return a.value();
} }
private FieldOption readFieldOption(Field field, FieldOption implicitOption) { private static FieldOption readFieldOption(Field field, FieldOption implicitOption) {
int mod = field.getModifiers(); int mod = field.getModifiers();
if(Modifier.isStatic(mod) || Modifier.isFinal(mod)) { if(Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
return FieldOption.IGNORE; return FieldOption.IGNORE;
@ -324,7 +322,7 @@ public abstract class TemplateBuilder {
} }
} }
private int readFieldIndex(Field field, int maxIndex) { private static int readFieldIndex(Field field, int maxIndex) {
Index a = field.getAnnotation(Index.class); Index a = field.getAnnotation(Index.class);
if(a == null) { if(a == null) {
return maxIndex + 1; return maxIndex + 1;
@ -333,7 +331,7 @@ public abstract class TemplateBuilder {
} }
} }
protected boolean isAnnotated(AccessibleObject ao, Class<? extends Annotation> with) { private static boolean isAnnotated(AccessibleObject ao, Class<? extends Annotation> with) {
return ao.getAnnotation(with) != null; return ao.getAnnotation(with) != null;
} }
} }

View File

@ -90,8 +90,8 @@ public class TemplateRegistry {
Class<?> target; Class<?> target;
// TODO // TODO
//if((Type)target instanceof GenericArrayType) { //if(targetType instanceof GenericArrayType) {
// return lookupArrayImpl((GenericArrayType)(Type)target); // return lookupGenericArray((GenericArrayType)targetType);
//} //}
if(targetType instanceof ParameterizedType) { if(targetType instanceof ParameterizedType) {
@ -109,6 +109,16 @@ public class TemplateRegistry {
return tmpl; return tmpl;
} }
// TODO
//if(target.isArray()) {
// // FIXME can't distinguish type-erased Object[T<>]?
// Type componentType = target.getComponentType();
// Template componentTemplate = lookup(componentType);
// tmpl = new ObjectArrayTemplate(componentTemplate);
// register(target, tmpl);
// return tmpl;
//}
if(isAnnotated(target, MessagePackMessage.class)) { if(isAnnotated(target, MessagePackMessage.class)) {
tmpl = TemplateBuilder.build(target); tmpl = TemplateBuilder.build(target);
register(target, tmpl); register(target, tmpl);
@ -156,6 +166,22 @@ public class TemplateRegistry {
} }
} }
private static synchronized Template lookupGenericArray(GenericArrayType arrayType) {
Template tmpl = map.get(arrayType);
if(tmpl != null) {
// TODO primitive types are included?
return tmpl;
}
Type componentType = arrayType.getGenericComponentType();
Template componentTemplate = lookup(componentType);
tmpl = new ObjectArrayTemplate(componentTemplate);
register(arrayType, tmpl);
return tmpl;
}
public static synchronized Template lookupGeneric(Type targetType) { public static synchronized Template lookupGeneric(Type targetType) {
if(targetType instanceof ParameterizedType) { if(targetType instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType)targetType; ParameterizedType parameterizedType = (ParameterizedType)targetType;
@ -185,26 +211,6 @@ public class TemplateRegistry {
return gtmpl.build(tmpls); return gtmpl.build(tmpls);
} }
public static synchronized Template lookupArray(Type targetType) {
if(targetType instanceof GenericArrayType) {
GenericArrayType arrayType = (GenericArrayType)targetType;
return lookupArrayImpl(arrayType);
} else {
throw new IllegalArgumentException("actual type of the array type is erased: "+targetType);
}
}
private static synchronized Template lookupArrayImpl(GenericArrayType arrayType) {
Template tmpl = map.get(arrayType);
if(tmpl != null) {
// TODO primitive types are included?
return tmpl;
}
Type component = arrayType.getGenericComponentType();
Template componentTemplate = lookup(component);
return new ObjectArrayTemplate(componentTemplate);
}
private static boolean isAnnotated(Class<?> ao, Class<? extends Annotation> with) { private static boolean isAnnotated(Class<?> ao, Class<? extends Annotation> with) {
return ao.getAnnotation(with) != null; return ao.getAnnotation(with) != null;
} }