com.fasterxml.jackson.databind.PropertyName Java Examples

The following examples show how to use com.fasterxml.jackson.databind.PropertyName. 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: TestAccessorGeneration.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
public void testSingleIntAccessorGeneration() throws Exception
{
    Method method = Bean1.class.getDeclaredMethod("getX");
    AnnotatedMethod annMethod = new AnnotatedMethod(null, method, null, null);
    PropertyAccessorCollector coll = new PropertyAccessorCollector(Bean1.class);
    BeanPropertyWriter bpw = new BeanPropertyWriter(SimpleBeanPropertyDefinition
            .construct(MAPPER_CONFIG, annMethod, new PropertyName("x")),
            annMethod, null,
            null,
            null, null, null,
            false, null, null);
    coll.addIntGetter(bpw);
    BeanPropertyAccessor acc = coll.findAccessor(null);
    Bean1 bean = new Bean1();
    int value = acc.intGetter(bean, 0);
    assertEquals(bean.getX(), value);
}
 
Example #2
Source File: BeanPropertyMap.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private Map<String,String> _buildAliasMapping(Map<String,List<PropertyName>> defs)
{
    if ((defs == null) || defs.isEmpty()) {
        return Collections.emptyMap();
    }
    Map<String,String> aliases = new HashMap<>();
    for (Map.Entry<String,List<PropertyName>> entry : defs.entrySet()) {
        String key = entry.getKey();
        if (_caseInsensitive) {
            key = key.toLowerCase();
        }
        for (PropertyName pn : entry.getValue()) {
            String mapped = pn.getSimpleName();
            if (_caseInsensitive) {
                mapped = mapped.toLowerCase();
            }
            aliases.put(mapped, key);
        }
    }
    return aliases;
}
 
Example #3
Source File: Java7SupportImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public PropertyName findConstructorName(AnnotatedParameter p)
{
    AnnotatedWithParams ctor = p.getOwner();
    if (ctor != null) {
        ConstructorProperties props = ctor.getAnnotation(ConstructorProperties.class);
        if (props != null) {
            String[] names = props.value();
            int ix = p.getIndex();
            if (ix < names.length) {
                return PropertyName.construct(names[ix]);
            }
        }
    }
    return null;
}
 
Example #4
Source File: ConcreteBeanPropertyBase.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public List<PropertyName> findAliases(MapperConfig<?> config)
{
    List<PropertyName> aliases = _aliases;
    if (aliases == null) {
        AnnotationIntrospector intr = config.getAnnotationIntrospector();
        if (intr != null) {
            aliases = intr.findPropertyAliases(getMember());
        }
        if (aliases == null) {
            aliases = Collections.emptyList();
        }
        _aliases = aliases;
    }
    return aliases;
}
 
Example #5
Source File: ParanamerOnJacksonAnnotationIntrospector.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
@Override
public PropertyName findNameForDeserialization(MapperConfig<?> config, Annotated a)
{
    /* 14-Apr-2014, tatu: Important -- we should NOT introspect name here,
     *   since we are not using annotations; instead it needs to be done
     *   in {@link #findParameterSourceName(AnnotatedParameter)}.
     */
    /*
    PropertyName name = super.findNameForDeserialization(a);
    if (name == null) {
        if (a instanceof AnnotatedParameter) {
            String rawName _paranamer.findParameterName((AnnotatedParameter) a);
            if (rawName != null) {
                return new PropertyName(rawName);
            }
        }
    }
    */
    return null;
}
 
Example #6
Source File: SwaggerJacksonAnnotationIntrospector.java    From netty-rest with Apache License 2.0 5 votes vote down vote up
@Override
public PropertyName findNameForDeserialization(Annotated a)
{
    ApiParam model = a.getAnnotation(ApiParam.class);
    if (model != null && !"".equals(model.value())) {
        return new PropertyName(model.value());
    }
    return null;
}
 
Example #7
Source File: CreatorCandidate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public PropertyName paramName(int i) {
    BeanPropertyDefinition propDef = _params[i].propDef;
    if (propDef != null) {
        return propDef.getFullName();
    }
    return null;
}
 
