From 8453b03def6a27ad9dbe994c2d72eb58c6510153 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Mon, 25 Sep 2017 22:19:41 -0700 Subject: [PATCH] Simplify property/accessor massaging due to later (and more accurate) merging of annotations --- .../introspect/POJOPropertiesCollector.java | 34 +++---- .../introspect/POJOPropertyBuilder.java | 98 +++++++------------ 2 files changed, 49 insertions(+), 83 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java b/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java index 9d1ad2abfa..18247b9eba 100644 --- a/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java +++ b/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java @@ -301,7 +301,9 @@ protected void collectAll() _removeUnwantedAccessor(props); // Rename remaining properties _renameProperties(props); - // then merge annotations, to simplify further processing + + // then merge annotations, to simplify further processing: has to be done AFTER + // preceding renaming step to get right propagation for (POJOPropertyBuilder property : props.values()) { property.mergeAnnotations(_forSerialization); } @@ -311,17 +313,15 @@ protected void collectAll() _renameUsing(props, naming); } - /* Sort by visibility (explicit over implicit); drop all but first - * of member type (getter, setter etc) if there is visibility - * difference - */ + // Sort by visibility (explicit over implicit); drop all but first + // of member type (getter, setter etc) if there is visibility + // difference for (POJOPropertyBuilder property : props.values()) { property.trimByVisibility(); } - /* and, if required, apply wrapper name: note, MUST be done after - * annotations are merged. - */ + // and, if required, apply wrapper name: note, MUST be done after + // annotations are merged. if (_config.isEnabled(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME)) { _renameWithWrappers(props); } @@ -337,7 +337,7 @@ protected void collectAll() /* Overridable internal methods, adding members /********************************************************** */ - + /** * Method for collecting basic information on all fields found */ @@ -450,9 +450,6 @@ protected void _addCreators(Map props) } } - /** - * @since 2.4 - */ protected void _addCreatorParam(Map props, AnnotatedParameter param) { @@ -840,9 +837,7 @@ protected void _renameUsing(Map propMap, } else { simpleName = fullName.getSimpleName(); } - /* As per [JACKSON-687], need to consider case where there may already be - * something in there... - */ + // Need to consider case where there may already be something in there... POJOPropertyBuilder old = propMap.get(simpleName); if (old == null) { propMap.put(simpleName, prop); @@ -903,8 +898,8 @@ protected void _renameWithWrappers(Map props) /********************************************************** */ - /* First, order by [JACKSON-90] (explicit ordering and/or alphabetic) - * and then for [JACKSON-170] (implicitly order creator properties before others) + /* First, explicit ordering and/or alphabetic + * and then implicitly order creator properties before others. */ protected void _sortProperties(Map props) { @@ -1036,9 +1031,8 @@ private PropertyNamingStrategy _findNamingStrategy() if (namingDef instanceof PropertyNamingStrategy) { return (PropertyNamingStrategy) namingDef; } - /* Alas, there's no way to force return type of "either class - * X or Y" -- need to throw an exception after the fact - */ + // Alas, there's no way to force return type of "either class + // X or Y" -- need to throw an exception after the fact if (!(namingDef instanceof Class)) { throw new IllegalStateException("AnnotationIntrospector returned PropertyNamingStrategy definition of type " +namingDef.getClass().getName()+"; expected type PropertyNamingStrategy or Class instead"); diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertyBuilder.java b/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertyBuilder.java index f32d4da196..559430a718 100644 --- a/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertyBuilder.java +++ b/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertyBuilder.java @@ -83,7 +83,6 @@ protected POJOPropertyBuilder(MapperConfig config, AnnotationIntrospector ai, _forSerialization = forSerialization; } - // protected since 2.9 (was public before) protected POJOPropertyBuilder(POJOPropertyBuilder src, PropertyName newName) { _config = src._config; @@ -134,9 +133,7 @@ public int compareTo(POJOPropertyBuilder other) } else if (other._ctorParameters != null) { return 1; } - /* otherwise sort by external name (including sorting of - * ctor parameters) - */ + // otherwise sort by external name (including sorting of ctor parameters) return getName().compareTo(other.getName()); } @@ -172,7 +169,7 @@ public PropertyName getWrapperName() { * occur, try commenting out full traversal code */ AnnotatedMember member = getPrimaryMember(); - return (member == null || _annotationIntrospector == null) ? null + return (member == null) ? null : _annotationIntrospector.findWrapperName(member); /* return fromMemberAnnotations(new WithMember() { @@ -605,12 +602,7 @@ protected int _setterPriority(AnnotatedMethod m) @Override public Class[] findViews() { - return fromMemberAnnotations(new WithMember[]>() { - @Override - public Class[] withMember(AnnotatedMember member) { - return _annotationIntrospector.findViews(member); - } - }); + return _annotationIntrospector.findViews(getPrimaryMember()); } @Override @@ -624,75 +616,58 @@ public AnnotationIntrospector.ReferenceProperty findReferenceType() { } return result; } - result = fromMemberAnnotations(new WithMember() { - @Override - public AnnotationIntrospector.ReferenceProperty withMember(AnnotatedMember member) { - return _annotationIntrospector.findReferenceType(member); - } - }); + AnnotatedMember m = getPrimaryMember(); + result = (m == null) ? null : _annotationIntrospector.findReferenceType(m); _referenceInfo = (result == null) ? NOT_REFEFERENCE_PROP : result; return result; } @Override public boolean isTypeId() { - Boolean b = fromMemberAnnotations(new WithMember() { - @Override - public Boolean withMember(AnnotatedMember member) { - return _annotationIntrospector.isTypeId(member); + AnnotatedMember m = getPrimaryMember(); + if (m != null) { + Boolean b = _annotationIntrospector.isTypeId(m); + if (b != null) { + return b.booleanValue(); } - }); - return (b != null) && b.booleanValue(); + } + return false; } protected Boolean _findRequired() { - return fromMemberAnnotations(new WithMember() { - @Override - public Boolean withMember(AnnotatedMember member) { - return _annotationIntrospector.hasRequiredMarker(member); - } - }); + AnnotatedMember m = getPrimaryMember(); + return (m == null) ? null + : _annotationIntrospector.hasRequiredMarker(m); } protected String _findDescription() { - return fromMemberAnnotations(new WithMember() { - @Override - public String withMember(AnnotatedMember member) { - return _annotationIntrospector.findPropertyDescription(member); - } - }); + AnnotatedMember m = getPrimaryMember(); + return (m == null) ? null + : _annotationIntrospector.findPropertyDescription(m); } protected Integer _findIndex() { - return fromMemberAnnotations(new WithMember() { - @Override - public Integer withMember(AnnotatedMember member) { - return _annotationIntrospector.findPropertyIndex(member); - } - }); + AnnotatedMember m = getPrimaryMember(); + return (m == null) ? null + : _annotationIntrospector.findPropertyIndex(m); } protected String _findDefaultValue() { - return fromMemberAnnotations(new WithMember() { - @Override - public String withMember(AnnotatedMember member) { - return _annotationIntrospector.findPropertyDefaultValue(member); - } - }); + AnnotatedMember m = getPrimaryMember(); + return (m == null) ? null + : _annotationIntrospector.findPropertyDefaultValue(m); } - + @Override public ObjectIdInfo findObjectIdInfo() { - return fromMemberAnnotations(new WithMember() { - @Override - public ObjectIdInfo withMember(AnnotatedMember member) { - ObjectIdInfo info = _annotationIntrospector.findObjectIdInfo(member); - if (info != null) { - info = _annotationIntrospector.findObjectReferenceInfo(member, info); - } - return info; + AnnotatedMember m = getPrimaryMember(); + if (m != null) { + ObjectIdInfo info = _annotationIntrospector.findObjectIdInfo(m); + if (info != null) { + return _annotationIntrospector.findObjectReferenceInfo(m, info); } - }); + } + return null; } @Override @@ -702,12 +677,13 @@ public JsonInclude.Value findInclusion() { // 17-Aug-2016, tatu: Do NOT include global, or per-type defaults, because // not all of this information (specifically, enclosing type's settings) // is available here - JsonInclude.Value v = (_annotationIntrospector == null) ? - null : _annotationIntrospector.findPropertyInclusion(a); + JsonInclude.Value v = _annotationIntrospector.findPropertyInclusion(a); return (v == null) ? JsonInclude.Value.empty() : v; } public JsonProperty.Access findAccess() { + // 25-Sep-2017, tatu: IMPORTANT! Called BEFORE merge occurs so MUST traverse + // accessors separately return fromMemberAnnotationsExcept(new WithMember() { @Override public JsonProperty.Access withMember(AnnotatedMember member) { @@ -1156,10 +1132,6 @@ protected T fromMemberAnnotations(WithMember func) protected T fromMemberAnnotationsExcept(WithMember func, T defaultValue) { - if (_annotationIntrospector == null) { - return null; - } - // NOTE: here we must ask ALL accessors, but the order varies between // serialization, deserialization if (_forSerialization) {