mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-21 06:11:18 +01:00
java: add test program for TemplatePrecompiler
This commit is contained in:
parent
58c0fe0f91
commit
65ddd1a455
@ -29,7 +29,6 @@ import org.msgpack.Template;
|
|||||||
public class TemplateRegistry {
|
public class TemplateRegistry {
|
||||||
private static Map<Type, Template> map;
|
private static Map<Type, Template> map;
|
||||||
private static Map<Type, GenericTemplate> genericMap;
|
private static Map<Type, GenericTemplate> genericMap;
|
||||||
|
|
||||||
private static BuilderSelectorRegistry builderSelectorRegistry;
|
private static BuilderSelectorRegistry builderSelectorRegistry;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
@ -41,38 +40,43 @@ public class TemplateRegistry {
|
|||||||
|
|
||||||
public static void register(Class<?> target) {
|
public static void register(Class<?> target) {
|
||||||
TemplateBuilder builder = builderSelectorRegistry.select(target);
|
TemplateBuilder builder = builderSelectorRegistry.select(target);
|
||||||
if(builder != null){
|
if (builder != null) {
|
||||||
register(target,builder.buildTemplate(target));
|
register(target,builder.buildTemplate(target));
|
||||||
}else{
|
} else {
|
||||||
register(target,builderSelectorRegistry.getForceBuilder().buildTemplate(target));
|
register(target,builderSelectorRegistry.getForceBuilder().buildTemplate(target));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void register(Class<?> target, FieldOption implicitOption) {
|
public static void register(Class<?> target, FieldOption implicitOption) {
|
||||||
TemplateBuilder builder = builderSelectorRegistry.select(target);
|
TemplateBuilder builder = builderSelectorRegistry.select(target);
|
||||||
if(builder != null && builder instanceof CustomTemplateBuilder){
|
if (builder != null && builder instanceof CustomTemplateBuilder) {
|
||||||
register(target, ((CustomTemplateBuilder)builder).buildTemplate(target, implicitOption));
|
register(target, ((CustomTemplateBuilder)builder).buildTemplate(target, implicitOption));
|
||||||
}else{
|
} else {
|
||||||
throw new TemplateBuildException("Cannot build template with filed option");
|
throw new TemplateBuildException("Cannot build template with filed option");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void register(Class<?> target, FieldList flist) throws NoSuchFieldException {
|
public static void register(Class<?> target, FieldList flist) throws NoSuchFieldException {
|
||||||
TemplateBuilder builder = builderSelectorRegistry.select(target);
|
TemplateBuilder builder = builderSelectorRegistry.select(target);
|
||||||
if(builder != null && builder instanceof CustomTemplateBuilder){
|
if (builder != null && builder instanceof CustomTemplateBuilder) {
|
||||||
register(target, ((CustomTemplateBuilder)builder).buildTemplate(target, flist));
|
register(target, ((CustomTemplateBuilder)builder).buildTemplate(target, flist));
|
||||||
}else{
|
} else {
|
||||||
throw new TemplateBuildException("Cannot build template with filed list");
|
throw new TemplateBuildException("Cannot build template with filed list");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized void register(Type rawType, Template tmpl) {
|
public static synchronized void register(Type rawType, Template tmpl) {
|
||||||
if(rawType instanceof ParameterizedType) {
|
if (rawType instanceof ParameterizedType) {
|
||||||
rawType = ((ParameterizedType)rawType).getRawType();
|
rawType = ((ParameterizedType)rawType).getRawType();
|
||||||
}
|
}
|
||||||
map.put(rawType, tmpl);
|
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) {
|
public static synchronized void registerGeneric(Type rawType, GenericTemplate gtmpl) {
|
||||||
if(rawType instanceof ParameterizedType) {
|
if(rawType instanceof ParameterizedType) {
|
||||||
rawType = ((ParameterizedType)rawType).getRawType();
|
rawType = ((ParameterizedType)rawType).getRawType();
|
||||||
@ -81,22 +85,27 @@ public class TemplateRegistry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized Template lookup(Type targetType) {
|
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) {
|
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) {
|
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) {
|
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;
|
Template tmpl;
|
||||||
|
|
||||||
if(targetType instanceof ParameterizedType) {
|
if(targetType instanceof ParameterizedType) {
|
||||||
@ -115,10 +124,13 @@ public class TemplateRegistry {
|
|||||||
|
|
||||||
// find match TemplateBuilder
|
// find match TemplateBuilder
|
||||||
TemplateBuilder builder = BuilderSelectorRegistry.getInstance().select(targetType);
|
TemplateBuilder builder = BuilderSelectorRegistry.getInstance().select(targetType);
|
||||||
if(builder != null){
|
if (builder != null) {
|
||||||
tmpl = builder.loadTemplate(targetType);
|
if (forceLoad) {
|
||||||
if (tmpl != null) {
|
tmpl = builder.loadTemplate(targetType);
|
||||||
return tmpl;
|
if (tmpl != null) {
|
||||||
|
register(targetType, tmpl);
|
||||||
|
return tmpl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpl = builder.buildTemplate(targetType);
|
tmpl = builder.buildTemplate(targetType);
|
||||||
|
@ -22,6 +22,7 @@ import java.io.IOException;
|
|||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import org.msgpack.template.builder.BuilderSelectorRegistry;
|
import org.msgpack.template.builder.BuilderSelectorRegistry;
|
||||||
|
import org.msgpack.template.builder.JavassistTemplateBuilder;
|
||||||
import org.msgpack.template.builder.TemplateBuilder;
|
import org.msgpack.template.builder.TemplateBuilder;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -29,35 +30,39 @@ import org.slf4j.LoggerFactory;
|
|||||||
public class TemplatePrecompiler {
|
public class TemplatePrecompiler {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(TemplatePrecompiler.class);
|
private static final Logger LOG = LoggerFactory.getLogger(TemplatePrecompiler.class);
|
||||||
|
|
||||||
//private static final String SRC = "msgpack.template.srcdir";
|
//public static final String SRC = "msgpack.template.srcdir";
|
||||||
|
|
||||||
private static final String DIST = "msgpack.template.distdir";
|
public static final String DIST = "msgpack.template.distdir";
|
||||||
|
|
||||||
//private static final String DEFAULT_SRC = ".";
|
//public static final String DEFAULT_SRC = ".";
|
||||||
|
|
||||||
private static final String DEFAULT_DIST = ".";
|
public static final String DEFAULT_DIST = ".";
|
||||||
|
|
||||||
|
private static TemplatePrecompiler INSTANCE = null;
|
||||||
|
|
||||||
private TemplatePrecompiler() {
|
private TemplatePrecompiler() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveTemplates(final String[] classFileNames) throws IOException {
|
public static void saveTemplates(final String[] classFileNames) throws IOException {
|
||||||
throw new UnsupportedOperationException("Not supported yet.");// TODO
|
throw new UnsupportedOperationException("Not supported yet.");// TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveTemplateClass(Class<?> targetClass) throws IOException {
|
public static void saveTemplateClass(Class<?> targetClass) throws IOException {
|
||||||
|
if (INSTANCE != null) {
|
||||||
|
INSTANCE = new TemplatePrecompiler();
|
||||||
|
}
|
||||||
LOG.info("Saving template of " + targetClass.getName() + "...");
|
LOG.info("Saving template of " + targetClass.getName() + "...");
|
||||||
Properties props = System.getProperties();
|
Properties props = System.getProperties();
|
||||||
String distDirName = getDirName(props, DIST, DEFAULT_DIST);
|
String distDirName = getDirName(props, DIST, DEFAULT_DIST);
|
||||||
if (targetClass.isEnum()) {
|
if (targetClass.isEnum()) {
|
||||||
throw new UnsupportedOperationException("Enum not supported yet: " + targetClass.getName());
|
throw new UnsupportedOperationException("Enum not supported yet: " + targetClass.getName());
|
||||||
} else {
|
} else {
|
||||||
TemplateBuilder builder = BuilderSelectorRegistry.getInstance().select(targetClass);
|
new JavassistTemplateBuilder().writeTemplate(targetClass, distDirName);
|
||||||
builder.writeTemplate(targetClass, distDirName);
|
|
||||||
}
|
}
|
||||||
LOG.info("Saved .class file of template class of " + targetClass.getName());
|
LOG.info("Saved .class file of template class of " + targetClass.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getDirName(Properties props, String dirName, String defaultDirName)
|
private static String getDirName(Properties props, String dirName, String defaultDirName)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
String dName = props.getProperty(dirName, defaultDirName);
|
String dName = props.getProperty(dirName, defaultDirName);
|
||||||
File d = new File(dName);
|
File d = new File(dName);
|
||||||
@ -68,7 +73,6 @@ public class TemplatePrecompiler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(final String[] args) throws Exception {
|
public static void main(final String[] args) throws Exception {
|
||||||
TemplatePrecompiler compiler = new TemplatePrecompiler();// TODO
|
TemplatePrecompiler.saveTemplates(args);
|
||||||
compiler.saveTemplates(args);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package org.msgpack.template;
|
package org.msgpack.template;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
@ -15,16 +13,13 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import org.msgpack.MessagePack;
|
import org.msgpack.MessagePack;
|
||||||
import org.msgpack.MessagePackable;
|
import org.msgpack.MessagePackable;
|
||||||
import org.msgpack.MessagePacker;
|
|
||||||
import org.msgpack.MessageTypeException;
|
import org.msgpack.MessageTypeException;
|
||||||
import org.msgpack.MessageUnpackable;
|
import org.msgpack.MessageUnpackable;
|
||||||
import org.msgpack.Packer;
|
import org.msgpack.Packer;
|
||||||
import org.msgpack.Template;
|
|
||||||
import org.msgpack.Unpacker;
|
import org.msgpack.Unpacker;
|
||||||
import org.msgpack.annotation.MessagePackMessage;
|
import org.msgpack.annotation.MessagePackMessage;
|
||||||
import org.msgpack.annotation.MessagePackOrdinalEnum;
|
import org.msgpack.annotation.MessagePackOrdinalEnum;
|
||||||
import org.msgpack.annotation.Optional;
|
import org.msgpack.annotation.Optional;
|
||||||
import org.msgpack.template.TestTemplateBuilderPackConvert.SampleInterface;
|
|
||||||
import org.msgpack.template.builder.BuilderSelectorRegistry;
|
import org.msgpack.template.builder.BuilderSelectorRegistry;
|
||||||
import org.msgpack.template.builder.TemplateBuilder;
|
import org.msgpack.template.builder.TemplateBuilder;
|
||||||
|
|
||||||
|
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() {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user