Example #8
Source File: RosettaAnnotationIntrospector.java    From Rosetta with Apache License 2.0 5 votes vote down vote up
private PropertyName findRosettaPropertyName(Annotated a) {
  RosettaProperty ann = a.getAnnotation(RosettaProperty.class);
  if (ann != null) {
    return ann.value().isEmpty() ? PropertyName.USE_DEFAULT : new PropertyName(ann.value());
  }
  return null;
}
 
Example #9
Source File: RosettaAnnotationIntrospector.java    From Rosetta with Apache License 2.0 5 votes vote down vote up
@Override
public PropertyName findNameForDeserialization(Annotated a) {
  PropertyName propertyName = findRosettaPropertyName(a);
  if (propertyName == null) {
    propertyName = super.findNameForDeserialization(a);
  }
  return propertyName;
}
 
Example #10
Source File: CreatorCandidate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public PropertyName findImplicitParamName(int i) {
    String str = _intr.findImplicitPropertyName(_params[i].annotated);
    if (str != null && !str.isEmpty()) {
        return PropertyName.construct(str);
    }
    return null;
}
 
Example #11
Source File: RosettaAnnotationIntrospector.java    From Rosetta with Apache License 2.0 5 votes vote down vote up
@Override
public PropertyName findNameForSerialization(Annotated a) {
  PropertyName propertyName = findRosettaPropertyName(a);
  if (propertyName == null) {
    propertyName = super.findNameForSerialization(a);
  }
  return propertyName;
}
 
Example #12
Source File: EnhancedSwaggerAnnotationIntrospector.java    From swagger-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public PropertyName findRootName(AnnotatedClass ac) {
    ApiModel model = ac.getAnnotation(ApiModel.class);
    if (model != null) {
        return new PropertyName(model.value());
    } else {
        return super.findRootName(ac);
    }
}
 
Example #13
Source File: ObjectIdInfo.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected ObjectIdInfo(PropertyName prop, Class<?> scope, Class<? extends ObjectIdGenerator<?>> gen,
        boolean alwaysAsId, Class<? extends ObjectIdResolver> resolver)
{
    _propertyName = prop;
    _scope = scope;
    _generator = gen;
    _alwaysAsId = alwaysAsId;
    if (resolver == null) {
        resolver = SimpleObjectIdResolver.class;
    }
    _resolver = resolver;
}
 
Example #14
Source File: KebabCaseEnforcingAnnotationInspector.java    From conjure with Apache License 2.0 5 votes vote down vote up
@Override
public PropertyName findNameForDeserialization(Annotated annotatedEntity) {
    // This logic relies on the fact that Immutables generates setters annotated with @JsonProperty.
    // It thus becomes obsolete whenever we move away from Immutables and the deserialization target no longer
    // carries those annotations.

    JsonProperty propertyAnnotation = _findAnnotation(annotatedEntity, JsonProperty.class);
    if (propertyAnnotation != null) {
        String jsonFieldName = propertyAnnotation.value();
        Preconditions.checkArgument(
                KEBAB_CASE_PATTERN.matcher(jsonFieldName).matches(),
                "Conjure grammar requires kebab-case field names: %s",
                jsonFieldName);
    }

    if (annotatedEntity instanceof AnnotatedMethod) {
        AnnotatedMethod maybeSetter = (AnnotatedMethod) annotatedEntity;
        if (maybeSetter.getName().startsWith("set")) {
            // As a pre-caution, require that all setters have a JsonProperty annotation.
            Preconditions.checkArgument(
                    _findAnnotation(annotatedEntity, JsonProperty.class) != null,
                    "All setter ({@code set*}) deserialization targets require @JsonProperty annotations: %s",
                    maybeSetter.getName());
        }
    }

    return null; // delegate to the next introspector in an AnnotationIntrospectorPair.
}
 
Example #15
Source File: UnsignedXrayClient.java    From aws-xray-sdk-java with Apache License 2.0 5 votes vote down vote up
@Override
public PropertyName findNameForDeserialization(Annotated a) {
    if (a.getName().equals("hTTPMethod")) {
        return HTTP_METHOD;
    }
    if (a.getName().equals("uRLPath")) {
        return URL_PATH;
    }
    return super.findNameForDeserialization(a);
}
 
