diff --git a/java/src/main/java/org/msgpack/Unpacker.java b/java/src/main/java/org/msgpack/Unpacker.java index 7b3c16c9..01e3ea1b 100644 --- a/java/src/main/java/org/msgpack/Unpacker.java +++ b/java/src/main/java/org/msgpack/Unpacker.java @@ -372,8 +372,7 @@ public class Unpacker implements Iterable { * @return offset position that is parsed. */ public int execute(byte[] buffer, int offset, int length) throws UnpackException { - int noffset = impl.execute(buffer, offset + impl.offset, length); - impl.offset = noffset - offset; + int noffset = impl.execute(buffer, offset, length); if(impl.isFinished()) { impl.resetState(); } diff --git a/java/src/main/java/org/msgpack/buffer/VectoredByteBuffer.java b/java/src/main/java/org/msgpack/buffer/VectoredByteBuffer.java index df73bc08..79b9d9eb 100644 --- a/java/src/main/java/org/msgpack/buffer/VectoredByteBuffer.java +++ b/java/src/main/java/org/msgpack/buffer/VectoredByteBuffer.java @@ -331,7 +331,7 @@ public class VectoredByteBuffer implements GatheringByteChannel, ScatteringByteC } 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()) { ByteBuffer r = vec.get(0); if(r.remaining() == 0) { diff --git a/java/src/main/java/org/msgpack/template/FieldList.java b/java/src/main/java/org/msgpack/template/FieldList.java index 8d6f28ff..daf59f5b 100644 --- a/java/src/main/java/org/msgpack/template/FieldList.java +++ b/java/src/main/java/org/msgpack/template/FieldList.java @@ -24,7 +24,7 @@ public class FieldList { public static class Entry { public Entry() { this.name = null; - this.option = null; + this.option = FieldOption.IGNORE; } public Entry(String name, FieldOption option) { @@ -44,7 +44,7 @@ public class FieldList { } boolean isAvailable() { - return this.name != null; + return this.option != FieldOption.IGNORE; } boolean isRequired() { diff --git a/java/src/main/java/org/msgpack/template/JavassistTemplateBuilder.java b/java/src/main/java/org/msgpack/template/JavassistTemplateBuilder.java index a1879221..dca54101 100644 --- a/java/src/main/java/org/msgpack/template/JavassistTemplateBuilder.java +++ b/java/src/main/java/org/msgpack/template/JavassistTemplateBuilder.java @@ -521,11 +521,24 @@ public class JavassistTemplateBuilder extends TemplateBuilder { } 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]; for(int i=0; i < entries.length; i++) { FieldEntry e = entries[i]; - Template tmpl = TemplateRegistry.lookup(e.getGenericType(), true); - tmpls[i] = tmpl; + if(!e.isAvailable()) { + tmpls[i] = null; + } else { + Template tmpl = TemplateRegistry.lookup(e.getGenericType(), true); + tmpls[i] = tmpl; + } } BuildContext bc = new BuildContext(this); diff --git a/java/src/main/java/org/msgpack/template/ObjectArrayTemplate.java b/java/src/main/java/org/msgpack/template/ObjectArrayTemplate.java index 93b94d47..f77a5b05 100644 --- a/java/src/main/java/org/msgpack/template/ObjectArrayTemplate.java +++ b/java/src/main/java/org/msgpack/template/ObjectArrayTemplate.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import java.io.IOException; import org.msgpack.*; +// FIXME public class ObjectArrayTemplate implements Template { static void load() { } @@ -40,7 +41,7 @@ public class ObjectArrayTemplate implements Template { if(!(target instanceof Object[])) { throw new MessageTypeException(); } - Object[] array = (Object[])target; + Object[] array = (Object[])target; // FIXME pk.packArray(array.length); for(Object a : array) { componentTemplate.pack(pk, a); @@ -49,7 +50,7 @@ public class ObjectArrayTemplate implements Template { public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { int length = pac.unpackArray(); - Object[] array; + Object[] array; // FIXME if(to != null && to instanceof Object[] && ((Object[])to).length == length) { array = (Object[])to; } else { @@ -63,7 +64,7 @@ public class ObjectArrayTemplate implements Template { public Object convert(MessagePackObject from, Object to) throws MessageTypeException { MessagePackObject[] src = from.asArray(); - Object[] array; + Object[] array; // FIXME if(to != null && to instanceof Object[] && ((Object[])to).length == src.length) { array = (Object[])to; } else { diff --git a/java/src/main/java/org/msgpack/template/ReflectionTemplateBuilder.java b/java/src/main/java/org/msgpack/template/ReflectionTemplateBuilder.java index 8f50185a..ce820ba4 100644 --- a/java/src/main/java/org/msgpack/template/ReflectionTemplateBuilder.java +++ b/java/src/main/java/org/msgpack/template/ReflectionTemplateBuilder.java @@ -36,7 +36,7 @@ public class ReflectionTemplateBuilder extends TemplateBuilder { } static abstract class ReflectionFieldEntry extends FieldEntry { - public ReflectionFieldEntry(FieldEntry e) { + ReflectionFieldEntry(FieldEntry e) { 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 { private Template template; @@ -377,7 +386,9 @@ public class ReflectionTemplateBuilder extends TemplateBuilder { for(int i=0; i < entries.length; i++) { FieldEntry e = entries[i]; 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); } else if(type.equals(byte.class)) { res[i] = new ByteFieldEntry(e); diff --git a/java/src/main/java/org/msgpack/template/TemplateBuilder.java b/java/src/main/java/org/msgpack/template/TemplateBuilder.java index af4ad65e..b4f38810 100644 --- a/java/src/main/java/org/msgpack/template/TemplateBuilder.java +++ b/java/src/main/java/org/msgpack/template/TemplateBuilder.java @@ -28,10 +28,17 @@ import org.msgpack.annotation.*; public abstract class TemplateBuilder { public static class FieldEntry { - private Field field = null; - private FieldOption option = FieldOption.IGNORE; + private Field field; + private FieldOption option; 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) { @@ -84,10 +91,6 @@ public abstract class TemplateBuilder { return option == FieldOption.NULLABLE; } - public boolean isAnnotated(Class with) { - return field.getAnnotation(with) != null; - } - static String arrayTypeToString(Class type) { int dim = 1; Class baseType = type.getComponentType(); @@ -111,19 +114,19 @@ public abstract class TemplateBuilder { public abstract Template buildOrdinalEnumTemplate(Class targetClass, Enum[] entries); - public Template buildTemplate(Class targetClass) { - checkTypeValidation(targetClass); - return buildTemplate(targetClass, readFieldEntries(targetClass)); + public Template buildTemplate(Class targetClass, FieldList flist) throws NoSuchFieldException { + checkValidation(targetClass); + return buildTemplate(targetClass, convertFieldEntries(targetClass, flist)); } public Template buildTemplate(Class targetClass, FieldOption implicitOption) { - checkTypeValidation(targetClass); + checkValidation(targetClass); return buildTemplate(targetClass, readFieldEntries(targetClass, implicitOption)); } - public Template buildTemplate(Class targetClass, FieldList flist) throws NoSuchFieldException { - checkTypeValidation(targetClass); - return buildTemplate(targetClass, convertFieldEntries(targetClass, flist)); + public Template buildTemplate(Class targetClass) { + FieldOption implicitOption = readImplicitFieldOption(targetClass); + return buildTemplate(targetClass, implicitOption); } 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()) { 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()) { 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 src = flist.getList(); FieldEntry[] result = new FieldEntry[src.size()]; for(int i=0; i < src.size(); i++) { @@ -203,12 +206,7 @@ public abstract class TemplateBuilder { return result; } - protected FieldEntry[] readFieldEntries(Class targetClass) { - FieldOption implicitOption = readImplicitFieldOption(targetClass); - return readFieldEntries(targetClass, implicitOption); - } - - protected FieldEntry[] readFieldEntries(Class targetClass, FieldOption implicitOption) { + static FieldEntry[] readFieldEntries(Class targetClass, FieldOption implicitOption) { Field[] allFields = readAllFields(targetClass); /* index: @@ -253,14 +251,14 @@ public abstract class TemplateBuilder { if(e == null) { result[i] = new FieldEntry(); } else { - result[i] = new FieldEntry(e.getField(), e.getOption()); + result[i] = e; } } return result; } - private Field[] readAllFields(Class targetClass) { + private static Field[] readAllFields(Class targetClass) { // order: [fields of super class, ..., fields of this class] List succ = new ArrayList(); int total = 0; @@ -279,7 +277,7 @@ public abstract class TemplateBuilder { return result; } - private FieldOption readImplicitFieldOption(Class targetClass) { + private static FieldOption readImplicitFieldOption(Class targetClass) { MessagePackMessage a = targetClass.getAnnotation(MessagePackMessage.class); if(a == null) { return FieldOption.DEFAULT; @@ -287,7 +285,7 @@ public abstract class TemplateBuilder { return a.value(); } - private FieldOption readFieldOption(Field field, FieldOption implicitOption) { + private static FieldOption readFieldOption(Field field, FieldOption implicitOption) { int mod = field.getModifiers(); if(Modifier.isStatic(mod) || Modifier.isFinal(mod)) { 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); if(a == null) { return maxIndex + 1; @@ -333,7 +331,7 @@ public abstract class TemplateBuilder { } } - protected boolean isAnnotated(AccessibleObject ao, Class with) { + private static boolean isAnnotated(AccessibleObject ao, Class with) { return ao.getAnnotation(with) != null; } } diff --git a/java/src/main/java/org/msgpack/template/TemplateRegistry.java b/java/src/main/java/org/msgpack/template/TemplateRegistry.java index a97fca97..9c7b5485 100644 --- a/java/src/main/java/org/msgpack/template/TemplateRegistry.java +++ b/java/src/main/java/org/msgpack/template/TemplateRegistry.java @@ -90,8 +90,8 @@ public class TemplateRegistry { Class target; // TODO - //if((Type)target instanceof GenericArrayType) { - // return lookupArrayImpl((GenericArrayType)(Type)target); + //if(targetType instanceof GenericArrayType) { + // return lookupGenericArray((GenericArrayType)targetType); //} if(targetType instanceof ParameterizedType) { @@ -109,6 +109,16 @@ public class TemplateRegistry { 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)) { tmpl = TemplateBuilder.build(target); 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) { if(targetType instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType)targetType; @@ -185,26 +211,6 @@ public class TemplateRegistry { 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 with) { return ao.getAnnotation(with) != null; }