mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-25 10:09:11 +01:00
add a packer and unpacker for Enum types
This commit is contained in:
parent
0bd4150a80
commit
cdd60e5f9c
@ -23,5 +23,9 @@ public class MessageTypeException extends RuntimeException {
|
|||||||
public MessageTypeException(String s) {
|
public MessageTypeException(String s) {
|
||||||
super(s);
|
super(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public MessageTypeException(String s, Throwable t) {
|
||||||
|
super(s, t);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ public interface BasicConstants {
|
|||||||
String KEYWORD_NEW = "new";
|
String KEYWORD_NEW = "new";
|
||||||
|
|
||||||
String KEYWORD_NULL = "null";
|
String KEYWORD_NULL = "null";
|
||||||
|
|
||||||
String KEYWORD_RETURN = "return";
|
String KEYWORD_RETURN = "return";
|
||||||
|
|
||||||
String KEYWORD_THROW = "throw";
|
String KEYWORD_THROW = "throw";
|
||||||
@ -98,8 +98,10 @@ public interface BasicConstants {
|
|||||||
|
|
||||||
String METHOD_NAME_NEXT = "next";
|
String METHOD_NAME_NEXT = "next";
|
||||||
|
|
||||||
|
String METHOD_NAME_ORDINAL = "ordinal";
|
||||||
|
|
||||||
String METHOD_NAME_CONVERT = "convert";
|
String METHOD_NAME_CONVERT = "convert";
|
||||||
|
|
||||||
String METHOD_NAME_MSGCONVERT = "messageConvert";
|
String METHOD_NAME_MSGCONVERT = "messageConvert";
|
||||||
|
|
||||||
String METHOD_NAME_PACK = "pack";
|
String METHOD_NAME_PACK = "pack";
|
||||||
|
@ -81,6 +81,24 @@ public class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Class<?> generateOrdinalEnumPackerClass(Class<?> origClass) {
|
||||||
|
try {
|
||||||
|
String origName = origClass.getName();
|
||||||
|
String packerName = origName + POSTFIX_TYPE_NAME_PACKER + inc();
|
||||||
|
checkClassValidation(origClass);
|
||||||
|
//checkDefaultConstructorValidation(origClass);
|
||||||
|
CtClass packerCtClass = pool.makeClass(packerName);
|
||||||
|
setInterface(packerCtClass, MessagePacker.class);
|
||||||
|
addDefaultConstructor(packerCtClass);
|
||||||
|
addPackMethodForOrdinalEnumTypes(packerCtClass, origClass);
|
||||||
|
return createClass(packerCtClass);
|
||||||
|
} catch (NotFoundException e) {
|
||||||
|
throw new DynamicCodeGenException(e.getMessage(), e);
|
||||||
|
} catch (CannotCompileException e) {
|
||||||
|
throw new DynamicCodeGenException(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Class<?> generateMessageUnpackerClass(Class<?> origClass) {
|
public Class<?> generateMessageUnpackerClass(Class<?> origClass) {
|
||||||
try {
|
try {
|
||||||
String origName = origClass.getName();
|
String origName = origClass.getName();
|
||||||
@ -100,6 +118,24 @@ public class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Class<?> generateOrdinalEnumUnpackerClass(Class<?> origClass) {
|
||||||
|
try {
|
||||||
|
String origName = origClass.getName();
|
||||||
|
String unpackerName = origName + POSTFIX_TYPE_NAME_UNPACKER + inc();
|
||||||
|
checkClassValidation(origClass);
|
||||||
|
checkDefaultConstructorValidation(origClass);
|
||||||
|
CtClass unpackerCtClass = pool.makeClass(unpackerName);
|
||||||
|
setInterface(unpackerCtClass, MessageUnpacker.class);
|
||||||
|
addDefaultConstructor(unpackerCtClass);
|
||||||
|
addUnpackMethodForOrdinalEnumTypes(unpackerCtClass, origClass);
|
||||||
|
return createClass(unpackerCtClass);
|
||||||
|
} catch (NotFoundException e) {
|
||||||
|
throw new DynamicCodeGenException(e.getMessage(), e);
|
||||||
|
} catch (CannotCompileException e) {
|
||||||
|
throw new DynamicCodeGenException(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Class<?> generateMessageConverterClass(Class<?> origClass) {
|
public Class<?> generateMessageConverterClass(Class<?> origClass) {
|
||||||
try {
|
try {
|
||||||
String origName = origClass.getName();
|
String origName = origClass.getName();
|
||||||
@ -119,6 +155,24 @@ public class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Class<?> generateOrdinalEnumConverterClass(Class<?> origClass) {
|
||||||
|
try {
|
||||||
|
String origName = origClass.getName();
|
||||||
|
String convName = origName + POSTFIX_TYPE_NAME_CONVERTER + inc();
|
||||||
|
checkClassValidation(origClass);
|
||||||
|
//checkDefaultConstructorValidation(origClass);
|
||||||
|
CtClass converterCtClass = pool.makeClass(convName);
|
||||||
|
setInterface(converterCtClass, MessageUnpacker.class);
|
||||||
|
addDefaultConstructor(converterCtClass);
|
||||||
|
addConvertMethodForOrdinalEnumTypes(converterCtClass, origClass);
|
||||||
|
return createClass(converterCtClass);
|
||||||
|
} catch (NotFoundException e) {
|
||||||
|
throw new DynamicCodeGenException(e.getMessage(), e);
|
||||||
|
} catch (CannotCompileException e) {
|
||||||
|
throw new DynamicCodeGenException(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Class<?> generateTemplateClass(Class<?> origClass) {
|
public Class<?> generateTemplateClass(Class<?> origClass) {
|
||||||
try {
|
try {
|
||||||
String origName = origClass.getName();
|
String origName = origClass.getName();
|
||||||
@ -139,18 +193,37 @@ public class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Class<?> generateOrdinalEnumTemplateClass(Class<?> origClass) {
|
||||||
|
try {
|
||||||
|
String origName = origClass.getName();
|
||||||
|
String tmplName = origName + POSTFIX_TYPE_NAME_TEMPLATE + inc();
|
||||||
|
checkClassValidation(origClass);
|
||||||
|
//checkDefaultConstructorValidation(origClass);
|
||||||
|
CtClass tmplCtClass = pool.makeClass(tmplName);
|
||||||
|
setInterface(tmplCtClass, Template.class);
|
||||||
|
addDefaultConstructor(tmplCtClass);
|
||||||
|
addUnpackMethodForOrdinalEnumTypes(tmplCtClass, origClass);
|
||||||
|
addConvertMethodForOrdinalEnumTypes(tmplCtClass, origClass);
|
||||||
|
return createClass(tmplCtClass);
|
||||||
|
} catch (NotFoundException e) {
|
||||||
|
throw new DynamicCodeGenException(e.getMessage(), e);
|
||||||
|
} catch (CannotCompileException e) {
|
||||||
|
throw new DynamicCodeGenException(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void checkClassValidation(Class<?> origClass) {
|
private void checkClassValidation(Class<?> origClass) {
|
||||||
// not public, abstract, final
|
// not public, abstract
|
||||||
int mod = origClass.getModifiers();
|
int mod = origClass.getModifiers();
|
||||||
if ((!(Modifier.isPublic(mod) || Modifier.isProtected(mod)))
|
if ((!(Modifier.isPublic(mod) || Modifier.isProtected(mod)))
|
||||||
|| Modifier.isAbstract(mod) || Modifier.isFinal(mod)) {
|
|| Modifier.isAbstract(mod)) {
|
||||||
throwClassValidationException(origClass,
|
throwClassValidationException(origClass,
|
||||||
"it must be a public class");
|
"a class must have a public modifier");
|
||||||
}
|
}
|
||||||
// interface, enum
|
// interface
|
||||||
if (origClass.isInterface() || origClass.isEnum()) {
|
if (origClass.isInterface()) {
|
||||||
throwClassValidationException(origClass,
|
throwClassValidationException(origClass,
|
||||||
"it must not be an interface or enum");
|
"cannot generate packer and unpacker for an interface");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -239,7 +312,7 @@ public class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
|||||||
Packer.class, Object.class }, new String[] { VARIABLE_NAME_PK,
|
Packer.class, Object.class }, new String[] { VARIABLE_NAME_PK,
|
||||||
VARIABLE_NAME_OBJECT }, new Class[] { IOException.class }, bsb
|
VARIABLE_NAME_OBJECT }, new Class[] { IOException.class }, bsb
|
||||||
.toString());
|
.toString());
|
||||||
// System.out.println("pack method: " + sb.toString());
|
System.out.println("pack method: " + sb.toString());
|
||||||
CtMethod newCtMethod = CtNewMethod.make(sb.toString(), packerCtClass);
|
CtMethod newCtMethod = CtNewMethod.make(sb.toString(), packerCtClass);
|
||||||
packerCtClass.addMethod(newCtMethod);
|
packerCtClass.addMethod(newCtMethod);
|
||||||
}
|
}
|
||||||
@ -277,9 +350,9 @@ public class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
|||||||
throw new UnsupportedOperationException("not supported yet. : "
|
throw new UnsupportedOperationException("not supported yet. : "
|
||||||
+ c.getName());
|
+ c.getName());
|
||||||
} else if (CustomMessage.isAnnotated(c, MessagePackOrdinalEnum.class)) {
|
} else if (CustomMessage.isAnnotated(c, MessagePackOrdinalEnum.class)) {
|
||||||
// FIXME OrdinalEnumPacker
|
// @MessagePackOrdinalEnum
|
||||||
throw new UnsupportedOperationException("not supported yet. : "
|
MessagePacker packer = DynamicCodeGenOrdinalEnumPacker.create(c);
|
||||||
+ c.getName());
|
CustomMessage.registerPacker(c, packer);
|
||||||
}
|
}
|
||||||
StringBuilder fa = new StringBuilder();
|
StringBuilder fa = new StringBuilder();
|
||||||
insertFieldAccess(fa, VARIABLE_NAME_TARGET, field.getName());
|
insertFieldAccess(fa, VARIABLE_NAME_TARGET, field.getName());
|
||||||
@ -288,9 +361,42 @@ public class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
|||||||
insertSemicolon(sb);
|
insertSemicolon(sb);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void addPackMethodForOrdinalEnumTypes(CtClass packerCtClass,
|
||||||
|
Class<?> c) throws CannotCompileException, NotFoundException {
|
||||||
|
// void pack(Packer pk, Object target) throws IOException;
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
StringBuilder bsb = new StringBuilder();
|
||||||
|
// FIXME
|
||||||
|
insertLocalVariableDecl(bsb, c, VARIABLE_NAME_TARGET);
|
||||||
|
StringBuilder mc = new StringBuilder();
|
||||||
|
insertTypeCast(mc, c, VARIABLE_NAME_OBJECT);
|
||||||
|
insertValueInsertion(bsb, mc.toString());
|
||||||
|
insertSemicolon(bsb);
|
||||||
|
insertMethodCall(bsb, VARIABLE_NAME_PK, METHOD_NAME_PACKARRAY,
|
||||||
|
new String[] { new Integer(1).toString() });
|
||||||
|
insertSemicolon(bsb);
|
||||||
|
StringBuilder fa = new StringBuilder();
|
||||||
|
insertTypeCast(fa, Enum.class, VARIABLE_NAME_TARGET);
|
||||||
|
String t = fa.toString();
|
||||||
|
fa = new StringBuilder();
|
||||||
|
insertMethodCall(fa, t, METHOD_NAME_ORDINAL, new String[0]);
|
||||||
|
insertMethodCall(bsb, VARIABLE_NAME_PK, METHOD_NAME_PACK, new String[] { fa.toString() });
|
||||||
|
insertSemicolon(bsb);
|
||||||
|
addPublicMethodDecl(sb, METHOD_NAME_PACK, void.class, new Class[] {
|
||||||
|
Packer.class, Object.class }, new String[] { VARIABLE_NAME_PK,
|
||||||
|
VARIABLE_NAME_OBJECT }, new Class[] { IOException.class }, bsb
|
||||||
|
.toString());
|
||||||
|
System.out.println("pack method: " + sb.toString());
|
||||||
|
try {
|
||||||
|
CtMethod newCtMethod = CtNewMethod.make(sb.toString(), packerCtClass);
|
||||||
|
packerCtClass.addMethod(newCtMethod);
|
||||||
|
} catch (CannotCompileException e) {
|
||||||
|
throw new CannotCompileException(e.getMessage() + ": " + sb.toString(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void addUnpackMethod(CtClass unpackerCtClass, Class<?> c, Field[] fs)
|
private void addUnpackMethod(CtClass unpackerCtClass, Class<?> c, Field[] fs)
|
||||||
throws CannotCompileException, NotFoundException {
|
throws CannotCompileException, NotFoundException {
|
||||||
// Object unpack(Unpacker pac) throws IOException, MessageTypeException;
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
StringBuilder bsb = new StringBuilder();
|
StringBuilder bsb = new StringBuilder();
|
||||||
insertUnpackMethodBody(bsb, c, fs);
|
insertUnpackMethodBody(bsb, c, fs);
|
||||||
@ -299,9 +405,13 @@ public class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
|||||||
new String[] { VARIABLE_NAME_PK }, new Class<?>[] {
|
new String[] { VARIABLE_NAME_PK }, new Class<?>[] {
|
||||||
MessageTypeException.class, IOException.class }, bsb
|
MessageTypeException.class, IOException.class }, bsb
|
||||||
.toString());
|
.toString());
|
||||||
// System.out.println("unpack method: " + sb.toString());
|
System.out.println("unpack method: " + sb.toString());
|
||||||
CtMethod newCtMethod = CtNewMethod.make(sb.toString(), unpackerCtClass);
|
try {
|
||||||
unpackerCtClass.addMethod(newCtMethod);
|
CtMethod newCtMethod = CtNewMethod.make(sb.toString(), unpackerCtClass);
|
||||||
|
unpackerCtClass.addMethod(newCtMethod);
|
||||||
|
} catch (CannotCompileException e) {
|
||||||
|
throw new CannotCompileException(e.getMessage() + ": " + sb.toString(), e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void insertUnpackMethodBody(StringBuilder sb, Class<?> c, Field[] fs) {
|
private void insertUnpackMethodBody(StringBuilder sb, Class<?> c, Field[] fs) {
|
||||||
@ -360,9 +470,10 @@ public class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
|||||||
throw new UnsupportedOperationException("not supported yet. : "
|
throw new UnsupportedOperationException("not supported yet. : "
|
||||||
+ c.getName());
|
+ c.getName());
|
||||||
} else if (CustomMessage.isAnnotated(c, MessagePackOrdinalEnum.class)) {
|
} else if (CustomMessage.isAnnotated(c, MessagePackOrdinalEnum.class)) {
|
||||||
// FIXME OrdinalEnumPacker
|
// @MessagePackOrdinalEnum
|
||||||
throw new UnsupportedOperationException("not supported yet. : "
|
Template tmpl = DynamicCodeGenOrdinalEnumTemplate.create(c);
|
||||||
+ c.getName());
|
CustomMessage.registerTemplate(c, tmpl);
|
||||||
|
insertCodeOfUnpackMethodCallForRegisteredType(sb, f, c);
|
||||||
} else {
|
} else {
|
||||||
throw new MessageTypeException("unknown type: " + c.getName());
|
throw new MessageTypeException("unknown type: " + c.getName());
|
||||||
}
|
}
|
||||||
@ -639,6 +750,29 @@ public class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
|||||||
sb.append(CHAR_NAME_SPACE);
|
sb.append(CHAR_NAME_SPACE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void addUnpackMethodForOrdinalEnumTypes(CtClass unpackerCtClass,
|
||||||
|
Class<?> c) throws CannotCompileException, NotFoundException {
|
||||||
|
// Object unpack(Unpacker pac) throws IOException, MessageTypeException;
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
StringBuilder bsb = new StringBuilder();
|
||||||
|
// FIXME
|
||||||
|
insertMethodCall(bsb, VARIABLE_NAME_PK, METHOD_NAME_UNPACKARRAY,
|
||||||
|
new String[0]);
|
||||||
|
insertSemicolon(bsb);
|
||||||
|
//insertUnpackMethodBody(bsb, c, new Field[0]);
|
||||||
|
bsb.append("int _$$_i = _$$_pk.unpackInt();");
|
||||||
|
bsb.append("return " + c.getName()
|
||||||
|
+ ".class.getEnumConstants()[_$$_i];");
|
||||||
|
addPublicMethodDecl(sb, METHOD_NAME_UNPACK, Object.class,
|
||||||
|
new Class<?>[] { Unpacker.class },
|
||||||
|
new String[] { VARIABLE_NAME_PK }, new Class<?>[] {
|
||||||
|
MessageTypeException.class, IOException.class }, bsb
|
||||||
|
.toString());
|
||||||
|
System.out.println("unpack method: " + sb.toString());
|
||||||
|
CtMethod newCtMethod = CtNewMethod.make(sb.toString(), unpackerCtClass);
|
||||||
|
unpackerCtClass.addMethod(newCtMethod);
|
||||||
|
}
|
||||||
|
|
||||||
public void addConvertMethod(CtClass tmplCtClass, Class<?> c, Field[] fs)
|
public void addConvertMethod(CtClass tmplCtClass, Class<?> c, Field[] fs)
|
||||||
throws CannotCompileException, NotFoundException {
|
throws CannotCompileException, NotFoundException {
|
||||||
// Object convert(MessagePackObject from) throws MessageTypeException;
|
// Object convert(MessagePackObject from) throws MessageTypeException;
|
||||||
@ -649,20 +783,20 @@ public class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
|||||||
new Class<?>[] { MessagePackObject.class },
|
new Class<?>[] { MessagePackObject.class },
|
||||||
new String[] { VARIABLE_NAME_MPO },
|
new String[] { VARIABLE_NAME_MPO },
|
||||||
new Class<?>[] { MessageTypeException.class }, bsb.toString());
|
new Class<?>[] { MessageTypeException.class }, bsb.toString());
|
||||||
// System.out.println("convert method: " + sb.toString());
|
System.out.println("convert method: " + sb.toString());
|
||||||
CtMethod newCtMethod = CtNewMethod.make(sb.toString(), tmplCtClass);
|
CtMethod newCtMethod = CtNewMethod.make(sb.toString(), tmplCtClass);
|
||||||
tmplCtClass.addMethod(newCtMethod);
|
tmplCtClass.addMethod(newCtMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void insertConvertMethodBody(StringBuilder sb, Class<?> c,
|
private void insertConvertMethodBody(StringBuilder sb, Class<?> c,
|
||||||
Field[] fields) throws CannotCompileException {
|
Field[] fs) throws CannotCompileException {
|
||||||
insertLocalVariableDecl(sb, c, VARIABLE_NAME_TARGET);
|
insertLocalVariableDecl(sb, c, VARIABLE_NAME_TARGET);
|
||||||
StringBuilder mc = new StringBuilder();
|
StringBuilder mc = new StringBuilder();
|
||||||
insertDefaultConsCall(mc, c);
|
insertDefaultConsCall(mc, c);
|
||||||
insertValueInsertion(sb, mc.toString());
|
insertValueInsertion(sb, mc.toString());
|
||||||
insertSemicolon(sb);
|
insertSemicolon(sb);
|
||||||
insertCodeOfMessagePackObjectArrayGet(sb);
|
insertCodeOfMessagePackObjectArrayGet(sb);
|
||||||
insertCodeOfConvertMethodCalls(sb, fields);
|
insertCodeOfConvertMethodCalls(sb, fs);
|
||||||
insertReturnStat(sb, VARIABLE_NAME_TARGET);
|
insertReturnStat(sb, VARIABLE_NAME_TARGET);
|
||||||
insertSemicolon(sb);
|
insertSemicolon(sb);
|
||||||
}
|
}
|
||||||
@ -726,9 +860,10 @@ public class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
|||||||
+ c.getName());
|
+ c.getName());
|
||||||
} else if (CustomMessage.isAnnotated(c,
|
} else if (CustomMessage.isAnnotated(c,
|
||||||
MessagePackOrdinalEnum.class)) {
|
MessagePackOrdinalEnum.class)) {
|
||||||
// FIXME OrdinalEnumPacker
|
// @MessagePackMessage
|
||||||
throw new UnsupportedOperationException("not supported yet. : "
|
Template tmpl = DynamicCodeGenOrdinalEnumTemplate.create(c);
|
||||||
+ c.getName());
|
CustomMessage.registerTemplate(c, tmpl);
|
||||||
|
insertCodeOfMessageConvertCallForRegisteredType(sb, f, c, i);
|
||||||
} else {
|
} else {
|
||||||
throw new MessageTypeException("Type error: " + c.getName());
|
throw new MessageTypeException("Type error: " + c.getName());
|
||||||
}
|
}
|
||||||
@ -1185,6 +1320,30 @@ public class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
|
|||||||
sb.append(CHAR_NAME_SPACE);
|
sb.append(CHAR_NAME_SPACE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addConvertMethodForOrdinalEnumTypes(CtClass tmplCtClass,
|
||||||
|
Class<?> c) throws CannotCompileException, NotFoundException {
|
||||||
|
// Object convert(MessagePackObject from) throws MessageTypeException;
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
StringBuilder bsb = new StringBuilder();
|
||||||
|
insertCodeOfMessagePackObjectArrayGet(bsb);
|
||||||
|
//insertConvertMethodBody(bsb, c, new Field[0]);
|
||||||
|
// FIXME
|
||||||
|
//bsb.append("_$$_ary[0].asInt(); ");
|
||||||
|
//bsb.append("int i = _$$_ary[1].asInt(); ");
|
||||||
|
bsb.append("int i = _$$_ary[0].asInt(); ");
|
||||||
|
bsb.append("java.lang.Object o = ").append(c.getName()).append(
|
||||||
|
".class.getEnumConstants()[i]; ");
|
||||||
|
bsb.append("return (").append(c.getName()).append(") o; ");
|
||||||
|
|
||||||
|
addPublicMethodDecl(sb, METHOD_NAME_CONVERT, Object.class,
|
||||||
|
new Class<?>[] { MessagePackObject.class },
|
||||||
|
new String[] { VARIABLE_NAME_MPO },
|
||||||
|
new Class<?>[] { MessageTypeException.class }, bsb.toString());
|
||||||
|
System.out.println("convert method: " + sb.toString());
|
||||||
|
CtMethod newCtMethod = CtNewMethod.make(sb.toString(), tmplCtClass);
|
||||||
|
tmplCtClass.addMethod(newCtMethod);
|
||||||
|
}
|
||||||
|
|
||||||
private Class<?> createClass(CtClass packerCtClass)
|
private Class<?> createClass(CtClass packerCtClass)
|
||||||
throws CannotCompileException {
|
throws CannotCompileException {
|
||||||
return packerCtClass.toClass(null, null);
|
return packerCtClass.toClass(null, null);
|
||||||
|
@ -0,0 +1,17 @@
|
|||||||
|
package org.msgpack.util.codegen;
|
||||||
|
|
||||||
|
import org.msgpack.MessageConverter;
|
||||||
|
|
||||||
|
public class DynamicCodeGenOrdinalEnumConverter {
|
||||||
|
public static MessageConverter create(Class<?> c) {
|
||||||
|
try {
|
||||||
|
DynamicCodeGen gen = DynamicCodeGen.getInstance();
|
||||||
|
Class<?> unpackerClass = gen.generateOrdinalEnumConverterClass(c);
|
||||||
|
return (MessageConverter) unpackerClass.newInstance();
|
||||||
|
} catch (InstantiationException e) {
|
||||||
|
throw new DynamicCodeGenException(e.getMessage(), e);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new DynamicCodeGenException(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package org.msgpack.util.codegen;
|
||||||
|
|
||||||
|
import org.msgpack.MessagePacker;
|
||||||
|
|
||||||
|
public class DynamicCodeGenOrdinalEnumPacker {
|
||||||
|
public static MessagePacker create(Class<?> c) {
|
||||||
|
try {
|
||||||
|
DynamicCodeGen gen = DynamicCodeGen.getInstance();
|
||||||
|
Class<?> packerClass = gen.generateOrdinalEnumPackerClass(c);
|
||||||
|
return (MessagePacker)packerClass.newInstance();
|
||||||
|
} catch (InstantiationException e) {
|
||||||
|
throw new DynamicCodeGenException(e.getMessage(), e);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new DynamicCodeGenException(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package org.msgpack.util.codegen;
|
||||||
|
|
||||||
|
import org.msgpack.Template;
|
||||||
|
|
||||||
|
public class DynamicCodeGenOrdinalEnumTemplate {
|
||||||
|
public static Template create(Class<?> c) {
|
||||||
|
try {
|
||||||
|
DynamicCodeGen gen = DynamicCodeGen.getInstance();
|
||||||
|
Class<?> tmplClass = gen.generateOrdinalEnumTemplateClass(c);
|
||||||
|
return (Template) tmplClass.newInstance();
|
||||||
|
} catch (InstantiationException e) {
|
||||||
|
throw new DynamicCodeGenException(e.getMessage(), e);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new DynamicCodeGenException(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package org.msgpack.util.codegen;
|
||||||
|
|
||||||
|
import org.msgpack.MessageUnpacker;
|
||||||
|
|
||||||
|
public class DynamicCodeGenOrdinalEnumUnpacker {
|
||||||
|
public static MessageUnpacker create(Class<?> c) {
|
||||||
|
try {
|
||||||
|
DynamicCodeGen gen = DynamicCodeGen.getInstance();
|
||||||
|
Class<?> unpackerClass = gen.generateOrdinalEnumUnpackerClass(c);
|
||||||
|
return (MessageUnpacker) unpackerClass.newInstance();
|
||||||
|
} catch (InstantiationException e) {
|
||||||
|
throw new DynamicCodeGenException(e.getMessage(), e);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new DynamicCodeGenException(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -21,6 +21,7 @@ import org.msgpack.Packer;
|
|||||||
import org.msgpack.Template;
|
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;
|
||||||
|
|
||||||
public class TestDynamicCodeGenPackerConverter extends TestCase {
|
public class TestDynamicCodeGenPackerConverter extends TestCase {
|
||||||
|
|
||||||
@ -372,9 +373,9 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
|||||||
public void testFinalClassAndAbstractClass01() throws Exception {
|
public void testFinalClassAndAbstractClass01() throws Exception {
|
||||||
try {
|
try {
|
||||||
DynamicCodeGenPacker.create(FinalModifierClass.class);
|
DynamicCodeGenPacker.create(FinalModifierClass.class);
|
||||||
fail();
|
|
||||||
} catch (DynamicCodeGenException e) {
|
|
||||||
assertTrue(true);
|
assertTrue(true);
|
||||||
|
} catch (DynamicCodeGenException e) {
|
||||||
|
fail();
|
||||||
}
|
}
|
||||||
assertTrue(true);
|
assertTrue(true);
|
||||||
try {
|
try {
|
||||||
@ -390,9 +391,9 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
|||||||
public void testFinalClassAndAbstractClass02() throws Exception {
|
public void testFinalClassAndAbstractClass02() throws Exception {
|
||||||
try {
|
try {
|
||||||
DynamicCodeGenTemplate.create(FinalModifierClass.class);
|
DynamicCodeGenTemplate.create(FinalModifierClass.class);
|
||||||
fail();
|
|
||||||
} catch (DynamicCodeGenException e) {
|
|
||||||
assertTrue(true);
|
assertTrue(true);
|
||||||
|
} catch (DynamicCodeGenException e) {
|
||||||
|
fail();
|
||||||
}
|
}
|
||||||
assertTrue(true);
|
assertTrue(true);
|
||||||
try {
|
try {
|
||||||
@ -429,7 +430,7 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInterfaceAndEnumType02() throws Exception {
|
public void testInterfaceType() throws Exception {
|
||||||
try {
|
try {
|
||||||
DynamicCodeGenTemplate.create(SampleInterface.class);
|
DynamicCodeGenTemplate.create(SampleInterface.class);
|
||||||
fail();
|
fail();
|
||||||
@ -437,19 +438,44 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
|
|||||||
assertTrue(true);
|
assertTrue(true);
|
||||||
}
|
}
|
||||||
assertTrue(true);
|
assertTrue(true);
|
||||||
try {
|
|
||||||
DynamicCodeGenTemplate.create(SampleEnum.class);
|
|
||||||
fail();
|
|
||||||
} catch (DynamicCodeGenException e) {
|
|
||||||
assertTrue(true);
|
|
||||||
}
|
|
||||||
assertTrue(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface SampleInterface {
|
public interface SampleInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEnumTypeForOrdinal() throws Exception {
|
||||||
|
SampleEnumFieldClass src = new SampleEnumFieldClass();
|
||||||
|
src.f0 = 0;
|
||||||
|
src.f1 = SampleEnum.ONE;
|
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
MessagePacker packer = DynamicCodeGenPacker
|
||||||
|
.create(SampleEnumFieldClass.class);
|
||||||
|
packer.pack(new Packer(out), src);
|
||||||
|
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||||
|
Template tmpl = DynamicCodeGenTemplate
|
||||||
|
.create(SampleEnumFieldClass.class);
|
||||||
|
Unpacker pac = new Unpacker(in);
|
||||||
|
Iterator<MessagePackObject> it = pac.iterator();
|
||||||
|
assertTrue(it.hasNext());
|
||||||
|
MessagePackObject mpo = it.next();
|
||||||
|
SampleEnumFieldClass dst = (SampleEnumFieldClass) tmpl.convert(mpo);
|
||||||
|
assertTrue(src.f0 == dst.f0);
|
||||||
|
assertTrue(src.f1 == dst.f1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class SampleEnumFieldClass {
|
||||||
|
public int f0;
|
||||||
|
|
||||||
|
public SampleEnum f1;
|
||||||
|
|
||||||
|
public SampleEnumFieldClass() {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@MessagePackOrdinalEnum
|
||||||
public enum SampleEnum {
|
public enum SampleEnum {
|
||||||
|
ONE, TWO, THREE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -18,6 +18,7 @@ import org.msgpack.Packer;
|
|||||||
import org.msgpack.Template;
|
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 junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
@ -351,9 +352,9 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
|||||||
public void testFinalClassAndAbstractClass01() throws Exception {
|
public void testFinalClassAndAbstractClass01() throws Exception {
|
||||||
try {
|
try {
|
||||||
DynamicCodeGenPacker.create(FinalModifierClass.class);
|
DynamicCodeGenPacker.create(FinalModifierClass.class);
|
||||||
fail();
|
|
||||||
} catch (DynamicCodeGenException e) {
|
|
||||||
assertTrue(true);
|
assertTrue(true);
|
||||||
|
} catch (DynamicCodeGenException e) {
|
||||||
|
fail();
|
||||||
}
|
}
|
||||||
assertTrue(true);
|
assertTrue(true);
|
||||||
try {
|
try {
|
||||||
@ -369,9 +370,9 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
|||||||
public void testFinalClassAndAbstractClass02() throws Exception {
|
public void testFinalClassAndAbstractClass02() throws Exception {
|
||||||
try {
|
try {
|
||||||
DynamicCodeGenUnpacker.create(FinalModifierClass.class);
|
DynamicCodeGenUnpacker.create(FinalModifierClass.class);
|
||||||
fail();
|
|
||||||
} catch (DynamicCodeGenException e) {
|
|
||||||
assertTrue(true);
|
assertTrue(true);
|
||||||
|
} catch (DynamicCodeGenException e) {
|
||||||
|
fail();
|
||||||
}
|
}
|
||||||
assertTrue(true);
|
assertTrue(true);
|
||||||
try {
|
try {
|
||||||
@ -390,7 +391,7 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInterfaceAndEnumType01() throws Exception {
|
public void testInterfaceType01() throws Exception {
|
||||||
try {
|
try {
|
||||||
DynamicCodeGenPacker.create(SampleInterface.class);
|
DynamicCodeGenPacker.create(SampleInterface.class);
|
||||||
fail();
|
fail();
|
||||||
@ -398,17 +399,10 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
|||||||
assertTrue(true);
|
assertTrue(true);
|
||||||
}
|
}
|
||||||
assertTrue(true);
|
assertTrue(true);
|
||||||
try {
|
|
||||||
DynamicCodeGenPacker.create(SampleEnum.class);
|
|
||||||
fail();
|
|
||||||
} catch (DynamicCodeGenException e) {
|
|
||||||
assertTrue(true);
|
|
||||||
}
|
|
||||||
assertTrue(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInterfaceAndEnumType02() throws Exception {
|
public void testInterfaceType02() throws Exception {
|
||||||
try {
|
try {
|
||||||
DynamicCodeGenUnpacker.create(SampleInterface.class);
|
DynamicCodeGenUnpacker.create(SampleInterface.class);
|
||||||
fail();
|
fail();
|
||||||
@ -416,19 +410,41 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
|
|||||||
assertTrue(true);
|
assertTrue(true);
|
||||||
}
|
}
|
||||||
assertTrue(true);
|
assertTrue(true);
|
||||||
try {
|
|
||||||
DynamicCodeGenUnpacker.create(SampleEnum.class);
|
|
||||||
fail();
|
|
||||||
} catch (DynamicCodeGenException e) {
|
|
||||||
assertTrue(true);
|
|
||||||
}
|
|
||||||
assertTrue(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface SampleInterface {
|
public interface SampleInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEnumTypeForOrdinal() throws Exception {
|
||||||
|
SampleEnumFieldClass src = new SampleEnumFieldClass();
|
||||||
|
src.f0 = 0;
|
||||||
|
src.f1 = SampleEnum.ONE;
|
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
MessagePacker packer = DynamicCodeGenPacker
|
||||||
|
.create(SampleEnumFieldClass.class);
|
||||||
|
packer.pack(new Packer(out), src);
|
||||||
|
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||||
|
Template tmpl = DynamicCodeGenTemplate
|
||||||
|
.create(SampleEnumFieldClass.class);
|
||||||
|
SampleEnumFieldClass dst = (SampleEnumFieldClass) tmpl
|
||||||
|
.unpack(new Unpacker(in));
|
||||||
|
assertTrue(src.f0 == dst.f0);
|
||||||
|
assertTrue(src.f1 == dst.f1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class SampleEnumFieldClass {
|
||||||
|
public int f0;
|
||||||
|
|
||||||
|
public SampleEnum f1;
|
||||||
|
|
||||||
|
public SampleEnumFieldClass() {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@MessagePackOrdinalEnum
|
||||||
public enum SampleEnum {
|
public enum SampleEnum {
|
||||||
|
ONE, TWO, THREE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user