com.mangofactory.swagger.models.property.BeanPropertyDefinitions Maven / Gradle / Ivy
package com.mangofactory.swagger.models.property;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
public class BeanPropertyDefinitions {
private BeanPropertyDefinitions() {
throw new UnsupportedOperationException();
}
public static Function beanPropertyByInternalName() {
return new Function() {
@Override
public String apply(BeanPropertyDefinition input) {
return input.getInternalName();
}
};
}
public static Optional jacksonPropertyWithSameInternalName(BeanDescription beanDescription,
BeanPropertyDefinition propertyDefinition) {
return FluentIterable.from(beanDescription.findProperties())
.firstMatch(withSameInternalName(propertyDefinition));
}
private static Predicate withSameInternalName(
final BeanPropertyDefinition propertyDefinition) {
return new Predicate() {
@Override
public boolean apply(BeanPropertyDefinition input) {
return input.getInternalName().equals(propertyDefinition.getInternalName());
}
};
}
public static Function overTheWireName(final BeanPropertyDefinition beanProperty,
final MapperConfig> config) {
return new Function() {
@Override
public String apply(PropertyNamingStrategy strategy) {
return getName(strategy, beanProperty, config);
}
};
}
private static String getName(PropertyNamingStrategy naming, BeanPropertyDefinition beanProperty,
MapperConfig> config) {
AnnotationIntrospector annotationIntrospector = config.isAnnotationProcessingEnabled()
? config.getAnnotationIntrospector()
: null;
POJOPropertyBuilder prop = new POJOPropertyBuilder(beanProperty.getName(), annotationIntrospector, true);
return naming.nameForField(config, prop.getField(), beanProperty.getName());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy