mirror of
https://github.com/msgpack/msgpack-c
synced 2026-06-08 16:11:40 +00:00
java: array type support
This commit is contained in:
@@ -42,7 +42,6 @@ public class BuiltInTemplateLoader {
|
||||
ListTemplate.load();
|
||||
MapTemplate.load();
|
||||
NullableTemplate.load();
|
||||
ObjectArrayTemplate.load();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -72,16 +72,6 @@ public class JavassistTemplateBuilder extends TemplateBuilder {
|
||||
return seqId++;
|
||||
}
|
||||
|
||||
public static abstract class JavassistTemplate extends AbstractTemplate {
|
||||
public Class targetClass;
|
||||
public Template[] templates;
|
||||
|
||||
public JavassistTemplate(Class targetClass, Template[] templates) {
|
||||
this.targetClass = targetClass;
|
||||
this.templates = templates;
|
||||
}
|
||||
}
|
||||
|
||||
private static abstract class BuildContextBase {
|
||||
protected JavassistTemplateBuilder director;
|
||||
protected Class<?> origClass;
|
||||
@@ -101,9 +91,9 @@ public class JavassistTemplateBuilder extends TemplateBuilder {
|
||||
this.director = director;
|
||||
}
|
||||
|
||||
protected Template build(Class<?> targetClass) {
|
||||
protected Template build(String className) {
|
||||
try {
|
||||
reset(targetClass);
|
||||
reset(className);
|
||||
buildClass();
|
||||
buildConstructor();
|
||||
buildMethodInit();
|
||||
@@ -124,10 +114,8 @@ public class JavassistTemplateBuilder extends TemplateBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
protected void reset(Class<?> targetClass) {
|
||||
this.origClass = targetClass;
|
||||
this.origName = this.origClass.getName();
|
||||
this.tmplName = this.origName + "_$$_Template" + director.nextSeqId();
|
||||
protected void reset(String className) {
|
||||
this.tmplName = className + "_$$_Template" + director.nextSeqId();
|
||||
this.tmplCtClass = director.makeCtClass(this.tmplName);
|
||||
}
|
||||
|
||||
@@ -220,8 +208,20 @@ public class JavassistTemplateBuilder extends TemplateBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
public static abstract class JavassistTemplate extends AbstractTemplate {
|
||||
public Class targetClass;
|
||||
public Template[] templates;
|
||||
|
||||
public JavassistTemplate(Class targetClass, Template[] templates) {
|
||||
this.targetClass = targetClass;
|
||||
this.templates = templates;
|
||||
}
|
||||
}
|
||||
|
||||
private static class BuildContext extends BuildContextBase {
|
||||
protected FieldEntry[] entries;
|
||||
protected Class<?> origClass;
|
||||
protected String origName;
|
||||
protected Template[] templates;
|
||||
protected int minimumArrayLength;
|
||||
|
||||
@@ -232,7 +232,9 @@ public class JavassistTemplateBuilder extends TemplateBuilder {
|
||||
public Template buildTemplate(Class<?> targetClass, FieldEntry[] entries, Template[] templates) {
|
||||
this.entries = entries;
|
||||
this.templates = templates;
|
||||
return build(targetClass);
|
||||
this.origClass = targetClass;
|
||||
this.origName = this.origClass.getName();
|
||||
return build(this.origName);
|
||||
}
|
||||
|
||||
protected void setSuperClass() throws CannotCompileException, NotFoundException {
|
||||
@@ -554,5 +556,38 @@ public class JavassistTemplateBuilder extends TemplateBuilder {
|
||||
public Template buildOrdinalEnumTemplate(Class<?> targetClass, Enum<?>[] entries) {
|
||||
return new JavassistOrdinalEnumTemplate(entries);
|
||||
}
|
||||
|
||||
public Template buildArrayTemplate(Type arrayType, Type genericBaseType, Class<?> baseClass, int dim) {
|
||||
if(dim == 1) {
|
||||
if(baseClass == boolean.class) {
|
||||
return BooleanArrayTemplate.getInstance();
|
||||
} else if(baseClass == short.class) {
|
||||
return ShortArrayTemplate.getInstance();
|
||||
} else if(baseClass == int.class) {
|
||||
return IntArrayTemplate.getInstance();
|
||||
} else if(baseClass == long.class) {
|
||||
return LongArrayTemplate.getInstance();
|
||||
} else if(baseClass == float.class) {
|
||||
return FloatArrayTemplate.getInstance();
|
||||
} else if(baseClass == double.class) {
|
||||
return DoubleArrayTemplate.getInstance();
|
||||
} else {
|
||||
// FIXME
|
||||
Template baseTemplate = TemplateRegistry.lookup(genericBaseType);
|
||||
return new ReflectionTemplateBuilder.ReflectionObjectArrayTemplate(baseClass, baseTemplate);
|
||||
}
|
||||
} else if(dim == 2) {
|
||||
// FIXME
|
||||
Class<?> componentClass = Array.newInstance(baseClass, 0).getClass();
|
||||
Template componentTemplate = buildArrayTemplate(arrayType, genericBaseType, baseClass, dim-1);
|
||||
return new ReflectionTemplateBuilder.ReflectionMultidimentionalArrayTemplate(componentClass, componentTemplate);
|
||||
} else {
|
||||
// FIXME
|
||||
ReflectionTemplateBuilder.ReflectionMultidimentionalArrayTemplate componentTemplate = (ReflectionTemplateBuilder.ReflectionMultidimentionalArrayTemplate)
|
||||
buildArrayTemplate(arrayType, genericBaseType, baseClass, dim-1);
|
||||
Class<?> componentClass = Array.newInstance(componentTemplate.getComponentClass(), 0).getClass();
|
||||
return new ReflectionTemplateBuilder.ReflectionMultidimentionalArrayTemplate(componentClass, componentTemplate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
// FIXME
|
||||
public class ObjectArrayTemplate implements Template {
|
||||
static void load() { }
|
||||
|
||||
private Template componentTemplate;
|
||||
|
||||
public ObjectArrayTemplate(Template componentTemplate) {
|
||||
this.componentTemplate = componentTemplate;
|
||||
}
|
||||
|
||||
public Template getcomponentTemplate() {
|
||||
return componentTemplate;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
if(!(target instanceof Object[])) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
Object[] array = (Object[])target; // FIXME
|
||||
pk.packArray(array.length);
|
||||
for(Object a : array) {
|
||||
componentTemplate.pack(pk, a);
|
||||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
int length = pac.unpackArray();
|
||||
Object[] array; // FIXME
|
||||
if(to != null && to instanceof Object[] && ((Object[])to).length == length) {
|
||||
array = (Object[])to;
|
||||
} else {
|
||||
array = new Object[length];
|
||||
}
|
||||
for(int i=0; i < length; i++) {
|
||||
array[i] = componentTemplate.unpack(pac, null);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
MessagePackObject[] src = from.asArray();
|
||||
Object[] array; // FIXME
|
||||
if(to != null && to instanceof Object[] && ((Object[])to).length == src.length) {
|
||||
array = (Object[])to;
|
||||
} else {
|
||||
array = new Object[src.length];
|
||||
}
|
||||
for(int i=0; i < src.length; i++) {
|
||||
MessagePackObject s = src[i];
|
||||
array[i] = componentTemplate.convert(s, array[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -451,5 +451,119 @@ public class ReflectionTemplateBuilder extends TemplateBuilder {
|
||||
public Template buildOrdinalEnumTemplate(Class<?> targetClass, Enum<?>[] entries) {
|
||||
return new ReflectionOrdinalEnumTemplate(entries);
|
||||
}
|
||||
|
||||
|
||||
static class ReflectionObjectArrayTemplate extends AbstractTemplate {
|
||||
private Class<?> componentClass;
|
||||
private Template elementTemplate;
|
||||
|
||||
public ReflectionObjectArrayTemplate(Class<?> componentClass, Template elementTemplate) {
|
||||
this.componentClass = componentClass;
|
||||
this.elementTemplate = elementTemplate;
|
||||
}
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
if(!(target instanceof Object[]) || !componentClass.isAssignableFrom(target.getClass().getComponentType())) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
Object[] array = (Object[])target;
|
||||
int length = array.length;
|
||||
pk.packArray(length);
|
||||
for(int i=0; i < length; i++) {
|
||||
elementTemplate.pack(pk, array[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException {
|
||||
int length = pac.unpackArray();
|
||||
Object[] array = (Object[])Array.newInstance(componentClass, length);
|
||||
for(int i=0; i < length; i++) {
|
||||
array[i] = elementTemplate.unpack(pac, null);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
MessagePackObject[] src = from.asArray();
|
||||
int length = src.length;
|
||||
Object[] array = (Object[])Array.newInstance(componentClass, length);
|
||||
for(int i=0; i < length; i++) {
|
||||
array[i] = elementTemplate.convert(src[i], null);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
||||
static class ReflectionMultidimentionalArrayTemplate extends AbstractTemplate {
|
||||
private Class<?> componentClass;
|
||||
private Template componentTemplate;
|
||||
|
||||
public ReflectionMultidimentionalArrayTemplate(Class<?> componentClass, Template componentTemplate) {
|
||||
this.componentClass = componentClass;
|
||||
this.componentTemplate = componentTemplate;
|
||||
}
|
||||
|
||||
Class<?> getComponentClass() {
|
||||
return componentClass;
|
||||
}
|
||||
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
Object[] array = (Object[])target;
|
||||
int length = array.length;
|
||||
pk.packArray(length);
|
||||
for(int i=0; i < length; i++) {
|
||||
componentTemplate.pack(pk, array[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
int length = pac.unpackArray();
|
||||
Object[] array = (Object[])Array.newInstance(componentClass, length);
|
||||
for(int i=0; i < length; i++) {
|
||||
array[i] = componentTemplate.unpack(pac, null);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
MessagePackObject[] src = from.asArray();
|
||||
int length = src.length;
|
||||
Object[] array = (Object[])Array.newInstance(componentClass, length);
|
||||
for(int i=0; i < length; i++) {
|
||||
array[i] = componentTemplate.convert(src[i], null);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
||||
public Template buildArrayTemplate(Type arrayType, Type genericBaseType, Class<?> baseClass, int dim) {
|
||||
if(dim == 1) {
|
||||
if(baseClass == boolean.class) {
|
||||
return BooleanArrayTemplate.getInstance();
|
||||
} else if(baseClass == short.class) {
|
||||
return ShortArrayTemplate.getInstance();
|
||||
} else if(baseClass == int.class) {
|
||||
return IntArrayTemplate.getInstance();
|
||||
} else if(baseClass == long.class) {
|
||||
return LongArrayTemplate.getInstance();
|
||||
} else if(baseClass == float.class) {
|
||||
return FloatArrayTemplate.getInstance();
|
||||
} else if(baseClass == double.class) {
|
||||
return DoubleArrayTemplate.getInstance();
|
||||
} else {
|
||||
Template baseTemplate = TemplateRegistry.lookup(genericBaseType);
|
||||
return new ReflectionObjectArrayTemplate(baseClass, baseTemplate);
|
||||
}
|
||||
} else if(dim == 2) {
|
||||
Class<?> componentClass = Array.newInstance(baseClass, 0).getClass();
|
||||
Template componentTemplate = buildArrayTemplate(arrayType, genericBaseType, baseClass, dim-1);
|
||||
return new ReflectionMultidimentionalArrayTemplate(componentClass, componentTemplate);
|
||||
} else {
|
||||
ReflectionMultidimentionalArrayTemplate componentTemplate = (ReflectionMultidimentionalArrayTemplate)
|
||||
buildArrayTemplate(arrayType, genericBaseType, baseClass, dim-1);
|
||||
Class<?> componentClass = Array.newInstance(componentTemplate.getComponentClass(), 0).getClass();
|
||||
return new ReflectionMultidimentionalArrayTemplate(componentClass, componentTemplate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -113,6 +113,9 @@ public abstract class TemplateBuilder {
|
||||
// Override this method
|
||||
public abstract Template buildOrdinalEnumTemplate(Class<?> targetClass, Enum<?>[] entries);
|
||||
|
||||
// Override this method
|
||||
public abstract Template buildArrayTemplate(Type arrayType, Type genericBaseType, Class<?> baseClass, int dim);
|
||||
|
||||
|
||||
public Template buildTemplate(Class<?> targetClass, FieldList flist) throws NoSuchFieldException {
|
||||
checkValidation(targetClass);
|
||||
@@ -135,6 +138,38 @@ public abstract class TemplateBuilder {
|
||||
return buildOrdinalEnumTemplate(targetClass, entries);
|
||||
}
|
||||
|
||||
public Template buildArrayTemplate(Type arrayType) {
|
||||
Type baseType;
|
||||
Class<?> baseClass;
|
||||
int dim = 1;
|
||||
if(arrayType instanceof GenericArrayType) {
|
||||
GenericArrayType type = (GenericArrayType)arrayType;
|
||||
baseType = type.getGenericComponentType();
|
||||
while(baseType instanceof GenericArrayType) {
|
||||
baseType = ((GenericArrayType)baseType).getGenericComponentType();
|
||||
dim += 1;
|
||||
}
|
||||
baseClass = (Class<?>)((ParameterizedType)baseType).getRawType();
|
||||
} else {
|
||||
Class<?> type = (Class<?>)arrayType;
|
||||
baseClass = type.getComponentType();
|
||||
while(baseClass.isArray()) {
|
||||
baseClass = baseClass.getComponentType();
|
||||
dim += 1;
|
||||
}
|
||||
baseType = baseClass;
|
||||
}
|
||||
return buildArrayTemplate(arrayType, baseType, baseClass, dim);
|
||||
}
|
||||
|
||||
private static Type getComponentType(Type arrayType) {
|
||||
if(arrayType instanceof GenericArrayType) {
|
||||
return ((GenericArrayType)arrayType).getGenericComponentType();
|
||||
} else {
|
||||
return ((Class<?>)arrayType).getComponentType();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static TemplateBuilder instance;
|
||||
static {
|
||||
@@ -172,6 +207,10 @@ public abstract class TemplateBuilder {
|
||||
return instance.buildOrdinalEnumTemplate(targetClass);
|
||||
}
|
||||
|
||||
public static Template buildArray(Type arrayType) {
|
||||
return instance.buildArrayTemplate(arrayType);
|
||||
}
|
||||
|
||||
|
||||
private static void checkValidation(Class<?> targetClass) {
|
||||
if(targetClass.isInterface()) {
|
||||
|
||||
@@ -87,37 +87,36 @@ public class TemplateRegistry {
|
||||
|
||||
private static synchronized Template lookupImpl(Type targetType, boolean forceBuild, boolean fallbackDefault) {
|
||||
Template tmpl;
|
||||
Class<?> target;
|
||||
|
||||
// TODO
|
||||
//if(targetType instanceof GenericArrayType) {
|
||||
// return lookupGenericArray((GenericArrayType)targetType);
|
||||
//}
|
||||
|
||||
if(targetType instanceof ParameterizedType) {
|
||||
// ParameterizedType is not a Class<?>?
|
||||
tmpl = lookupGenericImpl((ParameterizedType)targetType);
|
||||
if(tmpl != null) {
|
||||
return tmpl;
|
||||
}
|
||||
target = (Class<?>)((ParameterizedType)targetType).getRawType();
|
||||
} else {
|
||||
target = (Class<?>)targetType;
|
||||
targetType = ((ParameterizedType)targetType).getRawType();
|
||||
}
|
||||
|
||||
tmpl = map.get(target);
|
||||
tmpl = map.get(targetType);
|
||||
if(tmpl != null) {
|
||||
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(targetType instanceof GenericArrayType) {
|
||||
// GenericArrayType is not a Class<?>
|
||||
tmpl = TemplateBuilder.buildArray(targetType);
|
||||
register(targetType, tmpl);
|
||||
return tmpl;
|
||||
}
|
||||
|
||||
Class<?> target = (Class<?>)targetType;
|
||||
|
||||
if(target.isArray()) {
|
||||
// FIXME can't distinguish type-erased T<>[]?
|
||||
tmpl = TemplateBuilder.buildArray(target);
|
||||
register(target, tmpl);
|
||||
return tmpl;
|
||||
}
|
||||
|
||||
if(isAnnotated(target, MessagePackMessage.class)) {
|
||||
tmpl = TemplateBuilder.build(target);
|
||||
@@ -166,22 +165,6 @@ 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;
|
||||
|
||||
Reference in New Issue
Block a user