|
21 | 21 |
|
22 | 22 | import static org.apache.fury.builder.Generated.GeneratedMetaSharedSerializer.SERIALIZER_FIELD_NAME;
|
23 | 23 |
|
| 24 | +import java.lang.reflect.Field; |
24 | 25 | import java.util.Collection;
|
25 | 26 | import java.util.SortedMap;
|
26 | 27 | import org.apache.fury.Fury;
|
|
34 | 35 | import org.apache.fury.meta.ClassDef;
|
35 | 36 | import org.apache.fury.reflect.TypeRef;
|
36 | 37 | import org.apache.fury.serializer.CodegenSerializer;
|
| 38 | +import org.apache.fury.serializer.FieldMismatchCallback; |
37 | 39 | import org.apache.fury.serializer.MetaSharedSerializer;
|
38 | 40 | import org.apache.fury.serializer.ObjectSerializer;
|
39 | 41 | import org.apache.fury.serializer.Serializer;
|
@@ -179,11 +181,49 @@ protected Expression createRecord(SortedMap<Integer, Expression> recordComponent
|
179 | 181 | @Override
|
180 | 182 | protected Expression setFieldValue(Expression bean, Descriptor descriptor, Expression value) {
|
181 | 183 | if (descriptor.getField() == null) {
|
182 |
| - // Field doesn't exist in current class, skip set this field value. |
183 |
| - // Note that the field value shouldn't be an inlined value, otherwise field value read may |
184 |
| - // be ignored. |
185 |
| - // Add an ignored call here to make expression type to void. |
186 |
| - return new Expression.StaticInvoke(ExceptionUtils.class, "ignore", value); |
| 184 | + FieldMismatchCallback.FieldAdjustment adjustment = |
| 185 | + fury.getFieldMismatchCallback() |
| 186 | + .onMismatch(beanClass, descriptor.getTypeName(), descriptor.getName()); |
| 187 | + |
| 188 | + if (adjustment == null) { |
| 189 | + // Field doesn't exist in current class, skip set this field value. |
| 190 | + // Note that the field value shouldn't be an inlined value, otherwise field value read may |
| 191 | + // be ignored. |
| 192 | + // Add an ignored call here to make expression type to void. |
| 193 | + return new Expression.StaticInvoke(ExceptionUtils.class, "ignore", value); |
| 194 | + } else { |
| 195 | + |
| 196 | + Field newTargetField = adjustment.getTargetField(); |
| 197 | + |
| 198 | + // Field doesn't exist in current class, invoke field mismatch callback. |
| 199 | + Expression fieldMismatchCallback = |
| 200 | + Expression.Invoke.inlineInvoke( |
| 201 | + new Expression.Reference(FURY_NAME, TypeRef.of(Fury.class)), |
| 202 | + "getFieldMismatchCallback", |
| 203 | + TypeRef.of(FieldMismatchCallback.class), |
| 204 | + /* needTryCatch */ false); |
| 205 | + |
| 206 | + Expression onMismatch = |
| 207 | + Expression.Invoke.inlineInvoke( |
| 208 | + fieldMismatchCallback, |
| 209 | + "onMismatch", |
| 210 | + TypeRef.of(FieldMismatchCallback.FieldAdjustment.class), |
| 211 | + new Expression.StaticInvoke( |
| 212 | + Class.class, |
| 213 | + "forName", |
| 214 | + TypeRef.of(Class.class), |
| 215 | + true, |
| 216 | + new Literal(beanClass.getName())), |
| 217 | + new Literal(descriptor.getTypeName()), |
| 218 | + new Literal(descriptor.getName())); |
| 219 | + |
| 220 | + Expression invokeHandler = |
| 221 | + new Expression.Invoke(onMismatch, "adjustValue", TypeRef.of(Object.class), value); |
| 222 | + |
| 223 | + Descriptor updatedDescriptor = new Descriptor(newTargetField, null, null, null); |
| 224 | + |
| 225 | + return super.setFieldValue(bean, updatedDescriptor, invokeHandler); |
| 226 | + } |
187 | 227 | }
|
188 | 228 | return super.setFieldValue(bean, descriptor, value);
|
189 | 229 | }
|
|
0 commit comments