Java Code Examples for com.fasterxml.jackson.annotation.JacksonInject#Value

The following examples show how to use com.fasterxml.jackson.annotation.JacksonInject#Value . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: POJOPropertiesCollector.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected void _doAddInjectable(JacksonInject.Value injectable, AnnotatedMember m)
{
    if (injectable == null) {
        return;
    }
    Object id = injectable.getId();
    if (_injectables == null) {
        _injectables = new LinkedHashMap<Object, AnnotatedMember>();
    }
    AnnotatedMember prev = _injectables.put(id, m);
    if (prev != null) {
        // 12-Apr-2017, tatu: Let's allow masking of Field by Method
        if (prev.getClass() == m.getClass()) {
            String type = id.getClass().getName();
            throw new IllegalArgumentException("Duplicate injectable value with id '"
                    +String.valueOf(id)+"' (of type "+type+")");
        }
    }
}
 
Example 2
Source File: BasicDeserializerFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
     * Helper method called when there is the explicit "is-creator" with mode of "properties-based"
     *
     * @since 2.9.2
     */
    protected void _addExplicitPropertyCreator(DeserializationContext ctxt,
            BeanDescription beanDesc, CreatorCollector creators,
            CreatorCandidate candidate)
        throws JsonMappingException
    {
        final int paramCount = candidate.paramCount();
        SettableBeanProperty[] properties = new SettableBeanProperty[paramCount];

        for (int i = 0; i < paramCount; ++i) {
            JacksonInject.Value injectId = candidate.injection(i);
            AnnotatedParameter param = candidate.parameter(i);
            PropertyName name = candidate.paramName(i);
            if (name == null) {
                // 21-Sep-2017, tatu: Looks like we want to block accidental use of Unwrapped,
                //   as that will not work with Creators well at all
                NameTransformer unwrapper = ctxt.getAnnotationIntrospector().findUnwrappingNameTransformer(param);
                if (unwrapper != null) {
                    _reportUnwrappedCreatorProperty(ctxt, beanDesc, param);
                    /*
                    properties[i] = constructCreatorProperty(ctxt, beanDesc, UNWRAPPED_CREATOR_PARAM_NAME, i, param, null);
                    ++explicitNameCount;
                    */
                }
                name = candidate.findImplicitParamName(i);
                // Must be injectable or have name; without either won't work
                if ((name == null) && (injectId == null)) {
                    ctxt.reportBadTypeDefinition(beanDesc,
"Argument #%d has no property name, is not Injectable: can not use as Creator %s", i, candidate);
                }
            }
            properties[i] = constructCreatorProperty(ctxt, beanDesc, name, i, param, injectId);
        }
        creators.addPropertyCreator(candidate.creator(), true, properties);
    }
 
Example 3
Source File: CreatorCandidate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static CreatorCandidate construct(AnnotationIntrospector intr,
        AnnotatedWithParams creator, BeanPropertyDefinition[] propDefs)
{
    final int pcount = creator.getParameterCount();
    Param[] params = new Param[pcount];
    for (int i = 0; i < pcount; ++i) {
        AnnotatedParameter annParam = creator.getParameter(i);
        JacksonInject.Value injectId = intr.findInjectableValue(annParam);
        params[i] = new Param(annParam, (propDefs == null) ? null : propDefs[i], injectId);
    }
    return new CreatorCandidate(intr, creator, params, pcount);
}
 
Example 4
Source File: CreatorCandidate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public Param(AnnotatedParameter p, BeanPropertyDefinition pd,
        JacksonInject.Value i)
{
    annotated = p;
    propDef = pd;
    injection = i;
}
 
Example 5
Source File: GuiceAnnotationIntrospector.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
@Override // since 2.9
public JacksonInject.Value findInjectableValue(MapperConfig<?> config, AnnotatedMember m) {
    Object id = _findGuiceInjectId(m);
    if (id == null) {
        return null;
    }
    return JacksonInject.Value.forId(id);
}
 
Example 6
Source File: AnnotationIntrospectorPair.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public JacksonInject.Value findInjectableValue(AnnotatedMember m) {
    JacksonInject.Value r = _primary.findInjectableValue(m);
    return (r == null) ? _secondary.findInjectableValue(m) : r;
}
 
Example 7
Source File: BasicDeserializerFactory.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Helper method called when there is the explicit "is-creator" with mode of "delegating"
 *
 * @since 2.9.2
 */
