Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.fitbur.fasterxml.jackson.databind.introspect.POJOPropertiesCollector Maven / Gradle / Ivy
package com.fitbur.fasterxml.jackson.databind.introspect;
import java.util.*;
import com.fitbur.fasterxml.jackson.databind.*;
import com.fitbur.fasterxml.jackson.databind.cfg.HandlerInstantiator;
import com.fitbur.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fitbur.fasterxml.jackson.databind.util.BeanUtil;
import com.fitbur.fasterxml.jackson.databind.util.ClassUtil;
public class POJOPropertiesCollector
{
protected final MapperConfig _config;
protected final boolean _forSerialization;
protected final JavaType _type;
protected final AnnotatedClass _classDef;
protected final VisibilityChecker _visibilityChecker;
protected final AnnotationIntrospector _annotationIntrospector;
protected final String _mutatorPrefix;
protected final LinkedHashMap _properties
= new LinkedHashMap();
protected LinkedList _creatorProperties = null ;
protected LinkedList _anyGetters = null ;
protected LinkedList _anySetters = null ;
protected LinkedList _jsonValueGetters = null ;
protected HashSet _ignoredPropertyNames;
protected LinkedHashMap _injectables;
protected POJOPropertiesCollector (MapperConfig config, boolean forSerialization,
JavaType type, AnnotatedClass classDef, String mutatorPrefix)
{
_config = config;
_forSerialization = forSerialization;
_type = type;
_classDef = classDef;
_mutatorPrefix = (mutatorPrefix == null ) ? "set" : mutatorPrefix;
_annotationIntrospector = config.isAnnotationProcessingEnabled() ?
_config.getAnnotationIntrospector() : null ;
if (_annotationIntrospector == null ) {
_visibilityChecker = _config.getDefaultVisibilityChecker();
} else {
_visibilityChecker = _annotationIntrospector.findAutoDetectVisibility(classDef,
_config.getDefaultVisibilityChecker());
}
}
public MapperConfig getConfig () {
return _config;
}
public JavaType getType () {
return _type;
}
public AnnotatedClass getClassDef () {
return _classDef;
}
public AnnotationIntrospector getAnnotationIntrospector () {
return _annotationIntrospector;
}
public List getProperties () {
return new ArrayList(_properties.values());
}
public Map getInjectables () {
return _injectables;
}
public AnnotatedMethod getJsonValueMethod ()
{
if (_jsonValueGetters != null ) {
if (_jsonValueGetters.size() > 1 ) {
reportProblem("Multiple value properties com.fitburfined (" +_jsonValueGetters.get(0 )+" vs "
+_jsonValueGetters.get(1 )+")" );
}
return _jsonValueGetters.get(0 );
}
return null ;
}
public AnnotatedMember getAnyGetter ()
{
if (_anyGetters != null ) {
if (_anyGetters.size() > 1 ) {
reportProblem("Multiple 'any-getters' com.fitburfined (" +_anyGetters.get(0 )+" vs "
+_anyGetters.get(1 )+")" );
}
return _anyGetters.getFirst();
}
return null ;
}
public AnnotatedMethod getAnySetterMethod ()
{
if (_anySetters != null ) {
if (_anySetters.size() > 1 ) {
reportProblem("Multiple 'any-setters' com.fitburfined (" +_anySetters.get(0 )+" vs "
+_anySetters.get(1 )+")" );
}
return _anySetters.getFirst();
}
return null ;
}
public Set getIgnoredPropertyNames () {
return _ignoredPropertyNames;
}
public ObjectIdInfo getObjectIdInfo () {
if (_annotationIntrospector == null ) {
return null ;
}
ObjectIdInfo info = _annotationIntrospector.findObjectIdInfo(_classDef);
if (info != null ) {
info = _annotationIntrospector.findObjectReferenceInfo(_classDef, info);
}
return info;
}
public Class findPOJOBuilderClass ()
{
return _annotationIntrospector.findPOJOBuilder(_classDef);
}
protected Map getPropertyMap () {
return _properties;
}
public POJOPropertiesCollector collect ()
{
_properties.clear();
_addFields();
_addMethods();
_addCreators();
_addInjectables();
_removeUnwantedProperties();
_renameProperties();
PropertyNamingStrategy naming = _findNamingStrategy();
if (naming != null ) {
_renameUsing(naming);
}
for (POJOPropertyBuilder property : _properties.values()) {
property.trimByVisibility();
}
for (POJOPropertyBuilder property : _properties.values()) {
property.mergeAnnotations(_forSerialization);
}
if (_config.isEnabled(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME)) {
_renameWithWrappers();
}
_sortProperties();
return this ;
}
protected void _sortProperties ()
{
AnnotationIntrospector intr = _annotationIntrospector;
boolean sort;
Boolean alpha = (intr == null ) ? null : intr.findSerializationSortAlphabetically(_classDef);
if (alpha == null ) {
sort = _config.shouldSortPropertiesAlphabetically();
} else {
sort = alpha.booleanValue();
}
String[] propertyOrder = (intr == null ) ? null : intr.findSerializationPropertyOrder(_classDef);
if (!sort && (_creatorProperties == null ) && (propertyOrder == null )) {
return ;
}
int size = _properties.size();
Map all;
if (sort) {
all = new TreeMap();
} else {
all = new LinkedHashMap(size+size);
}
for (POJOPropertyBuilder prop : _properties.values()) {
all.put(prop.getName(), prop);
}
Map ordered = new LinkedHashMap(size+size);
if (propertyOrder != null ) {
for (String name : propertyOrder) {
POJOPropertyBuilder w = all.get(name);
if (w == null ) {
for (POJOPropertyBuilder prop : _properties.values()) {
if (name.equals(prop.getInternalName())) {
w = prop;
name = prop.getName();
break ;
}
}
}
if (w != null ) {
ordered.put(name, w);
}
}
}
if (_creatorProperties != null ) {
for (POJOPropertyBuilder prop : _creatorProperties) {
ordered.put(prop.getName(), prop);
}
}
ordered.putAll(all);
_properties.clear();
_properties.putAll(ordered);
}
protected void _addFields ()
{
final AnnotationIntrospector ai = _annotationIntrospector;
for (AnnotatedField f : _classDef.fields()) {
String implName = f.getName();
String explName;
if (ai == null ) {
explName = null ;
} else if (_forSerialization) {
PropertyName pn = ai.findNameForSerialization(f);
explName = (pn == null ) ? null : pn.getSimpleName();
} else {
PropertyName pn = ai.findNameForDeserialization(f);
explName = (pn == null ) ? null : pn.getSimpleName();
}
if ("" .equals(explName)) {
explName = implName;
}
boolean visible = (explName != null );
if (!visible) {
visible = _visibilityChecker.isFieldVisible(f);
}
boolean ignored = (ai != null ) && ai.hasIgnoreMarker(f);
_property(implName).addField(f, explName, visible, ignored);
}
}
protected void _addCreators ()
{
final AnnotationIntrospector ai = _annotationIntrospector;
if (ai == null ) {
return ;
}
for (AnnotatedConstructor ctor : _classDef.getConstructors()) {
if (_creatorProperties == null ) {
_creatorProperties = new LinkedList();
}
for (int i = 0 , len = ctor.getParameterCount(); i < len; ++i) {
AnnotatedParameter param = ctor.getParameter(i);
PropertyName pn = ai.findNameForDeserialization(param);
String name = (pn == null ) ? null : pn.getSimpleName();
if (name != null ) {
POJOPropertyBuilder prop = _property(name);
prop.addCtor(param, name, true , false );
_creatorProperties.add(prop);
}
}
}
for (AnnotatedMethod factory : _classDef.getStaticMethods()) {
if (_creatorProperties == null ) {
_creatorProperties = new LinkedList();
}
for (int i = 0 , len = factory.getParameterCount(); i < len; ++i) {
AnnotatedParameter param = factory.getParameter(i);
PropertyName pn = ai.findNameForDeserialization(param);
String name = (pn == null ) ? null : pn.getSimpleName();
if (name != null ) {
POJOPropertyBuilder prop = _property(name);
prop.addCtor(param, name, true , false );
_creatorProperties.add(prop);
}
}
}
}
protected void _addMethods ()
{
final AnnotationIntrospector ai = _annotationIntrospector;
for (AnnotatedMethod m : _classDef.memberMethods()) {
int argCount = m.getParameterCount();
if (argCount == 0 ) {
_addGetterMethod(m, ai);
} else if (argCount == 1 ) {
_addSetterMethod(m, ai);
} else if (argCount == 2 ) {
if (ai != null && ai.hasAnySetterAnnotation(m)) {
if (_anySetters == null ) {
_anySetters = new LinkedList();
}
_anySetters.add(m);
}
}
}
}
protected void _addGetterMethod (AnnotatedMethod m, AnnotationIntrospector ai)
{
if (ai != null ) {
if (ai.hasAnyGetterAnnotation(m)) {
if (_anyGetters == null ) {
_anyGetters = new LinkedList();
}
_anyGetters.add(m);
return ;
}
if (ai.hasAsValueAnnotation(m)) {
if (_jsonValueGetters == null ) {
_jsonValueGetters = new LinkedList();
}
_jsonValueGetters.add(m);
return ;
}
}
String implName;
boolean visible;
PropertyName pn = (ai == null ) ? null : ai.findNameForSerialization(m);
String explName = (pn == null ) ? null : pn.getSimpleName();
if (explName == null ) {
implName = BeanUtil.okNameForRegularGetter(m, m.getName());
if (implName == null ) {
implName = BeanUtil.okNameForIsGetter(m, m.getName());
if (implName == null ) {
return ;
}
visible = _visibilityChecker.isIsGetterVisible(m);
} else {
visible = _visibilityChecker.isGetterVisible(m);
}
} else {
implName = BeanUtil.okNameForGetter(m);
if (implName == null ) {
implName = m.getName();
}
if (explName.length() == 0 ) {
explName = implName;
}
visible = true ;
}
boolean ignore = (ai == null ) ? false : ai.hasIgnoreMarker(m);
_property(implName).addGetter(m, explName, visible, ignore);
}
protected void _addSetterMethod (AnnotatedMethod m, AnnotationIntrospector ai)
{
String implName;
boolean visible;
PropertyName pn = (ai == null ) ? null : ai.findNameForDeserialization(m);
String explName = (pn == null ) ? null : pn.getSimpleName();
if (explName == null ) {
implName = BeanUtil.okNameForMutator(m, _mutatorPrefix);
if (implName == null ) {
return ;
}
visible = _visibilityChecker.isSetterVisible(m);
} else {
implName = BeanUtil.okNameForMutator(m, _mutatorPrefix);
if (implName == null ) {
implName = m.getName();
}
if (explName.length() == 0 ) {
explName = implName;
}
visible = true ;
}
boolean ignore = (ai == null ) ? false : ai.hasIgnoreMarker(m);
_property(implName).addSetter(m, explName, visible, ignore);
}
protected void _addInjectables ()
{
final AnnotationIntrospector ai = _annotationIntrospector;
if (ai == null ) {
return ;
}
for (AnnotatedField f : _classDef.fields()) {
_doAddInjectable(ai.findInjectableValueId(f), f);
}
for (AnnotatedMethod m : _classDef.memberMethods()) {
if (m.getParameterCount() != 1 ) {
continue ;
}
_doAddInjectable(ai.findInjectableValueId(m), m);
}
}
protected void _doAddInjectable (Object id, AnnotatedMember m)
{
if (id == null ) {
return ;
}
if (_injectables == null ) {
_injectables = new LinkedHashMap();
}
AnnotatedMember prev = _injectables.put(id, m);
if (prev != null ) {
String type = (id == null ) ? "[null]" : id.getClass().getName();
throw new IllegalArgumentException("Duplicate injectable value with id '"
+String.valueOf(id)+"' (of type " +type+")" );
}
}
protected void _removeUnwantedProperties ()
{
Iterator> it = _properties.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = it.next();
POJOPropertyBuilder prop = entry.getValue();
if (!prop.anyVisible()) {
it.remove();
continue ;
}
if (prop.anyIgnorals()) {
if (!prop.isExplicitlyIncluded()) {
it.remove();
_addIgnored(prop.getName());
continue ;
}
prop.removeIgnored();
if (!_forSerialization && !prop.couldDeserialize()) {
_addIgnored(prop.getName());
}
}
prop.removeNonVisible();
}
}
private void _addIgnored (String name)
{
if (!_forSerialization) {
if (_ignoredPropertyNames == null ) {
_ignoredPropertyNames = new HashSet();
}
_ignoredPropertyNames.add(name);
}
}
protected void _renameProperties ()
{
Iterator> it = _properties.entrySet().iterator();
LinkedList renamed = null ;
while (it.hasNext()) {
Map.Entry entry = it.next();
POJOPropertyBuilder prop = entry.getValue();
String newName = prop.findNewName();
if (newName != null ) {
if (renamed == null ) {
renamed = new LinkedList();
}
prop = prop.withName(newName);
renamed.add(prop);
it.remove();
}
}
if (renamed != null ) {
for (POJOPropertyBuilder prop : renamed) {
String name = prop.getName();
POJOPropertyBuilder old = _properties.get(name);
if (old == null ) {
_properties.put(name, prop);
} else {
old.addAll(prop);
}
}
}
}
protected void _renameUsing (PropertyNamingStrategy naming)
{
POJOPropertyBuilder[] props = _properties.values().toArray(new POJOPropertyBuilder[_properties.size()]);
_properties.clear();
for (POJOPropertyBuilder prop : props) {
String name = prop.getName();
if (_forSerialization) {
if (prop.hasGetter()) {
name = naming.nameForGetterMethod(_config, prop.getGetter(), name);
} else if (prop.hasField()) {
name = naming.nameForField(_config, prop.getField(), name);
}
} else {
if (prop.hasSetter()) {
name = naming.nameForSetterMethod(_config, prop.getSetter(), name);
} else if (prop.hasConstructorParameter()) {
name = naming.nameForConstructorParameter(_config, prop.getConstructorParameter(), name);
} else if (prop.hasField()) {
name = naming.nameForField(_config, prop.getField(), name);
} else if (prop.hasGetter()) {
name = naming.nameForGetterMethod(_config, prop.getGetter(), name);
}
}
if (!name.equals(prop.getName())) {
prop = prop.withName(name);
}
POJOPropertyBuilder old = _properties.get(name);
if (old == null ) {
_properties.put(name, prop);
} else {
old.addAll(prop);
}
}
}
protected void _renameWithWrappers ()
{
Iterator> it = _properties.entrySet().iterator();
LinkedList renamed = null ;
while (it.hasNext()) {
Map.Entry entry = it.next();
POJOPropertyBuilder prop = entry.getValue();
AnnotatedMember member = prop.getPrimaryMember();
if (member == null ) {
continue ;
}
PropertyName wrapperName = _annotationIntrospector.findWrapperName(member);
if (wrapperName == null || !wrapperName.hasSimpleName()) {
continue ;
}
String name = wrapperName.getSimpleName();
if (!name.equals(prop.getName())) {
if (renamed == null ) {
renamed = new LinkedList();
}
prop = prop.withName(name);
renamed.add(prop);
it.remove();
}
}
if (renamed != null ) {
for (POJOPropertyBuilder prop : renamed) {
String name = prop.getName();
POJOPropertyBuilder old = _properties.get(name);
if (old == null ) {
_properties.put(name, prop);
} else {
old.addAll(prop);
}
}
}
}
protected void reportProblem (String msg) {
throw new IllegalArgumentException("Problem with com.fitburfinition of " +_classDef+": " +msg);
}
protected POJOPropertyBuilder _property (String implName)
{
POJOPropertyBuilder prop = _properties.get(implName);
if (prop == null ) {
prop = new POJOPropertyBuilder(implName, _annotationIntrospector,
_forSerialization);
_properties.put(implName, prop);
}
return prop;
}
private PropertyNamingStrategy _findNamingStrategy ()
{
Object namingDef = (_annotationIntrospector == null )? null
: _annotationIntrospector.findNamingStrategy(_classDef);
if (namingDef == null ) {
return _config.getPropertyNamingStrategy();
}
if (namingDef instanceof PropertyNamingStrategy) {
return (PropertyNamingStrategy) namingDef;
}
if (!(namingDef instanceof Class)) {
throw new IllegalStateException("AnnotationIntrospector returned PropertyNamingStrategy com.fitburfinition of type "
+namingDef.getClass().getName()+"; expected type PropertyNamingStrategy or Class instead" );
}
Class namingClass = (Class)namingDef;
if (!PropertyNamingStrategy.class.isAssignableFrom(namingClass)) {
throw new IllegalStateException("AnnotationIntrospector returned Class "
+namingClass.getName()+"; expected Class " );
}
HandlerInstantiator hi = _config.getHandlerInstantiator();
if (hi != null ) {
PropertyNamingStrategy pns = hi.namingStrategyInstance(_config, _classDef, namingClass);
if (pns != null ) {
return pns;
}
}
return (PropertyNamingStrategy) ClassUtil.createInstance(namingClass,
_config.canOverrideAccessModifiers());
}
}