2010-11-30 21:59:07 +09:00
|
|
|
//
|
|
|
|
|
// 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.template;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.lang.reflect.*;
|
2010-12-01 20:38:58 +09:00
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.HashMap;
|
2010-11-30 21:59:07 +09:00
|
|
|
import org.msgpack.*;
|
|
|
|
|
|
|
|
|
|
public class ReflectionTemplateBuilder extends TemplateBuilder {
|
2010-12-01 20:38:58 +09:00
|
|
|
private static ReflectionTemplateBuilder instance;
|
|
|
|
|
public synchronized static ReflectionTemplateBuilder getInstance() {
|
|
|
|
|
if(instance == null) {
|
|
|
|
|
instance = new ReflectionTemplateBuilder();
|
|
|
|
|
}
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ReflectionTemplateBuilder() {
|
2010-11-30 21:59:07 +09:00
|
|
|
}
|
|
|
|
|
|
2010-12-01 20:38:58 +09:00
|
|
|
static abstract class ReflectionFieldEntry extends FieldEntry {
|
2010-11-30 21:59:07 +09:00
|
|
|
public ReflectionFieldEntry(FieldEntry e) {
|
|
|
|
|
super(e.getField(), e.getOption());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract void pack(Object target, Packer pac) throws IOException;
|
|
|
|
|
|
|
|
|
|
public abstract void convert(Object target, MessagePackObject obj) throws MessageTypeException, IllegalAccessException;
|
|
|
|
|
|
|
|
|
|
public abstract void unpack(Object target, Unpacker pac) throws IOException, MessageTypeException, IllegalAccessException;
|
|
|
|
|
|
|
|
|
|
public void setNull(Object target) throws IllegalAccessException {
|
|
|
|
|
getField().set(target, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-01 20:38:58 +09:00
|
|
|
static class ObjectFieldEntry extends ReflectionFieldEntry {
|
|
|
|
|
private Template template;
|
|
|
|
|
|
|
|
|
|
ObjectFieldEntry(FieldEntry e, Template template) {
|
2010-11-30 21:59:07 +09:00
|
|
|
super(e);
|
2010-12-01 20:38:58 +09:00
|
|
|
this.template = template;
|
2010-11-30 21:59:07 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void pack(Object target, Packer pac) throws IOException {
|
2010-12-01 20:38:58 +09:00
|
|
|
template.pack(pac, target);
|
2010-11-30 21:59:07 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void convert(Object target, MessagePackObject obj) throws MessageTypeException, IllegalAccessException {
|
|
|
|
|
Field f = getField();
|
|
|
|
|
Class<Object> type = (Class<Object>)f.getType();
|
|
|
|
|
Object fieldReference = f.get(target);
|
2010-12-01 20:38:58 +09:00
|
|
|
Object valueReference = template.convert(obj, fieldReference);
|
2010-11-30 21:59:07 +09:00
|
|
|
if(valueReference != fieldReference) {
|
|
|
|
|
f.set(target, valueReference);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void unpack(Object target, Unpacker pac) throws IOException, MessageTypeException, IllegalAccessException {
|
|
|
|
|
Field f = getField();
|
|
|
|
|
Class<Object> type = (Class<Object>)f.getType();
|
|
|
|
|
Object fieldReference = f.get(target);
|
2010-12-01 20:38:58 +09:00
|
|
|
Object valueReference = template.unpack(pac, fieldReference);
|
2010-11-30 21:59:07 +09:00
|
|
|
if(valueReference != fieldReference) {
|
|
|
|
|
f.set(target, valueReference);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-01 20:38:58 +09:00
|
|
|
static class BooleanFieldEntry extends ReflectionFieldEntry {
|
|
|
|
|
BooleanFieldEntry(FieldEntry e) {
|
2010-11-30 21:59:07 +09:00
|
|
|
super(e);
|
|
|
|
|
}
|
|
|
|
|
public void pack(Object target, Packer pac) throws IOException {
|
|
|
|
|
pac.pack((boolean)(Boolean)target);
|
|
|
|
|
}
|
|
|
|
|
public void convert(Object target, MessagePackObject obj) throws MessageTypeException, IllegalAccessException {
|
|
|
|
|
getField().setBoolean(target, obj.asBoolean());
|
|
|
|
|
}
|
|
|
|
|
public void unpack(Object target, Unpacker pac) throws IOException, MessageTypeException, IllegalAccessException {
|
|
|
|
|
getField().setBoolean(target, pac.unpackBoolean());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-01 20:38:58 +09:00
|
|
|
static class ByteFieldEntry extends ReflectionFieldEntry {
|
|
|
|
|
ByteFieldEntry(FieldEntry e) {
|
2010-11-30 21:59:07 +09:00
|
|
|
super(e);
|
|
|
|
|
}
|
|
|
|
|
public void pack(Object target, Packer pac) throws IOException {
|
|
|
|
|
pac.pack((byte)(Byte)target);
|
|
|
|
|
}
|
|
|
|
|
public void convert(Object target, MessagePackObject obj) throws MessageTypeException, IllegalAccessException {
|
|
|
|
|
getField().setByte(target, obj.asByte());
|
|
|
|
|
}
|
|
|
|
|
public void unpack(Object target, Unpacker pac) throws IOException, MessageTypeException, IllegalAccessException {
|
|
|
|
|
getField().setByte(target, pac.unpackByte());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-01 20:38:58 +09:00
|
|
|
static class ShortFieldEntry extends ReflectionFieldEntry {
|
|
|
|
|
ShortFieldEntry(FieldEntry e) {
|
2010-11-30 21:59:07 +09:00
|
|
|
super(e);
|
|
|
|
|
}
|
|
|
|
|
public void pack(Object target, Packer pac) throws IOException {
|
|
|
|
|
pac.pack((short)(Short)target);
|
|
|
|
|
}
|
|
|
|
|
public void convert(Object target, MessagePackObject obj) throws MessageTypeException, IllegalAccessException {
|
|
|
|
|
getField().setShort(target, obj.asShort());
|
|
|
|
|
}
|
|
|
|
|
public void unpack(Object target, Unpacker pac) throws IOException, MessageTypeException, IllegalAccessException {
|
|
|
|
|
getField().setShort(target, pac.unpackShort());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-01 20:38:58 +09:00
|
|
|
static class IntFieldEntry extends ReflectionFieldEntry {
|
|
|
|
|
IntFieldEntry(FieldEntry e) {
|
2010-11-30 21:59:07 +09:00
|
|
|
super(e);
|
|
|
|
|
}
|
|
|
|
|
public void pack(Object target, Packer pac) throws IOException {
|
|
|
|
|
pac.pack((int)(Integer)target);
|
|
|
|
|
}
|
|
|
|
|
public void convert(Object target, MessagePackObject obj) throws MessageTypeException, IllegalAccessException {
|
|
|
|
|
getField().setInt(target, obj.asInt());
|
|
|
|
|
}
|
|
|
|
|
public void unpack(Object target, Unpacker pac) throws IOException, MessageTypeException, IllegalAccessException {
|
|
|
|
|
getField().setInt(target, pac.unpackInt());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-01 20:38:58 +09:00
|
|
|
static class LongFieldEntry extends ReflectionFieldEntry {
|
|
|
|
|
LongFieldEntry(FieldEntry e) {
|
2010-11-30 21:59:07 +09:00
|
|
|
super(e);
|
|
|
|
|
}
|
|
|
|
|
public void pack(Object target, Packer pac) throws IOException {
|
|
|
|
|
pac.pack((long)(Long)target);
|
|
|
|
|
}
|
|
|
|
|
public void convert(Object target, MessagePackObject obj) throws MessageTypeException, IllegalAccessException {
|
|
|
|
|
getField().setLong(target, obj.asLong());
|
|
|
|
|
}
|
|
|
|
|
public void unpack(Object target, Unpacker pac) throws IOException, MessageTypeException, IllegalAccessException {
|
|
|
|
|
getField().setLong(target, pac.unpackLong());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-01 20:38:58 +09:00
|
|
|
static class FloatFieldEntry extends ReflectionFieldEntry {
|
|
|
|
|
FloatFieldEntry(FieldEntry e) {
|
2010-11-30 21:59:07 +09:00
|
|
|
super(e);
|
|
|
|
|
}
|
|
|
|
|
public void pack(Object target, Packer pac) throws IOException {
|
|
|
|
|
pac.pack((float)(Float)target);
|
|
|
|
|
}
|
|
|
|
|
public void convert(Object target, MessagePackObject obj) throws MessageTypeException, IllegalAccessException {
|
|
|
|
|
getField().setFloat(target, obj.asFloat());
|
|
|
|
|
}
|
|
|
|
|
public void unpack(Object target, Unpacker pac) throws IOException, MessageTypeException, IllegalAccessException {
|
|
|
|
|
getField().setFloat(target, pac.unpackFloat());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-01 20:38:58 +09:00
|
|
|
static class DoubleFieldEntry extends ReflectionFieldEntry {
|
|
|
|
|
DoubleFieldEntry(FieldEntry e) {
|
2010-11-30 21:59:07 +09:00
|
|
|
super(e);
|
|
|
|
|
}
|
|
|
|
|
public void pack(Object target, Packer pac) throws IOException {
|
|
|
|
|
pac.pack((double)(Double)target);
|
|
|
|
|
}
|
|
|
|
|
public void convert(Object target, MessagePackObject obj) throws MessageTypeException, IllegalAccessException {
|
|
|
|
|
getField().setDouble(target, obj.asDouble());
|
|
|
|
|
}
|
|
|
|
|
public void unpack(Object target, Unpacker pac) throws IOException, MessageTypeException, IllegalAccessException {
|
|
|
|
|
getField().setDouble(target, pac.unpackDouble());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-01 20:38:58 +09:00
|
|
|
static class ReflectionTemplate extends AbstractTemplate {
|
|
|
|
|
protected Class<?> targetClass;
|
|
|
|
|
protected ReflectionFieldEntry[] entries;
|
|
|
|
|
protected int minimumArrayLength;
|
2010-11-30 21:59:07 +09:00
|
|
|
|
2010-12-01 20:38:58 +09:00
|
|
|
ReflectionTemplate(Class<?> targetClass, ReflectionFieldEntry[] entries) {
|
2010-11-30 21:59:07 +09:00
|
|
|
this.targetClass = targetClass;
|
|
|
|
|
this.entries = entries;
|
2010-12-01 20:38:58 +09:00
|
|
|
this.minimumArrayLength = 0;
|
2010-11-30 21:59:07 +09:00
|
|
|
for(int i=0; i < entries.length; i++) {
|
2010-12-01 20:38:58 +09:00
|
|
|
ReflectionFieldEntry e = entries[i];
|
|
|
|
|
if(e.isRequired() || e.isNullable()) {
|
|
|
|
|
this.minimumArrayLength = i+1;
|
2010-11-30 21:59:07 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void pack(Packer pk, Object target) throws IOException {
|
|
|
|
|
try {
|
|
|
|
|
pk.packArray(entries.length);
|
|
|
|
|
for(ReflectionFieldEntry e : entries) {
|
|
|
|
|
if(!e.isAvailable()) {
|
|
|
|
|
pk.packNil();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
Object obj = e.getField().get(target);
|
|
|
|
|
if(obj == null) {
|
|
|
|
|
if(!e.isNullable() && !e.isOptional()) {
|
|
|
|
|
throw new MessageTypeException();
|
|
|
|
|
}
|
|
|
|
|
pk.packNil();
|
|
|
|
|
} else {
|
2010-12-01 20:38:58 +09:00
|
|
|
e.pack(obj, pk);
|
2010-11-30 21:59:07 +09:00
|
|
|
}
|
|
|
|
|
}
|
2010-12-01 20:38:58 +09:00
|
|
|
|
|
|
|
|
} catch (MessageTypeException e) {
|
|
|
|
|
throw e;
|
2010-11-30 21:59:07 +09:00
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw e;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new MessageTypeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
|
|
|
|
try {
|
|
|
|
|
if(to == null) {
|
|
|
|
|
to = targetClass.newInstance();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int length = pac.unpackArray();
|
|
|
|
|
if(length < minimumArrayLength) {
|
|
|
|
|
throw new MessageTypeException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
for(i=0; i < minimumArrayLength; i++) {
|
|
|
|
|
ReflectionFieldEntry e = entries[i];
|
2010-12-01 20:38:58 +09:00
|
|
|
if(!e.isAvailable()) {
|
|
|
|
|
pac.unpackObject();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-30 21:59:07 +09:00
|
|
|
if(pac.tryUnpackNull()) {
|
|
|
|
|
if(e.isRequired()) {
|
|
|
|
|
// Requred + nil => exception
|
|
|
|
|
throw new MessageTypeException();
|
|
|
|
|
} else if(e.isOptional()) {
|
|
|
|
|
// Optional + nil => keep default value
|
|
|
|
|
} else { // Nullable
|
|
|
|
|
// Nullable + nil => set null
|
|
|
|
|
e.setNull(to);
|
|
|
|
|
}
|
2010-12-01 20:38:58 +09:00
|
|
|
} else {
|
|
|
|
|
e.unpack(to, pac);
|
2010-11-30 21:59:07 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int max = length < entries.length ? length : entries.length;
|
|
|
|
|
for(; i < max; i++) {
|
|
|
|
|
ReflectionFieldEntry e = entries[i];
|
2010-12-01 20:38:58 +09:00
|
|
|
if(!e.isAvailable()) {
|
|
|
|
|
pac.unpackObject();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-30 21:59:07 +09:00
|
|
|
if(pac.tryUnpackNull()) {
|
|
|
|
|
// this is Optional field becaue i >= minimumArrayLength
|
|
|
|
|
// Optional + nil => keep default value
|
2010-12-01 20:38:58 +09:00
|
|
|
} else {
|
|
|
|
|
e.unpack(to, pac);
|
2010-11-30 21:59:07 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// latter entries are all Optional + nil => keep default value
|
|
|
|
|
|
|
|
|
|
for(; i < length; i++) {
|
|
|
|
|
pac.unpackObject();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return to;
|
|
|
|
|
|
2010-12-01 20:38:58 +09:00
|
|
|
} catch (MessageTypeException e) {
|
|
|
|
|
throw e;
|
2010-11-30 21:59:07 +09:00
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw e;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new MessageTypeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
|
|
|
|
try {
|
|
|
|
|
if(to == null) {
|
|
|
|
|
to = targetClass.newInstance();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MessagePackObject[] array = from.asArray();
|
|
|
|
|
int length = array.length;
|
|
|
|
|
if(length < minimumArrayLength) {
|
|
|
|
|
throw new MessageTypeException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
for(i=0; i < minimumArrayLength; i++) {
|
|
|
|
|
ReflectionFieldEntry e = entries[i];
|
2010-12-01 20:38:58 +09:00
|
|
|
if(!e.isAvailable()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-30 21:59:07 +09:00
|
|
|
MessagePackObject obj = array[i];
|
|
|
|
|
if(obj.isNil()) {
|
|
|
|
|
if(e.isRequired()) {
|
|
|
|
|
// Requred + nil => exception
|
|
|
|
|
throw new MessageTypeException();
|
|
|
|
|
} else if(e.isOptional()) {
|
|
|
|
|
// Optional + nil => keep default value
|
|
|
|
|
} else { // Nullable
|
|
|
|
|
// Nullable + nil => set null
|
|
|
|
|
e.setNull(to);
|
|
|
|
|
}
|
2010-12-01 20:38:58 +09:00
|
|
|
} else {
|
|
|
|
|
e.convert(to, obj);
|
2010-11-30 21:59:07 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int max = length < entries.length ? length : entries.length;
|
|
|
|
|
for(; i < max; i++) {
|
|
|
|
|
ReflectionFieldEntry e = entries[i];
|
2010-12-01 20:38:58 +09:00
|
|
|
if(!e.isAvailable()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-30 21:59:07 +09:00
|
|
|
MessagePackObject obj = array[i];
|
|
|
|
|
if(obj.isNil()) {
|
|
|
|
|
// this is Optional field becaue i >= minimumArrayLength
|
|
|
|
|
// Optional + nil => keep default value
|
2010-12-01 20:38:58 +09:00
|
|
|
} else {
|
|
|
|
|
e.convert(to, obj);
|
2010-11-30 21:59:07 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// latter entries are all Optional + nil => keep default value
|
|
|
|
|
|
|
|
|
|
return to;
|
|
|
|
|
|
2010-12-01 20:38:58 +09:00
|
|
|
} catch (MessageTypeException e) {
|
|
|
|
|
throw e;
|
2010-11-30 21:59:07 +09:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new MessageTypeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Template buildTemplate(Class<?> targetClass, FieldEntry[] entries) {
|
|
|
|
|
for(FieldEntry e : entries) {
|
|
|
|
|
Field f = e.getField();
|
|
|
|
|
int mod = f.getModifiers();
|
|
|
|
|
if(!Modifier.isPublic(mod)) {
|
|
|
|
|
f.setAccessible(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReflectionFieldEntry[] res = new ReflectionFieldEntry[entries.length];
|
|
|
|
|
for(int i=0; i < entries.length; i++) {
|
|
|
|
|
FieldEntry e = entries[i];
|
|
|
|
|
Class<?> type = e.getType();
|
|
|
|
|
if(type.equals(boolean.class)) {
|
|
|
|
|
res[i] = new BooleanFieldEntry(e);
|
|
|
|
|
} else if(type.equals(byte.class)) {
|
|
|
|
|
res[i] = new ByteFieldEntry(e);
|
|
|
|
|
} else if(type.equals(short.class)) {
|
|
|
|
|
res[i] = new ShortFieldEntry(e);
|
|
|
|
|
} else if(type.equals(int.class)) {
|
|
|
|
|
res[i] = new IntFieldEntry(e);
|
|
|
|
|
} else if(type.equals(long.class)) {
|
|
|
|
|
res[i] = new LongFieldEntry(e);
|
|
|
|
|
} else if(type.equals(float.class)) {
|
|
|
|
|
res[i] = new FloatFieldEntry(e);
|
|
|
|
|
} else if(type.equals(double.class)) {
|
|
|
|
|
res[i] = new DoubleFieldEntry(e);
|
|
|
|
|
} else {
|
2010-12-01 20:38:58 +09:00
|
|
|
Template tmpl = TemplateRegistry.lookup(e.getGenericType(), true);
|
|
|
|
|
res[i] = new ObjectFieldEntry(e, tmpl);
|
2010-11-30 21:59:07 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new ReflectionTemplate(targetClass, res);
|
|
|
|
|
}
|
2010-12-01 20:38:58 +09:00
|
|
|
|
|
|
|
|
static class ReflectionOrdinalEnumTemplate extends AbstractTemplate {
|
|
|
|
|
protected Enum<?>[] entries;
|
|
|
|
|
protected Map<Enum<?>, Integer> reverse;
|
|
|
|
|
|
|
|
|
|
ReflectionOrdinalEnumTemplate(Enum<?>[] entries) {
|
|
|
|
|
this.entries = entries;
|
|
|
|
|
this.reverse = new HashMap<Enum<?>, Integer>();
|
|
|
|
|
for(int i=0; i < entries.length; i++) {
|
|
|
|
|
this.reverse.put(entries[i], i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void pack(Packer pk, Object target) throws IOException {
|
|
|
|
|
Integer ord = reverse.get(target);
|
|
|
|
|
if(ord == null) {
|
|
|
|
|
throw new MessageTypeException();
|
|
|
|
|
}
|
|
|
|
|
pk.pack((int)ord);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
|
|
|
|
int ord = pac.unpackInt();
|
|
|
|
|
if(entries.length <= ord) {
|
|
|
|
|
throw new MessageTypeException();
|
|
|
|
|
}
|
|
|
|
|
return entries[ord];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
|
|
|
|
int ord = from.asInt();
|
|
|
|
|
if(entries.length <= ord) {
|
|
|
|
|
throw new MessageTypeException();
|
|
|
|
|
}
|
|
|
|
|
return entries[ord];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Template buildOrdinalEnumTemplate(Class<?> targetClass, Enum<?>[] entries) {
|
|
|
|
|
return new ReflectionOrdinalEnumTemplate(entries);
|
|
|
|
|
}
|
2010-11-30 21:59:07 +09:00
|
|
|
}
|
|
|
|
|
|