protected void _addExplicitDelegatingCreator(DeserializationContext ctxt,
        BeanDescription beanDesc, CreatorCollector creators,
        CreatorCandidate candidate)
    throws JsonMappingException
{
    // Somewhat simple: find injectable values, if any, ensure there is one
    // and just one delegated argument; report violations if any

    int ix = -1;
    final int argCount = candidate.paramCount();
    SettableBeanProperty[] properties = new SettableBeanProperty[argCount];
    for (int i = 0; i < argCount; ++i) {
        AnnotatedParameter param = candidate.parameter(i);
        JacksonInject.Value injectId = candidate.injection(i);
        if (injectId != null) {
            properties[i] = constructCreatorProperty(ctxt, beanDesc, null, i, param, injectId);
            continue;
        }
        if (ix < 0) {
            ix = i;
            continue;
        }
        // Illegal to have more than one value to delegate to
        ctxt.reportBadTypeDefinition(beanDesc,
                "More than one argument (#%d and #%d) left as delegating for Creator %s: only one allowed",
                ix, i, candidate);
    }
    // Also, let's require that one Delegating argument does eixt
    if (ix < 0) {
        ctxt.reportBadTypeDefinition(beanDesc,
                "No argument left as delegating for Creator %s: exactly one required", candidate);
    }
    // 17-Jan-2018, tatu: as per [databind#1853] need to ensure we will distinguish
    //   "well-known" single-arg variants (String, int/long, boolean) from "generic" delegating...
    if (argCount == 1) {
        _handleSingleArgumentCreator(creators, candidate.creator(), true, true);
        // one more thing: sever link to creator property, to avoid possible later
        // problems with "unresolved" constructor property
        BeanPropertyDefinition paramDef = candidate.propertyDef(0);
        if (paramDef != null) {
            ((POJOPropertyBuilder) paramDef).removeConstructors();
        }
        return;
    }
    creators.addDelegatingCreator(candidate.creator(), true, properties, ix);
}
 
Example 8
Source File: BasicDeserializerFactory.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Method that will construct a property object that represents
 * a logical property passed via Creator (constructor or static
 * factory method)
 */
protected SettableBeanProperty constructCreatorProperty(DeserializationContext ctxt,
        BeanDescription beanDesc, PropertyName name, int index,
        AnnotatedParameter param,
        JacksonInject.Value injectable)
    throws JsonMappingException
{
    final DeserializationConfig config = ctxt.getConfig();
    final AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
    PropertyMetadata metadata;
    {
        if (intr == null) {
            metadata = PropertyMetadata.STD_REQUIRED_OR_OPTIONAL;
        } else {
            Boolean b = intr.hasRequiredMarker(param);
            String desc = intr.findPropertyDescription(param);
            Integer idx = intr.findPropertyIndex(param);
            String def = intr.findPropertyDefaultValue(param);
            metadata = PropertyMetadata.construct(b, desc, idx, def);
        }
    }
    JavaType type = resolveMemberAndTypeAnnotations(ctxt, param, param.getType());
    BeanProperty.Std property = new BeanProperty.Std(name, type,
            intr.findWrapperName(param), param, metadata);
    // Type deserializer: either comes from property (and already resolved)
    TypeDeserializer typeDeser = (TypeDeserializer) type.getTypeHandler();
    // or if not, based on type being referenced:
    if (typeDeser == null) {
        typeDeser = findTypeDeserializer(config, type);
    }
    // Note: contextualization of typeDeser _should_ occur in constructor of CreatorProperty
    // so it is not called directly here

    Object injectableValueId = (injectable == null) ? null : injectable.getId();
    
    SettableBeanProperty prop = new CreatorProperty(name, type, property.getWrapperName(),
            typeDeser, beanDesc.getClassAnnotations(), param, index, injectableValueId,
            metadata);
    JsonDeserializer<?> deser = findDeserializerFromAnnotation(ctxt, param);
    if (deser == null) {
        deser = type.getValueHandler();
    }
    if (deser != null) {
        // As per [databind#462] need to ensure we contextualize deserializer before passing it on
        deser = ctxt.handlePrimaryContextualization(deser, prop, type);
        prop = prop.withValueDeserializer(deser);
    }
    return prop;
}
 
Example 9
Source File: CreatorCandidate.java    From lams with GNU General Public License v2.0 votes vote down vote up
public JacksonInject.Value injection(int i) { return _params[i].injection; }