Example #16
Source File: CustomAnnotationIntrospector.java    From elepy with Apache License 2.0 5 votes vote down vote up
@Override
public PropertyName findNameForSerialization(Annotated a) {

    String rawName = findPropertyName(a);
    if (rawName != null) {
        return new PropertyName(rawName);
    }
    return null;

}
 
Example #17
Source File: CustomAnnotationIntrospector.java    From elepy with Apache License 2.0 5 votes vote down vote up
@Override
public PropertyName findNameForDeserialization(Annotated a) {
    String rawName = findPropertyName(a);
    if (rawName != null) {
        return new PropertyName(rawName);
    }
    return null;
}
 
Example #18
Source File: CreatorCandidate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public PropertyName explicitParamName(int i) {
    BeanPropertyDefinition propDef = _params[i].propDef;
    if (propDef != null) {
        if (propDef.isExplicitlyNamed()) {
            return propDef.getFullName();
        }
    }
    return null;
}
 
Example #19
Source File: TestAccessorGeneration.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
public void testLotsaIntAccessorGeneration() throws Exception
{
    PropertyAccessorCollector coll = new PropertyAccessorCollector(BeanN.class);
    String[] methodNames = new String[] {
            "getX", "getY", "get3", "get4", "get5", "get6", "get7"
    };
    for (String methodName : methodNames) {
        Method method = BeanN.class.getDeclaredMethod(methodName);
        AnnotatedMethod annMethod = new AnnotatedMethod(null, method, null, null);
        coll.addIntGetter(new BeanPropertyWriter(SimpleBeanPropertyDefinition.construct(
                MAPPER_CONFIG, annMethod, new PropertyName(methodName)),
                annMethod, null,
                null,
                null, null, null,
                false, null, null));
    }

    BeanPropertyAccessor acc = coll.findAccessor(null);
    BeanN bean = new BeanN();

    assertEquals(bean.getX(), acc.intGetter(bean, 0));
    assertEquals(bean.getY(), acc.intGetter(bean, 1));

    assertEquals(bean.get3(), acc.intGetter(bean, 2));
    assertEquals(bean.get4(), acc.intGetter(bean, 3));
    assertEquals(bean.get5(), acc.intGetter(bean, 4));
    assertEquals(bean.get6(), acc.intGetter(bean, 5));
    assertEquals(bean.get7(), acc.intGetter(bean, 6));
}
 
Example #20
Source File: CreatorCandidate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public boolean hasFullName() {
    if (propDef == null) {
        return false;
    }
    PropertyName n = propDef.getFullName();
    return n.hasSimpleName();
}
 
Example #21
Source File: CustomAnnotationIntrospector.java    From elepy with Apache License 2.0 5 votes vote down vote up
@Override
public List<PropertyName> findPropertyAliases(Annotated ann) {
    if (isId(ann)) {
        return List.of(new PropertyName("_id"), new PropertyName("id"), new PropertyName(ann.getName()));
    }
    return super.findPropertyAliases(ann);
}
 
Example #22
Source File: SwaggerJacksonAnnotationIntrospector.java    From netty-rest with Apache License 2.0 5 votes vote down vote up
@Override
public PropertyName findNameForSerialization(Annotated a)
{
    ApiParam model = a.getAnnotation(ApiParam.class);
    if (model != null && !"".equals(model.value())) {
        return new PropertyName(model.value());
    }
    return null;
}
 
Example #23
Source File: InvalidNullException.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static InvalidNullException from(DeserializationContext ctxt,
        PropertyName name, JavaType type)
{
    String msg = String.format("Invalid `null` value encountered for property %s",
            ClassUtil.quotedOr(name, "<UNKNOWN>"));
    InvalidNullException exc = new InvalidNullException(ctxt, msg, name);
    if (type != null) {
        exc.setTargetType(type);
    }
    return exc;
}
 
Example #24
Source File: JSONAnnotationIntrospector.java    From core-ng-project with Apache License 2.0 5 votes vote down vote up
private PropertyName propertyName(Annotated annotated) {
    Property element = annotated.getAnnotation(Property.class);
    if (element != null) {
        return new PropertyName(element.name(), null);
    }
    return null;
}
 
