java: adds FieldList class

This commit is contained in:
frsyuki 2010-11-09 22:11:47 +09:00
parent 8a7a391166
commit e9d44b90bc
9 changed files with 323 additions and 218 deletions

View File

@ -77,7 +77,7 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
}
public Class<?> generateTemplateClass(Class<?> origClass,
List<FieldOption> fieldOpts) {
FieldList fieldList) {
try {
LOG.debug("start generating a template class for "
+ origClass.getName());
@ -91,9 +91,8 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
addClassTypeConstructor(tmplCtClass);
Field[] fields = getDeclaredFields(origClass);
Template[] tmpls = null;
if (fieldOpts != null) {
fields = sortFields(fields, fieldOpts);
tmpls = createTemplates(fieldOpts);
if (fieldList != null) {
tmpls = createTemplates(fields, fieldList);
} else {
tmpls = createTemplates(fields);
}
@ -212,18 +211,19 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
}
}
Field[] sortFields(Field[] fields, List<FieldOption> fieldOpts) {
if (fields.length != fieldOpts.size()) {
Field[] sortFields(Field[] fields, FieldList fieldList) {
List<FieldList.Entry> list = fieldList.getList();
if (fields.length != list.size()) {
throwFieldSortingException(String.format(
"Mismatch: public field num: %d, option num: %d",
new Object[] { fields.length, fieldOpts.size() }));
new Object[] { fields.length, list.size() }));
}
Field[] sorted = new Field[fields.length];
for (int i = 0; i < sorted.length; ++i) {
FieldOption opt = fieldOpts.get(i);
FieldList.Entry e = list.get(i);
Field match = null;
for (Field f : fields) {
if (opt.name.equals(f.getName())) {
if (e.getName().equals(f.getName())) {
match = f;
break;
}
@ -233,16 +233,42 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
} else {
throwFieldSortingException(String.format(
"Mismatch: a %s field option is not declared",
new Object[] { opt.name }));
new Object[] { e.getName() }));
}
}
return sorted;
}
Template[] createTemplates(List<FieldOption> fieldOpts) {
Template[] tmpls = new Template[fieldOpts.size()];
for (int i = 0; i < tmpls.length; ++i) {
tmpls[i] = fieldOpts.get(i).tmpl;
Template[] createTemplates(Field[] fields, FieldList fieldList) {
List<FieldList.Entry> list = fieldList.getList();
//if (fields.length != list.size()) {
// throwFieldSortingException(String.format(
// "Mismatch: public field num: %d, option num: %d",
// new Object[] { fields.length, list.size() }));
//}
Template[] tmpls = new Template[list.size()];
for(int i = 0; i < list.size(); ++i) {
FieldList.Entry e = list.get(i);
Field match = null;
// FIXME if(!e.isAvailable())
for (Field f : fields) {
if (e.getName().equals(f.getName())) {
match = f;
break;
}
}
if (match == null) {
throwFieldSortingException(String.format(
"Mismatch: a %s field option is not declared",
new Object[] { e.getName() }));
}
Template tmpl = createTemplate(match);
if(e.isOptional()) {
tmpl = new OptionalTemplate(tmpl);
} else if(e.isNullable()) {
tmpl = new NullableTemplate(tmpl);
}
tmpls[i] = tmpl;
}
return tmpls;
}

View File

@ -27,7 +27,7 @@ public class DynamicConverter {
}
public static MessageConverter create(Class<?> c,
List<FieldOption> fieldOpts) {
return DynamicTemplate.create(c, fieldOpts);
FieldList fieldList) {
return DynamicTemplate.create(c, fieldList);
}
}

View File

@ -27,7 +27,7 @@ public class DynamicPacker {
return create(c, null);
}
public static MessagePacker create(Class<?> c, List<FieldOption> fieldOpts) {
return DynamicTemplate.create(c, fieldOpts);
public static MessagePacker create(Class<?> c, FieldList fieldList) {
return DynamicTemplate.create(c, fieldList);
}
}

View File

@ -29,10 +29,10 @@ public class DynamicTemplate {
return create(c, null);
}
public static Template create(Class<?> c, List<FieldOption> fieldOpts) {
public static Template create(Class<?> c, FieldList fieldList) {
try {
DynamicCodeGen gen = DynamicCodeGen.getInstance();
Class<?> tmplClass = gen.generateTemplateClass(c, fieldOpts);
Class<?> tmplClass = gen.generateTemplateClass(c, fieldList);
Constructor<?> cons = tmplClass
.getDeclaredConstructor(new Class[] { Class.class });
Object obj = cons.newInstance(new Object[] { c });

View File

@ -26,7 +26,7 @@ public class DynamicUnpacker {
return create(c, null);
}
public static MessageUnpacker create(Class<?> c, List<FieldOption> fieldOpts) {
return DynamicTemplate.create(c, fieldOpts);
public static MessageUnpacker create(Class<?> c, FieldList fieldList) {
return DynamicTemplate.create(c, fieldList);
}
}

View File

@ -0,0 +1,96 @@
//
// 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.util.codegen;
import java.util.List;
import java.util.ArrayList;
public class FieldList {
public static class Entry {
public Entry() {
this.name = null;
this.option = null;
}
public Entry(String name, FieldOption option) {
this.name = name;
this.option = option;
}
private String name;
private FieldOption option;
public String getName() {
return name;
}
public FieldOption getOption() {
return option;
}
boolean isAvailable() {
return this.name != null;
}
boolean isRequired() {
return this.option == FieldOption.REQUIRED;
}
boolean isOptional() {
return this.option == FieldOption.OPTIONAL;
}
boolean isNullable() {
return this.option == FieldOption.NULLABLE;
}
}
private ArrayList<Entry> list;
public FieldList() {
list = new ArrayList<Entry>();
}
public void add(final String name) {
add(name, FieldOption.REQUIRED);
}
public void add(final String name, final FieldOption option) {
list.add(new Entry(name, option));
}
public void put(int index, final String name) {
put(index, name, FieldOption.REQUIRED);
}
public void put(int index, final String name, final FieldOption option) {
if(list.size() < index) {
do {
list.add(new Entry());
} while(list.size() < index);
list.add(new Entry(name, option));
} else {
list.set(index, new Entry(name, option));
}
}
List<Entry> getList() {
return list;
}
}

View File

@ -17,26 +17,9 @@
//
package org.msgpack.util.codegen;
import org.msgpack.Template;
public class FieldOption {
private static final String NULL_ERR_MSG = "param is FieldOption is null.";
String name;
Template tmpl;
public FieldOption(final String name, final Template tmpl) {
if (name == null) {
throw new NullPointerException(String.format("%s %s", new Object[] {
"1st", NULL_ERR_MSG }));
}
if (tmpl == null) {
throw new NullPointerException(String.format("%s %s", new Object[] {
"2nd", NULL_ERR_MSG }));
}
this.name = name;
this.tmpl = tmpl;
}
public enum FieldOption {
REQUIRED,
OPTIONAL,
NULLABLE;
}

View File

@ -47,14 +47,14 @@ public class TestPackConvertWithFieldOption extends TestCase {
src.f5 = 5;
src.f6 = false;
List<FieldOption> opts = new ArrayList<FieldOption>();
opts.add(new FieldOption("f0", tByte()));
opts.add(new FieldOption("f1", tShort()));
opts.add(new FieldOption("f2", tInteger()));
opts.add(new FieldOption("f3", tLong()));
opts.add(new FieldOption("f4", tFloat()));
opts.add(new FieldOption("f5", tDouble()));
opts.add(new FieldOption("f6", tBoolean()));
FieldList opts = new FieldList();
opts.add("f0");
opts.add("f1");
opts.add("f2");
opts.add("f3");
opts.add("f4");
opts.add("f5");
opts.add("f6");
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = DynamicPacker.create(
PrimitiveTypeFieldsClass.class, opts);
@ -80,14 +80,14 @@ public class TestPackConvertWithFieldOption extends TestCase {
public void testPrimitiveTypeFieldsClass01() throws Exception {
PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass();
List<FieldOption> opts = new ArrayList<FieldOption>();
opts.add(new FieldOption("f0", new OptionalTemplate(tByte())));
opts.add(new FieldOption("f1", new OptionalTemplate(tShort())));
opts.add(new FieldOption("f2", new OptionalTemplate(tInteger())));
opts.add(new FieldOption("f3", new OptionalTemplate(tLong())));
opts.add(new FieldOption("f4", new OptionalTemplate(tFloat())));
opts.add(new FieldOption("f5", new OptionalTemplate(tDouble())));
opts.add(new FieldOption("f6", new OptionalTemplate(tBoolean())));
FieldList opts = new FieldList();
opts.add("f0", FieldOption.OPTIONAL);
opts.add("f1", FieldOption.OPTIONAL);
opts.add("f2", FieldOption.OPTIONAL);
opts.add("f3", FieldOption.OPTIONAL);
opts.add("f4", FieldOption.OPTIONAL);
opts.add("f5", FieldOption.OPTIONAL);
opts.add("f6", FieldOption.OPTIONAL);
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = DynamicPacker.create(
PrimitiveTypeFieldsClass.class, opts);
@ -113,14 +113,14 @@ public class TestPackConvertWithFieldOption extends TestCase {
public void testPrimitiveTypeFieldsClass02() throws Exception {
PrimitiveTypeFieldsClass src = null;
List<FieldOption> opts = new ArrayList<FieldOption>();
opts.add(new FieldOption("f0", tByte()));
opts.add(new FieldOption("f1", tShort()));
opts.add(new FieldOption("f2", tInteger()));
opts.add(new FieldOption("f3", tLong()));
opts.add(new FieldOption("f4", tFloat()));
opts.add(new FieldOption("f5", tDouble()));
opts.add(new FieldOption("f6", tBoolean()));
FieldList opts = new FieldList();
opts.add("f0");
opts.add("f1");
opts.add("f2");
opts.add("f3");
opts.add("f4");
opts.add("f5");
opts.add("f6");
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = new OptionalPacker(DynamicPacker.create(
PrimitiveTypeFieldsClass.class, opts));
@ -163,17 +163,17 @@ public class TestPackConvertWithFieldOption extends TestCase {
src.f8 = "8";
src.f9 = new byte[] { 0x01, 0x02 };
List<FieldOption> opts = new ArrayList<FieldOption>();
opts.add(new FieldOption("f0", tByte()));
opts.add(new FieldOption("f1", tShort()));
opts.add(new FieldOption("f2", tInteger()));
opts.add(new FieldOption("f3", tLong()));
opts.add(new FieldOption("f4", tFloat()));
opts.add(new FieldOption("f5", tDouble()));
opts.add(new FieldOption("f6", tBoolean()));
opts.add(new FieldOption("f7", tBigInteger()));
opts.add(new FieldOption("f8", tString()));
opts.add(new FieldOption("f9", tByteArray()));
FieldList opts = new FieldList();
opts.add("f0");
opts.add("f1");
opts.add("f2");
opts.add("f3");
opts.add("f4");
opts.add("f5");
opts.add("f6");
opts.add("f7");
opts.add("f8");
opts.add("f9");
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = DynamicPacker
.create(GeneralReferenceTypeFieldsClass.class, opts);
@ -213,17 +213,17 @@ public class TestPackConvertWithFieldOption extends TestCase {
src.f8 = null;
src.f9 = null;
List<FieldOption> opts = new ArrayList<FieldOption>();
opts.add(new FieldOption("f0", new OptionalTemplate(tByte())));
opts.add(new FieldOption("f1", new OptionalTemplate(tShort())));
opts.add(new FieldOption("f2", new OptionalTemplate(tInteger())));
opts.add(new FieldOption("f3", new OptionalTemplate(tLong())));
opts.add(new FieldOption("f4", new OptionalTemplate(tFloat())));
opts.add(new FieldOption("f5", new OptionalTemplate(tDouble())));
opts.add(new FieldOption("f6", new OptionalTemplate(tBoolean())));
opts.add(new FieldOption("f7", new OptionalTemplate(tBigInteger())));
opts.add(new FieldOption("f8", new OptionalTemplate(tString())));
opts.add(new FieldOption("f9", new OptionalTemplate(tByteArray())));
FieldList opts = new FieldList();
opts.add("f0", FieldOption.OPTIONAL);
opts.add("f1", FieldOption.OPTIONAL);
opts.add("f2", FieldOption.OPTIONAL);
opts.add("f3", FieldOption.OPTIONAL);
opts.add("f4", FieldOption.OPTIONAL);
opts.add("f5", FieldOption.OPTIONAL);
opts.add("f6", FieldOption.OPTIONAL);
opts.add("f7", FieldOption.OPTIONAL);
opts.add("f8", FieldOption.OPTIONAL);
opts.add("f9", FieldOption.OPTIONAL);
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = DynamicPacker
.create(GeneralReferenceTypeFieldsClass.class, opts);
@ -253,17 +253,17 @@ public class TestPackConvertWithFieldOption extends TestCase {
throws Exception {
GeneralReferenceTypeFieldsClass src = null;
List<FieldOption> opts = new ArrayList<FieldOption>();
opts.add(new FieldOption("f0", new OptionalTemplate(tByte())));
opts.add(new FieldOption("f1", new OptionalTemplate(tShort())));
opts.add(new FieldOption("f2", new OptionalTemplate(tInteger())));
opts.add(new FieldOption("f3", new OptionalTemplate(tLong())));
opts.add(new FieldOption("f4", new OptionalTemplate(tFloat())));
opts.add(new FieldOption("f5", new OptionalTemplate(tDouble())));
opts.add(new FieldOption("f6", new OptionalTemplate(tBoolean())));
opts.add(new FieldOption("f7", new OptionalTemplate(tBigInteger())));
opts.add(new FieldOption("f8", new OptionalTemplate(tString())));
opts.add(new FieldOption("f9", new OptionalTemplate(tByteArray())));
FieldList opts = new FieldList();
opts.add("f0", FieldOption.OPTIONAL);
opts.add("f1", FieldOption.OPTIONAL);
opts.add("f2", FieldOption.OPTIONAL);
opts.add("f3", FieldOption.OPTIONAL);
opts.add("f4", FieldOption.OPTIONAL);
opts.add("f5", FieldOption.OPTIONAL);
opts.add("f6", FieldOption.OPTIONAL);
opts.add("f7", FieldOption.OPTIONAL);
opts.add("f8", FieldOption.OPTIONAL);
opts.add("f9", FieldOption.OPTIONAL);
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = new OptionalPacker(DynamicPacker
.create(GeneralReferenceTypeFieldsClass.class, opts));
@ -316,12 +316,12 @@ public class TestPackConvertWithFieldOption extends TestCase {
slnt.f1 = "muga";
src.f4.add(slnt);
List<FieldOption> opts = new ArrayList<FieldOption>();
opts.add(new FieldOption("f0", new ListTemplate(tInteger())));
opts.add(new FieldOption("f1", new ListTemplate(tInteger())));
opts.add(new FieldOption("f2", new ListTemplate(tString())));
opts.add(new FieldOption("f3", new ListTemplate(new ListTemplate(tString()))));
opts.add(new FieldOption("f4", new ListTemplate(DynamicTemplate.create(SampleListNestedType.class))));
FieldList opts = new FieldList();
opts.add("f0");
opts.add("f1");
opts.add("f2");
opts.add("f3");
opts.add("f4");
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = DynamicPacker
.create(SampleListTypes.class, opts);
@ -371,12 +371,12 @@ public class TestPackConvertWithFieldOption extends TestCase {
src.f3 = new ArrayList<List<String>>();
src.f4 = null;
List<FieldOption> opts = new ArrayList<FieldOption>();
opts.add(new FieldOption("f0", new OptionalTemplate(new ListTemplate(tInteger()))));
opts.add(new FieldOption("f1", new OptionalTemplate(new ListTemplate(tInteger()))));
opts.add(new FieldOption("f2", new OptionalTemplate(new ListTemplate(tString()))));
opts.add(new FieldOption("f3", new OptionalTemplate(new ListTemplate(new ListTemplate(tString())))));
opts.add(new FieldOption("f4", new OptionalTemplate(new ListTemplate(DynamicTemplate.create(SampleListNestedType.class)))));
FieldList opts = new FieldList();
opts.add("f0", FieldOption.OPTIONAL);
opts.add("f1", FieldOption.OPTIONAL);
opts.add("f2", FieldOption.OPTIONAL);
opts.add("f3", FieldOption.OPTIONAL);
opts.add("f4", FieldOption.OPTIONAL);
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = DynamicPacker
.create(SampleListTypes.class, opts);
@ -400,12 +400,12 @@ public class TestPackConvertWithFieldOption extends TestCase {
public void testListTypes02() throws Exception {
SampleListTypes src = null;
List<FieldOption> opts = new ArrayList<FieldOption>();
opts.add(new FieldOption("f0", new OptionalTemplate(new ListTemplate(tInteger()))));
opts.add(new FieldOption("f1", new OptionalTemplate(new ListTemplate(tInteger()))));
opts.add(new FieldOption("f2", new OptionalTemplate(new ListTemplate(tString()))));
opts.add(new FieldOption("f3", new OptionalTemplate(new ListTemplate(new ListTemplate(tString())))));
opts.add(new FieldOption("f4", new OptionalTemplate(new ListTemplate(DynamicTemplate.create(SampleListNestedType.class)))));
FieldList opts = new FieldList();
opts.add("f0", FieldOption.OPTIONAL);
opts.add("f1", FieldOption.OPTIONAL);
opts.add("f2", FieldOption.OPTIONAL);
opts.add("f3", FieldOption.OPTIONAL);
opts.add("f4", FieldOption.OPTIONAL);
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = new OptionalPacker(DynamicPacker
.create(SampleListTypes.class));
@ -455,10 +455,10 @@ public class TestPackConvertWithFieldOption extends TestCase {
src.f2.put("k2", 2);
src.f2.put("k3", 3);
List<FieldOption> opts = new ArrayList<FieldOption>();
opts.add(new FieldOption("f0", new MapTemplate(tInteger(), tInteger())));
opts.add(new FieldOption("f1", new MapTemplate(tInteger(), tInteger())));
opts.add(new FieldOption("f2", new MapTemplate(tString(), tInteger())));
FieldList opts = new FieldList();
opts.add("f0");
opts.add("f1");
opts.add("f2");
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = DynamicPacker
.create(SampleMapTypes.class, opts);
@ -499,10 +499,10 @@ public class TestPackConvertWithFieldOption extends TestCase {
src.f1 = null;
src.f2 = new HashMap<String, Integer>();
List<FieldOption> opts = new ArrayList<FieldOption>();
opts.add(new FieldOption("f0", new OptionalTemplate(new MapTemplate(tInteger(), tInteger()))));
opts.add(new FieldOption("f1", new OptionalTemplate(new MapTemplate(tInteger(), tInteger()))));
opts.add(new FieldOption("f2", new OptionalTemplate(new MapTemplate(tString(), tInteger()))));
FieldList opts = new FieldList();
opts.add("f0", FieldOption.OPTIONAL);
opts.add("f1", FieldOption.OPTIONAL);
opts.add("f2", FieldOption.OPTIONAL);
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = DynamicPacker
.create(SampleMapTypes.class, opts);
@ -524,10 +524,10 @@ public class TestPackConvertWithFieldOption extends TestCase {
public void testMapTypes02() throws Exception {
SampleMapTypes src = null;
List<FieldOption> opts = new ArrayList<FieldOption>();
opts.add(new FieldOption("f0", new OptionalTemplate(new MapTemplate(tInteger(), tInteger()))));
opts.add(new FieldOption("f1", new OptionalTemplate(new MapTemplate(tInteger(), tInteger()))));
opts.add(new FieldOption("f2", new OptionalTemplate(new MapTemplate(tString(), tInteger()))));
FieldList opts = new FieldList();
opts.add("f0", FieldOption.OPTIONAL);
opts.add("f1", FieldOption.OPTIONAL);
opts.add("f2", FieldOption.OPTIONAL);
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = new OptionalPacker(DynamicPacker
.create(SampleMapTypes.class, opts));

View File

@ -46,14 +46,14 @@ public class TestPackUnpackWithFieldOption extends TestCase {
src.f5 = 5;
src.f6 = false;
List<FieldOption> opts = new ArrayList<FieldOption>();
opts.add(new FieldOption("f0", tByte()));
opts.add(new FieldOption("f1", tShort()));
opts.add(new FieldOption("f2", tInteger()));
opts.add(new FieldOption("f3", tLong()));
opts.add(new FieldOption("f4", tFloat()));
opts.add(new FieldOption("f5", tDouble()));
opts.add(new FieldOption("f6", tBoolean()));
FieldList opts = new FieldList();
opts.add("f0");
opts.add("f1");
opts.add("f2");
opts.add("f3");
opts.add("f4");
opts.add("f5");
opts.add("f6");
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = DynamicPacker.create(
PrimitiveTypeFieldsClass.class, opts);
@ -76,14 +76,14 @@ public class TestPackUnpackWithFieldOption extends TestCase {
public void testPrimitiveTypeFieldsClass01() throws Exception {
PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass();
List<FieldOption> opts = new ArrayList<FieldOption>();
opts.add(new FieldOption("f0", new OptionalTemplate(tByte())));
opts.add(new FieldOption("f1", new OptionalTemplate(tShort())));
opts.add(new FieldOption("f2", new OptionalTemplate(tInteger())));
opts.add(new FieldOption("f3", new OptionalTemplate(tLong())));
opts.add(new FieldOption("f4", new OptionalTemplate(tFloat())));
opts.add(new FieldOption("f5", new OptionalTemplate(tDouble())));
opts.add(new FieldOption("f6", new OptionalTemplate(tBoolean())));
FieldList opts = new FieldList();
opts.add("f0", FieldOption.OPTIONAL);
opts.add("f1", FieldOption.OPTIONAL);
opts.add("f2", FieldOption.OPTIONAL);
opts.add("f3", FieldOption.OPTIONAL);
opts.add("f4", FieldOption.OPTIONAL);
opts.add("f5", FieldOption.OPTIONAL);
opts.add("f6", FieldOption.OPTIONAL);
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = DynamicPacker.create(
PrimitiveTypeFieldsClass.class, opts);
@ -106,14 +106,14 @@ public class TestPackUnpackWithFieldOption extends TestCase {
public void testPrimitiveTypeFieldsClass02() throws Exception {
PrimitiveTypeFieldsClass src = null;
List<FieldOption> opts = new ArrayList<FieldOption>();
opts.add(new FieldOption("f0", tByte()));
opts.add(new FieldOption("f1", tShort()));
opts.add(new FieldOption("f2", tInteger()));
opts.add(new FieldOption("f3", tLong()));
opts.add(new FieldOption("f4", tFloat()));
opts.add(new FieldOption("f5", tDouble()));
opts.add(new FieldOption("f6", tBoolean()));
FieldList opts = new FieldList();
opts.add("f0");
opts.add("f1");
opts.add("f2");
opts.add("f3");
opts.add("f4");
opts.add("f5");
opts.add("f6");
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = new OptionalPacker(DynamicPacker.create(
PrimitiveTypeFieldsClass.class, opts));
@ -153,17 +153,17 @@ public class TestPackUnpackWithFieldOption extends TestCase {
src.f8 = "8";
src.f9 = new byte[] { 0x01, 0x02 };
List<FieldOption> opts = new ArrayList<FieldOption>();
opts.add(new FieldOption("f0", tByte()));
opts.add(new FieldOption("f1", tShort()));
opts.add(new FieldOption("f2", tInteger()));
opts.add(new FieldOption("f3", tLong()));
opts.add(new FieldOption("f4", tFloat()));
opts.add(new FieldOption("f5", tDouble()));
opts.add(new FieldOption("f6", tBoolean()));
opts.add(new FieldOption("f7", tBigInteger()));
opts.add(new FieldOption("f8", tString()));
opts.add(new FieldOption("f9", tByteArray()));
FieldList opts = new FieldList();
opts.add("f0");
opts.add("f1");
opts.add("f2");
opts.add("f3");
opts.add("f4");
opts.add("f5");
opts.add("f6");
opts.add("f7");
opts.add("f8");
opts.add("f9");
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = DynamicPacker
.create(GeneralReferenceTypeFieldsClass.class, opts);
@ -200,17 +200,17 @@ public class TestPackUnpackWithFieldOption extends TestCase {
src.f8 = null;
src.f9 = null;
List<FieldOption> opts = new ArrayList<FieldOption>();
opts.add(new FieldOption("f0", new OptionalTemplate(tByte())));
opts.add(new FieldOption("f1", new OptionalTemplate(tShort())));
opts.add(new FieldOption("f2", new OptionalTemplate(tInteger())));
opts.add(new FieldOption("f3", new OptionalTemplate(tLong())));
opts.add(new FieldOption("f4", new OptionalTemplate(tFloat())));
opts.add(new FieldOption("f5", new OptionalTemplate(tDouble())));
opts.add(new FieldOption("f6", new OptionalTemplate(tBoolean())));
opts.add(new FieldOption("f7", new OptionalTemplate(tBigInteger())));
opts.add(new FieldOption("f8", new OptionalTemplate(tString())));
opts.add(new FieldOption("f9", new OptionalTemplate(tByteArray())));
FieldList opts = new FieldList();
opts.add("f0", FieldOption.OPTIONAL);
opts.add("f1", FieldOption.OPTIONAL);
opts.add("f2", FieldOption.OPTIONAL);
opts.add("f3", FieldOption.OPTIONAL);
opts.add("f4", FieldOption.OPTIONAL);
opts.add("f5", FieldOption.OPTIONAL);
opts.add("f6", FieldOption.OPTIONAL);
opts.add("f7", FieldOption.OPTIONAL);
opts.add("f8", FieldOption.OPTIONAL);
opts.add("f9", FieldOption.OPTIONAL);
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = DynamicPacker
.create(GeneralReferenceTypeFieldsClass.class, opts);
@ -237,17 +237,17 @@ public class TestPackUnpackWithFieldOption extends TestCase {
throws Exception {
GeneralReferenceTypeFieldsClass src = null;
List<FieldOption> opts = new ArrayList<FieldOption>();
opts.add(new FieldOption("f0", new OptionalTemplate(tByte())));
opts.add(new FieldOption("f1", new OptionalTemplate(tShort())));
opts.add(new FieldOption("f2", new OptionalTemplate(tInteger())));
opts.add(new FieldOption("f3", new OptionalTemplate(tLong())));
opts.add(new FieldOption("f4", new OptionalTemplate(tFloat())));
opts.add(new FieldOption("f5", new OptionalTemplate(tDouble())));
opts.add(new FieldOption("f6", new OptionalTemplate(tBoolean())));
opts.add(new FieldOption("f7", new OptionalTemplate(tBigInteger())));
opts.add(new FieldOption("f8", new OptionalTemplate(tString())));
opts.add(new FieldOption("f9", new OptionalTemplate(tByteArray())));
FieldList opts = new FieldList();
opts.add("f0", FieldOption.OPTIONAL);
opts.add("f1", FieldOption.OPTIONAL);
opts.add("f2", FieldOption.OPTIONAL);
opts.add("f3", FieldOption.OPTIONAL);
opts.add("f4", FieldOption.OPTIONAL);
opts.add("f5", FieldOption.OPTIONAL);
opts.add("f6", FieldOption.OPTIONAL);
opts.add("f7", FieldOption.OPTIONAL);
opts.add("f8", FieldOption.OPTIONAL);
opts.add("f9", FieldOption.OPTIONAL);
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = new OptionalPacker(DynamicPacker
.create(GeneralReferenceTypeFieldsClass.class, opts));
@ -296,12 +296,12 @@ public class TestPackUnpackWithFieldOption extends TestCase {
slnt.f1 = "muga";
src.f4.add(slnt);
List<FieldOption> opts = new ArrayList<FieldOption>();
opts.add(new FieldOption("f0", new ListTemplate(tInteger())));
opts.add(new FieldOption("f1", new ListTemplate(tInteger())));
opts.add(new FieldOption("f2", new ListTemplate(tString())));
opts.add(new FieldOption("f3", new ListTemplate(new ListTemplate(tString()))));
opts.add(new FieldOption("f4", new ListTemplate(DynamicTemplate.create(SampleListNestedType.class))));
FieldList opts = new FieldList();
opts.add("f0", FieldOption.OPTIONAL);
opts.add("f1", FieldOption.OPTIONAL);
opts.add("f2", FieldOption.OPTIONAL);
opts.add("f3", FieldOption.OPTIONAL);
opts.add("f4", FieldOption.OPTIONAL);
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = DynamicPacker
.create(SampleListTypes.class, opts);
@ -347,12 +347,12 @@ public class TestPackUnpackWithFieldOption extends TestCase {
src.f3 = new ArrayList<List<String>>();
src.f4 = null;
List<FieldOption> opts = new ArrayList<FieldOption>();
opts.add(new FieldOption("f0", new OptionalTemplate(new ListTemplate(tInteger()))));
opts.add(new FieldOption("f1", new OptionalTemplate(new ListTemplate(tInteger()))));
opts.add(new FieldOption("f2", new OptionalTemplate(new ListTemplate(tString()))));
opts.add(new FieldOption("f3", new OptionalTemplate(new ListTemplate(new ListTemplate(tString())))));
opts.add(new FieldOption("f4", new OptionalTemplate(new ListTemplate(DynamicTemplate.create(SampleListNestedType.class)))));
FieldList opts = new FieldList();
opts.add("f0", FieldOption.OPTIONAL);
opts.add("f1", FieldOption.OPTIONAL);
opts.add("f2", FieldOption.OPTIONAL);
opts.add("f3", FieldOption.OPTIONAL);
opts.add("f4", FieldOption.OPTIONAL);
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = DynamicPacker
.create(SampleListTypes.class, opts);
@ -372,12 +372,12 @@ public class TestPackUnpackWithFieldOption extends TestCase {
public void testListTypes02() throws Exception {
SampleListTypes src = null;
List<FieldOption> opts = new ArrayList<FieldOption>();
opts.add(new FieldOption("f0", new OptionalTemplate(new ListTemplate(tInteger()))));
opts.add(new FieldOption("f1", new OptionalTemplate(new ListTemplate(tInteger()))));
opts.add(new FieldOption("f2", new OptionalTemplate(new ListTemplate(tString()))));
opts.add(new FieldOption("f3", new OptionalTemplate(new ListTemplate(new ListTemplate(tString())))));
opts.add(new FieldOption("f4", new OptionalTemplate(new ListTemplate(DynamicTemplate.create(SampleListNestedType.class)))));
FieldList opts = new FieldList();
opts.add("f0", FieldOption.OPTIONAL);
opts.add("f1", FieldOption.OPTIONAL);
opts.add("f2", FieldOption.OPTIONAL);
opts.add("f3", FieldOption.OPTIONAL);
opts.add("f4", FieldOption.OPTIONAL);
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = new OptionalPacker(DynamicPacker
.create(SampleListTypes.class));
@ -422,10 +422,10 @@ public class TestPackUnpackWithFieldOption extends TestCase {
src.f2.put("k2", 2);
src.f2.put("k3", 3);
List<FieldOption> opts = new ArrayList<FieldOption>();
opts.add(new FieldOption("f0", new MapTemplate(tInteger(), tInteger())));
opts.add(new FieldOption("f1", new MapTemplate(tInteger(), tInteger())));
opts.add(new FieldOption("f2", new MapTemplate(tString(), tInteger())));
FieldList opts = new FieldList();
opts.add("f0");
opts.add("f1");
opts.add("f2");
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = DynamicPacker
.create(SampleMapTypes.class, opts);
@ -462,10 +462,10 @@ public class TestPackUnpackWithFieldOption extends TestCase {
src.f1 = null;
src.f2 = new HashMap<String, Integer>();
List<FieldOption> opts = new ArrayList<FieldOption>();
opts.add(new FieldOption("f0", new OptionalTemplate(new MapTemplate(tInteger(), tInteger()))));
opts.add(new FieldOption("f1", new OptionalTemplate(new MapTemplate(tInteger(), tInteger()))));
opts.add(new FieldOption("f2", new OptionalTemplate(new MapTemplate(tString(), tInteger()))));
FieldList opts = new FieldList();
opts.add("f0", FieldOption.OPTIONAL);
opts.add("f1", FieldOption.OPTIONAL);
opts.add("f2", FieldOption.OPTIONAL);
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = DynamicPacker
.create(SampleMapTypes.class, opts);
@ -483,10 +483,10 @@ public class TestPackUnpackWithFieldOption extends TestCase {
public void testMapTypes02() throws Exception {
SampleMapTypes src = null;
List<FieldOption> opts = new ArrayList<FieldOption>();
opts.add(new FieldOption("f0", new OptionalTemplate(new MapTemplate(tInteger(), tInteger()))));
opts.add(new FieldOption("f1", new OptionalTemplate(new MapTemplate(tInteger(), tInteger()))));
opts.add(new FieldOption("f2", new OptionalTemplate(new MapTemplate(tString(), tInteger()))));
FieldList opts = new FieldList();
opts.add("f0", FieldOption.OPTIONAL);
opts.add("f1", FieldOption.OPTIONAL);
opts.add("f2", FieldOption.OPTIONAL);
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = new OptionalPacker(DynamicPacker
.create(SampleMapTypes.class, opts));