Skip to content

Commit 672c727

Browse files
committed
removed Required option; integrated to NotNullable
1 parent 9e21556 commit 672c727

11 files changed

+87
-320
lines changed

src/main/java/org/msgpack/annotation/Required.java

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/main/java/org/msgpack/template/FieldList.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,6 @@ public FieldOption getOption() {
4747
public boolean isAvailable() {
4848
return option != FieldOption.IGNORE;
4949
}
50-
51-
public boolean isRequired() {
52-
return option == FieldOption.REQUIRED;
53-
}
54-
55-
public boolean isOptional() {
56-
return option == FieldOption.OPTIONAL;
57-
}
58-
59-
public boolean isNullable() {
60-
return option == FieldOption.NOTNULLABLE;
61-
}
6250
}
6351

6452
private ArrayList<Entry> list;
@@ -68,15 +56,15 @@ public FieldList() {
6856
}
6957

7058
public void add(final String name) {
71-
add(name, FieldOption.REQUIRED);
59+
add(name, FieldOption.DEFAULT);
7260
}
7361

7462
public void add(final String name, final FieldOption option) {
7563
list.add(new Entry(name, option));
7664
}
7765

7866
public void put(final int index, final String name) {
79-
put(index, name, FieldOption.REQUIRED);
67+
put(index, name, FieldOption.DEFAULT);
8068
}
8169