Example #25
Source File: EntityAnnotationIntrospector.java    From requery with Apache License 2.0 5 votes vote down vote up
private PropertyName getMappedName(Annotated annotated) {
    if (annotated.hasAnnotation(Table.class)) {
        Table table = annotated.getAnnotation(Table.class);
        return new PropertyName(table.name());
    } if (annotated.hasAnnotation(View.class)) {
        View view = annotated.getAnnotation(View.class);
        return new PropertyName(view.name());
    } else if (annotated.hasAnnotation(Column.class)) {
        Column column = annotated.getAnnotation(Column.class);
        return new PropertyName(column.name());
    } else {
        return null;
    }
}
 
Example #26
Source File: EntityAnnotationIntrospector.java    From requery with Apache License 2.0 5 votes vote down vote up
@Override
public PropertyName findNameForSerialization(Annotated annotated) {
    if (useTableNames) {
        return getMappedName(annotated);
    } else {
        return super.findNameForSerialization(annotated);
    }
}
 
Example #27
Source File: EntityAnnotationIntrospector.java    From requery with Apache License 2.0 5 votes vote down vote up
@Override
public PropertyName findNameForDeserialization(Annotated annotated) {
    if (useTableNames) {
        return getMappedName(annotated);
    } else {
        return super.findNameForDeserialization(annotated);
    }
}
 
Example #28
Source File: EntityAnnotationIntrospector.java    From requery with Apache License 2.0 5 votes vote down vote up
@Override
public ObjectIdInfo findObjectIdInfo(Annotated annotated) {
    Class<?> rawClass = annotated.getType().getRawClass();
    for (Type<?> type : model.getTypes()) {
        if (type.getClassType() == rawClass && type.getSingleKeyAttribute() != null) {
            Attribute<?, ?> attribute = type.getSingleKeyAttribute();
            String name = removePrefix(attribute.getPropertyName());
            if (useTableNames) {
                name = attribute.getName();
            }

            // if the name is overridden use that
            Class<?> superClass = rawClass.getSuperclass();
            while (superClass != Object.class && superClass != null) {
                try {
                    Field field = superClass.getDeclaredField(attribute.getPropertyName());
                    JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class);
                    if (jsonProperty != null) {
                        name = jsonProperty.value();
                        break;
                    }
                } catch (NoSuchFieldException ignored) {
                }
                superClass = superClass.getSuperclass();
            }

            return new ObjectIdInfo(new PropertyName(name), rawClass,
                    ObjectIdGenerators.PropertyGenerator.class,
                    EntityStoreResolver.class);
        }
    }
    return super.findObjectIdInfo(annotated);
}
 
Example #29
Source File: ApiAnnotationIntrospector.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@Override
public PropertyName findNameForSerialization(Annotated a) {
  ApiResourceProperty apiName = a.getAnnotation(ApiResourceProperty.class);
  if (apiName != null && apiName.ignored() != AnnotationBoolean.TRUE) {
    return PropertyName.construct(apiName.name());
  }
  return null;
}
 
Example #30
Source File: TestAccessorGeneration.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
public void testDualIntAccessorGeneration() throws Exception
{
    PropertyAccessorCollector coll = new PropertyAccessorCollector(Bean3.class);

    String[] methodNames = new String[] {
            "getX", "getY", "get3"
    };
    
    /*
public BeanPropertyWriter(BeanPropertyDefinition propDef,
        AnnotatedMember member, Annotations contextAnnotations,
        JavaType declaredType,
        JsonSerializer<Object> ser, TypeSerializer typeSer, JavaType serType,
        boolean suppressNulls, Object suppressableValue)
     */
    
    for (String methodName : methodNames) {
        Method method = Bean3.class.getDeclaredMethod(methodName);
        AnnotatedMethod annMethod = new AnnotatedMethod(null, method, null, null);
        // should we translate from method name to property name?
        coll.addIntGetter(new BeanPropertyWriter(SimpleBeanPropertyDefinition
                .construct(MAPPER_CONFIG, annMethod, new PropertyName(methodName)),
                annMethod, null,
                null,
                null, null, null,
                false, null, null));
    }

    BeanPropertyAccessor acc = coll.findAccessor(null);
    Bean3 bean = new Bean3();

    assertEquals(bean.getX(), acc.intGetter(bean, 0));
    assertEquals(bean.getY(), acc.intGetter(bean, 1));
    assertEquals(bean.get3(), acc.intGetter(bean, 2));
}