mirror of
https://github.com/msgpack/msgpack-python.git
synced 2026-02-09 11:20:14 +00:00
java: adds several annotations in an org.msgpack.annotation package and edits Packer.java and its test program
This commit is contained in:
parent
0a41b253f3
commit
dfb97e7961
6 changed files with 101 additions and 18 deletions
|
|
@ -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>();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -22,8 +22,13 @@ import java.io.IOException;
|
|||
import java.nio.ByteBuffer;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.lang.annotation.Annotation;
|
||||
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.
|
||||
*
|
||||
|
|
@ -473,19 +478,36 @@ public class Packer {
|
|||
return packDouble((Double)o);
|
||||
} else if(o instanceof BigInteger) {
|
||||
return packBigInteger((BigInteger)o);
|
||||
}
|
||||
}
|
||||
|
||||
Class klass = o.getClass();
|
||||
|
||||
MessagePacker packer = CustomPacker.get(klass);
|
||||
if(packer != null) {
|
||||
Class<?> klass = o.getClass();
|
||||
if (CustomPacker.isRegistered(klass)) {
|
||||
MessagePacker packer = CustomPacker.get(klass);
|
||||
packer.pack(this, o);
|
||||
return this;
|
||||
} else if (isAnnotated(klass, MessagePackMessage.class)) {
|
||||
MessagePacker packer = ReflectionPacker.create(klass);
|
||||
CustomPacker.register(klass, packer);
|
||||
packer.pack(this, o);
|
||||
return this;
|
||||
} else if (isAnnotated(klass, MessagePackDelegate.class)) {
|
||||
throw new UnsupportedOperationException("not supported yet. : " + klass.getName());
|
||||
} else if (isAnnotated(klass, MessagePackOrdinalEnum.class)) {
|
||||
throw new UnsupportedOperationException("not supported yet. : " + klass.getName());
|
||||
}
|
||||
|
||||
// FIXME check annotations -> code generation -> CustomMessage.registerPacker
|
||||
// Class<?> klass = o.getClass();
|
||||
// MessagePacker packer = CustomPacker.get(klass);
|
||||
// if(packer != null) {
|
||||
// packer.pack(this, o);
|
||||
// return this;
|
||||
// }
|
||||
//
|
||||
// // FIXME check annotations -> code generation -> CustomMessage.registerPacker
|
||||
|
||||
throw new MessageTypeException("unknown object "+o+" ("+o.getClass()+")");
|
||||
}
|
||||
|
||||
static boolean isAnnotated(Class<?> target, Class<? extends Annotation> with) {
|
||||
return target.getAnnotation(with) != null;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 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 {
|
||||
|
||||
}
|
||||
|
|
@ -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 {
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue