mirror of
				https://github.com/msgpack/msgpack-c.git
				synced 2025-10-25 18:23:00 +02:00 
			
		
		
		
	Merge branch 'master' of github.com:msgpack/msgpack
This commit is contained in:
		| @@ -3,7 +3,7 @@ | ||||
|   <modelVersion>4.0.0</modelVersion> | ||||
|   <groupId>org.msgpack</groupId> | ||||
|   <artifactId>msgpack</artifactId> | ||||
|   <version>0.3</version> | ||||
|   <version>0.4</version> | ||||
|   <description>MessagePack for Java</description> | ||||
|  | ||||
|   <name>MessagePack for Java</name> | ||||
|   | ||||
| @@ -17,23 +17,22 @@ | ||||
| // | ||||
| package org.msgpack; | ||||
|  | ||||
| import java.util.Map; | ||||
| import java.util.HashMap; | ||||
| import java.util.concurrent.ConcurrentHashMap; | ||||
|  | ||||
| // FIXME package private? | ||||
| public class CustomConverter { | ||||
| 	public static void register(Class target, MessageConverter converter) { | ||||
| 		map.put(target, converter); | ||||
| 	private static ConcurrentHashMap<Class<?>, MessageConverter> map = new ConcurrentHashMap<Class<?>, MessageConverter>(); | ||||
| 	 | ||||
| 	public static void register(Class<?> target, MessageConverter converter) { | ||||
| 		map.putIfAbsent(target, converter); | ||||
| 	} | ||||
|  | ||||
| 	public static MessageConverter get(Class target) { | ||||
| 	public static MessageConverter get(Class<?> target) { | ||||
| 		return map.get(target); | ||||
| 	} | ||||
|  | ||||
| 	public static boolean isRegistered(Class target) { | ||||
| 	public static boolean isRegistered(Class<?> target) { | ||||
| 		return map.containsKey(target); | ||||
| 	} | ||||
|  | ||||
| 	private static Map<Class, MessageConverter> map = new HashMap<Class, MessageConverter>(); | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -17,14 +17,19 @@ | ||||
| // | ||||
| package org.msgpack; | ||||
|  | ||||
| import java.lang.annotation.Annotation; | ||||
|  | ||||
| public class CustomMessage { | ||||
| 	public static void registerPacker(Class target, MessagePacker packer) { | ||||
| 	public static void registerPacker(Class<?> target, MessagePacker packer) { | ||||
| 		CustomPacker.register(target, packer); | ||||
| 	} | ||||
|  | ||||
| 	public static void registerTemplate(Class target, Template tmpl) { | ||||
| 	public static void registerTemplate(Class<?> target, Template tmpl) { | ||||
| 		CustomUnpacker.register(target, tmpl); | ||||
| 		CustomConverter.register(target, tmpl); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| 	static boolean isAnnotated(Class<?> target, Class<? extends Annotation> with) { | ||||
| 		return target.getAnnotation(with) != null; | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -17,23 +17,20 @@ | ||||
| // | ||||
| package org.msgpack; | ||||
|  | ||||
| import java.util.Map; | ||||
| import java.util.HashMap; | ||||
| import java.util.concurrent.ConcurrentHashMap; | ||||
|  | ||||
| // FIXME package private? | ||||
| public class CustomPacker { | ||||
| 	public static void register(Class target, MessagePacker converter) { | ||||
| 		map.put(target, converter); | ||||
| 	private static ConcurrentHashMap<Class<?>, MessagePacker> map = new ConcurrentHashMap<Class<?>, MessagePacker>(); | ||||
|  | ||||
| 	public static void register(Class<?> target, MessagePacker packer) { | ||||
| 		map.putIfAbsent(target, packer); | ||||
| 	} | ||||
|  | ||||
| 	public static MessagePacker get(Class target) { | ||||
| 	public static MessagePacker get(Class<?> target) { | ||||
| 		return map.get(target); | ||||
| 	} | ||||
|  | ||||
| 	public static boolean isRegistered(Class target) { | ||||
| 	public static boolean isRegistered(Class<?> target) { | ||||
| 		return map.containsKey(target); | ||||
| 	} | ||||
|  | ||||
| 	private static Map<Class, MessagePacker> map = new HashMap<Class, MessagePacker>(); | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -17,23 +17,21 @@ | ||||
| // | ||||
| package org.msgpack; | ||||
|  | ||||
| import java.util.Map; | ||||
| import java.util.HashMap; | ||||
| import java.util.concurrent.ConcurrentHashMap; | ||||
|  | ||||
| // FIXME package private? | ||||
| public class CustomUnpacker { | ||||
| 	public static void register(Class target, MessageUnpacker converter) { | ||||
| 		map.put(target, converter); | ||||
| 	private static ConcurrentHashMap<Class<?>, MessageUnpacker> map = new ConcurrentHashMap<Class<?>, MessageUnpacker>(); | ||||
|  | ||||
| 	public static void register(Class<?> target, MessageUnpacker converter) { | ||||
| 		map.putIfAbsent(target, converter); | ||||
| 	} | ||||
|  | ||||
| 	public static MessageUnpacker get(Class target) { | ||||
| 	public static MessageUnpacker get(Class<?> target) { | ||||
| 		return map.get(target); | ||||
| 	} | ||||
|  | ||||
| 	public static boolean isRegistered(Class target) { | ||||
| 	public static boolean isRegistered(Class<?> target) { | ||||
| 		return map.containsKey(target); | ||||
| 	} | ||||
|  | ||||
| 	private static Map<Class, MessageUnpacker> map = new HashMap<Class, MessageUnpacker>(); | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -17,9 +17,7 @@ | ||||
| // | ||||
| package org.msgpack; | ||||
|  | ||||
| import java.io.IOException; | ||||
|  | ||||
| public interface MessageConverter { | ||||
| 	public Object convert(MessagePackObject from) throws MessageTypeException; | ||||
| 	Object convert(MessagePackObject from) throws MessageTypeException; | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -23,6 +23,10 @@ import java.util.Map; | ||||
| import java.math.BigInteger; | ||||
|  | ||||
| public abstract class MessagePackObject implements Cloneable, MessagePackable { | ||||
| 	static { | ||||
| 		Templates.load(); | ||||
| 	} | ||||
|  | ||||
| 	public boolean isNil() { | ||||
| 		return false; | ||||
| 	} | ||||
|   | ||||
| @@ -20,6 +20,6 @@ package org.msgpack; | ||||
| import java.io.IOException; | ||||
|  | ||||
| public interface MessagePacker { | ||||
| 	public void pack(Packer pk, Object target) throws IOException; | ||||
| 	void pack(Packer pk, Object target) throws IOException; | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -20,6 +20,6 @@ package org.msgpack; | ||||
| import java.io.IOException; | ||||
|  | ||||
| public interface MessageUnpacker { | ||||
| 	public Object unpack(Unpacker pac) throws IOException, MessageTypeException; | ||||
| 	Object unpack(Unpacker pac) throws IOException, MessageTypeException; | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -24,6 +24,10 @@ import java.util.List; | ||||
| import java.util.Map; | ||||
| import java.math.BigInteger; | ||||
|  | ||||
| import org.msgpack.annotation.MessagePackDelegate; | ||||
| import org.msgpack.annotation.MessagePackMessage; | ||||
| import org.msgpack.annotation.MessagePackOrdinalEnum; | ||||
|  | ||||
| /** | ||||
|  * Packer enables you to serialize objects into OutputStream. | ||||
|  * | ||||
| @@ -475,17 +479,26 @@ public class Packer { | ||||
| 			return packBigInteger((BigInteger)o); | ||||
| 		}  | ||||
|  | ||||
| 		Class klass = o.getClass(); | ||||
|  | ||||
| 		Class<?> klass = o.getClass(); | ||||
| 		MessagePacker packer = CustomPacker.get(klass); | ||||
| 		if(packer != null) { | ||||
| 			packer.pack(this, o); | ||||
| 			return this; | ||||
| 		} else if (CustomMessage.isAnnotated(klass, MessagePackMessage.class)) { | ||||
| 			packer = ReflectionPacker.create(klass); | ||||
| 			packer.pack(this, o); | ||||
| 			return this; | ||||
| 		} else if (CustomMessage.isAnnotated(klass, MessagePackDelegate.class)) { | ||||
| 			// FIXME DelegatePacker | ||||
| 			throw new UnsupportedOperationException("not supported yet. : " + klass.getName()); | ||||
| 		} else if (CustomMessage.isAnnotated(klass, MessagePackOrdinalEnum.class)) { | ||||
| 			// FIXME OrdinalEnumPacker | ||||
| 			throw new UnsupportedOperationException("not supported yet. : " + klass.getName()); | ||||
| 		} | ||||
| 		if (packer != null) { | ||||
| 			CustomMessage.registerPacker(klass, packer); | ||||
| 		} | ||||
|  | ||||
| 		// FIXME check annotations -> code generation -> CustomMessage.registerPacker | ||||
|  | ||||
| 		throw new MessageTypeException("unknown object "+o+" ("+o.getClass()+")"); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -20,6 +20,8 @@ package org.msgpack; | ||||
| import org.msgpack.template.*; | ||||
|  | ||||
| public class Templates { | ||||
| 	public static void load() { } | ||||
|  | ||||
| 	public static Template tList(Template elementTemplate) { | ||||
| 		return new ListTemplate(elementTemplate); | ||||
| 	} | ||||
|   | ||||
| @@ -24,6 +24,10 @@ import java.util.Iterator; | ||||
| import java.nio.ByteBuffer; | ||||
| import java.math.BigInteger; | ||||
|  | ||||
| import org.msgpack.annotation.MessagePackDelegate; | ||||
| import org.msgpack.annotation.MessagePackMessage; | ||||
| import org.msgpack.annotation.MessagePackOrdinalEnum; | ||||
|  | ||||
| /** | ||||
|  * Unpacker enables you to deserialize objects from stream. | ||||
|  * | ||||
| @@ -103,6 +107,9 @@ import java.math.BigInteger; | ||||
|  * </pre> | ||||
|  */ | ||||
| public class Unpacker implements Iterable<MessagePackObject> { | ||||
| 	static { | ||||
| 		Templates.load(); | ||||
| 	} | ||||
|  | ||||
| 	// buffer: | ||||
| 	// +---------------------------------------------+ | ||||
| @@ -573,7 +580,7 @@ public class Unpacker implements Iterable<MessagePackObject> { | ||||
| 		obj.messageUnpack(this); | ||||
| 	} | ||||
|  | ||||
| 	final public Object unpack(Class klass) throws IOException, MessageTypeException, InstantiationException, IllegalAccessException { | ||||
| 	final public Object unpack(Class<?> klass) throws IOException, MessageTypeException, InstantiationException, IllegalAccessException { | ||||
| 		if(MessageUnpackable.class.isAssignableFrom(klass)) { | ||||
| 			Object obj = klass.newInstance(); | ||||
| 			((MessageUnpackable)obj).messageUnpack(this); | ||||
| @@ -585,13 +592,24 @@ public class Unpacker implements Iterable<MessagePackObject> { | ||||
| 			return unpacker.unpack(this); | ||||
| 		} | ||||
| 		 | ||||
| 		// FIXME check annotations -> code generation -> CustomMessage.registerTemplate | ||||
|  | ||||
| 		Template tmpl = null; | ||||
| 		if (CustomMessage.isAnnotated(klass, MessagePackMessage.class)) { | ||||
| 			tmpl = ReflectionTemplate.create(klass); | ||||
| 			return tmpl.unpack(this); | ||||
| 		} else if (CustomMessage.isAnnotated(klass, MessagePackDelegate.class)) { | ||||
| 			// FIXME DelegateTemplate | ||||
| 			throw new UnsupportedOperationException("not supported yet. : " + klass.getName()); | ||||
| 		} else if (CustomMessage.isAnnotated(klass, MessagePackOrdinalEnum.class)) { | ||||
| 			// FIXME OrdinalEnumTemplate | ||||
| 			throw new UnsupportedOperationException("not supported yet. : " + klass.getName()); | ||||
| 		} | ||||
| 		if (tmpl != null) { | ||||
| 			CustomMessage.registerTemplate(klass, tmpl); | ||||
| 		} | ||||
| 		MessageConverter converter = CustomConverter.get(klass); | ||||
| 		if(converter != null) { | ||||
| 			return converter.convert(unpackObject()); | ||||
| 		} | ||||
|  | ||||
| 		throw new MessageTypeException(); | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -1,4 +1,4 @@ | ||||
| package org.msgpack.util.annotation; | ||||
| package org.msgpack.annotation; | ||||
| 
 | ||||
| import java.lang.annotation.ElementType; | ||||
| import java.lang.annotation.Retention; | ||||
| @@ -7,5 +7,6 @@ import java.lang.annotation.Target; | ||||
| 
 | ||||
| @Target(ElementType.TYPE) | ||||
| @Retention(RetentionPolicy.RUNTIME) | ||||
| public @interface MessagePackUnpackable { | ||||
| public @interface MessagePackDelegate { | ||||
| 
 | ||||
| } | ||||
| @@ -0,0 +1,12 @@ | ||||
| package org.msgpack.annotation; | ||||
|  | ||||
| import java.lang.annotation.ElementType; | ||||
| import java.lang.annotation.Retention; | ||||
| import java.lang.annotation.RetentionPolicy; | ||||
| import java.lang.annotation.Target; | ||||
|  | ||||
| @Target(ElementType.TYPE) | ||||
| @Retention(RetentionPolicy.RUNTIME) | ||||
| public @interface MessagePackMessage { | ||||
|  | ||||
| } | ||||
| @@ -1,4 +1,4 @@ | ||||
| package org.msgpack.util.annotation; | ||||
| package org.msgpack.annotation; | ||||
| 
 | ||||
| import java.lang.annotation.ElementType; | ||||
| import java.lang.annotation.Retention; | ||||
| @@ -0,0 +1,12 @@ | ||||
| package org.msgpack.annotation; | ||||
|  | ||||
| import java.lang.annotation.ElementType; | ||||
| import java.lang.annotation.Retention; | ||||
| import java.lang.annotation.RetentionPolicy; | ||||
| import java.lang.annotation.Target; | ||||
|  | ||||
| @Target(ElementType.TYPE) | ||||
| @Retention(RetentionPolicy.RUNTIME) | ||||
| public @interface MessagePackOrdinalEnum { | ||||
|  | ||||
| } | ||||
| @@ -1,4 +1,4 @@ | ||||
| package org.msgpack.util.annotation; | ||||
| package org.msgpack.annotation; | ||||
| 
 | ||||
| import java.lang.annotation.ElementType; | ||||
| import java.lang.annotation.Retention; | ||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @@ -1,12 +0,0 @@ | ||||
| package org.msgpack.util.annotation; | ||||
|  | ||||
| public class PackUnpackUtilException extends RuntimeException { | ||||
|  | ||||
| 	public PackUnpackUtilException(String reason) { | ||||
| 		super(reason); | ||||
| 	} | ||||
| 	 | ||||
| 	public PackUnpackUtilException(String reason, Throwable t) { | ||||
| 		super(reason, t); | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										162
									
								
								java/src/main/java/org/msgpack/util/codegen/BasicConstants.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										162
									
								
								java/src/main/java/org/msgpack/util/codegen/BasicConstants.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,162 @@ | ||||
| package org.msgpack.util.codegen; | ||||
|  | ||||
| public interface BasicConstants { | ||||
|  | ||||
| 	String KEYWORD_MODIFIER_PUBLIC = "public"; | ||||
|  | ||||
| 	String KEYWORD_CATCH = "catch"; | ||||
|  | ||||
| 	String KEYWORD_ELSE = "else"; | ||||
|  | ||||
| 	String KEYWORD_ELSEIF = "else if"; | ||||
|  | ||||
| 	String KEYWORD_FOR = "for"; | ||||
|  | ||||
| 	String KEYWORD_IF = "if"; | ||||
|  | ||||
| 	String KEYWORD_INSTANCEOF = "instanceof"; | ||||
|  | ||||
| 	String KEYWORD_NEW = "new"; | ||||
|  | ||||
| 	String KEYWORD_NULL = "null"; | ||||
| 	 | ||||
| 	String KEYWORD_RETURN = "return"; | ||||
|  | ||||
| 	String KEYWORD_THROW = "throw"; | ||||
|  | ||||
| 	String KEYWORD_THROWS = "throws"; | ||||
|  | ||||
| 	String KEYWORD_TRY = "try"; | ||||
|  | ||||
| 	String CHAR_NAME_SPACE = " "; | ||||
|  | ||||
| 	String CHAR_NAME_COMMA = ","; | ||||
|  | ||||
| 	String CHAR_NAME_EQUAL = "="; | ||||
|  | ||||
| 	String CHAR_NAME_PLUS = "+"; | ||||
|  | ||||
| 	String CHAR_NAME_MINUS = "-"; | ||||
|  | ||||
| 	String CHAR_NAME_LESSTHAN = "<"; | ||||
|  | ||||
| 	String CHAR_NAME_RIGHT_PARENTHESIS = ")"; | ||||
|  | ||||
| 	String CHAR_NAME_LEFT_PARENTHESIS = "("; | ||||
|  | ||||
| 	String CHAR_NAME_RIGHT_CURLY_BRACKET = "}"; | ||||
|  | ||||
| 	String CHAR_NAME_LEFT_CURLY_BRACKET = "{"; | ||||
|  | ||||
| 	String CHAR_NAME_RIGHT_SQUARE_BRACKET = "]"; | ||||
|  | ||||
| 	String CHAR_NAME_LEFT_SQUARE_BRACKET = "["; | ||||
|  | ||||
| 	String CHAR_NAME_DOT = "."; | ||||
|  | ||||
| 	String CHAR_NAME_SEMICOLON = ";"; | ||||
|  | ||||
| 	String VARIABLE_NAME_PK = "_$$_pk"; | ||||
|  | ||||
| 	String VARIABLE_NAME_OBJECT = "_$$_obj"; | ||||
|  | ||||
| 	String VARIABLE_NAME_TARGET = "_$$_target"; | ||||
|  | ||||
| 	String VARIABLE_NAME_SIZE = "_$$_len"; | ||||
|  | ||||
| 	String VARIABLE_NAME_ARRAY = "_$$_ary"; | ||||
|  | ||||
| 	String VARIABLE_NAME_LIST = "_$$_list"; | ||||
|  | ||||
| 	String VARIABLE_NAME_MAP = "_$$_map"; | ||||
|  | ||||
| 	String VARIABLE_NAME_KEY = "_$$_key"; | ||||
|  | ||||
| 	String VARIABLE_NAME_VAL = "_$$_val"; | ||||
|  | ||||
| 	String VARIABLE_NAME_ITER = "_$$_iter"; | ||||
|  | ||||
| 	String VARIABLE_NAME_MPO = "_$$_mpo"; | ||||
|  | ||||
| 	String VARIABLE_NAME_I = "i"; | ||||
|  | ||||
| 	String METHOD_NAME_VALUEOF = "valueOf"; | ||||
|  | ||||
| 	String METHOD_NAME_ADD = "add"; | ||||
|  | ||||
| 	String METHOD_NAME_PUT = "put"; | ||||
|  | ||||
| 	String METHOD_NAME_GET = "get"; | ||||
|  | ||||
| 	String METHOD_NAME_SIZE = "size"; | ||||
|  | ||||
| 	String METHOD_NAME_KEYSET = "keySet"; | ||||
|  | ||||
| 	String METHOD_NAME_ITERATOR = "iterator"; | ||||
|  | ||||
| 	String METHOD_NAME_HASNEXT = "hasNext"; | ||||
|  | ||||
| 	String METHOD_NAME_NEXT = "next"; | ||||
|  | ||||
| 	String METHOD_NAME_CONVERT = "convert"; | ||||
| 	 | ||||
| 	String METHOD_NAME_MSGCONVERT = "messageConvert"; | ||||
|  | ||||
| 	String METHOD_NAME_PACK = "pack"; | ||||
|  | ||||
| 	String METHOD_NAME_PACKARRAY = "packArray"; | ||||
|  | ||||
| 	String METHOD_NAME_UNPACK = "unpack"; | ||||
|  | ||||
| 	String METHOD_NAME_UNPACKBOOLEAN = "unpackBoolean"; | ||||
|  | ||||
| 	String METHOD_NAME_UNPACKBYTE = "unpackByte"; | ||||
|  | ||||
| 	String METHOD_NAME_UNPACKDOUBLE = "unpackDouble"; | ||||
|  | ||||
| 	String METHOD_NAME_UNPACKFLOAT = "unpackFloat"; | ||||
|  | ||||
| 	String METHOD_NAME_UNPACKINT = "unpackInt"; | ||||
|  | ||||
| 	String METHOD_NAME_UNPACKLONG = "unpackLong"; | ||||
|  | ||||
| 	String METHOD_NAME_UNPACKSHORT = "unpackShort"; | ||||
|  | ||||
| 	String METHOD_NAME_UNPACKSTRING = "unpackString"; | ||||
|  | ||||
| 	String METHOD_NAME_UNPACKBIGINTEGER = "unpackBigInteger"; | ||||
|  | ||||
| 	String METHOD_NAME_UNPACKOBJECT = "unpackObject"; | ||||
|  | ||||
| 	String METHOD_NAME_UNPACKBYTEARRAY = "unpackByteArray"; | ||||
|  | ||||
| 	String METHOD_NAME_UNPACKARRAY = "unpackArray"; | ||||
|  | ||||
| 	String METHOD_NAME_UNPACKMAP = "unpackMap"; | ||||
|  | ||||
| 	String METHOD_NAME_ASARRAY = "asArray"; | ||||
|  | ||||
| 	String METHOD_NAME_ASBOOLEAN = "asBoolean"; | ||||
|  | ||||
| 	String METHOD_NAME_ASBYTE = "asByte"; | ||||
|  | ||||
| 	String METHOD_NAME_ASSHORT = "asShort"; | ||||
|  | ||||
| 	String METHOD_NAME_ASINT = "asInt"; | ||||
|  | ||||
| 	String METHOD_NAME_ASFLOAT = "asFloat"; | ||||
|  | ||||
| 	String METHOD_NAME_ASLONG = "asLong"; | ||||
|  | ||||
| 	String METHOD_NAME_ASDOUBLE = "asDouble"; | ||||
|  | ||||
| 	String METHOD_NAME_ASSTRING = "asString"; | ||||
|  | ||||
| 	String METHOD_NAME_ASBYTEARRAY = "asByteArray"; | ||||
|  | ||||
| 	String METHOD_NAME_ASBIGINTEGER = "asBigInteger"; | ||||
|  | ||||
| 	String METHOD_NAME_ASLIST = "asList"; | ||||
|  | ||||
| 	String METHOD_NAME_ASMAP = "asMap"; | ||||
| } | ||||
| @@ -0,0 +1,9 @@ | ||||
| package org.msgpack.util.codegen; | ||||
|  | ||||
| public interface Constants extends BasicConstants { | ||||
| 	String POSTFIX_TYPE_NAME_PACKER = "_$$_Packer"; | ||||
|  | ||||
| 	String POSTFIX_TYPE_NAME_UNPACKER = "_$$_Unpacker"; | ||||
|  | ||||
| 	String POSTFIX_TYPE_NAME_TEMPLATE = "_$$_Template"; | ||||
| } | ||||
							
								
								
									
										1053
									
								
								java/src/main/java/org/msgpack/util/codegen/DynamicCodeGen.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1053
									
								
								java/src/main/java/org/msgpack/util/codegen/DynamicCodeGen.java
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @@ -0,0 +1,258 @@ | ||||
| package org.msgpack.util.codegen; | ||||
|  | ||||
| import java.math.BigInteger; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
|  | ||||
| public class DynamicCodeGenBase implements BasicConstants { | ||||
| 	public DynamicCodeGenBase() { | ||||
| 	} | ||||
|  | ||||
| 	public void addPublicFieldDecl(StringBuilder sb, Class<?> type, String name) { | ||||
| 		sb.append(KEYWORD_MODIFIER_PUBLIC); | ||||
| 		sb.append(CHAR_NAME_SPACE); | ||||
| 		sb.append(type.getName()); | ||||
| 		sb.append(CHAR_NAME_SPACE); | ||||
| 		sb.append(name); | ||||
| 	} | ||||
|  | ||||
| 	public void addPublicMethodDecl(StringBuilder sb, String mname, | ||||
| 			Class<?> returnType, Class<?>[] paramTypes, String[] anames, | ||||
| 			Class<?>[] exceptTypes, String methodBody) { | ||||
| 		sb.append(KEYWORD_MODIFIER_PUBLIC); | ||||
| 		sb.append(CHAR_NAME_SPACE); | ||||
| 		sb.append(returnType.getName()); | ||||
| 		sb.append(CHAR_NAME_SPACE); | ||||
| 		sb.append(mname); | ||||
| 		sb.append(CHAR_NAME_LEFT_PARENTHESIS); | ||||
| 		for (int i = 0; i < paramTypes.length; ++i) { | ||||
| 			sb.append(paramTypes[i].getName()); | ||||
| 			sb.append(CHAR_NAME_SPACE); | ||||
| 			sb.append(anames[i]); | ||||
| 			if (i + 1 != paramTypes.length) { | ||||
| 				sb.append(CHAR_NAME_COMMA); | ||||
| 				sb.append(CHAR_NAME_SPACE); | ||||
| 			} | ||||
| 		} | ||||
| 		sb.append(CHAR_NAME_RIGHT_PARENTHESIS); | ||||
| 		sb.append(CHAR_NAME_SPACE); | ||||
| 		sb.append(KEYWORD_THROWS); | ||||
| 		sb.append(CHAR_NAME_SPACE); | ||||
| 		for (int i = 0; i < exceptTypes.length; ++i) { | ||||
| 			sb.append(exceptTypes[i].getName()); | ||||
| 			if (i + 1 != exceptTypes.length) { | ||||
| 				sb.append(CHAR_NAME_COMMA); | ||||
| 				sb.append(CHAR_NAME_SPACE); | ||||
| 			} | ||||
| 		} | ||||
| 		sb.append(CHAR_NAME_SPACE); | ||||
| 		sb.append(CHAR_NAME_LEFT_CURLY_BRACKET); | ||||
| 		sb.append(CHAR_NAME_SPACE); | ||||
| 		sb.append(methodBody); | ||||
| 		sb.append(CHAR_NAME_RIGHT_CURLY_BRACKET); | ||||
| 		sb.append(CHAR_NAME_SPACE); | ||||
| 	} | ||||
|  | ||||
| 	public void insertSemicolon(StringBuilder sb) { | ||||
| 		// ; | ||||
| 		sb.append(CHAR_NAME_SEMICOLON); | ||||
| 		sb.append(CHAR_NAME_SPACE); | ||||
| 	} | ||||
|  | ||||
| 	public void insertLocalVariableDecl(StringBuilder sb, Class<?> type, | ||||
| 			String name) { | ||||
| 		// int lv | ||||
| 		sb.append(type.getName()); | ||||
| 		sb.append(CHAR_NAME_SPACE); | ||||
| 		sb.append(name); | ||||
| 	} | ||||
|  | ||||
| 	public void insertValueInsertion(StringBuilder sb, String expr) { | ||||
| 		// = expr | ||||
| 		sb.append(CHAR_NAME_SPACE); | ||||
| 		sb.append(CHAR_NAME_EQUAL); | ||||
| 		sb.append(CHAR_NAME_SPACE); | ||||
| 		sb.append(expr); | ||||
| 	} | ||||
|  | ||||
| 	public void insertFieldAccess(StringBuilder sb, String target, String field) { | ||||
| 		// target.field | ||||
| 		sb.append(target); | ||||
| 		sb.append(CHAR_NAME_DOT); | ||||
| 		sb.append(field); | ||||
| 	} | ||||
|  | ||||
| 	public void insertDefaultConsCall(StringBuilder sb, Class<?> type) { | ||||
| 		// new tname() | ||||
| 		insertConsCall(sb, type, null); | ||||
| 	} | ||||
|  | ||||
| 	public void insertConsCall(StringBuilder sb, Class<?> type, String expr) { | ||||
| 		// new tname(expr) | ||||
| 		sb.append(KEYWORD_NEW); | ||||
| 		sb.append(CHAR_NAME_SPACE); | ||||
| 		sb.append(type.getName()); | ||||
| 		sb.append(CHAR_NAME_LEFT_PARENTHESIS); | ||||
| 		if (expr != null) { | ||||
| 			sb.append(expr); | ||||
| 		} | ||||
| 		sb.append(CHAR_NAME_RIGHT_PARENTHESIS); | ||||
| 	} | ||||
|  | ||||
| 	public void insertMethodCall(StringBuilder sb, String tname, String mname, | ||||
| 			String[] anames) { | ||||
| 		// tname.mname(anames[0], anames[1], ...) | ||||
| 		int len = anames.length; | ||||
| 		sb.append(tname); | ||||
| 		sb.append(CHAR_NAME_DOT); | ||||
| 		sb.append(mname); | ||||
| 		sb.append(CHAR_NAME_LEFT_PARENTHESIS); | ||||
| 		for (int i = 0; i < len; ++i) { | ||||
| 			sb.append(anames[i]); | ||||
| 			if (i + 1 != len) { | ||||
| 				sb.append(CHAR_NAME_COMMA); | ||||
| 				sb.append(CHAR_NAME_SPACE); | ||||
| 			} | ||||
| 		} | ||||
| 		sb.append(CHAR_NAME_RIGHT_PARENTHESIS); | ||||
| 	} | ||||
|  | ||||
| 	public void insertTypeCast(StringBuilder sb, Class<?> type) { | ||||
| 		// (type) | ||||
| 		sb.append(CHAR_NAME_LEFT_PARENTHESIS); | ||||
| 		sb.append(type.getName()); | ||||
| 		sb.append(CHAR_NAME_RIGHT_PARENTHESIS); | ||||
| 	} | ||||
|  | ||||
| 	public void insertTypeCast(StringBuilder sb, Class<?> type, String varName) { | ||||
| 		// ((type)var) | ||||
| 		sb.append(CHAR_NAME_LEFT_PARENTHESIS); | ||||
| 		sb.append(CHAR_NAME_LEFT_PARENTHESIS); | ||||
| 		sb.append(type.getName()); | ||||
| 		sb.append(CHAR_NAME_RIGHT_PARENTHESIS); | ||||
| 		sb.append(varName); | ||||
| 		sb.append(CHAR_NAME_RIGHT_PARENTHESIS); | ||||
| 	} | ||||
|  | ||||
| 	public void insertReturnStat(StringBuilder sb, String expr) { | ||||
| 		// return expr | ||||
| 		sb.append(KEYWORD_RETURN); | ||||
| 		sb.append(CHAR_NAME_SPACE); | ||||
| 		sb.append(expr); | ||||
| 	} | ||||
|  | ||||
| 	public void insertTypeConvToObjectType(StringBuilder sb, Class<?> type, | ||||
| 			String expr) throws DynamicCodeGenException { | ||||
| 		if (type.isPrimitive()) { // primitive type | ||||
| 			if (type.equals(boolean.class)) { | ||||
| 				// new Boolean(expr) | ||||
| 				insertConsCall(sb, Boolean.class, expr); | ||||
| 			} else if (type.equals(byte.class)) { | ||||
| 				insertConsCall(sb, Byte.class, expr); | ||||
| 			} else if (type.equals(short.class)) { | ||||
| 				insertConsCall(sb, Short.class, expr); | ||||
| 			} else if (type.equals(int.class)) { | ||||
| 				insertConsCall(sb, Integer.class, expr); | ||||
| 			} else if (type.equals(long.class)) { | ||||
| 				insertConsCall(sb, Long.class, expr); | ||||
| 			} else if (type.equals(float.class)) { | ||||
| 				insertConsCall(sb, Float.class, expr); | ||||
| 			} else if (type.equals(double.class)) { | ||||
| 				insertConsCall(sb, Double.class, expr); | ||||
| 			} else { | ||||
| 				throw new DynamicCodeGenException("Type error: " | ||||
| 						+ type.getName()); | ||||
| 			} | ||||
| 		} else { // reference type | ||||
| 			sb.append(expr); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	public void insertTryCatchBlocks(StringBuilder sb, String tryBody, | ||||
| 			List<Class<?>> types, List<String> names, List<String> catchBodies) { | ||||
| 		int len = types.size(); | ||||
| 		sb.append(KEYWORD_TRY); | ||||
| 		sb.append(CHAR_NAME_SPACE); | ||||
| 		sb.append(CHAR_NAME_LEFT_CURLY_BRACKET); | ||||
| 		sb.append(CHAR_NAME_SPACE); | ||||
| 		sb.append(tryBody); | ||||
| 		sb.append(CHAR_NAME_RIGHT_CURLY_BRACKET); | ||||
| 		sb.append(CHAR_NAME_SPACE); | ||||
| 		for (int i = 0; i < len; ++i) { | ||||
| 			sb.append(KEYWORD_CATCH); | ||||
| 			sb.append(CHAR_NAME_SPACE); | ||||
| 			sb.append(CHAR_NAME_LEFT_PARENTHESIS); | ||||
| 			sb.append(types.get(i).getName()); | ||||
| 			sb.append(CHAR_NAME_SPACE); | ||||
| 			sb.append(names.get(i)); | ||||
| 			sb.append(CHAR_NAME_RIGHT_PARENTHESIS); | ||||
| 			sb.append(CHAR_NAME_SPACE); | ||||
| 			sb.append(CHAR_NAME_LEFT_CURLY_BRACKET); | ||||
| 			sb.append(CHAR_NAME_SPACE); | ||||
| 			sb.append(catchBodies.get(i)); | ||||
| 			sb.append(CHAR_NAME_RIGHT_CURLY_BRACKET); | ||||
| 			sb.append(CHAR_NAME_SPACE); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	public String getUnpackMethodName(Class<?> c) | ||||
| 			throws DynamicCodeGenException { | ||||
| 		if (c.equals(boolean.class) || c.equals(Boolean.class)) { | ||||
| 			return METHOD_NAME_UNPACKBOOLEAN; | ||||
| 		} else if (c.equals(byte.class) || c.equals(Byte.class)) { | ||||
| 			return METHOD_NAME_UNPACKBYTE; | ||||
| 		} else if (c.equals(short.class) || c.equals(Short.class)) { | ||||
| 			return METHOD_NAME_UNPACKSHORT; | ||||
| 		} else if (c.equals(int.class) || c.equals(Integer.class)) { | ||||
| 			return METHOD_NAME_UNPACKINT; | ||||
| 		} else if (c.equals(float.class) || c.equals(Float.class)) { | ||||
| 			return METHOD_NAME_UNPACKFLOAT; | ||||
| 		} else if (c.equals(long.class) || c.equals(Long.class)) { | ||||
| 			return METHOD_NAME_UNPACKLONG; | ||||
| 		} else if (c.equals(double.class) || c.equals(Double.class)) { | ||||
| 			return METHOD_NAME_UNPACKDOUBLE; | ||||
| 		} else if (c.equals(String.class)) { | ||||
| 			return METHOD_NAME_UNPACKSTRING; | ||||
| 		} else if (c.equals(byte[].class)) { | ||||
| 			return METHOD_NAME_UNPACKBYTEARRAY; | ||||
| 		} else if (c.equals(BigInteger.class)) { | ||||
| 			return METHOD_NAME_UNPACKBIGINTEGER; | ||||
| 		} else if (List.class.isAssignableFrom(c)) { | ||||
| 			return METHOD_NAME_UNPACK; | ||||
| 		} else if (Map.class.isAssignableFrom(c)) { | ||||
| 			return METHOD_NAME_UNPACK; | ||||
| 		} else { | ||||
| 			throw new DynamicCodeGenException("Type error: " + c.getName()); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	public String getAsMethodName(Class<?> c) throws DynamicCodeGenException { | ||||
| 		if (c.equals(boolean.class) || c.equals(Boolean.class)) { | ||||
| 			return METHOD_NAME_ASBOOLEAN; | ||||
| 		} else if (c.equals(byte.class) || c.equals(Byte.class)) { | ||||
| 			return METHOD_NAME_ASBYTE; | ||||
| 		} else if (c.equals(short.class) || c.equals(Short.class)) { | ||||
| 			return METHOD_NAME_ASSHORT; | ||||
| 		} else if (c.equals(int.class) || c.equals(Integer.class)) { | ||||
| 			return METHOD_NAME_ASINT; | ||||
| 		} else if (c.equals(float.class) || c.equals(Float.class)) { | ||||
| 			return METHOD_NAME_ASFLOAT; | ||||
| 		} else if (c.equals(long.class) || c.equals(Long.class)) { | ||||
| 			return METHOD_NAME_ASLONG; | ||||
| 		} else if (c.equals(double.class) || c.equals(Double.class)) { | ||||
| 			return METHOD_NAME_ASDOUBLE; | ||||
| 		} else if (c.equals(String.class)) { | ||||
| 			return METHOD_NAME_ASSTRING; | ||||
| 		} else if (c.equals(byte[].class)) { | ||||
| 			return METHOD_NAME_ASBYTEARRAY; | ||||
| 		} else if (c.equals(BigInteger.class)) { | ||||
| 			return METHOD_NAME_ASBIGINTEGER; | ||||
| 		} else if (List.class.isAssignableFrom(c)) { | ||||
| 			return METHOD_NAME_ASLIST; | ||||
| 		} else if (Map.class.isAssignableFrom(c)) { | ||||
| 			return METHOD_NAME_ASMAP; | ||||
| 		} else { | ||||
| 			throw new DynamicCodeGenException("Type error: " + c.getName()); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| @@ -0,0 +1,12 @@ | ||||
| package org.msgpack.util.codegen; | ||||
|  | ||||
| public class DynamicCodeGenException extends RuntimeException { | ||||
|  | ||||
| 	public DynamicCodeGenException(String reason) { | ||||
| 		super(reason); | ||||
| 	} | ||||
| 	 | ||||
| 	public DynamicCodeGenException(String reason, Throwable t) { | ||||
| 		super(reason, t); | ||||
| 	} | ||||
| } | ||||
| @@ -0,0 +1,18 @@ | ||||
| package org.msgpack.util.codegen; | ||||
|  | ||||
| import org.msgpack.MessagePacker; | ||||
|  | ||||
| public class DynamicCodeGenPacker { | ||||
|  | ||||
| 	public static MessagePacker create(Class<?> c) { | ||||
| 		try { | ||||
| 			DynamicCodeGen gen = DynamicCodeGen.getInstance(); | ||||
| 			Class<?> packerClass = gen.generateMessagePackerClass(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 DynamicCodeGenTemplate { | ||||
| 	public static Template create(Class<?> c) { | ||||
| 		try { | ||||
| 			DynamicCodeGen gen = DynamicCodeGen.getInstance(); | ||||
| 			Class<?> tmplClass = gen.generateTemplateClass(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 DynamicCodeGenUnpacker { | ||||
| 	public static MessageUnpacker create(Class<?> c) { | ||||
| 		try { | ||||
| 			DynamicCodeGen gen = DynamicCodeGen.getInstance(); | ||||
| 			Class<?> unpackerClass = gen.generateMessageUnpackerClass(c); | ||||
| 			return (MessageUnpacker) unpackerClass.newInstance(); | ||||
| 		} catch (InstantiationException e) { | ||||
| 			throw new DynamicCodeGenException(e.getMessage(), e); | ||||
| 		} catch (IllegalAccessException e) { | ||||
| 			throw new DynamicCodeGenException(e.getMessage(), e); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| @@ -18,8 +18,6 @@ public class TestReflectionPackerTemplate { | ||||
|  | ||||
| 	@Test | ||||
| 	public void testPackConvert() throws Exception { | ||||
| 		tString();  // FIXME link StringTemplate | ||||
|  | ||||
| 		ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||||
|  | ||||
| 		MessagePacker packer = ReflectionPacker.create(StringFieldClass.class); | ||||
| @@ -42,5 +40,31 @@ public class TestReflectionPackerTemplate { | ||||
| 		assertEquals(src.s1, dst.s1); | ||||
| 		assertEquals(src.s2, dst.s2); | ||||
| 	} | ||||
|  | ||||
| 	@Test | ||||
| 	public void testPackConvert02() throws Exception { | ||||
| 		ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||||
|  | ||||
| 		CustomPacker.register(StringFieldClass.class, ReflectionPacker.create(StringFieldClass.class)); | ||||
| 				 | ||||
|  | ||||
| 		StringFieldClass src = new StringFieldClass(); | ||||
|  | ||||
| 		src.s1 = "kumofs"; | ||||
| 		src.s2 = "frsyuki"; | ||||
|  | ||||
| 		new Packer(out).pack(src); | ||||
|  | ||||
| 		Template tmpl = ReflectionTemplate.create(StringFieldClass.class); | ||||
|  | ||||
| 		ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); | ||||
|  | ||||
| 		Object obj = tmpl.unpack(new Unpacker(in)); | ||||
| 		assertEquals(obj.getClass(), StringFieldClass.class); | ||||
|  | ||||
| 		StringFieldClass dst = (StringFieldClass)obj; | ||||
| 		assertEquals(src.s1, dst.s1); | ||||
| 		assertEquals(src.s2, dst.s2); | ||||
| 	} | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -0,0 +1,77 @@ | ||||
| package org.msgpack.util; | ||||
|  | ||||
| import java.io.ByteArrayInputStream; | ||||
| import java.io.ByteArrayOutputStream; | ||||
|  | ||||
| import org.junit.Test; | ||||
| import org.msgpack.CustomPacker; | ||||
| import org.msgpack.MessagePacker; | ||||
| import org.msgpack.Packer; | ||||
| import org.msgpack.ReflectionTemplate; | ||||
| import org.msgpack.Template; | ||||
| import org.msgpack.Unpacker; | ||||
| import org.msgpack.util.codegen.DynamicCodeGenPacker; | ||||
| import org.msgpack.util.codegen.DynamicCodeGenTemplate; | ||||
|  | ||||
| import static org.junit.Assert.*; | ||||
|  | ||||
| public class TestDynamicCodeGenPackerTemplate { | ||||
|  | ||||
| 	public static class StringFieldClass { | ||||
| 		public String s1; | ||||
| 		public String s2; | ||||
| 		public StringFieldClass() { } | ||||
| 	} | ||||
|  | ||||
| 	@Test | ||||
| 	public void testPackConvert() throws Exception { | ||||
| 		ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||||
|  | ||||
| 		MessagePacker packer = DynamicCodeGenPacker.create(StringFieldClass.class); | ||||
|  | ||||
| 		StringFieldClass src = new StringFieldClass(); | ||||
|  | ||||
| 		src.s1 = "kumofs"; | ||||
| 		src.s2 = "frsyuki"; | ||||
|  | ||||
| 		packer.pack(new Packer(out), src); | ||||
|  | ||||
| 		Template tmpl = DynamicCodeGenTemplate.create(StringFieldClass.class); | ||||
|  | ||||
| 		ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); | ||||
|  | ||||
| 		Object obj = tmpl.unpack(new Unpacker(in)); | ||||
| 		assertEquals(obj.getClass(), StringFieldClass.class); | ||||
|  | ||||
| 		StringFieldClass dst = (StringFieldClass)obj; | ||||
| 		assertEquals(src.s1, dst.s1); | ||||
| 		assertEquals(src.s2, dst.s2); | ||||
| 	} | ||||
|  | ||||
| 	@Test | ||||
| 	public void testPackConvert02() throws Exception { | ||||
| 		ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||||
|  | ||||
| 		CustomPacker.register(StringFieldClass.class, DynamicCodeGenPacker.create(StringFieldClass.class)); | ||||
| 				 | ||||
|  | ||||
| 		StringFieldClass src = new StringFieldClass(); | ||||
|  | ||||
| 		src.s1 = "kumofs"; | ||||
| 		src.s2 = "frsyuki"; | ||||
|  | ||||
| 		new Packer(out).pack(src); | ||||
|  | ||||
| 		Template tmpl = ReflectionTemplate.create(StringFieldClass.class); | ||||
|  | ||||
| 		ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); | ||||
|  | ||||
| 		Object obj = tmpl.unpack(new Unpacker(in)); | ||||
| 		assertEquals(obj.getClass(), StringFieldClass.class); | ||||
|  | ||||
| 		StringFieldClass dst = (StringFieldClass)obj; | ||||
| 		assertEquals(src.s1, dst.s1); | ||||
| 		assertEquals(src.s2, dst.s2); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -1,700 +0,0 @@ | ||||
| package org.msgpack.util.annotation; | ||||
|  | ||||
| import java.io.ByteArrayInputStream; | ||||
| import java.io.ByteArrayOutputStream; | ||||
| import java.math.BigInteger; | ||||
| import java.util.ArrayList; | ||||
| import java.util.HashMap; | ||||
| import java.util.Iterator; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
|  | ||||
| import junit.framework.TestCase; | ||||
|  | ||||
| import org.junit.Test; | ||||
| import org.msgpack.MessagePackObject; | ||||
| import org.msgpack.MessageUnpackable; | ||||
| import org.msgpack.Packer; | ||||
| import org.msgpack.Unpacker; | ||||
|  | ||||
| public class TestMessagePackUnpackable extends TestCase { | ||||
|  | ||||
| 	@Test | ||||
| 	public void testPrimitiveTypeFields01() throws Exception { | ||||
| 		PrimitiveTypeFieldsClass src = (PrimitiveTypeFieldsClass) PackUnpackUtil | ||||
| 				.newEnhancedInstance(PrimitiveTypeFieldsClass.class); | ||||
| 		src.f0 = (byte) 0; | ||||
| 		src.f1 = 1; | ||||
| 		src.f2 = 2; | ||||
| 		src.f3 = 3; | ||||
| 		src.f4 = 4; | ||||
| 		src.f5 = 5; | ||||
| 		src.f6 = false; | ||||
| 		ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||||
| 		new Packer(out).pack(src); | ||||
| 		PrimitiveTypeFieldsClass dst = (PrimitiveTypeFieldsClass) PackUnpackUtil | ||||
| 				.newEnhancedInstance(PrimitiveTypeFieldsClass.class); | ||||
| 		ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); | ||||
| 		Unpacker pac = new Unpacker(in); | ||||
| 		pac.unpack((MessageUnpackable) dst); | ||||
| 		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); | ||||
| 	} | ||||
|  | ||||
| 	@Test | ||||
| 	public void testPrimitiveTypeFields02() throws Exception { | ||||
| 		PrimitiveTypeFieldsClass src = (PrimitiveTypeFieldsClass) PackUnpackUtil | ||||
| 				.newEnhancedInstance(PrimitiveTypeFieldsClass.class); | ||||
| 		src.f0 = (byte) 0; | ||||
| 		src.f1 = 1; | ||||
| 		src.f2 = 2; | ||||
| 		src.f3 = 3; | ||||
| 		src.f4 = 4; | ||||
| 		src.f5 = 5; | ||||
| 		src.f6 = false; | ||||
| 		ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||||
| 		new Packer(out).pack(src); | ||||
| 		ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); | ||||
| 		Unpacker pac = new Unpacker(in); | ||||
| 		Iterator<MessagePackObject> it = pac.iterator(); | ||||
| 		assertTrue(it.hasNext()); | ||||
| 		MessagePackObject mpo = it.next(); | ||||
| 		PrimitiveTypeFieldsClass dst = (PrimitiveTypeFieldsClass) PackUnpackUtil | ||||
| 				.initEnhancedInstance(mpo, 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); | ||||
| 		assertFalse(it.hasNext()); | ||||
| 	} | ||||
|  | ||||
| 	@MessagePackUnpackable | ||||
| 	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() { | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	@Test | ||||
| 	public void testGeneralReferenceTypeFieldsClass01() throws Exception { | ||||
| 		GeneralReferenceTypeFieldsClass src = (GeneralReferenceTypeFieldsClass) PackUnpackUtil | ||||
| 				.newEnhancedInstance(GeneralReferenceTypeFieldsClass.class); | ||||
| 		src.f0 = 0; | ||||
| 		src.f1 = 1; | ||||
| 		src.f2 = 2; | ||||
| 		src.f3 = (long) 3; | ||||
| 		src.f4 = (float) 4; | ||||
| 		src.f5 = (double) 5; | ||||
| 		src.f6 = false; | ||||
| 		src.f7 = new BigInteger("7"); | ||||
| 		src.f8 = "8"; | ||||
| 		src.f9 = new byte[] { 0x01, 0x02 }; | ||||
| 		ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||||
| 		new Packer(out).pack(src); | ||||
| 		GeneralReferenceTypeFieldsClass dst = (GeneralReferenceTypeFieldsClass) PackUnpackUtil | ||||
| 				.newEnhancedInstance(GeneralReferenceTypeFieldsClass.class); | ||||
| 		ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); | ||||
| 		Unpacker pac = new Unpacker(in); | ||||
| 		pac.unpack((MessageUnpackable) dst); | ||||
| 		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); | ||||
| 		assertEquals(src.f7, dst.f7); | ||||
| 		assertEquals(src.f8, dst.f8); | ||||
| 		assertEquals(src.f9[0], dst.f9[0]); | ||||
| 		assertEquals(src.f9[1], dst.f9[1]); | ||||
| 	} | ||||
|  | ||||
| 	@Test | ||||
| 	public void testGeneralReferenceTypeFieldsClass02() throws Exception { | ||||
| 		GeneralReferenceTypeFieldsClass src = (GeneralReferenceTypeFieldsClass) PackUnpackUtil | ||||
| 				.newEnhancedInstance(GeneralReferenceTypeFieldsClass.class); | ||||
| 		src.f0 = 0; | ||||
| 		src.f1 = 1; | ||||
| 		src.f2 = 2; | ||||
| 		src.f3 = (long) 3; | ||||
| 		src.f4 = (float) 4; | ||||
| 		src.f5 = (double) 5; | ||||
| 		src.f6 = false; | ||||
| 		src.f7 = new BigInteger("7"); | ||||
| 		src.f8 = "8"; | ||||
| 		src.f9 = new byte[] { 0x01, 0x02 }; | ||||
| 		ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||||
| 		new Packer(out).pack(src); | ||||
| 		ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); | ||||
| 		Unpacker pac = new Unpacker(in); | ||||
| 		Iterator<MessagePackObject> it = pac.iterator(); | ||||
| 		assertTrue(it.hasNext()); | ||||
| 		MessagePackObject mpo = it.next(); | ||||
| 		GeneralReferenceTypeFieldsClass dst = (GeneralReferenceTypeFieldsClass) PackUnpackUtil | ||||
| 				.initEnhancedInstance(mpo, | ||||
| 						GeneralReferenceTypeFieldsClass.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); | ||||
| 		assertEquals(src.f7, dst.f7); | ||||
| 		assertEquals(src.f8, dst.f8); | ||||
| 		assertEquals(src.f9[0], dst.f9[0]); | ||||
| 		assertEquals(src.f9[1], dst.f9[1]); | ||||
| 		assertFalse(it.hasNext()); | ||||
| 	} | ||||
|  | ||||
| 	@MessagePackUnpackable | ||||
| 	public static class GeneralReferenceTypeFieldsClass { | ||||
| 		public Byte f0; | ||||
| 		public Short f1; | ||||
| 		public Integer f2; | ||||
| 		public Long f3; | ||||
| 		public Float f4; | ||||
| 		public Double f5; | ||||
| 		public Boolean f6; | ||||
| 		public BigInteger f7; | ||||
| 		public String f8; | ||||
| 		public byte[] f9; | ||||
|  | ||||
| 		public GeneralReferenceTypeFieldsClass() { | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	public void testListTypes01() throws Exception { | ||||
| 		SampleListTypes src = (SampleListTypes) PackUnpackUtil | ||||
| 				.newEnhancedInstance(SampleListTypes.class); | ||||
| 		src.f0 = new ArrayList<Integer>(); | ||||
| 		src.f1 = new ArrayList<Integer>(); | ||||
| 		src.f1.add(1); | ||||
| 		src.f1.add(2); | ||||
| 		src.f1.add(3); | ||||
| 		src.f2 = new ArrayList<String>(); | ||||
| 		src.f2.add("e1"); | ||||
| 		src.f2.add("e2"); | ||||
| 		src.f2.add("e3"); | ||||
| 		ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||||
| 		new Packer(out).pack(src); | ||||
| 		SampleListTypes dst = (SampleListTypes) PackUnpackUtil | ||||
| 				.newEnhancedInstance(SampleListTypes.class); | ||||
| 		ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); | ||||
| 		Unpacker pac = new Unpacker(in); | ||||
| 		pac.unpack((MessageUnpackable) dst); | ||||
| 		assertEquals(src.f0.size(), 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)); | ||||
| 		} | ||||
| 		assertEquals(src.f2.size(), dst.f2.size()); | ||||
| 		for (int i = 0; i < src.f2.size(); ++i) { | ||||
| 			assertEquals(src.f2.get(i), dst.f2.get(i)); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	public void testListTypes02() throws Exception { | ||||
| 		SampleListTypes src = (SampleListTypes) PackUnpackUtil | ||||
| 				.newEnhancedInstance(SampleListTypes.class); | ||||
| 		src.f0 = new ArrayList<Integer>(); | ||||
| 		src.f1 = new ArrayList<Integer>(); | ||||
| 		src.f1.add(1); | ||||
| 		src.f1.add(2); | ||||
| 		src.f1.add(3); | ||||
| 		src.f2 = new ArrayList<String>(); | ||||
| 		src.f2.add("e1"); | ||||
| 		src.f2.add("e2"); | ||||
| 		src.f2.add("e3"); | ||||
| 		ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||||
| 		new Packer(out).pack(src); | ||||
| 		ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); | ||||
| 		Unpacker pac = new Unpacker(in); | ||||
| 		Iterator<MessagePackObject> it = pac.iterator(); | ||||
| 		assertTrue(it.hasNext()); | ||||
| 		MessagePackObject mpo = it.next(); | ||||
| 		SampleListTypes dst = (SampleListTypes) PackUnpackUtil | ||||
| 				.initEnhancedInstance(mpo, SampleListTypes.class); | ||||
| 		assertEquals(src.f0.size(), 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)); | ||||
| 		} | ||||
| 		assertEquals(src.f2.size(), dst.f2.size()); | ||||
| 		for (int i = 0; i < src.f2.size(); ++i) { | ||||
| 			assertEquals(src.f2.get(i), dst.f2.get(i)); | ||||
| 		} | ||||
| 		assertFalse(it.hasNext()); | ||||
| 	} | ||||
|  | ||||
| 	@MessagePackUnpackable | ||||
| 	public static class SampleListTypes { | ||||
| 		public List<Integer> f0; | ||||
| 		public List<Integer> f1; | ||||
| 		public List<String> f2; | ||||
|  | ||||
| 		public SampleListTypes() { | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	public void testMapTypes01() throws Exception { | ||||
| 		SampleMapTypes src = (SampleMapTypes) PackUnpackUtil | ||||
| 				.newEnhancedInstance(SampleMapTypes.class); | ||||
| 		src.f0 = new HashMap<Integer, Integer>(); | ||||
| 		src.f1 = new HashMap<Integer, Integer>(); | ||||
| 		src.f1.put(1, 1); | ||||
| 		src.f1.put(2, 2); | ||||
| 		src.f1.put(3, 3); | ||||
| 		src.f2 = new HashMap<String, Integer>(); | ||||
| 		src.f2.put("k1", 1); | ||||
| 		src.f2.put("k2", 2); | ||||
| 		src.f2.put("k3", 3); | ||||
| 		ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||||
| 		new Packer(out).pack(src); | ||||
| 		SampleMapTypes dst = (SampleMapTypes) PackUnpackUtil | ||||
| 				.newEnhancedInstance(SampleMapTypes.class); | ||||
| 		ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); | ||||
| 		Unpacker pac = new Unpacker(in); | ||||
| 		pac.unpack((MessageUnpackable) dst); | ||||
| 		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.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)); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	public void testMapTypes02() throws Exception { | ||||
| 		SampleMapTypes src = (SampleMapTypes) PackUnpackUtil | ||||
| 				.newEnhancedInstance(SampleMapTypes.class); | ||||
| 		src.f0 = new HashMap<Integer, Integer>(); | ||||
| 		src.f1 = new HashMap<Integer, Integer>(); | ||||
| 		src.f1.put(1, 1); | ||||
| 		src.f1.put(2, 2); | ||||
| 		src.f1.put(3, 3); | ||||
| 		src.f2 = new HashMap<String, Integer>(); | ||||
| 		src.f2.put("k1", 1); | ||||
| 		src.f2.put("k2", 2); | ||||
| 		src.f2.put("k3", 3); | ||||
| 		ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||||
| 		new Packer(out).pack(src); | ||||
| 		ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); | ||||
| 		Unpacker pac = new Unpacker(in); | ||||
| 		Iterator<MessagePackObject> it = pac.iterator(); | ||||
| 		assertTrue(it.hasNext()); | ||||
| 		MessagePackObject mpo = it.next(); | ||||
| 		SampleMapTypes dst = (SampleMapTypes) PackUnpackUtil | ||||
| 				.initEnhancedInstance(mpo, 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.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)); | ||||
| 		} | ||||
| 		assertFalse(it.hasNext()); | ||||
| 	} | ||||
|  | ||||
| 	@MessagePackUnpackable | ||||
| 	public static class SampleMapTypes { | ||||
| 		public Map<Integer, Integer> f0; | ||||
| 		public Map<Integer, Integer> f1; | ||||
| 		public Map<String, Integer> f2; | ||||
|  | ||||
| 		public SampleMapTypes() { | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	@Test | ||||
| 	public void testDefaultConstructorModifiers() throws Exception { | ||||
| 		try { | ||||
| 			PackUnpackUtil.newEnhancedInstance(NoDefaultConstructorClass.class); | ||||
| 			fail(); | ||||
| 		} catch (PackUnpackUtilException e) { | ||||
| 			assertTrue(true); | ||||
| 		} | ||||
| 		assertTrue(true); | ||||
| 		try { | ||||
| 			PackUnpackUtil | ||||
| 					.newEnhancedInstance(PrivateDefaultConstructorClass.class); | ||||
| 			fail(); | ||||
| 		} catch (PackUnpackUtilException e) { | ||||
| 			assertTrue(true); | ||||
| 		} | ||||
| 		assertTrue(true); | ||||
| 		try { | ||||
| 			PackUnpackUtil | ||||
| 					.newEnhancedInstance(ProtectedDefaultConstructorClass.class); | ||||
| 			assertTrue(true); | ||||
| 		} catch (PackUnpackUtilException e) { | ||||
| 			fail(); | ||||
| 		} | ||||
| 		assertTrue(true); | ||||
| 		try { | ||||
| 			PackUnpackUtil | ||||
| 					.newEnhancedInstance(PackageDefaultConstructorClass.class); | ||||
| 			fail(); | ||||
| 		} catch (PackUnpackUtilException e) { | ||||
| 			assertTrue(true); | ||||
| 		} | ||||
| 		assertTrue(true); | ||||
| 	} | ||||
|  | ||||
| 	@MessagePackUnpackable | ||||
| 	public static class NoDefaultConstructorClass { | ||||
| 		public NoDefaultConstructorClass(int i) { | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	@MessagePackUnpackable | ||||
| 	public static class PrivateDefaultConstructorClass { | ||||
| 		private PrivateDefaultConstructorClass() { | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	@MessagePackUnpackable | ||||
| 	public static class ProtectedDefaultConstructorClass { | ||||
| 		protected ProtectedDefaultConstructorClass() { | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	@MessagePackUnpackable | ||||
| 	public static class PackageDefaultConstructorClass { | ||||
| 		PackageDefaultConstructorClass() { | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	@Test | ||||
| 	public void testClassModifiers() throws Exception { | ||||
| 		try { | ||||
| 			PackUnpackUtil.newEnhancedInstance(PrivateModifierClass.class); | ||||
| 			fail(); | ||||
| 		} catch (PackUnpackUtilException e) { | ||||
| 			assertTrue(true); | ||||
| 		} | ||||
| 		assertTrue(true); | ||||
| 		try { | ||||
| 			PackUnpackUtil.newEnhancedInstance(ProtectedModifierClass.class); | ||||
| 			assertTrue(true); | ||||
| 		} catch (PackUnpackUtilException e) { | ||||
| 			fail(); | ||||
| 		} | ||||
| 		assertTrue(true); | ||||
| 		try { | ||||
| 			PackUnpackUtil.newEnhancedInstance(PackageModifierClass.class); | ||||
| 			fail(); | ||||
| 		} catch (PackUnpackUtilException e) { | ||||
| 			assertTrue(true); | ||||
| 		} | ||||
| 		assertTrue(true); | ||||
| 	} | ||||
|  | ||||
| 	@MessagePackUnpackable | ||||
| 	private static class PrivateModifierClass { | ||||
| 	} | ||||
|  | ||||
| 	@MessagePackUnpackable | ||||
| 	protected static class ProtectedModifierClass { | ||||
| 		protected ProtectedModifierClass() { | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	@MessagePackUnpackable | ||||
| 	static class PackageModifierClass { | ||||
| 	} | ||||
|  | ||||
| 	@Test | ||||
| 	public void testFinalClassAndAbstractClass() throws Exception { | ||||
| 		try { | ||||
| 			PackUnpackUtil.newEnhancedInstance(FinalModifierClass.class); | ||||
| 			fail(); | ||||
| 		} catch (PackUnpackUtilException e) { | ||||
| 			assertTrue(true); | ||||
| 		} | ||||
| 		assertTrue(true); | ||||
| 		try { | ||||
| 			PackUnpackUtil.newEnhancedInstance(AbstractModifierClass.class); | ||||
| 			fail(); | ||||
| 		} catch (PackUnpackUtilException e) { | ||||
| 			assertTrue(true); | ||||
| 		} | ||||
| 		assertTrue(true); | ||||
| 	} | ||||
|  | ||||
| 	@MessagePackUnpackable | ||||
| 	public final static class FinalModifierClass { | ||||
| 	} | ||||
|  | ||||
| 	@MessagePackUnpackable | ||||
| 	public abstract static class AbstractModifierClass { | ||||
| 	} | ||||
|  | ||||
| 	@Test | ||||
| 	public void testInterfaceAndEnumType() throws Exception { | ||||
| 		try { | ||||
| 			PackUnpackUtil.newEnhancedInstance(SampleInterface.class); | ||||
| 			fail(); | ||||
| 		} catch (PackUnpackUtilException e) { | ||||
| 			assertTrue(true); | ||||
| 		} | ||||
| 		assertTrue(true); | ||||
| 		try { | ||||
| 			PackUnpackUtil.newEnhancedInstance(SampleEnum.class); | ||||
| 			fail(); | ||||
| 		} catch (PackUnpackUtilException e) { | ||||
| 			assertTrue(true); | ||||
| 		} | ||||
| 		assertTrue(true); | ||||
| 	} | ||||
|  | ||||
| 	@MessagePackUnpackable | ||||
| 	public interface SampleInterface { | ||||
| 	} | ||||
|  | ||||
| 	@MessagePackUnpackable | ||||
| 	public enum SampleEnum { | ||||
| 	} | ||||
|  | ||||
| 	@Test | ||||
| 	public void testFieldModifiers01() throws Exception { | ||||
| 		FieldModifiersClass src = (FieldModifiersClass) PackUnpackUtil | ||||
| 				.newEnhancedInstance(FieldModifiersClass.class); | ||||
| 		src.f0 = 0; | ||||
| 		src.f2 = 2; | ||||
| 		src.f3 = 3; | ||||
| 		src.f4 = 4; | ||||
| 		ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||||
| 		new Packer(out).pack(src); | ||||
| 		FieldModifiersClass dst = (FieldModifiersClass) PackUnpackUtil | ||||
| 				.newEnhancedInstance(FieldModifiersClass.class); | ||||
| 		ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); | ||||
| 		Unpacker pac = new Unpacker(in); | ||||
| 		pac.unpack((MessageUnpackable) dst); | ||||
| 		assertTrue(src.f0 == dst.f0); | ||||
| 		assertTrue(src.f1 == dst.f1); | ||||
| 		assertTrue(src.f2 != dst.f2); | ||||
| 		assertTrue(src.f3 == dst.f3); | ||||
| 		assertTrue(src.f4 != dst.f4); | ||||
| 	} | ||||
|  | ||||
| 	@Test | ||||
| 	public void testFieldModifiers02() throws Exception { | ||||
| 		FieldModifiersClass src = (FieldModifiersClass) PackUnpackUtil | ||||
| 				.newEnhancedInstance(FieldModifiersClass.class); | ||||
| 		src.f0 = 0; | ||||
| 		src.f2 = 2; | ||||
| 		src.f3 = 3; | ||||
| 		src.f4 = 4; | ||||
| 		ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||||
| 		new Packer(out).pack(src); | ||||
| 		ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); | ||||
| 		Unpacker pac = new Unpacker(in); | ||||
| 		Iterator<MessagePackObject> it = pac.iterator(); | ||||
| 		assertTrue(it.hasNext()); | ||||
| 		MessagePackObject mpo = it.next(); | ||||
| 		FieldModifiersClass dst = (FieldModifiersClass) PackUnpackUtil | ||||
| 				.initEnhancedInstance(mpo, FieldModifiersClass.class); | ||||
| 		assertTrue(src.f0 == dst.f0); | ||||
| 		assertTrue(src.f1 == dst.f1); | ||||
| 		assertTrue(src.f2 != dst.f2); | ||||
| 		assertTrue(src.f3 == dst.f3); | ||||
| 		assertTrue(src.f4 != dst.f4); | ||||
| 		assertFalse(it.hasNext()); | ||||
| 	} | ||||
|  | ||||
| 	@MessagePackUnpackable | ||||
| 	public static class FieldModifiersClass { | ||||
| 		public int f0; | ||||
| 		public final int f1 = 1; | ||||
| 		private int f2; | ||||
| 		protected int f3; | ||||
| 		int f4; | ||||
|  | ||||
| 		public FieldModifiersClass() { | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	@Test | ||||
| 	public void testNestedAnnotatedFieldClass01() throws Exception { | ||||
| 		NestedClass src2 = (NestedClass) PackUnpackUtil | ||||
| 				.newEnhancedInstance(NestedClass.class); | ||||
| 		BaseClass src = (BaseClass) PackUnpackUtil | ||||
| 				.newEnhancedInstance(BaseClass.class); | ||||
| 		src.f0 = 0; | ||||
| 		src2.f2 = 2; | ||||
| 		src.f1 = src2; | ||||
| 		ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||||
| 		new Packer(out).pack(src); | ||||
| 		BaseClass dst = (BaseClass) PackUnpackUtil | ||||
| 				.newEnhancedInstance(BaseClass.class); | ||||
| 		ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); | ||||
| 		Unpacker pac = new Unpacker(in); | ||||
| 		pac.unpack((MessageUnpackable) dst); | ||||
| 		assertTrue(src.f0 == dst.f0); | ||||
| 		assertTrue(src2.f2 == dst.f1.f2); | ||||
| 	} | ||||
|  | ||||
| 	@Test | ||||
| 	public void testNestedAnnotatedFieldClass02() throws Exception { | ||||
| 		NestedClass src2 = (NestedClass) PackUnpackUtil | ||||
| 				.newEnhancedInstance(NestedClass.class); | ||||
| 		BaseClass src = (BaseClass) PackUnpackUtil | ||||
| 				.newEnhancedInstance(BaseClass.class); | ||||
| 		src.f0 = 0; | ||||
| 		src2.f2 = 2; | ||||
| 		src.f1 = src2; | ||||
| 		ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||||
| 		new Packer(out).pack(src); | ||||
| 		ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); | ||||
| 		Unpacker pac = new Unpacker(in); | ||||
| 		Iterator<MessagePackObject> it = pac.iterator(); | ||||
| 		assertTrue(it.hasNext()); | ||||
| 		MessagePackObject mpo = it.next(); | ||||
| 		BaseClass dst = (BaseClass) PackUnpackUtil.initEnhancedInstance(mpo, | ||||
| 				BaseClass.class); | ||||
| 		assertTrue(src.f0 == dst.f0); | ||||
| 		assertTrue(src2.f2 == dst.f1.f2); | ||||
| 		assertFalse(it.hasNext()); | ||||
| 	} | ||||
|  | ||||
| 	@MessagePackUnpackable | ||||
| 	public static class BaseClass { | ||||
| 		public int f0; | ||||
| 		public NestedClass f1; | ||||
|  | ||||
| 		public BaseClass() { | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	@MessagePackUnpackable | ||||
| 	public static class NestedClass { | ||||
| 		public int f2; | ||||
|  | ||||
| 		public NestedClass() { | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	@Test | ||||
| 	public void testExtendedClass01() throws Exception { | ||||
| 		SampleSubClass src = (SampleSubClass) PackUnpackUtil | ||||
| 				.newEnhancedInstance(SampleSubClass.class); | ||||
| 		src.f0 = 0; | ||||
| 		src.f2 = 2; | ||||
| 		src.f3 = 3; | ||||
| 		src.f4 = 4; | ||||
| 		src.f5 = 5; | ||||
| 		src.f8 = 8; | ||||
| 		src.f9 = 9; | ||||
| 		ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||||
| 		new Packer(out).pack(src); | ||||
| 		SampleSubClass dst = (SampleSubClass) PackUnpackUtil | ||||
| 				.newEnhancedInstance(SampleSubClass.class); | ||||
| 		ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); | ||||
| 		Unpacker pac = new Unpacker(in); | ||||
| 		pac.unpack((MessageUnpackable) dst); | ||||
| 		assertTrue(src.f0 == dst.f0); | ||||
| 		assertTrue(src.f1 == dst.f1); | ||||
| 		assertTrue(src.f2 != dst.f2); | ||||
| 		assertTrue(src.f3 == dst.f3); | ||||
| 		assertTrue(src.f4 != dst.f4); | ||||
| 		assertTrue(src.f5 == dst.f5); | ||||
| 		assertTrue(src.f6 == dst.f6); | ||||
| 		assertTrue(src.f8 == dst.f8); | ||||
| 		assertTrue(src.f9 != dst.f9); | ||||
| 	} | ||||
|  | ||||
| 	@Test | ||||
| 	public void testExtendedClass02() throws Exception { | ||||
| 		SampleSubClass src = (SampleSubClass) PackUnpackUtil | ||||
| 				.newEnhancedInstance(SampleSubClass.class); | ||||
| 		src.f0 = 0; | ||||
| 		src.f2 = 2; | ||||
| 		src.f3 = 3; | ||||
| 		src.f4 = 4; | ||||
| 		src.f5 = 5; | ||||
| 		src.f8 = 8; | ||||
| 		src.f9 = 9; | ||||
| 		ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||||
| 		new Packer(out).pack(src); | ||||
| 		ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); | ||||
| 		Unpacker pac = new Unpacker(in); | ||||
| 		Iterator<MessagePackObject> it = pac.iterator(); | ||||
| 		assertTrue(it.hasNext()); | ||||
| 		MessagePackObject mpo = it.next(); | ||||
| 		SampleSubClass dst = (SampleSubClass) PackUnpackUtil | ||||
| 				.initEnhancedInstance(mpo, SampleSubClass.class); | ||||
| 		assertTrue(src.f0 == dst.f0); | ||||
| 		assertTrue(src.f1 == dst.f1); | ||||
| 		assertTrue(src.f2 != dst.f2); | ||||
| 		assertTrue(src.f3 == dst.f3); | ||||
| 		assertTrue(src.f4 != dst.f4); | ||||
| 		assertTrue(src.f5 == dst.f5); | ||||
| 		assertTrue(src.f6 == dst.f6); | ||||
| 		assertTrue(src.f8 == dst.f8); | ||||
| 		assertTrue(src.f9 != dst.f9); | ||||
| 		assertFalse(it.hasNext()); | ||||
| 	} | ||||
|  | ||||
| 	@MessagePackUnpackable | ||||
| 	public static class SampleSubClass extends SampleSuperClass { | ||||
| 		public int f0; | ||||
| 		public final int f1 = 1; | ||||
| 		private int f2; | ||||
| 		protected int f3; | ||||
| 		int f4; | ||||
|  | ||||
| 		public SampleSubClass() { | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	public static class SampleSuperClass { | ||||
| 		public int f5; | ||||
| 		public final int f6 = 2; | ||||
| 		private int f7; | ||||
| 		protected int f8; | ||||
| 		int f9; | ||||
|  | ||||
| 		public SampleSuperClass() { | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 tokuhirom
					tokuhirom