java: adds MessagePackNullable annotation

This commit is contained in:
frsyuki 2010-11-09 02:46:23 +09:00
parent 517509db6e
commit 33b43d03ac
3 changed files with 187 additions and 5 deletions

View file

@ -0,0 +1,28 @@
//
// 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.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MessagePackNullable {
}

View file

@ -39,7 +39,9 @@ import org.msgpack.Packer;
import org.msgpack.Template;
import org.msgpack.Unpacker;
import org.msgpack.annotation.MessagePackOptional;
import org.msgpack.annotation.MessagePackNullable;
import org.msgpack.template.OptionalTemplate;
import org.msgpack.template.NullableTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -253,7 +255,6 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
}
Template createTemplate(Field field) {
boolean isOptional = isAnnotated(field, MessagePackOptional.class);
Class<?> c = field.getType();
Template tmpl = null;
if (List.class.isAssignableFrom(c) || Map.class.isAssignableFrom(c)) {
@ -261,12 +262,15 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
} else {
tmpl = createTemplate(c);
}
if (isOptional) {
// for pack
if (isAnnotated(field, MessagePackOptional.class)) {
// @Optional types
return new OptionalTemplate(tmpl);
} else {
return tmpl;
}
if (!c.isPrimitive() && isAnnotated(field, MessagePackNullable.class)) {
// @Nullable reference types
return new NullableTemplate(tmpl);
}
return tmpl;
}
private boolean isAnnotated(Field field, Class<? extends Annotation> with) {