mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-20 13:33:51 +01:00
Merge branch 'master' of git://github.com/msgpack/msgpack
This commit is contained in:
commit
f6de4c9479
@ -63,8 +63,8 @@
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
<source>1.5</source>
|
||||
<target>1.5</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -23,5 +23,4 @@ public abstract class AbstractTemplate implements Template {
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
return convert(pac.unpackObject(), to);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -146,10 +146,10 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
|
||||
advance(3);
|
||||
return (int)castBuffer.getShort(0);
|
||||
case 0xd2: // signed int 32
|
||||
more(4);
|
||||
more(5);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 4);
|
||||
advance(4);
|
||||
advance(5);
|
||||
return (int)castBuffer.getInt(0);
|
||||
case 0xd3: // signed int 64
|
||||
more(9);
|
||||
@ -215,10 +215,10 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
|
||||
advance(3);
|
||||
return (long)castBuffer.getShort(0);
|
||||
case 0xd2: // signed int 32
|
||||
more(4);
|
||||
more(5);
|
||||
castBuffer.rewind();
|
||||
castBuffer.put(buffer, offset+1, 4);
|
||||
advance(4);
|
||||
advance(5);
|
||||
return (long)castBuffer.getInt(0);
|
||||
case 0xd3: // signed int 64
|
||||
more(9);
|
||||
|
@ -22,7 +22,6 @@ import java.io.InputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import org.msgpack.template.TemplateRegistry;
|
||||
import org.msgpack.template.TemplateClassWriter;
|
||||
import org.msgpack.template.FieldList;
|
||||
|
||||
public class MessagePack {
|
||||
@ -146,10 +145,6 @@ public class MessagePack {
|
||||
}
|
||||
}
|
||||
|
||||
public static void write(Class<?> target, String directoryName) {
|
||||
TemplateClassWriter.write(target, directoryName);
|
||||
}
|
||||
|
||||
public static void register(Class<?> target) {
|
||||
TemplateRegistry.register(target);
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -18,6 +18,5 @@
|
||||
package org.msgpack;
|
||||
|
||||
public interface MessagePackTemplateProvider {
|
||||
|
||||
Template getTemplate();
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -18,5 +18,4 @@
|
||||
package org.msgpack;
|
||||
|
||||
public interface Template extends MessagePacker, MessageUnpacker, MessageConverter {
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
|
||||
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
@ -1,71 +0,0 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2011 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 org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class TemplateClassWriter {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(TemplateClassWriter.class);
|
||||
|
||||
public static void write(Class<?> target, String directoryName) {
|
||||
if (target.isEnum()) {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
} else {
|
||||
//TemplateBuilder.writeClass(target, directoryName);
|
||||
}
|
||||
LOG.info("finished writing .class file of template class for " + target.getName());
|
||||
}
|
||||
|
||||
private String directoryName;
|
||||
|
||||
private String[] classNames;
|
||||
|
||||
private TemplateClassWriter() {
|
||||
}
|
||||
|
||||
private void parseOpts(final String[] args) {// TODO
|
||||
if (args.length == 0) {
|
||||
usage();
|
||||
}
|
||||
}
|
||||
|
||||
private void usage() {// TODO
|
||||
System.err.println("java org.msgpack.template.TemplateClassWriter ");
|
||||
System.err.println("");
|
||||
}
|
||||
|
||||
private void writeTemplateClasses() {
|
||||
ClassLoader cl = this.getClass().getClassLoader();// TODO
|
||||
for (String className : classNames) {
|
||||
Class<?> origClass = null;
|
||||
try {
|
||||
origClass = cl.loadClass(className);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new TemplateBuildException(e);
|
||||
}
|
||||
write(origClass, directoryName);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(final String[] args) throws Exception {
|
||||
TemplateClassWriter writer = new TemplateClassWriter();
|
||||
writer.parseOpts(args);
|
||||
writer.writeTemplateClasses();
|
||||
}
|
||||
}
|
@ -20,12 +20,7 @@ package org.msgpack.template;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.lang.reflect.Type;
|
||||
import java.lang.reflect.GenericArrayType;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.annotation.Annotation;
|
||||
import org.msgpack.annotation.MessagePackMessage;
|
||||
import org.msgpack.annotation.MessagePackDelegate;
|
||||
import org.msgpack.annotation.MessagePackOrdinalEnum;
|
||||
import org.msgpack.template.builder.BuilderSelectorRegistry;
|
||||
import org.msgpack.template.builder.CustomTemplateBuilder;
|
||||
import org.msgpack.template.builder.TemplateBuilder;
|
||||
@ -34,7 +29,6 @@ import org.msgpack.Template;
|
||||
public class TemplateRegistry {
|
||||
private static Map<Type, Template> map;
|
||||
private static Map<Type, GenericTemplate> genericMap;
|
||||
|
||||
private static BuilderSelectorRegistry builderSelectorRegistry;
|
||||
|
||||
static {
|
||||
@ -44,45 +38,45 @@ public class TemplateRegistry {
|
||||
builderSelectorRegistry = BuilderSelectorRegistry.getInstance();
|
||||
}
|
||||
|
||||
public static void register(Class<?> target) { // auto-detect
|
||||
public static void register(Class<?> target) {
|
||||
TemplateBuilder builder = builderSelectorRegistry.select(target);
|
||||
if(builder != null){
|
||||
if (builder != null) {
|
||||
register(target,builder.buildTemplate(target));
|
||||
}else{
|
||||
} else {
|
||||
register(target,builderSelectorRegistry.getForceBuilder().buildTemplate(target));
|
||||
}
|
||||
/*if(target.isEnum()) {
|
||||
register(target, TemplateBuilder.buildOrdinalEnum(target));
|
||||
} else {
|
||||
register(target, TemplateBuilder.build(target));
|
||||
}*/
|
||||
}
|
||||
|
||||
public static void register(Class<?> target, FieldOption implicitOption) {
|
||||
TemplateBuilder builder = builderSelectorRegistry.select(target);
|
||||
if(builder != null && builder instanceof CustomTemplateBuilder){
|
||||
if (builder != null && builder instanceof CustomTemplateBuilder) {
|
||||
register(target, ((CustomTemplateBuilder)builder).buildTemplate(target, implicitOption));
|
||||
}else{
|
||||
throw new TemplateBuildException("cannot build template with filed option");
|
||||
} else {
|
||||
throw new TemplateBuildException("Cannot build template with filed option");
|
||||
}
|
||||
}
|
||||
|
||||
public static void register(Class<?> target, FieldList flist) throws NoSuchFieldException {
|
||||
TemplateBuilder builder = builderSelectorRegistry.select(target);
|
||||
if(builder != null && builder instanceof CustomTemplateBuilder){
|
||||
if (builder != null && builder instanceof CustomTemplateBuilder) {
|
||||
register(target, ((CustomTemplateBuilder)builder).buildTemplate(target, flist));
|
||||
}else{
|
||||
throw new TemplateBuildException("cannot build template with filed list");
|
||||
} else {
|
||||
throw new TemplateBuildException("Cannot build template with filed list");
|
||||
}
|
||||
}
|
||||
|
||||
public static synchronized void register(Type rawType, Template tmpl) {
|
||||
if(rawType instanceof ParameterizedType) {
|
||||
if (rawType instanceof ParameterizedType) {
|
||||
rawType = ((ParameterizedType)rawType).getRawType();
|
||||
}
|
||||
map.put(rawType, tmpl);
|
||||
}
|
||||
|
||||
public static boolean unregister(Class<?> target) {
|
||||
Template tmpl = map.remove(target);
|
||||
return tmpl != null;
|
||||
}
|
||||
|
||||
public static synchronized void registerGeneric(Type rawType, GenericTemplate gtmpl) {
|
||||
if(rawType instanceof ParameterizedType) {
|
||||
rawType = ((ParameterizedType)rawType).getRawType();
|
||||
@ -91,22 +85,27 @@ public class TemplateRegistry {
|
||||
}
|
||||
|
||||
public static synchronized Template lookup(Type targetType) {
|
||||
return lookupImpl(targetType, false, true);
|
||||
return lookupImpl(targetType, true, false, true);
|
||||
}
|
||||
|
||||
public static synchronized Template lookup(Type targetType, boolean forceBuild) {
|
||||
return lookupImpl(targetType, forceBuild, true);
|
||||
return lookupImpl(targetType, true, forceBuild, true);
|
||||
}
|
||||
|
||||
public static synchronized Template lookup(Type targetType, boolean forceLoad, boolean forceBuild) {
|
||||
return lookupImpl(targetType, forceLoad, forceBuild, true);
|
||||
}
|
||||
|
||||
public static synchronized Template tryLookup(Type targetType) {
|
||||
return lookupImpl(targetType, false, false);
|
||||
return lookupImpl(targetType, true, false, false);
|
||||
}
|
||||
|
||||
public static synchronized Template tryLookup(Type targetType, boolean forceBuild) {
|
||||
return lookupImpl(targetType, forceBuild, false);
|
||||
return lookupImpl(targetType, true, forceBuild, false);
|
||||
}
|
||||
|
||||
private static synchronized Template lookupImpl(Type targetType, boolean forceBuild, boolean fallbackDefault) {
|
||||
private static synchronized Template lookupImpl(Type targetType,
|
||||
boolean forceLoad, boolean forceBuild, boolean fallbackDefault) {
|
||||
Template tmpl;
|
||||
|
||||
if(targetType instanceof ParameterizedType) {
|
||||
@ -123,50 +122,24 @@ public class TemplateRegistry {
|
||||
return tmpl;
|
||||
}
|
||||
|
||||
/*if(targetType instanceof GenericArrayType) {
|
||||
// GenericArrayType is not a Class<?>
|
||||
tmpl = TemplateBuilder.buildArray(targetType);
|
||||
register(targetType, tmpl);
|
||||
return tmpl;
|
||||
}
|
||||
|
||||
Class<?> target = (Class<?>)targetType;
|
||||
|
||||
Class<?> tmplClass = TemplateBuilder.load(target);
|
||||
if (tmplClass != null) {
|
||||
tmpl = TemplateBuilder.initialize(target, tmplClass);
|
||||
register(target, tmpl);
|
||||
return tmpl;
|
||||
}
|
||||
|
||||
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);
|
||||
register(target, tmpl);
|
||||
return tmpl;
|
||||
} else if(isAnnotated(target, MessagePackDelegate.class)) {
|
||||
// TODO DelegateTemplate
|
||||
throw new UnsupportedOperationException("not supported yet. : " + target.getName());
|
||||
} else if(isAnnotated(target, MessagePackOrdinalEnum.class)) {
|
||||
tmpl = TemplateBuilder.buildOrdinalEnum(target);
|
||||
register(target, tmpl);
|
||||
return tmpl;
|
||||
}*/
|
||||
// find match TemplateBuilder
|
||||
TemplateBuilder builder = BuilderSelectorRegistry.getInstance().select(targetType);
|
||||
if(builder != null){
|
||||
if (builder != null) {
|
||||
if (forceLoad) {
|
||||
tmpl = builder.loadTemplate(targetType);
|
||||
if (tmpl != null) {
|
||||
register(targetType, tmpl);
|
||||
return tmpl;
|
||||
}
|
||||
}
|
||||
|
||||
tmpl = builder.buildTemplate(targetType);
|
||||
register(targetType,tmpl);
|
||||
return tmpl;
|
||||
if (tmpl != null) {
|
||||
register(targetType, tmpl);
|
||||
return tmpl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Class<?> target = (Class<?>)targetType;
|
||||
|
||||
for(Class<?> i : target.getInterfaces()) {
|
||||
@ -212,7 +185,7 @@ public class TemplateRegistry {
|
||||
}
|
||||
return new DefaultTemplate((Class<?>)parameterizedType.getRawType(), parameterizedType);
|
||||
} else {
|
||||
throw new IllegalArgumentException("actual types of the generic type are erased: "+targetType);
|
||||
throw new IllegalArgumentException("Actual types of the generic type are erased: "+targetType);
|
||||
}
|
||||
}
|
||||
|
||||
@ -231,10 +204,5 @@ public class TemplateRegistry {
|
||||
|
||||
return gtmpl.build(tmpls);
|
||||
}
|
||||
|
||||
private static boolean isAnnotated(Class<?> ao, Class<? extends Annotation> with) {
|
||||
return ao.getAnnotation(with) != null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -22,34 +22,33 @@ import java.lang.reflect.Type;
|
||||
|
||||
import org.msgpack.annotation.MessagePackMessage;
|
||||
|
||||
public class MessagePackMessageBuilderSelector implements BuilderSelector{
|
||||
|
||||
public static final String NAME = "MessagePackMessageTemplateBuilder";
|
||||
|
||||
|
||||
public class AnnotationTemplateBuilderSelector implements BuilderSelector{
|
||||
|
||||
public static final String NAME = "AnnotationTemplateBuilder";
|
||||
|
||||
TemplateBuilder builder;
|
||||
public MessagePackMessageBuilderSelector(TemplateBuilder builder){
|
||||
|
||||
public AnnotationTemplateBuilderSelector(TemplateBuilder builder){
|
||||
this.builder = builder;
|
||||
}
|
||||
|
||||
public String getName(){
|
||||
|
||||
@Override
|
||||
public String getName(){
|
||||
return NAME;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean matchType(Type targetType) {
|
||||
Class<?> target = (Class<?>)targetType;
|
||||
return isAnnotated(target, MessagePackMessage.class);
|
||||
Class<?> targetClass = (Class<?>)targetType;
|
||||
return isAnnotated(targetClass, MessagePackMessage.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TemplateBuilder getTemplateBuilder(Type targetType) {
|
||||
return builder;
|
||||
}
|
||||
|
||||
|
||||
private boolean isAnnotated(Class<?> ao, Class<? extends Annotation> with) {
|
||||
return ao.getAnnotation(with) != null;
|
||||
public static boolean isAnnotated(Class<?> targetClass, Class<? extends Annotation> with) {
|
||||
return targetClass.getAnnotation(with) != null;
|
||||
}
|
||||
|
||||
}
|
@ -22,8 +22,6 @@ import java.lang.reflect.Array;
|
||||
import java.lang.reflect.GenericArrayType;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.msgpack.AbstractTemplate;
|
||||
import org.msgpack.MessagePackObject;
|
||||
@ -34,17 +32,12 @@ import org.msgpack.Unpacker;
|
||||
import org.msgpack.template.BooleanArrayTemplate;
|
||||
import org.msgpack.template.DoubleArrayTemplate;
|
||||
import org.msgpack.template.FloatArrayTemplate;
|
||||
import org.msgpack.template.IFieldEntry;
|
||||
import org.msgpack.template.IFieldEntryReader;
|
||||
import org.msgpack.template.IntArrayTemplate;
|
||||
import org.msgpack.template.LongArrayTemplate;
|
||||
import org.msgpack.template.ShortArrayTemplate;
|
||||
import org.msgpack.template.TemplateRegistry;
|
||||
|
||||
public class ArrayTemplateBuilder extends TemplateBuilder {
|
||||
|
||||
|
||||
|
||||
public class ArrayTemplateBuilder implements TemplateBuilder {
|
||||
|
||||
static class ReflectionObjectArrayTemplate extends AbstractTemplate {
|
||||
private Class<?> componentClass;
|
||||
@ -128,6 +121,7 @@ public class ArrayTemplateBuilder extends TemplateBuilder {
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Template buildTemplate(Type arrayType) {
|
||||
Type baseType;
|
||||
@ -157,6 +151,7 @@ public class ArrayTemplateBuilder extends TemplateBuilder {
|
||||
return toTemplate(arrayType, baseType, baseClass, dim);
|
||||
|
||||
}
|
||||
|
||||
private Template toTemplate(Type arrayType, Type genericBaseType, Class<?> baseClass, int dim) {
|
||||
if(dim == 1) {
|
||||
if(baseClass == boolean.class) {
|
||||
@ -187,4 +182,13 @@ public class ArrayTemplateBuilder extends TemplateBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTemplate(Type targetType, String directoryName) {
|
||||
throw new UnsupportedOperationException(targetType.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Template loadTemplate(Type targetType) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -20,18 +20,17 @@ package org.msgpack.template.builder;
|
||||
import java.lang.reflect.GenericArrayType;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import org.msgpack.Template;
|
||||
|
||||
public class ArrayTemplateBuilderSelector implements BuilderSelector {
|
||||
|
||||
public static final String NAME = "ArrayTemplateBuilder";
|
||||
|
||||
ArrayTemplateBuilder templateBuilder = new ArrayTemplateBuilder();
|
||||
|
||||
@Override
|
||||
public String getName(){
|
||||
return NAME;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean matchType(Type targetType) {
|
||||
if(targetType instanceof GenericArrayType){
|
||||
@ -41,8 +40,6 @@ public class ArrayTemplateBuilderSelector implements BuilderSelector {
|
||||
return targetClass.isArray();
|
||||
}
|
||||
|
||||
ArrayTemplateBuilder templateBuilder = new ArrayTemplateBuilder();
|
||||
|
||||
@Override
|
||||
public TemplateBuilder getTemplateBuilder(Type target) {
|
||||
return templateBuilder;
|
||||
|
@ -15,33 +15,19 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template.javassist;
|
||||
package org.msgpack.template.builder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.lang.Thread;
|
||||
|
||||
import org.msgpack.*;
|
||||
import org.msgpack.template.*;
|
||||
|
||||
import javassist.CannotCompileException;
|
||||
import javassist.ClassPool;
|
||||
import javassist.CtClass;
|
||||
import javassist.CtConstructor;
|
||||
import javassist.CtMethod;
|
||||
import javassist.CtNewConstructor;
|
||||
import javassist.CtNewMethod;
|
||||
import javassist.LoaderClassPath;
|
||||
import javassist.NotFoundException;
|
||||
import javassist.ClassClassPath;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
|
||||
public class BeansBuildContext extends BuildContextBase<BeansFieldEntry> {
|
||||
protected BeansFieldEntry[] entries;
|
||||
@ -64,7 +50,7 @@ public class BeansBuildContext extends BuildContextBase<BeansFieldEntry> {
|
||||
|
||||
protected void setSuperClass() throws CannotCompileException, NotFoundException {
|
||||
this.tmplCtClass.setSuperclass(
|
||||
director.getCtClass(JavassistTemplate.class.getName()));
|
||||
director.getCtClass(JavassistTemplateBuilder.JavassistTemplate.class.getName()));
|
||||
}
|
||||
|
||||
protected void buildConstructor() throws CannotCompileException, NotFoundException {
|
||||
@ -286,4 +272,14 @@ public class BeansBuildContext extends BuildContextBase<BeansFieldEntry> {
|
||||
return getBuiltString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTemplate(Class<?> targetClass, BeansFieldEntry[] entries,
|
||||
Template[] templates, String directoryName) {
|
||||
throw new UnsupportedOperationException(targetClass.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Template loadTemplate(Class<?> targetClass, BeansFieldEntry[] entries, Template[] templates) {
|
||||
throw new UnsupportedOperationException(targetClass.getName());
|
||||
}
|
||||
}
|
@ -15,10 +15,10 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
package org.msgpack.template.builder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import org.msgpack.AbstractTemplate;
|
||||
import org.msgpack.MessagePackObject;
|
||||
@ -26,27 +26,22 @@ import org.msgpack.MessageTypeException;
|
||||
import org.msgpack.Packer;
|
||||
import org.msgpack.Template;
|
||||
import org.msgpack.Unpacker;
|
||||
import org.msgpack.template.ReflectionTemplateBuilder.BooleanFieldEntry;
|
||||
import org.msgpack.template.ReflectionTemplateBuilder.ByteFieldEntry;
|
||||
import org.msgpack.template.ReflectionTemplateBuilder.DoubleFieldEntry;
|
||||
import org.msgpack.template.ReflectionTemplateBuilder.FloatFieldEntry;
|
||||
import org.msgpack.template.ReflectionTemplateBuilder.IntFieldEntry;
|
||||
import org.msgpack.template.ReflectionTemplateBuilder.LongFieldEntry;
|
||||
import org.msgpack.template.ReflectionTemplateBuilder.NullFieldEntry;
|
||||
import org.msgpack.template.ReflectionTemplateBuilder.ObjectFieldEntry;
|
||||
import org.msgpack.template.ReflectionTemplateBuilder.ShortFieldEntry;
|
||||
import org.msgpack.template.builder.CustomTemplateBuilder;
|
||||
import org.msgpack.template.BeansFieldEntry;
|
||||
import org.msgpack.template.BeansFieldEntryReader;
|
||||
import org.msgpack.template.IFieldEntry;
|
||||
import org.msgpack.template.IFieldEntryReader;
|
||||
import org.msgpack.template.TemplateRegistry;
|
||||
|
||||
/**
|
||||
* Class for building java reflection template builder for java beans class.
|
||||
* @author takeshita
|
||||
*
|
||||
*/
|
||||
public class BeansReflectionTemplateBuilder extends CustomTemplateBuilder{
|
||||
public class BeansTemplateBuilder extends CustomTemplateBuilder{
|
||||
|
||||
IFieldEntryReader reader = new BeansFieldEntryReader();
|
||||
|
||||
public BeansReflectionTemplateBuilder(){}
|
||||
public BeansTemplateBuilder(){}
|
||||
|
||||
@Override
|
||||
public IFieldEntryReader getFieldEntryReader(){
|
||||
@ -305,7 +300,6 @@ public class BeansReflectionTemplateBuilder extends CustomTemplateBuilder{
|
||||
|
||||
@Override
|
||||
public Template buildTemplate(Class<?> targetClass, IFieldEntry[] entries) {
|
||||
|
||||
ReflectionEntry[] refEntries = new ReflectionEntry[entries.length];
|
||||
for(int i = 0;i < entries.length;i++){
|
||||
BeansFieldEntry e = (BeansFieldEntry)entries[i];
|
||||
@ -329,10 +323,6 @@ public class BeansReflectionTemplateBuilder extends CustomTemplateBuilder{
|
||||
refEntries[i] = new ObjectFieldEntry(e, tmpl);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return new BeansReflectionTemplate(targetClass,refEntries);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -17,41 +17,33 @@
|
||||
//
|
||||
package org.msgpack.template.builder;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import org.msgpack.annotation.MessagePackBeans;
|
||||
import org.msgpack.annotation.MessagePackMessage;
|
||||
|
||||
public class MessagePackBeansBuilderSelector implements BuilderSelector{
|
||||
|
||||
public static final String NAME = "MessagePackBeansTemplateBuilder";
|
||||
|
||||
|
||||
public class BeansTemplateBuilderSelector implements BuilderSelector{
|
||||
|
||||
public static final String NAME = "BeansTemplateBuilder";
|
||||
|
||||
TemplateBuilder builder;
|
||||
public MessagePackBeansBuilderSelector(TemplateBuilder builder){
|
||||
|
||||
public BeansTemplateBuilderSelector(TemplateBuilder builder){
|
||||
this.builder = builder;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String getName(){
|
||||
return NAME;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean matchType(Type targetType) {
|
||||
Class<?> target = (Class<?>)targetType;
|
||||
return isAnnotated(target, MessagePackBeans.class);
|
||||
Class<?> targetClass = (Class<?>)targetType;
|
||||
return AnnotationTemplateBuilderSelector.isAnnotated(targetClass, MessagePackBeans.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TemplateBuilder getTemplateBuilder(Type targetType) {
|
||||
return builder;
|
||||
}
|
||||
|
||||
|
||||
private boolean isAnnotated(Class<?> ao, Class<? extends Annotation> with) {
|
||||
return ao.getAnnotation(with) != null;
|
||||
}
|
||||
|
||||
}
|
@ -15,33 +15,19 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template.javassist;
|
||||
package org.msgpack.template.builder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.lang.Thread;
|
||||
|
||||
import org.msgpack.*;
|
||||
import org.msgpack.template.*;
|
||||
|
||||
import javassist.CannotCompileException;
|
||||
import javassist.ClassPool;
|
||||
import javassist.CtClass;
|
||||
import javassist.CtConstructor;
|
||||
import javassist.CtMethod;
|
||||
import javassist.CtNewConstructor;
|
||||
import javassist.CtNewMethod;
|
||||
import javassist.LoaderClassPath;
|
||||
import javassist.NotFoundException;
|
||||
import javassist.ClassClassPath;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
|
||||
public class BuildContext extends BuildContextBase<FieldEntry> {
|
||||
protected IFieldEntry[] entries;
|
||||
@ -64,7 +50,7 @@ public class BuildContext extends BuildContextBase<FieldEntry> {
|
||||
|
||||
protected void setSuperClass() throws CannotCompileException, NotFoundException {
|
||||
this.tmplCtClass.setSuperclass(
|
||||
director.getCtClass(JavassistTemplate.class.getName()));
|
||||
director.getCtClass(JavassistTemplateBuilder.JavassistTemplate.class.getName()));
|
||||
}
|
||||
|
||||
protected void buildConstructor() throws CannotCompileException, NotFoundException {
|
||||
@ -286,4 +272,22 @@ public class BuildContext extends BuildContextBase<FieldEntry> {
|
||||
return getBuiltString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTemplate(Class<?> targetClass, FieldEntry[] entries,
|
||||
Template[] templates, String directoryName) {
|
||||
this.entries = entries;
|
||||
this.templates = templates;
|
||||
this.origClass = targetClass;
|
||||
this.origName = this.origClass.getName();
|
||||
write(this.origName, directoryName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Template loadTemplate(Class<?> targetClass, FieldEntry[] entries, Template[] templates) {
|
||||
this.entries = entries;
|
||||
this.templates = templates;
|
||||
this.origClass = targetClass;
|
||||
this.origName = this.origClass.getName();
|
||||
return load(this.origName);
|
||||
}
|
||||
}
|
@ -15,37 +15,26 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template.javassist;
|
||||
package org.msgpack.template.builder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.lang.Thread;
|
||||
|
||||
import org.msgpack.*;
|
||||
import org.msgpack.template.*;
|
||||
|
||||
import javassist.CannotCompileException;
|
||||
import javassist.ClassPool;
|
||||
import javassist.CtClass;
|
||||
import javassist.CtConstructor;
|
||||
import javassist.CtMethod;
|
||||
import javassist.CtNewConstructor;
|
||||
import javassist.CtNewMethod;
|
||||
import javassist.LoaderClassPath;
|
||||
import javassist.NotFoundException;
|
||||
import javassist.ClassClassPath;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
public abstract class BuildContextBase<T extends IFieldEntry> {
|
||||
|
||||
private static Logger LOG = LoggerFactory.getLogger(JavassistTemplateBuilder.class);
|
||||
|
||||
|
||||
protected JavassistTemplateBuilder director;
|
||||
|
||||
@ -53,6 +42,8 @@ public abstract class BuildContextBase<T extends IFieldEntry> {
|
||||
|
||||
protected CtClass tmplCtClass;
|
||||
|
||||
protected abstract Template buildTemplate(Class<?> targetClass, T[] entries, Template[] templates);
|
||||
|
||||
protected abstract void setSuperClass() throws CannotCompileException, NotFoundException;
|
||||
|
||||
protected abstract void buildConstructor() throws CannotCompileException, NotFoundException;
|
||||
@ -65,19 +56,21 @@ public abstract class BuildContextBase<T extends IFieldEntry> {
|
||||
|
||||
protected abstract String buildConvertMethodBody();
|
||||
|
||||
protected abstract Template buildInstance(Class<?> c) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException;
|
||||
protected abstract Template buildInstance(Class<?> c)
|
||||
throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException;
|
||||
|
||||
protected abstract void writeTemplate(Class<?> targetClass, T[] entries,
|
||||
Template[] templates, String directoryName);
|
||||
|
||||
protected abstract Template loadTemplate(Class<?> targetClass, T[] entries, Template[] templates);
|
||||
|
||||
public BuildContextBase(JavassistTemplateBuilder director) {
|
||||
this.director = director;
|
||||
}
|
||||
|
||||
|
||||
public abstract Template buildTemplate(Class<?> targetClass, T[] entries, Template[] templates);
|
||||
|
||||
|
||||
protected Template build(final String className) {
|
||||
try {
|
||||
reset(className);
|
||||
reset(className, false);
|
||||
buildClass();
|
||||
buildConstructor();
|
||||
buildMethodInit();
|
||||
@ -89,15 +82,20 @@ public abstract class BuildContextBase<T extends IFieldEntry> {
|
||||
String code = getBuiltString();
|
||||
if(code != null) {
|
||||
LOG.error("builder: " + code, e);
|
||||
throw new TemplateBuildException("cannot compile: " + code, e);
|
||||
throw new TemplateBuildException("Cannot compile: " + code, e);
|
||||
} else {
|
||||
throw new TemplateBuildException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void reset(String className) {
|
||||
tmplName = className + "_$$_Template" + director.nextSeqId();
|
||||
protected void reset(String className, boolean isWritten) {
|
||||
String tmplName = null;
|
||||
if (!isWritten) {
|
||||
tmplName = className + "_$$_Template" + director.nextSeqId();
|
||||
} else {
|
||||
tmplName = className + "_$$_Template";
|
||||
}
|
||||
tmplCtClass = director.makeCtClass(tmplName);
|
||||
}
|
||||
|
||||
@ -164,6 +162,10 @@ public abstract class BuildContextBase<T extends IFieldEntry> {
|
||||
return (Class<?>) tmplCtClass.toClass(null, null);
|
||||
}
|
||||
|
||||
protected void saveClass(final String directoryName) throws CannotCompileException, IOException {
|
||||
tmplCtClass.writeFile(directoryName);
|
||||
}
|
||||
|
||||
protected StringBuilder stringBuilder = null;
|
||||
|
||||
protected void resetStringBuilder() {
|
||||
@ -184,7 +186,7 @@ public abstract class BuildContextBase<T extends IFieldEntry> {
|
||||
}
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
|
||||
protected String primitivePackName(Class<?> type) {
|
||||
if(type == boolean.class) {
|
||||
return "packBoolean";
|
||||
@ -241,4 +243,43 @@ public abstract class BuildContextBase<T extends IFieldEntry> {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected void write(final String className, final String directoryName) {
|
||||
try {
|
||||
reset(className, true);
|
||||
buildClass();
|
||||
buildConstructor();
|
||||
buildMethodInit();
|
||||
buildPackMethod();
|
||||
buildUnpackMethod();
|
||||
buildConvertMethod();
|
||||
saveClass(directoryName);
|
||||
} catch (Exception e) {
|
||||
String code = getBuiltString();
|
||||
if(code != null) {
|
||||
LOG.error("builder: " + code, e);
|
||||
throw new TemplateBuildException("Cannot compile: " + code, e);
|
||||
} else {
|
||||
throw new TemplateBuildException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected Template load(final String className) {
|
||||
String tmplName = className + "_$$_Template";
|
||||
try {
|
||||
Class<?> tmplClass = getClass().getClassLoader().loadClass(tmplName);
|
||||
return buildInstance(tmplClass);
|
||||
} catch (ClassNotFoundException e) {
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
String code = getBuiltString();
|
||||
if(code != null) {
|
||||
LOG.error("builder: " + code, e);
|
||||
throw new TemplateBuildException("Cannot compile: " + code, e);
|
||||
} else {
|
||||
throw new TemplateBuildException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -15,9 +15,8 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template.javassist;
|
||||
package org.msgpack.template.builder;
|
||||
|
||||
import org.msgpack.template.JavassistTemplateBuilder;
|
||||
|
||||
public interface BuildContextFactory {
|
||||
|
@ -25,20 +25,9 @@ import java.lang.reflect.Type;
|
||||
*
|
||||
*/
|
||||
public interface BuilderSelector {
|
||||
|
||||
|
||||
/**
|
||||
* Name of this.
|
||||
* @return
|
||||
*/
|
||||
public String getName();
|
||||
|
||||
|
||||
public abstract boolean matchType(Type targetType);
|
||||
|
||||
|
||||
public abstract TemplateBuilder getTemplateBuilder(Type targetType);
|
||||
|
||||
|
||||
String getName();
|
||||
|
||||
boolean matchType(Type targetType);
|
||||
|
||||
TemplateBuilder getTemplateBuilder(Type targetType);
|
||||
}
|
||||
|
@ -22,13 +22,6 @@ import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.msgpack.template.BeansFieldEntryReader;
|
||||
import org.msgpack.template.BeansReflectionTemplateBuilder;
|
||||
import org.msgpack.template.JavassistTemplateBuilder;
|
||||
import org.msgpack.template.ReflectionTemplateBuilder;
|
||||
import org.msgpack.template.javassist.BeansBuildContext;
|
||||
import org.msgpack.template.javassist.BuildContext;
|
||||
import org.msgpack.template.javassist.BuildContextBase;
|
||||
import org.msgpack.template.javassist.BuildContextFactory;
|
||||
|
||||
/**
|
||||
* Registry for BuilderSelectors.
|
||||
@ -38,8 +31,9 @@ import org.msgpack.template.javassist.BuildContextFactory;
|
||||
*
|
||||
*/
|
||||
public class BuilderSelectorRegistry {
|
||||
|
||||
|
||||
private static BuilderSelectorRegistry instance = new BuilderSelectorRegistry();
|
||||
|
||||
static{
|
||||
initForJava();
|
||||
}
|
||||
@ -50,26 +44,25 @@ public class BuilderSelectorRegistry {
|
||||
|
||||
TemplateBuilder forceBuilder;
|
||||
|
||||
|
||||
List<BuilderSelector> builderSelectors = new LinkedList<BuilderSelector>();
|
||||
|
||||
private BuilderSelectorRegistry(){
|
||||
}
|
||||
|
||||
/**
|
||||
* initialize BuilderSelectors for basic java enviroment.
|
||||
*/
|
||||
private static void initForJava(){
|
||||
|
||||
instance.append(new ArrayTemplateBuilderSelector());
|
||||
|
||||
if(isSupportJavassist()){
|
||||
instance.append(
|
||||
new MessagePackMessageBuilderSelector(
|
||||
new AnnotationTemplateBuilderSelector(
|
||||
new JavassistTemplateBuilder()));
|
||||
instance.forceBuilder = new JavassistTemplateBuilder();
|
||||
|
||||
//Java beans
|
||||
instance.append(new MessagePackBeansBuilderSelector(
|
||||
instance.append(new BeansTemplateBuilderSelector(
|
||||
new JavassistTemplateBuilder(
|
||||
new BeansFieldEntryReader(),
|
||||
new BuildContextFactory() {
|
||||
@ -81,17 +74,17 @@ public class BuilderSelectorRegistry {
|
||||
)));
|
||||
}else{
|
||||
instance.append(
|
||||
new MessagePackMessageBuilderSelector(
|
||||
new AnnotationTemplateBuilderSelector(
|
||||
new ReflectionTemplateBuilder()));
|
||||
instance.forceBuilder = new ReflectionTemplateBuilder();
|
||||
|
||||
//Java beans
|
||||
instance.append(new MessagePackBeansBuilderSelector(
|
||||
new BeansReflectionTemplateBuilder()));
|
||||
instance.append(new BeansTemplateBuilderSelector(
|
||||
new BeansTemplateBuilder()));
|
||||
}
|
||||
|
||||
instance.append(new MessagePackOrdinalEnumBuilderSelector());
|
||||
instance.append(new EnumBuilderSelector());
|
||||
instance.append(new OrdinalEnumTemplateBuilderSelector());
|
||||
instance.append(new EnumTemplateBuilderSelector());
|
||||
}
|
||||
public static boolean isSupportJavassist(){
|
||||
try {
|
||||
|
@ -25,21 +25,19 @@ import org.msgpack.template.FieldOption;
|
||||
import org.msgpack.template.IFieldEntry;
|
||||
import org.msgpack.template.IFieldEntryReader;
|
||||
import org.msgpack.template.TemplateBuildException;
|
||||
import org.msgpack.template.javassist.BuildContextFactory;
|
||||
|
||||
public abstract class CustomTemplateBuilder extends TemplateBuilder {
|
||||
|
||||
public abstract class CustomTemplateBuilder implements TemplateBuilder {
|
||||
|
||||
public abstract IFieldEntryReader getFieldEntryReader();
|
||||
|
||||
|
||||
public abstract Template buildTemplate(Class<?> targetClass , IFieldEntry[] entries);
|
||||
|
||||
public Template buildTemplate(Class<?> targetClass ,FieldOption implicitOption ){
|
||||
|
||||
public Template buildTemplate(Class<?> targetClass, FieldOption implicitOption ){
|
||||
checkValidation(targetClass);
|
||||
return buildTemplate(targetClass,
|
||||
getFieldEntryReader().readFieldEntries(targetClass, implicitOption));
|
||||
}
|
||||
|
||||
public Template buildTemplate(Class<?> targetClass, FieldList flist) throws NoSuchFieldException {
|
||||
checkValidation(targetClass);
|
||||
return buildTemplate(targetClass, getFieldEntryReader().convertFieldEntries(targetClass, flist));
|
||||
@ -51,20 +49,29 @@ public abstract class CustomTemplateBuilder extends TemplateBuilder {
|
||||
IFieldEntryReader reader = getFieldEntryReader();
|
||||
FieldOption implicitOption = reader.readImplicitFieldOption(targetClass);
|
||||
checkValidation(targetClass);
|
||||
|
||||
IFieldEntry[] entries = reader.readFieldEntries(targetClass, implicitOption);
|
||||
|
||||
return buildTemplate(targetClass,entries);
|
||||
return buildTemplate(targetClass, entries);
|
||||
}
|
||||
private void checkValidation(Class<?> targetClass) {
|
||||
|
||||
protected void checkValidation(Class<?> targetClass) {
|
||||
if(targetClass.isInterface()) {
|
||||
throw new TemplateBuildException("cannot build template of interface");
|
||||
throw new TemplateBuildException("Cannot build template of interface");
|
||||
}
|
||||
if(targetClass.isArray()) {
|
||||
throw new TemplateBuildException("cannot build template of array class");
|
||||
throw new TemplateBuildException("Cannot build template of array class");
|
||||
}
|
||||
if(targetClass.isPrimitive()) {
|
||||
throw new TemplateBuildException("cannot build template of primitive type");
|
||||
throw new TemplateBuildException("Cannot build template of primitive type");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTemplate(Type targetType, String directoryName) {
|
||||
throw new UnsupportedOperationException(targetType.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Template loadTemplate(Type targetType) {
|
||||
return null;
|
||||
}
|
||||
}
|
@ -19,25 +19,24 @@ package org.msgpack.template.builder;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class EnumBuilderSelector implements BuilderSelector {
|
||||
public class EnumTemplateBuilderSelector implements BuilderSelector {
|
||||
|
||||
public static final String NAME = "EnumTemplateBuilder";
|
||||
|
||||
|
||||
OrdinalEnumTemplateBuilder builder = new OrdinalEnumTemplateBuilder();
|
||||
|
||||
@Override
|
||||
public String getName(){
|
||||
return NAME;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean matchType(Type targetType) {
|
||||
return ((Class<?>)targetType).isEnum();
|
||||
}
|
||||
|
||||
|
||||
OrdinalEnumTemplateBuilder builder = new OrdinalEnumTemplateBuilder();
|
||||
|
||||
@Override
|
||||
public TemplateBuilder getTemplateBuilder(Type targetType) {
|
||||
return builder;
|
||||
}
|
||||
|
||||
}
|
@ -15,9 +15,10 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
package org.msgpack.template.builder;
|
||||
|
||||
import java.lang.Thread;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import org.msgpack.*;
|
||||
|
||||
@ -28,10 +29,23 @@ import javassist.NotFoundException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.msgpack.template.builder.CustomTemplateBuilder;
|
||||
import org.msgpack.template.javassist.*;
|
||||
import org.msgpack.template.FieldEntryReader;
|
||||
import org.msgpack.template.FieldOption;
|
||||
import org.msgpack.template.IFieldEntry;
|
||||
import org.msgpack.template.IFieldEntryReader;
|
||||
import org.msgpack.template.TemplateRegistry;
|
||||
|
||||
public class JavassistTemplateBuilder extends CustomTemplateBuilder {
|
||||
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 Logger LOG = LoggerFactory.getLogger(JavassistTemplateBuilder.class);
|
||||
|
||||
private static JavassistTemplateBuilder instance;
|
||||
@ -47,27 +61,24 @@ public class JavassistTemplateBuilder extends CustomTemplateBuilder {
|
||||
getInstance().pool.appendClassPath(new LoaderClassPath(cl));
|
||||
}
|
||||
|
||||
|
||||
IFieldEntryReader reader = new FieldEntryReader();
|
||||
|
||||
|
||||
public void setFieldEntryReader(IFieldEntryReader reader){
|
||||
this.reader = reader;
|
||||
}
|
||||
|
||||
|
||||
BuildContextFactory buildContextFactory = new BuildContextFactory() {
|
||||
|
||||
@Override
|
||||
public BuildContextBase createBuildContext(JavassistTemplateBuilder builder) {
|
||||
|
||||
return new BuildContext(builder);
|
||||
}
|
||||
};
|
||||
|
||||
public void setBuildContextFactory(BuildContextFactory factory){
|
||||
this.buildContextFactory = factory;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public JavassistTemplateBuilder() {
|
||||
pool = new ClassPool();
|
||||
boolean appended = false;
|
||||
@ -94,6 +105,7 @@ public class JavassistTemplateBuilder extends CustomTemplateBuilder {
|
||||
pool.appendSystemPath();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace FieldEntryReader and BuilderContextFactory.
|
||||
* you can replace field entry rules and generated codes easily.
|
||||
@ -106,7 +118,6 @@ public class JavassistTemplateBuilder extends CustomTemplateBuilder {
|
||||
this.buildContextFactory = buildContextFactory;
|
||||
}
|
||||
|
||||
|
||||
protected ClassPool pool;
|
||||
|
||||
private int seqId = 0;
|
||||
@ -123,7 +134,6 @@ public class JavassistTemplateBuilder extends CustomTemplateBuilder {
|
||||
return seqId++;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Template buildTemplate(Class<?> targetClass, IFieldEntry[] entries) {
|
||||
// FIXME private / packagefields
|
||||
@ -134,10 +144,15 @@ public class JavassistTemplateBuilder extends CustomTemplateBuilder {
|
||||
// f.setAccessible(true);
|
||||
// }
|
||||
//}
|
||||
Template[] tmpls = toTemplate(entries);
|
||||
BuildContextBase bc = getBuildContextFacotry().createBuildContext(this);
|
||||
return bc.buildTemplate(targetClass, entries, tmpls);
|
||||
}
|
||||
|
||||
Template[] tmpls = new Template[entries.length];
|
||||
for(int i=0; i < entries.length; i++) {
|
||||
IFieldEntry e = entries[i];
|
||||
private static Template[] toTemplate(IFieldEntry[] from) {
|
||||
Template[] tmpls = new Template[from.length];
|
||||
for(int i=0; i < from.length; i++) {
|
||||
IFieldEntry e = from[i];
|
||||
if(!e.isAvailable()) {
|
||||
tmpls[i] = null;
|
||||
} else {
|
||||
@ -145,9 +160,7 @@ public class JavassistTemplateBuilder extends CustomTemplateBuilder {
|
||||
tmpls[i] = tmpl;
|
||||
}
|
||||
}
|
||||
|
||||
BuildContextBase bc = getBuildContextFacotry().createBuildContext(this);
|
||||
return bc.buildTemplate(targetClass, entries, tmpls);
|
||||
return tmpls;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -159,60 +172,36 @@ public class JavassistTemplateBuilder extends CustomTemplateBuilder {
|
||||
return buildContextFactory;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
static class JavassistOrdinalEnumTemplate extends ReflectionTemplateBuilder.ReflectionOrdinalEnumTemplate {
|
||||
JavassistOrdinalEnumTemplate(Enum<?>[] entries) {
|
||||
super(entries);
|
||||
}
|
||||
@Override
|
||||
public void writeTemplate(Type targetType, String directoryName) {
|
||||
Class<?> targetClass = (Class<?>)targetType;
|
||||
IFieldEntryReader reader = getFieldEntryReader();
|
||||
FieldOption implicitOption = reader.readImplicitFieldOption(targetClass);
|
||||
checkValidation(targetClass);
|
||||
IFieldEntry[] entries = reader.readFieldEntries(targetClass, implicitOption);
|
||||
writeTemplate(targetClass, entries, directoryName);
|
||||
}
|
||||
|
||||
private void writeTemplate(Class<?> targetClass, IFieldEntry[] entries, String directoryName) {
|
||||
Template[] tmpls = toTemplate(entries);
|
||||
BuildContextBase bc = getBuildContextFacotry().createBuildContext(this);
|
||||
bc.writeTemplate(targetClass, entries, tmpls, directoryName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeOrdinalEnumTemplateClass(Class<?> targetClass, Enum<?>[] entires, String directoryName) {
|
||||
throw new UnsupportedOperationException("not supported yet.");// TODO
|
||||
public Template loadTemplate(Type targetType) {
|
||||
Class<?> targetClass = (Class<?>)targetType;
|
||||
IFieldEntryReader reader = getFieldEntryReader();
|
||||
FieldOption implicitOption = reader.readImplicitFieldOption(targetClass);
|
||||
checkValidation(targetClass);
|
||||
IFieldEntry[] entries = reader.readFieldEntries(targetClass, implicitOption);
|
||||
return loadTemplate(targetClass, entries);
|
||||
}
|
||||
|
||||
public Template buildOrdinalEnumTemplate(Class<?> targetClass, Enum<?>[] entries) {
|
||||
return new JavassistOrdinalEnumTemplate(entries);
|
||||
private Template loadTemplate(Class<?> targetClass, IFieldEntry[] entries) {
|
||||
Template[] tmpls = toTemplate(entries);
|
||||
BuildContextBase bc = getBuildContextFacotry().createBuildContext(this);
|
||||
return bc.loadTemplate(targetClass, entries, tmpls);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeArrayTemplateClass(Type arrayType, Type genericBaseType,
|
||||
Class<?> baseClass, int dim, String directoryName) {
|
||||
throw new UnsupportedOperationException("not supported yet.");//TODO
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ import org.msgpack.Template;
|
||||
import org.msgpack.Unpacker;
|
||||
import org.msgpack.template.TemplateBuildException;
|
||||
|
||||
public class OrdinalEnumTemplateBuilder extends TemplateBuilder{
|
||||
public class OrdinalEnumTemplateBuilder implements TemplateBuilder{
|
||||
|
||||
static class ReflectionOrdinalEnumTemplate extends AbstractTemplate {
|
||||
protected Enum<?>[] entries;
|
||||
@ -68,6 +68,7 @@ public class OrdinalEnumTemplateBuilder extends TemplateBuilder{
|
||||
return entries[ord];
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Template buildTemplate(Type targetType) {
|
||||
Class<?> targetClass = (Class<?>)targetType;
|
||||
@ -76,10 +77,20 @@ public class OrdinalEnumTemplateBuilder extends TemplateBuilder{
|
||||
|
||||
return new ReflectionOrdinalEnumTemplate(entries);
|
||||
}
|
||||
|
||||
private void checkOrdinalEnumValidation(Class<?> targetClass) {
|
||||
if(!targetClass.isEnum()) {
|
||||
throw new TemplateBuildException("tried to build ordinal enum template of non-enum class");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTemplate(Type targetType, String directoryName) {
|
||||
throw new UnsupportedOperationException(targetType.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Template loadTemplate(Type targetType) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -17,35 +17,29 @@
|
||||
//
|
||||
package org.msgpack.template.builder;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import org.msgpack.annotation.MessagePackOrdinalEnum;
|
||||
|
||||
public class MessagePackOrdinalEnumBuilderSelector implements BuilderSelector {
|
||||
public class OrdinalEnumTemplateBuilderSelector implements BuilderSelector {
|
||||
|
||||
public static final String NAME = "MessagePackOrdinalEnumBuilderTemplate";
|
||||
|
||||
public static final String NAME = "OrdinalEnumTemplateBuilder";
|
||||
|
||||
OrdinalEnumTemplateBuilder builder = new OrdinalEnumTemplateBuilder();
|
||||
|
||||
@Override
|
||||
public String getName(){
|
||||
return NAME;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean matchType(Type targetType) {
|
||||
Class<?> target = (Class<?>)targetType;
|
||||
return isAnnotated(target, MessagePackOrdinalEnum.class);
|
||||
Class<?> targetClass = (Class<?>)targetType;
|
||||
return AnnotationTemplateBuilderSelector.isAnnotated(targetClass, MessagePackOrdinalEnum.class);
|
||||
}
|
||||
|
||||
OrdinalEnumTemplateBuilder builder = new OrdinalEnumTemplateBuilder();
|
||||
|
||||
@Override
|
||||
public TemplateBuilder getTemplateBuilder(Type targetType) {
|
||||
return builder;
|
||||
}
|
||||
|
||||
|
||||
private boolean isAnnotated(Class<?> ao, Class<? extends Annotation> with) {
|
||||
return ao.getAnnotation(with) != null;
|
||||
}
|
||||
|
||||
}
|
@ -15,14 +15,23 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
package org.msgpack.template;
|
||||
package org.msgpack.template.builder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
import org.msgpack.*;
|
||||
import org.msgpack.template.builder.CustomTemplateBuilder;
|
||||
import org.msgpack.AbstractTemplate;
|
||||
import org.msgpack.MessagePackObject;
|
||||
import org.msgpack.MessageTypeException;
|
||||
import org.msgpack.Packer;
|
||||
import org.msgpack.Template;
|
||||
import org.msgpack.Unpacker;
|
||||
import org.msgpack.template.FieldEntry;
|
||||
import org.msgpack.template.FieldEntryReader;
|
||||
import org.msgpack.template.IFieldEntry;
|
||||
import org.msgpack.template.IFieldEntryReader;
|
||||
import org.msgpack.template.TemplateRegistry;
|
||||
|
||||
public class ReflectionTemplateBuilder extends CustomTemplateBuilder {
|
||||
IFieldEntryReader reader = new FieldEntryReader();
|
||||
@ -408,7 +417,6 @@ public class ReflectionTemplateBuilder extends CustomTemplateBuilder {
|
||||
res[i] = new ObjectFieldEntry(e, tmpl);
|
||||
}
|
||||
}
|
||||
|
||||
return new ReflectionTemplate(targetClass, res);
|
||||
}
|
||||
}
|
@ -17,392 +17,15 @@
|
||||
//
|
||||
package org.msgpack.template.builder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.*;
|
||||
import java.lang.annotation.*;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.EnumSet;
|
||||
import org.msgpack.*;
|
||||
import org.msgpack.annotation.*;
|
||||
import org.msgpack.template.FieldList;
|
||||
import org.msgpack.template.FieldOption;
|
||||
import org.msgpack.template.IFieldEntry;
|
||||
import org.msgpack.template.IFieldEntryReader;
|
||||
import org.msgpack.template.JavassistTemplateBuilder;
|
||||
import org.msgpack.template.ReflectionTemplateBuilder;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public abstract class TemplateBuilder {
|
||||
import org.msgpack.Template;
|
||||
|
||||
public abstract Template buildTemplate(Type targetType);
|
||||
/*
|
||||
// Override this method
|
||||
<<<<<<< HEAD:java/src/main/java/org/msgpack/template/TemplateBuilder.java
|
||||
public abstract Class<?> loadTemplateClass(Class<?> targetClass);
|
||||
public interface TemplateBuilder {
|
||||
Template buildTemplate(Type targetType);
|
||||
|
||||
// Override this method
|
||||
public abstract Template initializeTemplate(Class<?> targetClass, Class<?> tmplClass, FieldEntry[] entries);
|
||||
void writeTemplate(Type targetType, String directoryName);
|
||||
|
||||
// Override this method
|
||||
public abstract void writeTemplateClass(Class<?> targetClass, FieldEntry[] entries, String directoryName);
|
||||
|
||||
// Override this method
|
||||
public abstract Template buildTemplate(Class<?> targetClass, FieldEntry[] entries);
|
||||
=======
|
||||
public abstract Template buildTemplate(Class<?> targetClass, IFieldEntry[] entries);
|
||||
>>>>>>> 21f0d0bfc47ddc6d9092621705047f3bef385ba5:java/src/main/java/org/msgpack/template/builder/TemplateBuilder.java
|
||||
|
||||
// Override this method
|
||||
public abstract void writeOrdinalEnumTemplateClass(Class<?> targetClass, Enum<?>[] entires, String directoryName);
|
||||
|
||||
// Override this method
|
||||
public abstract Template buildOrdinalEnumTemplate(Class<?> targetClass, Enum<?>[] entries);
|
||||
|
||||
// Override this method
|
||||
public abstract void writeArrayTemplateClass(Type arrayType, Type genericBaseType,
|
||||
Class<?> baseClass, int dim, String directoryName);
|
||||
|
||||
// Override this method
|
||||
public abstract Template buildArrayTemplate(Type arrayType, Type genericBaseType, Class<?> baseClass, int dim);
|
||||
<<<<<<< HEAD:java/src/main/java/org/msgpack/template/TemplateBuilder.java
|
||||
|
||||
public Template initializeTemplate(Class<?> targetClass, Class<?> tmplClass) {
|
||||
return initializeTemplate(targetClass, tmplClass, readFieldEntries(targetClass, readImplicitFieldOption(targetClass)));
|
||||
}
|
||||
|
||||
public void writeTemplateClass(Class<?> targetClass, FieldList fList, String directoryName) throws NoSuchFieldException {
|
||||
checkValidation(targetClass);
|
||||
writeTemplateClass(targetClass, convertFieldEntries(targetClass, fList), directoryName);
|
||||
}
|
||||
=======
|
||||
|
||||
public abstract IFieldEntryReader getFieldEntryReader();
|
||||
>>>>>>> 21f0d0bfc47ddc6d9092621705047f3bef385ba5:java/src/main/java/org/msgpack/template/builder/TemplateBuilder.java
|
||||
|
||||
public Template buildTemplate(Class<?> targetClass, FieldList flist) throws NoSuchFieldException {
|
||||
checkValidation(targetClass);
|
||||
return buildTemplate(targetClass, getFieldEntryReader().convertFieldEntries(targetClass, flist));
|
||||
}
|
||||
|
||||
public void writeTemplateClass(Class<?> targetClass, FieldOption implicitOption, String directoryName) {
|
||||
checkValidation(targetClass);
|
||||
writeTemplateClass(targetClass, readFieldEntries(targetClass, implicitOption), directoryName);
|
||||
}
|
||||
|
||||
public Template buildTemplate(Class<?> targetClass, FieldOption implicitOption) {
|
||||
checkValidation(targetClass);
|
||||
return buildTemplate(targetClass, getFieldEntryReader().readFieldEntries(targetClass, implicitOption));
|
||||
}
|
||||
|
||||
public void writeTemplateClass(Class<?> targetClass, final String directoryName) {
|
||||
FieldOption implicitOption = readImplicitFieldOption(targetClass);
|
||||
writeTemplateClass(targetClass, implicitOption, directoryName);
|
||||
}
|
||||
|
||||
public Template buildTemplate(Class<?> targetClass) {
|
||||
FieldOption implicitOption = getFieldEntryReader().readImplicitFieldOption(targetClass);
|
||||
return buildTemplate(targetClass, implicitOption);
|
||||
}
|
||||
|
||||
public void writeOrdinalEnumTemplateClass(Class<?> targetClass, String directoryName) {
|
||||
checkOrdinalEnumValidation(targetClass);
|
||||
Enum<?>[] entries = (Enum<?>[])targetClass.getEnumConstants();
|
||||
writeOrdinalEnumTemplateClass(targetClass, entries, directoryName);
|
||||
}
|
||||
|
||||
public Template buildOrdinalEnumTemplate(Class<?> targetClass) {
|
||||
checkOrdinalEnumValidation(targetClass);
|
||||
Enum<?>[] entries = (Enum<?>[])targetClass.getEnumConstants();
|
||||
return buildOrdinalEnumTemplate(targetClass, entries);
|
||||
}
|
||||
|
||||
public void writeArrayTemplateClass(Type arrayType, String directoryName) {
|
||||
throw new UnsupportedOperationException("not supported yet.");// TODO
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
if(baseType instanceof ParameterizedType) {
|
||||
baseClass = (Class<?>)((ParameterizedType)baseType).getRawType();
|
||||
} else {
|
||||
baseClass = (Class<?>)baseType;
|
||||
}
|
||||
} 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 void checkValidation(Class<?> targetClass) {
|
||||
if(targetClass.isInterface()) {
|
||||
throw new TemplateBuildException("cannot build template of interface");
|
||||
}
|
||||
if(targetClass.isArray()) {
|
||||
throw new TemplateBuildException("cannot build template of array class");
|
||||
}
|
||||
if(targetClass.isPrimitive()) {
|
||||
throw new TemplateBuildException("cannot build template of primitive type");
|
||||
}
|
||||
}
|
||||
private void checkOrdinalEnumValidation(Class<?> targetClass) {
|
||||
if(!targetClass.isEnum()) {
|
||||
throw new TemplateBuildException("tried to build ordinal enum template of non-enum class");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static TemplateBuilder instance;
|
||||
static {
|
||||
instance = selectDefaultTemplateBuilder();
|
||||
}
|
||||
|
||||
private static TemplateBuilder selectDefaultTemplateBuilder() {
|
||||
try {
|
||||
// FIXME JavassistTemplateBuilder doesn't work on DalvikVM
|
||||
if(System.getProperty("java.vm.name").equals("Dalvik")) {
|
||||
return ReflectionTemplateBuilder.getInstance();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
return JavassistTemplateBuilder.getInstance();
|
||||
}
|
||||
|
||||
public synchronized static void setInstance(TemplateBuilder builder) {
|
||||
instance = builder;
|
||||
}
|
||||
|
||||
public static Class<?> load(Class<?> targetClass) {
|
||||
return instance.loadTemplateClass(targetClass);
|
||||
}
|
||||
|
||||
public static Template initialize(Class<?> targetClass, Class<?> tmplClass) {
|
||||
return instance.initializeTemplate(targetClass, tmplClass);
|
||||
}
|
||||
|
||||
public static void writeClass(Class<?> targetClass, String directoryName) {
|
||||
instance.writeTemplateClass(targetClass, directoryName);
|
||||
}
|
||||
|
||||
public static Template build(Class<?> targetClass) {
|
||||
return instance.buildTemplate(targetClass);
|
||||
}
|
||||
|
||||
public static void writeClass(Class<?> targetClass, FieldOption implicitOption, String directoryName) {
|
||||
instance.writeTemplateClass(targetClass, implicitOption, directoryName);
|
||||
}
|
||||
|
||||
public static Template build(Class<?> targetClass, FieldOption implicitOption) {
|
||||
return instance.buildTemplate(targetClass, implicitOption);
|
||||
}
|
||||
|
||||
public static void writeClass(Class<?> targetClass, FieldList fList, String directoryName)
|
||||
throws NoSuchFieldException {
|
||||
instance.writeTemplateClass(targetClass, fList, directoryName);
|
||||
}
|
||||
|
||||
public static Template build(Class<?> targetClass, FieldList fList) throws NoSuchFieldException {
|
||||
return instance.buildTemplate(targetClass, fList);
|
||||
}
|
||||
|
||||
public static void writeOrdinalEnumClass(Class<?> targetClass, String directoryName) {
|
||||
instance.writeOrdinalEnumTemplateClass(targetClass, directoryName);
|
||||
}
|
||||
|
||||
public static Template buildOrdinalEnum(Class<?> targetClass) {
|
||||
return instance.buildOrdinalEnumTemplate(targetClass);
|
||||
}
|
||||
|
||||
public static void writeArrayClass(Type arrayType, String directoryName) {
|
||||
throw new UnsupportedOperationException("not supported yet.");// TODO
|
||||
}
|
||||
|
||||
public static Template buildArray(Type arrayType) {
|
||||
return instance.buildArrayTemplate(arrayType);
|
||||
}*/
|
||||
|
||||
/*
|
||||
private static void checkValidation(Class<?> targetClass) {
|
||||
if(targetClass.isInterface()) {
|
||||
throw new TemplateBuildException("cannot build template of interface");
|
||||
}
|
||||
if(targetClass.isArray()) {
|
||||
throw new TemplateBuildException("cannot build template of array class");
|
||||
}
|
||||
if(targetClass.isPrimitive()) {
|
||||
throw new TemplateBuildException("cannot build template of primitive type");
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkOrdinalEnumValidation(Class<?> targetClass) {
|
||||
if(!targetClass.isEnum()) {
|
||||
throw new TemplateBuildException("tried to build ordinal enum template of non-enum class");
|
||||
}
|
||||
}*/
|
||||
|
||||
/*
|
||||
static IFieldEntry[] convertFieldEntries(Class<?> targetClass, FieldList flist) throws NoSuchFieldException {
|
||||
List<FieldList.Entry> src = flist.getList();
|
||||
FieldEntry[] result = new FieldEntry[src.size()];
|
||||
for(int i=0; i < src.size(); i++) {
|
||||
FieldList.Entry s = src.get(i);
|
||||
if(s.isAvailable()) {
|
||||
result[i] = new FieldEntry(targetClass.getDeclaredField(s.getName()), s.getOption());
|
||||
} else {
|
||||
result[i] = new FieldEntry();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}*/
|
||||
|
||||
/*static IFieldEntry[] readFieldEntries(Class<?> targetClass, FieldOption implicitOption) {
|
||||
Field[] allFields = readAllFields(targetClass);
|
||||
|
||||
/* index:
|
||||
* @Index(0) int field_a; // 0
|
||||
* int field_b; // 1
|
||||
* @Index(3) int field_c; // 3
|
||||
* int field_d; // 4
|
||||
* @Index(2) int field_e; // 2
|
||||
* int field_f; // 5
|
||||
*//*
|
||||
List<FieldEntry> indexed = new ArrayList<FieldEntry>();
|
||||
int maxIndex = -1;
|
||||
for(Field f : allFields) {
|
||||
FieldOption opt = readFieldOption(f, implicitOption);
|
||||
if(opt == FieldOption.IGNORE) {
|
||||
// skip
|
||||
continue;
|
||||
}
|
||||
|
||||
int index = readFieldIndex(f, maxIndex);
|
||||
|
||||
if(indexed.size() > index && indexed.get(index) != null) {
|
||||
throw new TemplateBuildException("duplicated index: "+index);
|
||||
}
|
||||
if(index < 0) {
|
||||
throw new TemplateBuildException("invalid index: "+index);
|
||||
}
|
||||
|
||||
while(indexed.size() <= index) {
|
||||
indexed.add(null);
|
||||
}
|
||||
indexed.set(index, new FieldEntry(f, opt));
|
||||
|
||||
if(maxIndex < index) {
|
||||
maxIndex = index;
|
||||
}
|
||||
}
|
||||
|
||||
FieldEntry[] result = new FieldEntry[maxIndex+1];
|
||||
for(int i=0; i < indexed.size(); i++) {
|
||||
FieldEntry e = indexed.get(i);
|
||||
if(e == null) {
|
||||
result[i] = new FieldEntry();
|
||||
} else {
|
||||
result[i] = e;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}*/
|
||||
/*
|
||||
private static Field[] readAllFields(Class<?> targetClass) {
|
||||
// order: [fields of super class, ..., fields of this class]
|
||||
List<Field[]> succ = new ArrayList<Field[]>();
|
||||
int total = 0;
|
||||
for(Class<?> c = targetClass; c != Object.class; c = c.getSuperclass()) {
|
||||
Field[] fields = c.getDeclaredFields();
|
||||
total += fields.length;
|
||||
succ.add(fields);
|
||||
}
|
||||
Field[] result = new Field[total];
|
||||
int off = 0;
|
||||
for(int i=succ.size()-1; i >= 0; i--) {
|
||||
Field[] fields = succ.get(i);
|
||||
System.arraycopy(fields, 0, result, off, fields.length);
|
||||
off += fields.length;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static FieldOption readImplicitFieldOption(Class<?> targetClass) {
|
||||
MessagePackMessage a = targetClass.getAnnotation(MessagePackMessage.class);
|
||||
if(a == null) {
|
||||
return FieldOption.DEFAULT;
|
||||
}
|
||||
return a.value();
|
||||
}
|
||||
|
||||
private static FieldOption readFieldOption(Field field, FieldOption implicitOption) {
|
||||
int mod = field.getModifiers();
|
||||
if(Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
|
||||
return FieldOption.IGNORE;
|
||||
}
|
||||
|
||||
if(isAnnotated(field, Ignore.class)) {
|
||||
return FieldOption.IGNORE;
|
||||
} else if(isAnnotated(field, Required.class)) {
|
||||
return FieldOption.REQUIRED;
|
||||
} else if(isAnnotated(field, Optional.class)) {
|
||||
return FieldOption.OPTIONAL;
|
||||
} else if(isAnnotated(field, Nullable.class)) {
|
||||
if(field.getDeclaringClass().isPrimitive()) {
|
||||
return FieldOption.REQUIRED;
|
||||
} else {
|
||||
return FieldOption.NULLABLE;
|
||||
}
|
||||
}
|
||||
|
||||
if(implicitOption != FieldOption.DEFAULT) {
|
||||
return implicitOption;
|
||||
}
|
||||
|
||||
// default mode:
|
||||
// transient : Ignore
|
||||
// public : Required
|
||||
// others : Ignore
|
||||
if(Modifier.isTransient(mod)) {
|
||||
return FieldOption.IGNORE;
|
||||
} else if(Modifier.isPublic(mod)) {
|
||||
return FieldOption.REQUIRED;
|
||||
} else {
|
||||
return FieldOption.IGNORE;
|
||||
}
|
||||
}
|
||||
|
||||
private static int readFieldIndex(Field field, int maxIndex) {
|
||||
Index a = field.getAnnotation(Index.class);
|
||||
if(a == null) {
|
||||
return maxIndex + 1;
|
||||
} else {
|
||||
return a.value();
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isAnnotated(AccessibleObject ao, Class<? extends Annotation> with) {
|
||||
return ao.getAnnotation(with) != null;
|
||||
}*/
|
||||
Template loadTemplate(Type targetType);
|
||||
}
|
||||
|
||||
|
@ -1,52 +0,0 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2011 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.javassist;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.lang.Thread;
|
||||
|
||||
import org.msgpack.*;
|
||||
import org.msgpack.template.*;
|
||||
|
||||
import javassist.CannotCompileException;
|
||||
import javassist.ClassPool;
|
||||
import javassist.CtClass;
|
||||
import javassist.CtConstructor;
|
||||
import javassist.CtMethod;
|
||||
import javassist.CtNewConstructor;
|
||||
import javassist.CtNewMethod;
|
||||
import javassist.LoaderClassPath;
|
||||
import javassist.NotFoundException;
|
||||
import javassist.ClassClassPath;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public abstract class JavassistTemplate extends AbstractTemplate {
|
||||
public Class<?> targetClass;
|
||||
public Template[] templates;
|
||||
|
||||
public JavassistTemplate(Class<?> targetClass, Template[] templates) {
|
||||
this.targetClass = targetClass;
|
||||
this.templates = templates;
|
||||
}
|
||||
}
|
78
java/src/main/java/org/msgpack/util/TemplatePrecompiler.java
Normal file
78
java/src/main/java/org/msgpack/util/TemplatePrecompiler.java
Normal file
@ -0,0 +1,78 @@
|
||||
//
|
||||
// MessagePack for Java
|
||||
//
|
||||
// Copyright (C) 2009-2011 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.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.msgpack.template.builder.BuilderSelectorRegistry;
|
||||
import org.msgpack.template.builder.JavassistTemplateBuilder;
|
||||
import org.msgpack.template.builder.TemplateBuilder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class TemplatePrecompiler {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(TemplatePrecompiler.class);
|
||||
|
||||
//public static final String SRC = "msgpack.template.srcdir";
|
||||
|
||||
public static final String DIST = "msgpack.template.distdir";
|
||||
|
||||
//public static final String DEFAULT_SRC = ".";
|
||||
|
||||
public static final String DEFAULT_DIST = ".";
|
||||
|
||||
private static TemplatePrecompiler INSTANCE = null;
|
||||
|
||||
private TemplatePrecompiler() {
|
||||
}
|
||||
|
||||
public static void saveTemplates(final String[] classFileNames) throws IOException {
|
||||
throw new UnsupportedOperationException("Not supported yet.");// TODO
|
||||
}
|
||||
|
||||
public static void saveTemplateClass(Class<?> targetClass) throws IOException {
|
||||
if (INSTANCE != null) {
|
||||
INSTANCE = new TemplatePrecompiler();
|
||||
}
|
||||
LOG.info("Saving template of " + targetClass.getName() + "...");
|
||||
Properties props = System.getProperties();
|
||||
String distDirName = getDirName(props, DIST, DEFAULT_DIST);
|
||||
if (targetClass.isEnum()) {
|
||||
throw new UnsupportedOperationException("Enum not supported yet: " + targetClass.getName());
|
||||
} else {
|
||||
new JavassistTemplateBuilder().writeTemplate(targetClass, distDirName);
|
||||
}
|
||||
LOG.info("Saved .class file of template class of " + targetClass.getName());
|
||||
}
|
||||
|
||||
private static String getDirName(Properties props, String dirName, String defaultDirName)
|
||||
throws IOException {
|
||||
String dName = props.getProperty(dirName, defaultDirName);
|
||||
File d = new File(dName);
|
||||
if (!d.isDirectory() && !d.exists()) {
|
||||
throw new IOException("Directory not exists: " + dName);
|
||||
}
|
||||
return d.getAbsolutePath();
|
||||
}
|
||||
|
||||
public static void main(final String[] args) throws Exception {
|
||||
TemplatePrecompiler.saveTemplates(args);
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ package org.msgpack;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import java.math.BigInteger;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
@ -20,7 +21,7 @@ public class TestCases {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCases() throws Exception {
|
||||
public void testDynamicType() throws Exception {
|
||||
Unpacker pac = new Unpacker();
|
||||
Unpacker pac_compact = new Unpacker();
|
||||
|
||||
@ -28,13 +29,182 @@ public class TestCases {
|
||||
feedFile(pac_compact, "src/test/resources/cases_compact.mpac");
|
||||
|
||||
UnpackResult result = new UnpackResult();
|
||||
UnpackResult result_compact = new UnpackResult();
|
||||
while(pac.next(result)) {
|
||||
UnpackResult result_compact = new UnpackResult();
|
||||
assertTrue( pac_compact.next(result_compact) );
|
||||
assertTrue( result.getData().equals(result_compact.getData()) );
|
||||
}
|
||||
|
||||
assertFalse( pac_compact.next(result) );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDirectConversion() throws Exception {
|
||||
Unpacker pac = new Unpacker();
|
||||
Unpacker pac_compact = new Unpacker();
|
||||
|
||||
feedFile(pac, "src/test/resources/cases.mpac");
|
||||
feedFile(pac_compact, "src/test/resources/cases_compact.mpac");
|
||||
|
||||
UnpackResult result_compact = new UnpackResult();
|
||||
while(pac_compact.next(result_compact)) {
|
||||
MessagePackObject obj = result_compact.getData();
|
||||
testDirectConversionRecursive(pac, obj);
|
||||
}
|
||||
|
||||
assertFalse( pac_compact.next(result_compact) );
|
||||
}
|
||||
|
||||
private void testDirectConversionRecursive(Unpacker pac, MessagePackObject obj) throws Exception {
|
||||
if(obj.isBooleanType()) {
|
||||
boolean expect = obj.asBoolean();
|
||||
boolean actual = pac.unpackBoolean();
|
||||
assertEquals(expect, actual);
|
||||
|
||||
} else if(obj.isIntegerType()) {
|
||||
BigInteger expect = obj.asBigInteger();
|
||||
if(BigInteger.valueOf((long)Byte.MIN_VALUE).compareTo(expect) <= 0 &&
|
||||
expect.compareTo(BigInteger.valueOf((long)Byte.MAX_VALUE)) <= 0) {
|
||||
byte actual = pac.unpackByte();
|
||||
assertEquals(expect.byteValue(), actual);
|
||||
} else if(BigInteger.valueOf((long)Short.MIN_VALUE).compareTo(expect) <= 0 &&
|
||||
expect.compareTo(BigInteger.valueOf((long)Short.MAX_VALUE)) <= 0) {
|
||||
short actual = pac.unpackShort();
|
||||
assertEquals(expect.shortValue(), actual);
|
||||
} else if(BigInteger.valueOf((long)Integer.MIN_VALUE).compareTo(expect) <= 0 &&
|
||||
expect.compareTo(BigInteger.valueOf((long)Integer.MAX_VALUE)) <= 0) {
|
||||
int actual = pac.unpackInt();
|
||||
assertEquals(expect.intValue(), actual);
|
||||
} else if(BigInteger.valueOf(Long.MIN_VALUE).compareTo(expect) <= 0 &&
|
||||
expect.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) <= 0) {
|
||||
long actual = pac.unpackLong();
|
||||
assertEquals(expect.longValue(), actual);
|
||||
} else {
|
||||
BigInteger actual = pac.unpackBigInteger();
|
||||
assertEquals(expect, actual);
|
||||
}
|
||||
|
||||
} else if(obj.isFloatType()) {
|
||||
double expect = obj.asFloat();
|
||||
double actual = pac.unpackDouble();
|
||||
assertEquals(expect, actual, 0.01);
|
||||
|
||||
} else if(obj.isArrayType()) {
|
||||
MessagePackObject[] expect = obj.asArray();
|
||||
int length = pac.unpackArray();
|
||||
assertEquals(expect.length, length);
|
||||
for(int i=0; i < length; i++) {
|
||||
testDirectConversionRecursive(pac, expect[i]);
|
||||
}
|
||||
|
||||
} else if(obj.isMapType()) {
|
||||
Map<MessagePackObject, MessagePackObject> expect = obj.asMap();
|
||||
int size = pac.unpackMap();
|
||||
assertEquals(expect.size(), size);
|
||||
for(int i=0; i < size; i++) {
|
||||
MessagePackObject key = pac.unpackObject();
|
||||
MessagePackObject value = expect.get(key);
|
||||
assertNotNull(value);
|
||||
testDirectConversionRecursive(pac, value);
|
||||
}
|
||||
|
||||
} else if(obj.isRawType()) {
|
||||
byte[] expect = obj.asByteArray();
|
||||
int length = pac.unpackRaw();
|
||||
assertEquals(expect.length, length);
|
||||
byte[] actual = pac.unpackRawBody(length);
|
||||
assertTrue(Arrays.equals(expect, actual));
|
||||
|
||||
} else if(obj.isNil()) {
|
||||
pac.unpackNull();
|
||||
|
||||
} else {
|
||||
fail("unexpected object: "+obj);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIndirectConversion() throws Exception {
|
||||
Unpacker pac = new Unpacker();
|
||||
Unpacker pac_compact = new Unpacker();
|
||||
|
||||
feedFile(pac, "src/test/resources/cases.mpac");
|
||||
feedFile(pac_compact, "src/test/resources/cases_compact.mpac");
|
||||
|
||||
UnpackResult result = new UnpackResult();
|
||||
UnpackResult result_compact = new UnpackResult();
|
||||
while(pac.next(result)) {
|
||||
assertTrue( pac_compact.next(result_compact) );
|
||||
testIndirectConversionRecursive(result.getData(), result_compact.getData());
|
||||
}
|
||||
|
||||
assertFalse( pac_compact.next(result) );
|
||||
}
|
||||
|
||||
private void testIndirectConversionRecursive(MessagePackObject target, MessagePackObject obj) {
|
||||
if(obj.isBooleanType()) {
|
||||
boolean expect = obj.asBoolean();
|
||||
boolean actual = target.asBoolean();
|
||||
assertEquals(expect, actual);
|
||||
|
||||
} else if(obj.isIntegerType()) {
|
||||
BigInteger expect = obj.asBigInteger();
|
||||
if(BigInteger.valueOf((long)Byte.MIN_VALUE).compareTo(expect) <= 0 &&
|
||||
expect.compareTo(BigInteger.valueOf((long)Byte.MAX_VALUE)) <= 0) {
|
||||
byte actual = target.asByte();
|
||||
assertEquals(expect.byteValue(), actual);
|
||||
} else if(BigInteger.valueOf((long)Short.MIN_VALUE).compareTo(expect) <= 0 &&
|
||||
expect.compareTo(BigInteger.valueOf((long)Short.MAX_VALUE)) <= 0) {
|
||||
short actual = target.asShort();
|
||||
assertEquals(expect.shortValue(), actual);
|
||||
} else if(BigInteger.valueOf((long)Integer.MIN_VALUE).compareTo(expect) <= 0 &&
|
||||
expect.compareTo(BigInteger.valueOf((long)Integer.MAX_VALUE)) <= 0) {
|
||||
int actual = target.asInt();
|
||||
assertEquals(expect.intValue(), actual);
|
||||
} else if(BigInteger.valueOf(Long.MIN_VALUE).compareTo(expect) <= 0 &&
|
||||
expect.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) <= 0) {
|
||||
long actual = target.asLong();
|
||||
assertEquals(expect.longValue(), actual);
|
||||
} else {
|
||||
BigInteger actual = target.asBigInteger();
|
||||
assertEquals(expect, actual);
|
||||
}
|
||||
|
||||
} else if(obj.isFloatType()) {
|
||||
double expect = obj.asFloat();
|
||||
double actual = target.asDouble();
|
||||
assertEquals(expect, actual, 0.01);
|
||||
|
||||
} else if(obj.isArrayType()) {
|
||||
MessagePackObject[] expect = obj.asArray();
|
||||
MessagePackObject[] actual = target.asArray();
|
||||
assertEquals(expect.length, actual.length);
|
||||
for(int i=0; i < expect.length; i++) {
|
||||
testIndirectConversionRecursive(actual[i], expect[i]);
|
||||
}
|
||||
|
||||
} else if(obj.isMapType()) {
|
||||
Map<MessagePackObject, MessagePackObject> expect = obj.asMap();
|
||||
Map<MessagePackObject, MessagePackObject> actual = target.asMap();
|
||||
assertEquals(expect.size(), actual.size());
|
||||
for(Map.Entry<MessagePackObject,MessagePackObject> pair : expect.entrySet()) {
|
||||
MessagePackObject value = actual.get(pair.getKey());
|
||||
assertNotNull(value);
|
||||
testIndirectConversionRecursive(value, pair.getValue());
|
||||
}
|
||||
|
||||
} else if(obj.isRawType()) {
|
||||
byte[] expect = obj.asByteArray();
|
||||
byte[] actual = obj.asByteArray();
|
||||
assertEquals(expect.length, actual.length);
|
||||
assertTrue(Arrays.equals(expect, actual));
|
||||
|
||||
} else if(obj.isNil()) {
|
||||
assertTrue(target.isNil());
|
||||
|
||||
} else {
|
||||
fail("unexpected object: "+obj);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -30,9 +30,11 @@ import org.msgpack.annotation.MessagePackMessage;
|
||||
import org.msgpack.annotation.MessagePackOrdinalEnum;
|
||||
import org.msgpack.annotation.Optional;
|
||||
import org.msgpack.template.TestTemplateBuilderPackConvert.SampleInterface;
|
||||
import org.msgpack.template.builder.BeansTemplateBuilder;
|
||||
import org.msgpack.template.builder.BuilderSelectorRegistry;
|
||||
import org.msgpack.template.builder.MessagePackBeansBuilderSelector;
|
||||
import org.msgpack.template.builder.MessagePackMessageBuilderSelector;
|
||||
import org.msgpack.template.builder.BeansTemplateBuilderSelector;
|
||||
import org.msgpack.template.builder.AnnotationTemplateBuilderSelector;
|
||||
import org.msgpack.template.builder.ReflectionTemplateBuilder;
|
||||
import org.msgpack.template.builder.TemplateBuilder;
|
||||
|
||||
import org.junit.Assert;
|
||||
@ -46,11 +48,11 @@ public class TestReflectionTemplateBuilderJavaBeansPackUnpack extends TestCase {
|
||||
BuilderSelectorRegistry instance = BuilderSelectorRegistry.getInstance();
|
||||
|
||||
instance.replace(
|
||||
new MessagePackMessageBuilderSelector(
|
||||
new AnnotationTemplateBuilderSelector(
|
||||
new ReflectionTemplateBuilder()));
|
||||
instance.setForceBuilder( new ReflectionTemplateBuilder());
|
||||
instance.replace(new MessagePackBeansBuilderSelector(
|
||||
new BeansReflectionTemplateBuilder()));
|
||||
instance.replace(new BeansTemplateBuilderSelector(
|
||||
new BeansTemplateBuilder()));
|
||||
|
||||
MessagePack.register(PrimitiveTypeFieldsClass.class);
|
||||
MessagePack.register(OptionalPrimitiveTypeFieldsClass.class);
|
||||
@ -824,7 +826,7 @@ public class TestReflectionTemplateBuilderJavaBeansPackUnpack extends TestCase {
|
||||
|
||||
SampleOptionalListTypes dst =
|
||||
MessagePack.unpack(raw, SampleOptionalListTypes.class);
|
||||
assertEquals(src.f0.size(), dst.f0.size());
|
||||
assertEquals(0, dst.f0.size());
|
||||
assertEquals(src.f1.size(), dst.f1.size());
|
||||
for (int i = 0; i < src.f1.size(); ++i) {
|
||||
assertEquals(src.f1.get(i), dst.f1.get(i));
|
||||
@ -1011,22 +1013,18 @@ public class TestReflectionTemplateBuilderJavaBeansPackUnpack extends TestCase {
|
||||
|
||||
SampleMapTypes dst =
|
||||
MessagePack.unpack(raw, SampleMapTypes.class);
|
||||
assertEquals(src.f0.size(), dst.f0.size());
|
||||
assertEquals(src.f1.size(), dst.f1.size());
|
||||
Iterator<Integer> srcf1 = src.f1.keySet().iterator();
|
||||
Iterator<Integer> dstf1 = dst.f1.keySet().iterator();
|
||||
while (srcf1.hasNext()) {
|
||||
Integer s1 = srcf1.next();
|
||||
Integer d1 = dstf1.next();
|
||||
assertEquals(s1, d1);
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(d1));
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(s1));
|
||||
}
|
||||
assertEquals(src.f2.size(), dst.f2.size());
|
||||
Iterator<String> srcf2 = src.f2.keySet().iterator();
|
||||
Iterator<String> dstf2 = dst.f2.keySet().iterator();
|
||||
while (srcf2.hasNext()) {
|
||||
String s2 = srcf2.next();
|
||||
String d2 = dstf2.next();
|
||||
assertEquals(s2, d2);
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(d2));
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(s2));
|
||||
}
|
||||
}
|
||||
|
||||
@ -1095,21 +1093,16 @@ public class TestReflectionTemplateBuilderJavaBeansPackUnpack extends TestCase {
|
||||
assertEquals(src.f0.size(), dst.f0.size());
|
||||
assertEquals(src.f1.size(), dst.f1.size());
|
||||
Iterator<Integer> srcf1 = src.f1.keySet().iterator();
|
||||
Iterator<Integer> dstf1 = dst.f1.keySet().iterator();
|
||||
while (srcf1.hasNext()) {
|
||||
Integer s1 = srcf1.next();
|
||||
Integer d1 = dstf1.next();
|
||||
assertEquals(s1, d1);
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(d1));
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(s1));
|
||||
}
|
||||
assertEquals(src.f2.size(), dst.f2.size());
|
||||
Iterator<String> srcf2 = src.f2.keySet().iterator();
|
||||
Iterator<String> dstf2 = dst.f2.keySet().iterator();
|
||||
while (srcf2.hasNext()) {
|
||||
String s2 = srcf2.next();
|
||||
String d2 = dstf2.next();
|
||||
assertEquals(s2, d2);
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(d2));
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(s2));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -24,9 +24,11 @@ import org.msgpack.Unpacker;
|
||||
import org.msgpack.annotation.MessagePackMessage;
|
||||
import org.msgpack.annotation.MessagePackOrdinalEnum;
|
||||
import org.msgpack.annotation.Optional;
|
||||
import org.msgpack.template.builder.BeansTemplateBuilder;
|
||||
import org.msgpack.template.builder.BuilderSelectorRegistry;
|
||||
import org.msgpack.template.builder.MessagePackBeansBuilderSelector;
|
||||
import org.msgpack.template.builder.MessagePackMessageBuilderSelector;
|
||||
import org.msgpack.template.builder.BeansTemplateBuilderSelector;
|
||||
import org.msgpack.template.builder.AnnotationTemplateBuilderSelector;
|
||||
import org.msgpack.template.builder.ReflectionTemplateBuilder;
|
||||
import org.msgpack.template.builder.TemplateBuilder;
|
||||
|
||||
import junit.framework.Assert;
|
||||
@ -38,11 +40,11 @@ public class TestReflectionTemplateBuilderPackConvert extends TestCase {
|
||||
BuilderSelectorRegistry instance = BuilderSelectorRegistry.getInstance();
|
||||
|
||||
instance.replace(
|
||||
new MessagePackMessageBuilderSelector(
|
||||
new AnnotationTemplateBuilderSelector(
|
||||
new ReflectionTemplateBuilder()));
|
||||
instance.setForceBuilder( new ReflectionTemplateBuilder());
|
||||
instance.replace(new MessagePackBeansBuilderSelector(
|
||||
new BeansReflectionTemplateBuilder()));
|
||||
instance.replace(new BeansTemplateBuilderSelector(
|
||||
new BeansTemplateBuilder()));
|
||||
|
||||
MessagePack.register(PrimitiveTypeFieldsClass.class);
|
||||
MessagePack.register(OptionalPrimitiveTypeFieldsClass.class);
|
||||
@ -610,22 +612,18 @@ public class TestReflectionTemplateBuilderPackConvert extends TestCase {
|
||||
|
||||
SampleMapTypes dst =
|
||||
MessagePack.unpack(raw).convert(SampleMapTypes.class);
|
||||
assertEquals(src.f0.size(), dst.f0.size());
|
||||
assertEquals(src.f1.size(), dst.f1.size());
|
||||
Iterator<Integer> srcf1 = src.f1.keySet().iterator();
|
||||
Iterator<Integer> dstf1 = dst.f1.keySet().iterator();
|
||||
while (srcf1.hasNext()) {
|
||||
Integer s1 = srcf1.next();
|
||||
Integer d1 = dstf1.next();
|
||||
assertEquals(s1, d1);
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(d1));
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(s1));
|
||||
}
|
||||
assertEquals(src.f2.size(), dst.f2.size());
|
||||
Iterator<String> srcf2 = src.f2.keySet().iterator();
|
||||
Iterator<String> dstf2 = dst.f2.keySet().iterator();
|
||||
while (srcf2.hasNext()) {
|
||||
String s2 = srcf2.next();
|
||||
String d2 = dstf2.next();
|
||||
assertEquals(s2, d2);
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(d2));
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(s2));
|
||||
}
|
||||
}
|
||||
|
||||
@ -666,24 +664,18 @@ public class TestReflectionTemplateBuilderPackConvert extends TestCase {
|
||||
|
||||
SampleOptionalMapTypes dst =
|
||||
MessagePack.unpack(raw).convert(SampleOptionalMapTypes.class);
|
||||
assertEquals(src.f0.size(), dst.f0.size());
|
||||
assertEquals(0, dst.f0.size());
|
||||
assertEquals(src.f1.size(), dst.f1.size());
|
||||
Iterator<Integer> srcf1 = src.f1.keySet().iterator();
|
||||
Iterator<Integer> dstf1 = dst.f1.keySet().iterator();
|
||||
while (srcf1.hasNext()) {
|
||||
Integer s1 = srcf1.next();
|
||||
Integer d1 = dstf1.next();
|
||||
assertEquals(s1, d1);
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(d1));
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(s1));
|
||||
}
|
||||
assertEquals(src.f2.size(), dst.f2.size());
|
||||
Iterator<String> srcf2 = src.f2.keySet().iterator();
|
||||
Iterator<String> dstf2 = dst.f2.keySet().iterator();
|
||||
while (srcf2.hasNext()) {
|
||||
String s2 = srcf2.next();
|
||||
String d2 = dstf2.next();
|
||||
assertEquals(s2, d2);
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(d2));
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(s2));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -25,9 +25,11 @@ import org.msgpack.annotation.MessagePackMessage;
|
||||
import org.msgpack.annotation.MessagePackOrdinalEnum;
|
||||
import org.msgpack.annotation.Optional;
|
||||
import org.msgpack.template.TestTemplateBuilderPackConvert.SampleInterface;
|
||||
import org.msgpack.template.builder.BeansTemplateBuilder;
|
||||
import org.msgpack.template.builder.BuilderSelectorRegistry;
|
||||
import org.msgpack.template.builder.MessagePackBeansBuilderSelector;
|
||||
import org.msgpack.template.builder.MessagePackMessageBuilderSelector;
|
||||
import org.msgpack.template.builder.BeansTemplateBuilderSelector;
|
||||
import org.msgpack.template.builder.AnnotationTemplateBuilderSelector;
|
||||
import org.msgpack.template.builder.ReflectionTemplateBuilder;
|
||||
import org.msgpack.template.builder.TemplateBuilder;
|
||||
|
||||
import junit.framework.Assert;
|
||||
@ -41,11 +43,11 @@ public class TestReflectionTemplateBuilderPackUnpack extends TestCase {
|
||||
BuilderSelectorRegistry instance = BuilderSelectorRegistry.getInstance();
|
||||
|
||||
instance.replace(
|
||||
new MessagePackMessageBuilderSelector(
|
||||
new AnnotationTemplateBuilderSelector(
|
||||
new ReflectionTemplateBuilder()));
|
||||
instance.setForceBuilder( new ReflectionTemplateBuilder());
|
||||
instance.replace(new MessagePackBeansBuilderSelector(
|
||||
new BeansReflectionTemplateBuilder()));
|
||||
instance.replace(new BeansTemplateBuilderSelector(
|
||||
new BeansTemplateBuilder()));
|
||||
|
||||
|
||||
MessagePack.register(PrimitiveTypeFieldsClass.class);
|
||||
@ -615,22 +617,18 @@ public class TestReflectionTemplateBuilderPackUnpack extends TestCase {
|
||||
|
||||
SampleMapTypes dst =
|
||||
MessagePack.unpack(raw, SampleMapTypes.class);
|
||||
assertEquals(0, dst.f0.size());
|
||||
assertEquals(src.f1.size(), dst.f1.size());
|
||||
Iterator<Integer> srcf1 = src.f1.keySet().iterator();
|
||||
Iterator<Integer> dstf1 = dst.f1.keySet().iterator();
|
||||
while (srcf1.hasNext()) {
|
||||
Integer s1 = srcf1.next();
|
||||
Integer d1 = dstf1.next();
|
||||
assertEquals(s1, d1);
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(d1));
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(s1));
|
||||
}
|
||||
assertEquals(src.f2.size(), dst.f2.size());
|
||||
Iterator<String> srcf2 = src.f2.keySet().iterator();
|
||||
Iterator<String> dstf2 = dst.f2.keySet().iterator();
|
||||
while (srcf2.hasNext()) {
|
||||
String s2 = srcf2.next();
|
||||
String d2 = dstf2.next();
|
||||
assertEquals(s2, d2);
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(d2));
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(s2));
|
||||
}
|
||||
}
|
||||
|
||||
@ -671,24 +669,18 @@ public class TestReflectionTemplateBuilderPackUnpack extends TestCase {
|
||||
|
||||
SampleOptionalMapTypes dst =
|
||||
MessagePack.unpack(raw, SampleOptionalMapTypes.class);
|
||||
assertEquals(src.f0.size(), dst.f0.size());
|
||||
assertEquals(0, dst.f0.size());
|
||||
assertEquals(src.f1.size(), dst.f1.size());
|
||||
Iterator<Integer> srcf1 = src.f1.keySet().iterator();
|
||||
Iterator<Integer> dstf1 = dst.f1.keySet().iterator();
|
||||
while (srcf1.hasNext()) {
|
||||
Integer s1 = srcf1.next();
|
||||
Integer d1 = dstf1.next();
|
||||
assertEquals(s1, d1);
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(d1));
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(s1));
|
||||
}
|
||||
assertEquals(src.f2.size(), dst.f2.size());
|
||||
Iterator<String> srcf2 = src.f2.keySet().iterator();
|
||||
Iterator<String> dstf2 = dst.f2.keySet().iterator();
|
||||
while (srcf2.hasNext()) {
|
||||
String s2 = srcf2.next();
|
||||
String d2 = dstf2.next();
|
||||
assertEquals(s2, d2);
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(d2));
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(s2));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1038,22 +1038,18 @@ public class TestTemplateBuilderJavaBeansPackUnpack extends TestCase {
|
||||
|
||||
SampleMapTypes dst =
|
||||
MessagePack.unpack(raw, SampleMapTypes.class);
|
||||
assertEquals(0, dst.f0.size());
|
||||
assertEquals(src.f1.size(), dst.f1.size());
|
||||
Iterator<Integer> srcf1 = src.f1.keySet().iterator();
|
||||
Iterator<Integer> dstf1 = dst.f1.keySet().iterator();
|
||||
while (srcf1.hasNext()) {
|
||||
Integer s1 = srcf1.next();
|
||||
Integer d1 = dstf1.next();
|
||||
assertEquals(s1, d1);
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(d1));
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(s1));
|
||||
}
|
||||
assertEquals(src.f2.size(), dst.f2.size());
|
||||
Iterator<String> srcf2 = src.f2.keySet().iterator();
|
||||
Iterator<String> dstf2 = dst.f2.keySet().iterator();
|
||||
while (srcf2.hasNext()) {
|
||||
String s2 = srcf2.next();
|
||||
String d2 = dstf2.next();
|
||||
assertEquals(s2, d2);
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(d2));
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(s2));
|
||||
}
|
||||
}
|
||||
|
||||
@ -1119,24 +1115,20 @@ public class TestTemplateBuilderJavaBeansPackUnpack extends TestCase {
|
||||
|
||||
SampleOptionalMapTypes dst =
|
||||
MessagePack.unpack(raw, SampleOptionalMapTypes.class);
|
||||
assertEquals(src.f0.size(), dst.f0.size());
|
||||
assertEquals(0, dst.f0.size());
|
||||
assertEquals(src.f1.size(), dst.f1.size());
|
||||
Iterator<Integer> srcf1 = src.f1.keySet().iterator();
|
||||
Iterator<Integer> dstf1 = dst.f1.keySet().iterator();
|
||||
while (srcf1.hasNext()) {
|
||||
Integer s1 = srcf1.next();
|
||||
Integer d1 = dstf1.next();
|
||||
assertEquals(s1, d1);
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(d1));
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(s1));
|
||||
}
|
||||
assertEquals(src.f2.size(), dst.f2.size());
|
||||
Iterator<String> srcf2 = src.f2.keySet().iterator();
|
||||
Iterator<String> dstf2 = dst.f2.keySet().iterator();
|
||||
while (srcf2.hasNext()) {
|
||||
String s2 = srcf2.next();
|
||||
String d2 = dstf2.next();
|
||||
assertEquals(s2, d2);
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(d2));
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(s2));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -598,22 +598,18 @@ public class TestTemplateBuilderPackConvert extends TestCase {
|
||||
|
||||
SampleMapTypes dst =
|
||||
MessagePack.unpack(raw).convert(SampleMapTypes.class);
|
||||
assertEquals(0, dst.f0.size());
|
||||
assertEquals(src.f1.size(), dst.f1.size());
|
||||
Iterator<Integer> srcf1 = src.f1.keySet().iterator();
|
||||
Iterator<Integer> dstf1 = dst.f1.keySet().iterator();
|
||||
while (srcf1.hasNext()) {
|
||||
Integer s1 = srcf1.next();
|
||||
Integer d1 = dstf1.next();
|
||||
assertEquals(s1, d1);
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(d1));
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(s1));
|
||||
}
|
||||
assertEquals(src.f2.size(), dst.f2.size());
|
||||
Iterator<String> srcf2 = src.f2.keySet().iterator();
|
||||
Iterator<String> dstf2 = dst.f2.keySet().iterator();
|
||||
while (srcf2.hasNext()) {
|
||||
String s2 = srcf2.next();
|
||||
String d2 = dstf2.next();
|
||||
assertEquals(s2, d2);
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(d2));
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(s2));
|
||||
}
|
||||
}
|
||||
|
||||
@ -654,24 +650,18 @@ public class TestTemplateBuilderPackConvert extends TestCase {
|
||||
|
||||
SampleOptionalMapTypes dst =
|
||||
MessagePack.unpack(raw).convert(SampleOptionalMapTypes.class);
|
||||
assertEquals(src.f0.size(), dst.f0.size());
|
||||
assertEquals(0, dst.f0.size());
|
||||
assertEquals(src.f1.size(), dst.f1.size());
|
||||
Iterator<Integer> srcf1 = src.f1.keySet().iterator();
|
||||
Iterator<Integer> dstf1 = dst.f1.keySet().iterator();
|
||||
while (srcf1.hasNext()) {
|
||||
Integer s1 = srcf1.next();
|
||||
Integer d1 = dstf1.next();
|
||||
assertEquals(s1, d1);
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(d1));
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(s1));
|
||||
}
|
||||
assertEquals(src.f2.size(), dst.f2.size());
|
||||
Iterator<String> srcf2 = src.f2.keySet().iterator();
|
||||
Iterator<String> dstf2 = dst.f2.keySet().iterator();
|
||||
while (srcf2.hasNext()) {
|
||||
String s2 = srcf2.next();
|
||||
String d2 = dstf2.next();
|
||||
assertEquals(s2, d2);
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(d2));
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(s2));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,5 @@
|
||||
package org.msgpack.template;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.ByteBuffer;
|
||||
@ -15,16 +13,13 @@ import org.junit.Test;
|
||||
|
||||
import org.msgpack.MessagePack;
|
||||
import org.msgpack.MessagePackable;
|
||||
import org.msgpack.MessagePacker;
|
||||
import org.msgpack.MessageTypeException;
|
||||
import org.msgpack.MessageUnpackable;
|
||||
import org.msgpack.Packer;
|
||||
import org.msgpack.Template;
|
||||
import org.msgpack.Unpacker;
|
||||
import org.msgpack.annotation.MessagePackMessage;
|
||||
import org.msgpack.annotation.MessagePackOrdinalEnum;
|
||||
import org.msgpack.annotation.Optional;
|
||||
import org.msgpack.template.TestTemplateBuilderPackConvert.SampleInterface;
|
||||
import org.msgpack.template.builder.BuilderSelectorRegistry;
|
||||
import org.msgpack.template.builder.TemplateBuilder;
|
||||
|
||||
@ -599,22 +594,18 @@ public class TestTemplateBuilderPackUnpack extends TestCase {
|
||||
|
||||
SampleMapTypes dst =
|
||||
MessagePack.unpack(raw, SampleMapTypes.class);
|
||||
assertEquals(0, dst.f0.size());
|
||||
assertEquals(src.f1.size(), dst.f1.size());
|
||||
Iterator<Integer> srcf1 = src.f1.keySet().iterator();
|
||||
Iterator<Integer> dstf1 = dst.f1.keySet().iterator();
|
||||
while (srcf1.hasNext()) {
|
||||
Integer s1 = srcf1.next();
|
||||
Integer d1 = dstf1.next();
|
||||
assertEquals(s1, d1);
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(d1));
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(s1));
|
||||
}
|
||||
assertEquals(src.f2.size(), dst.f2.size());
|
||||
Iterator<String> srcf2 = src.f2.keySet().iterator();
|
||||
Iterator<String> dstf2 = dst.f2.keySet().iterator();
|
||||
while (srcf2.hasNext()) {
|
||||
String s2 = srcf2.next();
|
||||
String d2 = dstf2.next();
|
||||
assertEquals(s2, d2);
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(d2));
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(s2));
|
||||
}
|
||||
}
|
||||
|
||||
@ -655,24 +646,18 @@ public class TestTemplateBuilderPackUnpack extends TestCase {
|
||||
|
||||
SampleOptionalMapTypes dst =
|
||||
MessagePack.unpack(raw, SampleOptionalMapTypes.class);
|
||||
assertEquals(src.f0.size(), dst.f0.size());
|
||||
assertEquals(0, dst.f0.size());
|
||||
assertEquals(src.f1.size(), dst.f1.size());
|
||||
Iterator<Integer> srcf1 = src.f1.keySet().iterator();
|
||||
Iterator<Integer> dstf1 = dst.f1.keySet().iterator();
|
||||
while (srcf1.hasNext()) {
|
||||
Integer s1 = srcf1.next();
|
||||
Integer d1 = dstf1.next();
|
||||
assertEquals(s1, d1);
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(d1));
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(s1));
|
||||
}
|
||||
assertEquals(src.f2.size(), dst.f2.size());
|
||||
Iterator<String> srcf2 = src.f2.keySet().iterator();
|
||||
Iterator<String> dstf2 = dst.f2.keySet().iterator();
|
||||
while (srcf2.hasNext()) {
|
||||
String s2 = srcf2.next();
|
||||
String d2 = dstf2.next();
|
||||
assertEquals(s2, d2);
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(d2));
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(s2));
|
||||
}
|
||||
}
|
||||
|
||||
|
110
java/src/test/java/org/msgpack/util/TestTemplatePrecompiler.java
Normal file
110
java/src/test/java/org/msgpack/util/TestTemplatePrecompiler.java
Normal file
@ -0,0 +1,110 @@
|
||||
package org.msgpack.util;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
import org.msgpack.MessagePack;
|
||||
import org.msgpack.MessageTypeException;
|
||||
import org.msgpack.template.TemplateRegistry;
|
||||
|
||||
public class TestTemplatePrecompiler {
|
||||
|
||||
@Test
|
||||
public void testPrimitiveTypeFields00() throws Exception {
|
||||
System.getProperties().setProperty(TemplatePrecompiler.DIST, "./target/test-classes");
|
||||
Class<?> c = PrimitiveTypeFieldsClass.class;
|
||||
TemplatePrecompiler.saveTemplateClass(PrimitiveTypeFieldsClass.class);
|
||||
|
||||
PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass();
|
||||
src.f0 = (byte) 0;
|
||||
src.f1 = 1;
|
||||
src.f2 = 2;
|
||||
src.f3 = 3;
|
||||
src.f4 = 4;
|
||||
src.f5 = 5;
|
||||
src.f6 = false;
|
||||
|
||||
try {
|
||||
MessagePack.pack(src);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
assertTrue(TemplateRegistry.unregister(c));
|
||||
}
|
||||
try {
|
||||
TemplateRegistry.lookup(c, true, true);
|
||||
byte[] raw = MessagePack.pack(src);
|
||||
PrimitiveTypeFieldsClass dst = MessagePack.unpack(raw, PrimitiveTypeFieldsClass.class);
|
||||
assertEquals(src.f0, dst.f0);
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertEquals(src.f2, dst.f2);
|
||||
assertEquals(src.f3, dst.f3);
|
||||
//assertEquals(src.f4, dst.f4);
|
||||
//assertEquals(src.f5, dst.f5);
|
||||
assertEquals(src.f6, dst.f6);
|
||||
} finally {
|
||||
TemplateRegistry.unregister(c);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveTypeFields01() throws Exception {
|
||||
System.getProperties().setProperty(TemplatePrecompiler.DIST, "./target/test-classes");
|
||||
Class<?> c = PrimitiveTypeFieldsClass.class;
|
||||
TemplatePrecompiler.saveTemplateClass(PrimitiveTypeFieldsClass.class);
|
||||
|
||||
PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass();
|
||||
|
||||
try {
|
||||
MessagePack.pack(src);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
assertTrue(TemplateRegistry.unregister(c));
|
||||
}
|
||||
try {
|
||||
TemplateRegistry.lookup(c, true, true);
|
||||
byte[] raw = MessagePack.pack(src);
|
||||
PrimitiveTypeFieldsClass dst = MessagePack.unpack(raw, PrimitiveTypeFieldsClass.class);
|
||||
assertEquals(src.f0, dst.f0);
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertEquals(src.f2, dst.f2);
|
||||
assertEquals(src.f3, dst.f3);
|
||||
//assertEquals(src.f4, dst.f4);
|
||||
//assertEquals(src.f5, dst.f5);
|
||||
assertEquals(src.f6, dst.f6);
|
||||
} finally {
|
||||
TemplateRegistry.unregister(c);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveTypeFields02() throws Exception {
|
||||
System.getProperties().setProperty(TemplatePrecompiler.DIST, "./target/test-classes");
|
||||
Class<?> c = PrimitiveTypeFieldsClass.class;
|
||||
TemplatePrecompiler.saveTemplateClass(PrimitiveTypeFieldsClass.class);
|
||||
|
||||
PrimitiveTypeFieldsClass src = null;
|
||||
MessagePack.pack(src);
|
||||
try {
|
||||
TemplateRegistry.lookup(c, true, true);
|
||||
byte[] raw = MessagePack.pack(src);
|
||||
PrimitiveTypeFieldsClass dst = MessagePack.unpack(raw, PrimitiveTypeFieldsClass.class);
|
||||
assertEquals(src, dst);
|
||||
} finally {
|
||||
TemplateRegistry.unregister(c);
|
||||
}
|
||||
}
|
||||
|
||||
public static class PrimitiveTypeFieldsClass {
|
||||
public byte f0;
|
||||
public short f1;
|
||||
public int f2;
|
||||
public long f3;
|
||||
public float f4;
|
||||
public double f5;
|
||||
public boolean f6;
|
||||
|
||||
public PrimitiveTypeFieldsClass() {
|
||||
}
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.msgpack</groupId>
|
||||
<artifactId>scala-msgpack</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<version>0.0.1-devel</version>
|
||||
<name>${project.artifactId}</name>
|
||||
<description>My wonderfull scala app</description>
|
||||
<inceptionYear>2010</inceptionYear>
|
||||
|
@ -7,7 +7,8 @@ import java.lang.Class
|
||||
import collection.immutable.{ListMap, TreeMap}
|
||||
import java.lang.reflect.{Type, Modifier, Method, Field}
|
||||
import java.lang.annotation.{Annotation => JavaAnnotation}
|
||||
import javassist.{JavassistTemplate, BuildContextBase, BuildContext}
|
||||
import builder.{JavassistTemplateBuilder, BuildContextBase, BuildContext}
|
||||
import builder.JavassistTemplateBuilder.JavassistTemplate
|
||||
import scala.collection.JavaConverters._
|
||||
;
|
||||
/*
|
||||
@ -26,6 +27,23 @@ import scala.collection.JavaConverters._
|
||||
var templates : Array[Template] = null
|
||||
var minimumArrayLength : Int = 0
|
||||
|
||||
def writeTemplate(targetClass : Class[_] , entries : Array[IFieldEntry],
|
||||
templates : Array[Template], directoryName : String) = {
|
||||
this.entries = entries;
|
||||
this.templates = templates;
|
||||
this.origClass = targetClass;
|
||||
this.origName = this.origClass.getName();
|
||||
write(this.origName, directoryName);
|
||||
}
|
||||
|
||||
def loadTemplate(targetClass : Class[_] , entries : Array[IFieldEntry], templates : Array[Template]) = {
|
||||
this.entries = entries;
|
||||
this.templates = templates;
|
||||
this.origClass = targetClass;
|
||||
this.origName = this.origClass.getName();
|
||||
load(this.origName);
|
||||
}
|
||||
|
||||
def buildTemplate(targetClass : Class[_] , entries : Array[IFieldEntry], templates : Array[Template]) = {
|
||||
this.entries = entries;
|
||||
this.templates = templates;
|
||||
|
@ -1,10 +1,9 @@
|
||||
package org.msgpack
|
||||
|
||||
import template._
|
||||
import builder.{MessagePackMessageBuilderSelector, BuilderSelectorRegistry}
|
||||
import template.javassist.BuildContextFactory
|
||||
import builder.{AnnotationTemplateBuilderSelector, BuilderSelectorRegistry, BuildContextFactory}
|
||||
import collection.mutable.{MutableList, LinkedList}
|
||||
import collection.mutable.{Map => MMap,HashMap => MHashMap}
|
||||
import collection.mutable.{Map => MMap, HashMap => MHashMap}
|
||||
;
|
||||
/*
|
||||
* Created by IntelliJ IDEA.
|
||||
@ -18,7 +17,7 @@ object ScalaMessagePack {
|
||||
{
|
||||
// for scala object
|
||||
BuilderSelectorRegistry.getInstance.insertBefore(
|
||||
MessagePackMessageBuilderSelector.NAME,
|
||||
AnnotationTemplateBuilderSelector.NAME,
|
||||
new ScalaTemplateBuilderSelector)
|
||||
|
||||
// register scala's list classes
|
||||
|
@ -3,8 +3,7 @@ package org.msgpack
|
||||
import annotation.MessagePackMessage
|
||||
import template.builder.BuilderSelector
|
||||
import java.lang.reflect.Type
|
||||
import template.javassist.BuildContextFactory
|
||||
import template.JavassistTemplateBuilder
|
||||
import template.builder.{JavassistTemplateBuilder, BuildContextFactory}
|
||||
import java.lang.annotation.{Annotation => JAnnotation}
|
||||
;
|
||||
/*
|
||||
|
Loading…
x
Reference in New Issue
Block a user