Skip to content

Commit

Permalink
Ignore Records' immutable fields in BeanDeserializerFactory instead o…
Browse files Browse the repository at this point in the history
…f ignoring it in POJOPropertiesCollector, to preserve any annotation info the fields may be carrying.
  • Loading branch information
yihtserns committed Jul 21, 2024
1 parent 62e2893 commit ea3135a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -977,8 +977,12 @@ protected SettableBeanProperty constructSettableProperty(DeserializationContext
beanDesc.getClassAnnotations(), (AnnotatedMethod) mutator);
} else {
// 08-Sep-2016, tatu: wonder if we should verify it is `AnnotatedField` to be safe?
prop = new FieldProperty(propDef, type, typeDeser,
beanDesc.getClassAnnotations(), (AnnotatedField) mutator);
AnnotatedField field = (AnnotatedField) mutator;
if (field.isImmutable()) {
// [databind#3736] Pointless to create a SettableBeanProperty for an immutable field
return null;
}
prop = new FieldProperty(propDef, type, typeDeser, beanDesc.getClassAnnotations(), field);
}
JsonDeserializer<?> deser = findDeserializerFromAnnotation(ctxt, mutator);
if (deser == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ public Object getValue(Object pojo) throws IllegalArgumentException
*/
public boolean isTransient() { return Modifier.isTransient(getModifiers()); }

/**
* @since 2.18
*/
public boolean isImmutable() {
// Records' fields can't mutated via reflection (JDK-8247517)
return _typeContext.resolveType(getDeclaringClass()).isRecordType();
}

@Override
public int hashCode() {
return Objects.hashCode(_field);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,13 +434,7 @@ protected void collectAll()
// First: gather basic accessors
LinkedHashMap<String, POJOPropertyBuilder> props = new LinkedHashMap<String, POJOPropertyBuilder>();

// 15-Jan-2023, tatu: [databind#3736] Let's avoid detecting fields of Records
// altogether (unless we find a good reason to detect them)
// 17-Apr-2023: Need Records' fields for serialization for cases
// like [databind#3628], [databind#3895] and [databind#3992]
if (!isRecordType() || _forSerialization) {
_addFields(props); // note: populates _fieldRenameMappings
}
_addFields(props); // note: populates _fieldRenameMappings
_addMethods(props);
// 25-Jan-2016, tatu: Avoid introspecting (constructor-)creators for non-static
// inner classes, see [databind#1502]
Expand Down Expand Up @@ -1301,10 +1295,7 @@ protected void _removeUnwantedProperties(Map<String, POJOPropertyBuilder> props)
*/
protected void _removeUnwantedAccessor(Map<String, POJOPropertyBuilder> props)
{
// 15-Jan-2023, tatu: Avoid pulling in mutators for Records; Fields mostly
// since there should not be setters.
final boolean inferMutators = !isRecordType()
&& _config.isEnabled(MapperFeature.INFER_PROPERTY_MUTATORS);
final boolean inferMutators = _config.isEnabled(MapperFeature.INFER_PROPERTY_MUTATORS);
Iterator<POJOPropertyBuilder> it = props.values().iterator();

while (it.hasNext()) {
Expand Down

0 comments on commit ea3135a

Please sign in to comment.