8270
public void put(final int index, final String name, final FieldOption option) {

src/main/java/org/msgpack/template/FieldOption.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
public enum FieldOption {
2121
IGNORE,
22-
REQUIRED,
2322
OPTIONAL,
2423
NOTNULLABLE,
2524
DEFAULT;

src/main/java/org/msgpack/template/builder/AbstractTemplateBuilder.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.msgpack.annotation.MessagePackMessage;
3232
import org.msgpack.annotation.NotNullable;
3333
import org.msgpack.annotation.Optional;
34-
import org.msgpack.annotation.Required;
3534
import org.msgpack.template.FieldList;
3635
import org.msgpack.template.FieldOption;
3736
import org.msgpack.template.Template;
@@ -200,16 +199,10 @@ private static FieldOption readFieldOption(Field field, FieldOption implicitOpti
200199

201200
if (isAnnotated(field, Ignore.class)) {
202201
return FieldOption.IGNORE;
203-
} else if(isAnnotated(field, Required.class)) {
204-
return FieldOption.REQUIRED;
205202
} else if(isAnnotated(field, Optional.class)) {
206203
return FieldOption.OPTIONAL;
207204
} else if(isAnnotated(field, NotNullable.class)) {
208-
if(field.getDeclaringClass().isPrimitive()) {
209-
return FieldOption.REQUIRED;
210-
} else {
211-
return FieldOption.NOTNULLABLE;
212-
}
205+
return FieldOption.NOTNULLABLE;
213206
}
214207

215208
if (implicitOption != FieldOption.DEFAULT) {
@@ -218,12 +211,13 @@ private static FieldOption readFieldOption(Field field, FieldOption implicitOpti
218211

219212
// default mode:
220213
// transient : Ignore
221-
// public : Required
214+
// public : NotNullable // FIXME
222215
// others : Ignore
223216
if (Modifier.isTransient(mod)) {
224217
return FieldOption.IGNORE;
225218
} else if(Modifier.isPublic(mod)) {
226-
return FieldOption.REQUIRED;
219+
// FIXME primitive->NOTNULLABLE, otherwise->OPTIONAL
220+
return FieldOption.NOTNULLABLE;
227221
} else {
228222
return FieldOption.IGNORE;
229223
}

src/main/java/org/msgpack/template/builder/BeansBuildContext.java

Lines changed: 27 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ public class BeansBuildContext extends BuildContext<BeansFieldEntry> {
3939

4040
protected Template<?>[] templates;
4141

42-
protected int minArrayLength;
43-
4442
public BeansBuildContext(JavassistTemplateBuilder director) {
4543
super(director);
4644
}
@@ -79,14 +77,6 @@ protected Template buildInstance(Class<?> c) throws NoSuchMethodException,
7977
}
8078

8179
protected void buildMethodInit() {
82-
minArrayLength = 0;
83-
for (int i = 0; i < entries.length; i++) {
84-
FieldEntry e = entries[i];
85-
if (e.isRequired() || !e.isNotNullable()) {
86-
// TODO #MN
87-
minArrayLength = i + 1;
88-
}
89-
}
9080
}
9181

9282
@Override
@@ -106,7 +96,7 @@ protected String buildWriteMethodBody() {
10696
buildString("$1.%s(_$$_t.%s());", primitiveWriteName(type), e.getGetterName());
10797
} else {
10898
buildString("if(_$$_t.%s() == null) {", e.getGetterName());
109-
if (e.isNotNullable() && !e.isOptional()) {
99+
if (e.isNotNullable()) {
110100
buildString("throw new %s();", MessageTypeException.class.getName());
111101
} else {
112102
buildString("$1.writeNil();");
@@ -133,72 +123,40 @@ protected String buildReadMethodBody() {
133123
buildString(" _$$_t = (%s)$2;", origName);
134124
buildString("}");
135125

136-
buildString("int length = $1.readArrayBegin();");
137-
buildString("if(length < %d) {", minArrayLength);
138-
buildString(" throw new %s();", MessageTypeException.class.getName());
139-
buildString("}");
126+
buildString("$1.readArrayBegin();");
140127

141-
int i;
142-
for (i = 0; i < minArrayLength; i++) {
128+
for(int i=0; i < entries.length; i++) {
143129
BeansFieldEntry e = entries[i];
144-
if (!e.isAvailable()) {
145-
buildString("$1.skip();"); // TODO #MN
146-
continue;
147-
}
148-
149-
buildString("if ($1.tryReadNil()) {");
150-
if (e.isRequired()) {
151-
// Required + nil => exception
152-
buildString("throw new %s();", MessageTypeException.class.getName());
153-
} else if (e.isOptional()) {
154-
// Optional + nil => keep default value
155-
} else { // Nullable
156-
// Nullable + nil => set null
157-
buildString("_$$_t.%s(null);", e.getSetterName());
158-
}
159-
buildString("} else {");
160-
Class<?> type = e.getType();
161-
if (type.isPrimitive()) {
162-
buildString("_$$_t.set%s( $1.%s() );", e.getName(), primitiveReadName(type));
163-
} else {
164-
buildString("_$$_t.set%s( (%s)this.templates[%d].read($1, _$$_t.get%s()) );",
165-
e.getName(), e.getJavaTypeName(), i, e.getName());
166-
}
167-
buildString("}");
168-
}
169-
170-
for (; i < entries.length; i++) {
171-
buildString("if(length <= %d) { return _$$_t; }", i);
172130

173-
BeansFieldEntry e = entries[i];
174131
if (!e.isAvailable()) {
175132
buildString("$1.skip();"); // TODO #MN
176-
continue;
177-
}
178-
179-
buildString("if($1.tryReadNil()) {");
180-
// this is Optional field becaue i >= minimumArrayLength
181-
// Optional + nil => keep default value
182-
buildString("} else {");
183-
Class<?> type = e.getType();
184-
if (type.isPrimitive()) {
185-
buildString("_$$_t.%s( $1.%s() );", e.getSetterName(), primitiveReadName(type));
186-
} else {
187-
buildString("_$$_t.%s( (%s)this.templates[%d].read($1, _$$_t.%s()) );",
188-
e.getSetterName(), e.getJavaTypeName(), i, e.getGetterName());
189-
}
190-
buildString("}");
191-
}
192-
193-
// latter entries are all Optional + nil => keep default value
194-
195-
buildString("for(int i=%d; i < length; i++) {", i);
196-
buildString(" $1.skip();"); // TODO #MN
197-
buildString("}");
133+
continue;
134+
}
198135

136+
if (e.isOptional()) {
137+
buildString("if($1.trySkipNil()) {");
138+
buildString("_$$_t.%s(null);", e.getSetterName());
139+
buildString("} else {");
140+
}
141+
142+
Class<?> type = e.getType();
143+
if (type.isPrimitive()) {
144+
buildString("_$$_t.%s( $1.%s() );", e.getSetterName(), primitiveReadName(type));
145+
} else {
146+
buildString("_$$_t.%s( (%s)this.templates[%d].read($1, _$$_t.%s()) );",
147+
e.getSetterName(), e.getJavaTypeName(), i, e.getGetterName());
148+
}
149+
150+
if (e.isOptional()) {
151+
buildString("}");
152+
}
153+
}
154+
155+
buildString("$1.readArrayEnd();");
199156
buildString("return _$$_t;");
200157

201158
buildString("}");
159+
202160
return getBuiltString();
203161
}
204162

@@ -211,4 +169,4 @@ public void writeTemplate(Class<?> targetClass, BeansFieldEntry[] entries, Templ
211169
public Template loadTemplate(Class<?> targetClass, BeansFieldEntry[] entries, Template[] templates) {
212170
return null;
213171
}
214-
}
172+
}

src/main/java/org/msgpack/template/builder/DefaultBuildContext.java

Lines changed: 12 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ public class DefaultBuildContext extends BuildContext<FieldEntry> {
3838

3939
protected Template<?>[] templates;
4040

41-
protected int minArrayLength;
42-
4341
public DefaultBuildContext(JavassistTemplateBuilder director) {
4442
super(director);
4543
}
@@ -76,14 +74,6 @@ protected Template buildInstance(Class<?> c) throws NoSuchMethodException,
7674
}
7775

7876
protected void buildMethodInit() {
79-
minArrayLength = 0;
80-
for (int i = 0; i < entries.length; i++) {
81-
FieldEntry e = entries[i];
82-
if (e.isRequired() || !e.isNotNullable()) {
83-
// TODO #MN
84-
minArrayLength = i + 1;
85-
}
86-
}
8777
}
8878

8979
protected String buildWriteMethodBody() {
@@ -103,7 +93,7 @@ protected String buildWriteMethodBody() {
10393
e.getName());
10494
} else {
10595
buildString("if (_$$_t.%s == null) {", e.getName());
106-
if (e.isNotNullable() && !e.isOptional()) {
96+
if (e.isNotNullable()) {
10797
buildString("throw new %s();",
10898
MessageTypeException.class.getName());
10999
} else {
@@ -130,68 +120,34 @@ protected String buildReadMethodBody() {
130120
buildString(" _$$_t = (%s) $2;", origName);
131121
buildString("}");
132122

133-
buildString("int length = $1.readArrayBegin();");
134-
buildString("if (length < %d) {", minArrayLength);
135-
buildString(" throw new %s();", MessageTypeException.class.getName());
136-
buildString("}");
123+
buildString("$1.readArrayBegin();");
137124

138125
int i;
139-
for (i = 0; i < minArrayLength; i++) {
126+
for (i = 0; i < entries.length; i++) {
140127
FieldEntry e = entries[i];
141128
if (!e.isAvailable()) {
142129
buildString("$1.skip();"); // TODO #MN
143130
continue;
144131
}
145132

146-
buildString("if ($1.tryReadNil()) {");
147-
if (e.isRequired()) {
148-
// Required + nil => exception
149-
buildString("throw new %s();", MessageTypeException.class.getName());
150-
} else if (e.isOptional()) {
151-
// Optional + nil => keep default value
152-
} else { // Nullable
153-
// Nullable + nil => set null
133+
if (e.isOptional()) {
134+
buildString("if($1.trySkipNil()) {");
154135
buildString("_$$_t.%s = null;", e.getName());
155-
}
156-
buildString("} else {");
157-
Class<?> type = e.getType();
158-
if (type.isPrimitive()) {
159-
buildString("_$$_t.%s = $1.%s();", e.getName(), primitiveReadName(type));
160-
} else {
161-
buildString("_$$_t.%s = (%s) this.templates[%d].read($1, _$$_t.%s);",
162-
e.getName(), e.getJavaTypeName(), i, e.getName());
163-
}
164-
buildString("}");
165-
}
136+
buildString("} else {");
137+
}
166138

167-
for (; i < entries.length; i++) {
168-
buildString("if (length <= %d) { return _$$_t; }", i);
169-
170-
FieldEntry e = entries[i];
171-
if (!e.isAvailable()) {
172-
buildString("$1.skip();"); // TODO #MN
173-
continue;
174-
}
175-
176-
buildString("if ($1.tryReadNil()) {");
177-
// this is Optional field becaue i >= minimumArrayLength
178-
// Optional + nil => keep default value
179-
buildString("} else {");
180139
Class<?> type = e.getType();
181140
if (type.isPrimitive()) {
182141
buildString("_$$_t.%s = $1.%s();", e.getName(), primitiveReadName(type));
183142
} else {
184143
buildString("_$$_t.%s = (%s) this.templates[%d].read($1, _$$_t.%s);",
185144
e.getName(), e.getJavaTypeName(), i, e.getName());
186145
}
187-
buildString("}");
188-
}
189146

190-
// latter entries are all Optional + nil => keep default value
191-
192-
buildString("for (int i = %d; i < length; i++) {", i);
193-
buildString(" $1.skip();"); // TODO #MN
194-
buildString("}");
147+
if (e.isOptional()) {
148+
buildString("}");
149+
}
150+
}
195151

196152
buildString("$1.readArrayEnd();");
197153
buildString("return _$$_t;");
@@ -217,4 +173,4 @@ public Template loadTemplate(Class<?> targetClass, FieldEntry[] entries, Templat
217173
this.origName = origClass.getName();
218174
return load(origName);
219175
}
220-
}
176+
}

src/main/java/org/msgpack/template/builder/FieldEntry.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ public boolean isAvailable() {
4646
return option != FieldOption.IGNORE;
4747
}
4848

49-
public boolean isRequired() {
50-
return option == FieldOption.REQUIRED;
51-
}
52-
5349
public boolean isOptional() {
5450
return option == FieldOption.OPTIONAL;
5551
}
@@ -92,4 +88,4 @@ public String arrayTypeToString(Class<?> type) {
9288
return sb.toString();
9389
}
9490

95-
}
91+
}

0 commit comments

Comments
 (0)