java: merge CustomMessage.java and DynamicCodeGenBase.java that can be not merged automatically

This commit is contained in:
Muga Nishizawa 2010-10-24 23:24:27 +09:00
commit cdfac703ef
19 changed files with 502 additions and 40 deletions

View file

@ -24,10 +24,18 @@ public class CustomMessage {
CustomPacker.register(target, packer);
}
public static void registerTemplate(Class<?> target, Template tmpl) {
public static void registerConverter(Class<?> target, MessageConverter converter) {
CustomConverter.register(target, converter);
}
public static void registerUnpacker(Class<?> target, MessageUnpacker unpacker) {
CustomUnpacker.register(target, unpacker);
}
public static void register(Class<?> target, Template tmpl) {
CustomPacker.register(target, tmpl);
CustomUnpacker.register(target, tmpl);
CustomConverter.register(target, tmpl);
CustomUnpacker.register(target, tmpl);
}
public static boolean isAnnotated(Class<?> target, Class<? extends Annotation> with) {

View file

@ -0,0 +1,156 @@
//
// MessagePack for Java
//
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.msgpack;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
//import org.msgpack.util.codegen.DynamicTemplate; // FIXME
import org.msgpack.util.codegen.DynamicPacker;
import org.msgpack.util.codegen.DynamicConverter;
import org.msgpack.util.codegen.DynamicUnpacker;
public class MessagePack {
public static byte[] pack(Object obj) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
new Packer(out).pack(obj);
} catch (IOException e) {
throw new RuntimeException(e);
}
return out.toByteArray();
}
public static void pack(OutputStream out, Object obj) throws IOException {
new Packer(out).pack(obj);
}
public static byte[] pack(Object obj, Template tmpl) throws MessageTypeException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
new Packer(out).pack(obj, tmpl);
} catch (IOException e) {
throw new RuntimeException(e);
}
return out.toByteArray();
}
public static void pack(OutputStream out, Object obj, Template tmpl) throws IOException {
new Packer(out).pack(obj, tmpl);
}
public static MessagePackObject unpack(byte[] buffer) throws IOException {
Unpacker pac = new Unpacker();
pac.wrap(buffer);
try {
return pac.unpackObject();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static Object unpack(byte[] buffer, Template tmpl) throws MessageTypeException {
Unpacker pac = new Unpacker();
pac.wrap(buffer);
try {
return pac.unpack(tmpl);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static <T> T unpack(byte[] buffer, Class<T> klass) throws MessageTypeException {
Unpacker pac = new Unpacker();
pac.wrap(buffer);
try {
return pac.unpack(klass);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static MessagePackObject unpack(InputStream in) {
Unpacker pac = new Unpacker(in);
try {
return pac.unpackObject();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static Object unpack(InputStream in, Template tmpl) throws IOException, MessageTypeException {
Unpacker pac = new Unpacker(in);
try {
return pac.unpack(tmpl);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static <T> T unpack(InputStream in, Class<T> klass) throws IOException, MessageTypeException {
Unpacker pac = new Unpacker(in);
try {
return pac.unpack(klass);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void register(Class<?> target) { // auto-detect
// FIXME
//Template tmpl;
//if(List.isAssignableFrom(target)) {
//} else if(Set.isAssignableFrom(target)) {
//} else if(Map.isAssignableFrom(target)) {
//} else if(Collection.isAssignableFrom(target)) {
//} else if(BigInteger.isAssignableFrom(target)) {
//} else {
//}
// FIXME
//Template tmpl = DynamicTemplate.create(target);
//register(target, tmpl);
// FIXME
CustomPacker.register(target, DynamicPacker.create(target));
CustomConverter.register(target, DynamicConverter.create(target));
CustomUnpacker.register(target, DynamicUnpacker.create(target));
}
public static void register(Class<?> target, Template tmpl) {
CustomPacker.register(target, tmpl);
CustomConverter.register(target, tmpl);
CustomUnpacker.register(target, tmpl);
}
public static void registerPacker(Class<?> target, MessagePacker packer) {
CustomPacker.register(target, packer);
}
public static void registerConverter(Class<?> target, MessageConverter converter) {
CustomConverter.register(target, converter);
}
public static void registerUnpacker(Class<?> target, MessageUnpacker unpacker) {
CustomUnpacker.register(target, unpacker);
}
}

View file

@ -580,8 +580,9 @@ public class Unpacker implements Iterable<MessagePackObject> {
return tmpl.unpack(this);
}
final public Object unpack(Class<?> klass) throws IOException, MessageTypeException, InstantiationException, IllegalAccessException {
return unpack(Templates.tClass(klass));
final public <T> T unpack(Class<T> klass) throws IOException, MessageTypeException {
// FIXME optional?
return (T)unpack(Templates.tOptional(Templates.tClass(klass)));
}
}

View file

@ -46,7 +46,7 @@ public class AnyTemplate implements Template {
static final AnyTemplate instance = new AnyTemplate();
static {
CustomMessage.registerTemplate(MessagePackObject.class, instance);
CustomMessage.register(MessagePackObject.class, instance);
}
}

View file

@ -43,7 +43,7 @@ public class BigIntegerTemplate implements Template {
static final BigIntegerTemplate instance = new BigIntegerTemplate();
static {
CustomMessage.registerTemplate(BigInteger.class, instance);
CustomMessage.register(BigInteger.class, instance);
}
}

View file

@ -42,7 +42,7 @@ public class BooleanTemplate implements Template {
static final BooleanTemplate instance = new BooleanTemplate();
static {
CustomMessage.registerTemplate(Boolean.class, instance);
CustomMessage.register(Boolean.class, instance);
}
}

View file

@ -42,7 +42,7 @@ public class ByteArrayTemplate implements Template {
static final ByteArrayTemplate instance = new ByteArrayTemplate();
static {
CustomMessage.registerTemplate(byte[].class, instance);
CustomMessage.register(byte[].class, instance);
}
}

View file

@ -42,7 +42,7 @@ public class ByteTemplate implements Template {
static final ByteTemplate instance = new ByteTemplate();
static {
CustomMessage.registerTemplate(Byte.class, instance);
CustomMessage.register(Byte.class, instance);
}
}

View file

@ -31,10 +31,11 @@ public class CollectionTemplate implements Template {
}
public void pack(Packer pk, Object target) throws IOException {
if(target instanceof Collection) {
if(!(target instanceof Collection)) {
throw new MessageTypeException();
}
Collection<Object> collection = (Collection<Object>)target;
pk.packArray(collection.size());
for(Object element : collection) {
elementTemplate.pack(pk, element);
}

View file

@ -42,7 +42,7 @@ public class DoubleTemplate implements Template {
static final DoubleTemplate instance = new DoubleTemplate();
static {
CustomMessage.registerTemplate(Double.class, instance);
CustomMessage.register(Double.class, instance);
}
}

View file

@ -42,7 +42,7 @@ public class FloatTemplate implements Template {
static final FloatTemplate instance = new FloatTemplate();
static {
CustomMessage.registerTemplate(Float.class, instance);
CustomMessage.register(Float.class, instance);
}
}

View file

@ -42,7 +42,7 @@ public class IntegerTemplate implements Template {
static final IntegerTemplate instance = new IntegerTemplate();
static {
CustomMessage.registerTemplate(Integer.class, instance);
CustomMessage.register(Integer.class, instance);
}
}

View file

@ -42,7 +42,7 @@ public class LongTemplate implements Template {
static final LongTemplate instance = new LongTemplate();
static {
CustomMessage.registerTemplate(Long.class, instance);
CustomMessage.register(Long.class, instance);
}
}

View file

@ -42,7 +42,7 @@ public class ShortTemplate implements Template {
static final ShortTemplate instance = new ShortTemplate();
static {
CustomMessage.registerTemplate(Short.class, instance);
CustomMessage.register(Short.class, instance);
}
}

View file

@ -42,7 +42,7 @@ public class StringTemplate implements Template {
static final StringTemplate instance = new StringTemplate();
static {
CustomMessage.registerTemplate(String.class, instance);
CustomMessage.register(String.class, instance);
}
}

View file

@ -322,7 +322,7 @@ public class DynamicCodeGenBase implements Constants {
} else if (CustomMessage.isAnnotated(c, MessagePackMessage.class)) {
// @MessagePackMessage
Template tmpl = DynamicTemplate.create(c);
CustomMessage.registerTemplate(c, tmpl);
CustomMessage.register(c, tmpl);
return tmpl;
} else if (CustomMessage.isAnnotated(c, MessagePackDelegate.class)) {
// FIXME DelegatePacker
@ -330,20 +330,19 @@ public class DynamicCodeGenBase implements Constants {
"not supported yet. : " + c.getName());
LOG.error(e.getMessage(), e);
throw e;
} else if (CustomMessage.isAnnotated(c,
MessagePackOrdinalEnum.class)) {
} else if (CustomMessage.isAnnotated(c, MessagePackOrdinalEnum.class)) {
// @MessagePackOrdinalEnum
Template tmpl = DynamicOrdinalEnumTemplate.create(c);
CustomMessage.registerTemplate(c, tmpl);
CustomMessage.register(c, tmpl);
return tmpl;
} else if (MessageConvertable.class.isAssignableFrom(c)
} else if (MessagePackable.class.isAssignableFrom(c)
|| MessageConvertable.class.isAssignableFrom(c)
|| MessageUnpackable.class.isAssignableFrom(c)) {
Template tmpl = new MessagePackUnpackConvertableTemplate(c);
CustomMessage.registerTemplate(c, tmpl);
CustomMessage.register(c, tmpl);
return tmpl;
} else {
throw new MessageTypeException("Type error: "
+ ((Class<?>) t).getName());
throw new MessageTypeException("Type error: " + ((Class<?>) t).getName());
}
} else if (t instanceof GenericArrayType) {
GenericArrayType gat = (GenericArrayType) t;