org.codehaus.jackson.map.deser.Creator Maven / Gradle / Ivy
Go to download
Data Mapper package is a high-performance data binding package
built on Jackson JSON processor
package org.codehaus.jackson.map.deser;
import java.io.IOException;
import java.lang.reflect.*;
import java.util.*;
import org.codehaus.jackson.*;
import org.codehaus.jackson.type.JavaType;
import org.codehaus.jackson.map.*;
import org.codehaus.jackson.map.introspect.AnnotatedConstructor;
import org.codehaus.jackson.map.introspect.AnnotatedMember;
import org.codehaus.jackson.map.introspect.AnnotatedMethod;
import org.codehaus.jackson.map.introspect.BasicBeanDescription;
import org.codehaus.jackson.map.type.TypeBindings;
import org.codehaus.jackson.map.util.ClassUtil;
/**
* Container for different kinds of Creators; objects capable of instantiating
* (and possibly partially or completely initializing) POJOs.
*/
abstract class Creator
{
// Class only used for namespacing, not as base class
private Creator() { }
/**
* Creator implementation that can handle simple deserialization from
* Json String values.
*/
final static class StringBased
{
protected final Class> _valueClass;
protected final Method _factoryMethod;
protected final Constructor> _ctor;
public StringBased(Class> valueClass, AnnotatedConstructor ctor,
AnnotatedMethod factoryMethod)
{
_valueClass = valueClass;
_ctor = (ctor == null) ? null : ctor.getAnnotated();
_factoryMethod = (factoryMethod == null) ? null : factoryMethod.getAnnotated();
}
public Object construct(String value)
{
try {
if (_ctor != null) {
return _ctor.newInstance(value);
}
if (_factoryMethod != null) {
return _factoryMethod.invoke(_valueClass, value);
}
} catch (Exception e) {
ClassUtil.unwrapAndThrowAsIAE(e);
}
return null;
}
}
/**
* Creator implementation that can handle simple deserialization from
* Json Number values.
*/
final static class NumberBased
{
protected final Class> _valueClass;
protected final Constructor> _intCtor;
protected final Constructor> _longCtor;
protected final Method _intFactoryMethod;
protected final Method _longFactoryMethod;
public NumberBased(Class> valueClass,
AnnotatedConstructor intCtor, AnnotatedMethod ifm,
AnnotatedConstructor longCtor, AnnotatedMethod lfm)
{
_valueClass = valueClass;
_intCtor = (intCtor == null) ? null : intCtor.getAnnotated();
_longCtor = (longCtor == null) ? null : longCtor.getAnnotated();
_intFactoryMethod = (ifm == null) ? null : ifm.getAnnotated();
_longFactoryMethod = (lfm == null) ? null : lfm.getAnnotated();
}
public Object construct(int value)
{
// First: "native" int methods work best:
try {
if (_intCtor != null) {
return _intCtor.newInstance(value);
}
if (_intFactoryMethod != null) {
return _intFactoryMethod.invoke(_valueClass, Integer.valueOf(value));
}
} catch (Exception e) {
ClassUtil.unwrapAndThrowAsIAE(e);
}
// but if not, can do widening conversion
return construct((long) value);
}
public Object construct(long value)
{
/* For longs we don't even try casting down to ints;
* theoretically could try if value fits... but let's
* leave that as a future improvement
*/
try {
if (_longCtor != null) {
return _longCtor.newInstance(value);
}
if (_longFactoryMethod != null) {
return _longFactoryMethod.invoke(_valueClass, Long.valueOf(value));
}
} catch (Exception e) {
ClassUtil.unwrapAndThrowAsIAE(e);
}
return null;
}
}
/**
* Creator implementation used for cases where parts of deserialization
* are delegated to another serializer, by first binding to an intermediate
* object, and then passing that object to the delegate creator (and
* then deserializer).
*/
final static class Delegating
{
/**
* Annotated creator object (single-argument constructor or
* single-argument static method) that is used for instantation;
* as well as for providing contextual information.
*/
protected final AnnotatedMember _creator;
/**
* Type to deserialize JSON to, as well as the type to pass to
* creator (constructor, factory method)
*/
protected final JavaType _valueType;
protected final Constructor> _ctor;
protected final Method _factoryMethod;
/**
* Delegate deserializer to use for actual deserialization, before
* instantiating value
*/
protected JsonDeserializer