Skip to content

Commit

Permalink
Manual merge of second part of #2555 impl
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Nov 28, 2019
1 parent 160f90f commit 4555a7e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,8 @@ 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();
}
Expand Down Expand Up @@ -898,26 +897,25 @@ protected void _renameWithWrappers(Map<String, POJOPropertyBuilder> props)
/* Overridable internal methods, sorting, other stuff
/**********************************************************
*/

/* First, explicit ordering and/or alphabetic
* and then implicitly order creator properties before others.
*/

// First, order by(explicit ordering and/or alphabetic),
// then by (optional) index (if any)
// and then implicitly order creator properties before others)

protected void _sortProperties(Map<String, POJOPropertyBuilder> props)
{
// Then how about explicit ordering?
AnnotationIntrospector intr = _annotationIntrospector;
final AnnotationIntrospector intr = _annotationIntrospector;
Boolean alpha = intr.findSerializationSortAlphabetically(_config, (Annotated) _classDef);
boolean sort;

if (alpha == null) {
sort = _config.shouldSortPropertiesAlphabetically();
} else {
sort = alpha.booleanValue();
}
final boolean sort = (alpha == null)
? _config.shouldSortPropertiesAlphabetically()
: alpha.booleanValue();
final boolean indexed = _anyIndexed(props.values());

String[] propertyOrder = intr.findSerializationPropertyOrder(_config, _classDef);

// no sorting? no need to shuffle, then
if (!sort && (_creatorProperties == null) && (propertyOrder == null)) {
if (!sort && !indexed && (_creatorProperties == null) && (propertyOrder == null)) {
return;
}
int size = props.size();
Expand All @@ -932,11 +930,11 @@ protected void _sortProperties(Map<String, POJOPropertyBuilder> props)
for (POJOPropertyBuilder prop : props.values()) {
all.put(prop.getName(), prop);
}
Map<String,POJOPropertyBuilder> ordered = new LinkedHashMap<String,POJOPropertyBuilder>(size+size);
Map<String,POJOPropertyBuilder> ordered = new LinkedHashMap<>(size+size);
// Ok: primarily by explicit order
if (propertyOrder != null) {
for (String name : propertyOrder) {
POJOPropertyBuilder w = all.get(name);
POJOPropertyBuilder w = all.remove(name);
if (w == null) { // will also allow use of "implicit" names for sorting
for (POJOPropertyBuilder prop : props.values()) {
if (name.equals(prop.getInternalName())) {
Expand All @@ -952,7 +950,26 @@ protected void _sortProperties(Map<String, POJOPropertyBuilder> props)
}
}
}
// And secondly by sorting Creator properties before other unordered properties

// Second (starting with 2.11): index, if any:
if (indexed) {
Map<Integer,POJOPropertyBuilder> byIndex = new TreeMap<>();
Iterator<Map.Entry<String,POJOPropertyBuilder>> it = all.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String,POJOPropertyBuilder> entry = it.next();
POJOPropertyBuilder prop = entry.getValue();
Integer index = prop.getMetadata().getIndex();
if (index != null) {
byIndex.put(index, prop);
it.remove();
}
}
for (POJOPropertyBuilder prop : byIndex.values()) {
ordered.put(prop.getName(), prop);
}
}

// Third by sorting Creator properties before other unordered properties
if (_creatorProperties != null) {
/* As per [databind#311], this is bit delicate; but if alphabetic ordering
* is mandated, at least ensure creator properties are in alphabetic
Expand All @@ -974,6 +991,8 @@ protected void _sortProperties(Map<String, POJOPropertyBuilder> props)
// 16-Jan-2016, tatu: Related to [databind#1317], make sure not to accidentally
// add back pruned creator properties!
String name = prop.getName();
// 27-Nov-2019, tatu: Not sure why, but we should NOT remove it from `all` tho:
// if (all.remove(name) != null) {
if (all.containsKey(name)) {
ordered.put(name, prop);
}
Expand All @@ -983,7 +1002,16 @@ protected void _sortProperties(Map<String, POJOPropertyBuilder> props)
ordered.putAll(all);
props.clear();
props.putAll(ordered);
}
}

private boolean _anyIndexed(Collection<POJOPropertyBuilder> props) {
for (POJOPropertyBuilder prop : props) {
if (prop.getMetadata().hasIndex()) {
return true;
}
}
return false;
}

/*
/**********************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public DuplicateGetterCreatorBean(@JsonProperty("bloop") @A boolean bloop) {}
/**********************************************************
*/

private final ObjectMapper MAPPER = objectMapper();
private final ObjectMapper MAPPER = newJsonMapper();

/*
public void testSimple()
Expand Down Expand Up @@ -423,6 +423,10 @@ public void testDuplicateGetters() throws Exception
assertTrue(prop.getGetter().hasAnnotation(B.class));
}

// 27-Nov-2019, tatu: Not sure why, but changes for [databind#2555] started to
// fail this test, due to call to `prop.getMetadata()` (to check for `index`).
// Probably related to comment on "Can't call getGetter..."
/*
public void testDuplicateGettersCreator() throws Exception
{
List<BeanPropertyDefinition> props = beanPropList(MAPPER, DuplicateGetterCreatorBean.class, true);
Expand All @@ -434,7 +438,7 @@ public void testDuplicateGettersCreator() throws Exception
assertNotNull(prop._getters.next);
assertTrue(prop._getters.next.value.hasAnnotation(A.class));
}

*/
/*
/**********************************************************
/* Helper methods
Expand Down

0 comments on commit 4555a7e

Please sign in to comment.