com.fasterxml.jackson.databind.deser.impl.CreatorCollector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aem-sdk-api Show documentation
Show all versions of aem-sdk-api Show documentation
The Adobe Experience Manager SDK
package com.fasterxml.jackson.databind.deser.impl;
import java.lang.reflect.Member;
import java.util.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
import com.fasterxml.jackson.databind.deser.ValueInstantiator;
import com.fasterxml.jackson.databind.deser.std.StdValueInstantiator;
import com.fasterxml.jackson.databind.introspect.*;
import com.fasterxml.jackson.databind.util.ClassUtil;
/**
* Container class for storing information on creators (based on annotations,
* visibility), to be able to build actual {@code ValueInstantiator} later on.
*/
public class CreatorCollector
{
protected final static int C_DEFAULT = 0;
protected final static int C_STRING = 1;
protected final static int C_INT = 2;
protected final static int C_LONG = 3;
protected final static int C_BIG_INTEGER = 4;
protected final static int C_DOUBLE = 5;
protected final static int C_BIG_DECIMAL = 6;
protected final static int C_BOOLEAN = 7;
protected final static int C_DELEGATE = 8;
protected final static int C_PROPS = 9;
protected final static int C_ARRAY_DELEGATE = 10;
protected final static String[] TYPE_DESCS = new String[] { "default",
"from-String", "from-int", "from-long", "from-big-integer", "from-double",
"from-big-decimal", "from-boolean", "delegate", "property-based", "array-delegate"
};
/**
* Type of bean being created
*/
protected final BeanDescription _beanDesc;
protected final boolean _canFixAccess;
/**
* @since 2.7
*/
protected final boolean _forceAccess;
/**
* Set of creators we have collected so far
*
* @since 2.5
*/
protected final AnnotatedWithParams[] _creators = new AnnotatedWithParams[11];
/**
* Bitmask of creators that were explicitly marked as creators; false for
* auto-detected (ones included base on naming and/or visibility, not
* annotation)
*
* @since 2.5
*/
protected int _explicitCreators = 0;
protected boolean _hasNonDefaultCreator = false;
// when there are injectable values along with delegate:
protected SettableBeanProperty[] _delegateArgs;
protected SettableBeanProperty[] _arrayDelegateArgs;
protected SettableBeanProperty[] _propertyBasedArgs;
/*
/**********************************************************
/* Life-cycle
/**********************************************************
*/
public CreatorCollector(BeanDescription beanDesc, MapperConfig> config) {
_beanDesc = beanDesc;
_canFixAccess = config.canOverrideAccessModifiers();
_forceAccess = config
.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS);
}
public ValueInstantiator constructValueInstantiator(DeserializationContext ctxt)
throws JsonMappingException
{
final DeserializationConfig config = ctxt.getConfig();
final JavaType delegateType = _computeDelegateType(ctxt,
_creators[C_DELEGATE], _delegateArgs);
final JavaType arrayDelegateType = _computeDelegateType(ctxt,
_creators[C_ARRAY_DELEGATE], _arrayDelegateArgs);
final JavaType type = _beanDesc.getType();
StdValueInstantiator inst = new StdValueInstantiator(config, type);
inst.configureFromObjectSettings(_creators[C_DEFAULT], _creators[C_DELEGATE],
delegateType, _delegateArgs, _creators[C_PROPS],
_propertyBasedArgs);
inst.configureFromArraySettings(_creators[C_ARRAY_DELEGATE],
arrayDelegateType, _arrayDelegateArgs);
inst.configureFromStringCreator(_creators[C_STRING]);
inst.configureFromIntCreator(_creators[C_INT]);
inst.configureFromLongCreator(_creators[C_LONG]);
inst.configureFromBigIntegerCreator(_creators[C_BIG_INTEGER]);
inst.configureFromDoubleCreator(_creators[C_DOUBLE]);
inst.configureFromBigDecimalCreator(_creators[C_BIG_DECIMAL]);
inst.configureFromBooleanCreator(_creators[C_BOOLEAN]);
return inst;
}
/*
/**********************************************************
/* Setters
/**********************************************************
*/
/**
* Method called to indicate the default creator: no-arguments constructor
* or factory method that is called to instantiate a value before populating
* it with data. Default creator is only used if no other creators are
* indicated.
*
* @param creator
* Creator method; no-arguments constructor or static factory
* method.
*/
public void setDefaultCreator(AnnotatedWithParams creator) {
_creators[C_DEFAULT] = _fixAccess(creator);
}
public void addStringCreator(AnnotatedWithParams creator, boolean explicit) {
verifyNonDup(creator, C_STRING, explicit);
}
public void addIntCreator(AnnotatedWithParams creator, boolean explicit) {
verifyNonDup(creator, C_INT, explicit);
}
public void addLongCreator(AnnotatedWithParams creator, boolean explicit) {
verifyNonDup(creator, C_LONG, explicit);
}
public void addBigIntegerCreator(AnnotatedWithParams creator, boolean explicit) {
verifyNonDup(creator, C_BIG_INTEGER, explicit);
}
public void addDoubleCreator(AnnotatedWithParams creator, boolean explicit) {
verifyNonDup(creator, C_DOUBLE, explicit);
}
public void addBigDecimalCreator(AnnotatedWithParams creator, boolean explicit) {
verifyNonDup(creator, C_BIG_DECIMAL, explicit);
}
public void addBooleanCreator(AnnotatedWithParams creator, boolean explicit) {
verifyNonDup(creator, C_BOOLEAN, explicit);
}
public void addDelegatingCreator(AnnotatedWithParams creator,
boolean explicit, SettableBeanProperty[] injectables,
int delegateeIndex)
{
if (creator.getParameterType(delegateeIndex).isCollectionLikeType()) {
if (verifyNonDup(creator, C_ARRAY_DELEGATE, explicit)) {
_arrayDelegateArgs = injectables;
}
} else {
if (verifyNonDup(creator, C_DELEGATE, explicit)) {
_delegateArgs = injectables;
}
}
}
public void addPropertyCreator(AnnotatedWithParams creator,
boolean explicit, SettableBeanProperty[] properties)
{
if (verifyNonDup(creator, C_PROPS, explicit)) {
// Better ensure we have no duplicate names either...
if (properties.length > 1) {
HashMap names = new HashMap();
for (int i = 0, len = properties.length; i < len; ++i) {
String name = properties[i].getName();
// Need to consider Injectables, which may not have
// a name at all, and need to be skipped
if (name.isEmpty() && (properties[i].getInjectableValueId() != null)) {
continue;
}
Integer old = names.put(name, Integer.valueOf(i));
if (old != null) {
throw new IllegalArgumentException(String.format(
"Duplicate creator property \"%s\" (index %s vs %d) for type %s ",
name, old, i, ClassUtil.nameOf(_beanDesc.getBeanClass())));
}
}
}
_propertyBasedArgs = properties;
}
}
/*
/**********************************************************
/* Accessors
/**********************************************************
*/
/**
* @since 2.1
*/
public boolean hasDefaultCreator() {
return _creators[C_DEFAULT] != null;
}
/**
* @since 2.6
*/
public boolean hasDelegatingCreator() {
return _creators[C_DELEGATE] != null;
}
/**
* @since 2.6
*/
public boolean hasPropertyBasedCreator() {
return _creators[C_PROPS] != null;
}
/*
/**********************************************************
/* Helper methods
/**********************************************************
*/
private JavaType _computeDelegateType(DeserializationContext ctxt,
AnnotatedWithParams creator, SettableBeanProperty[] delegateArgs)
throws JsonMappingException
{
if (!_hasNonDefaultCreator || (creator == null)) {
return null;
}
// need to find type...
int ix = 0;
if (delegateArgs != null) {
for (int i = 0, len = delegateArgs.length; i < len; ++i) {
if (delegateArgs[i] == null) { // marker for delegate itself
ix = i;
break;
}
}
}
final DeserializationConfig config = ctxt.getConfig();
// 03-May-2018, tatu: need to check possible annotation-based
// custom deserializer [databind#2012],
// type refinement(s) [databind#2016].
JavaType baseType = creator.getParameterType(ix);
AnnotationIntrospector intr = config.getAnnotationIntrospector();
if (intr != null) {
AnnotatedParameter delegate = creator.getParameter(ix);
// First: custom deserializer(s):
Object deserDef = intr.findDeserializer(delegate);
if (deserDef != null) {
JsonDeserializer
© 2015 - 2024 Weber Informatics LLC | Privacy Policy