1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| import com.alibaba.fastjson2.codec.BeanInfo; import com.alibaba.fastjson2.codec.FieldInfo; import com.alibaba.fastjson2.modules.ObjectWriterAnnotationProcessor; import com.alibaba.fastjson2.util.BeanUtils; import com.alibaba.fastjson2.writer.ObjectWriterBaseModule; import com.alibaba.fastjson2.writer.ObjectWriterProvider;
import java.lang.reflect.Field; import java.lang.reflect.Method;
final class JSONFieldWriter extends ObjectWriterBaseModule {
private final ObjectWriterAnnotationProcessor processor;
JSONFieldWriter(ObjectWriterProvider provider) { super(provider); final ObjectWriterAnnotationProcessor processor = super.getAnnotationProcessor(); this.processor = new ObjectWriterAnnotationProcessor() {
@Override public void getBeanInfo(final BeanInfo beanInfo, final Class objectClass) { processor.getBeanInfo(beanInfo, objectClass); }
@Override public void getFieldInfo(final BeanInfo beanInfo, final FieldInfo fieldInfo, final Class objectClass, final Field field) { final JSONField2 annotation = field.getAnnotation(JSONField2.class); if (annotation == null) { processor.getFieldInfo(beanInfo, fieldInfo, objectClass, field); } else { fieldInfo.ignore = false; fieldInfo.fieldClassMixIn = false; fieldInfo.fieldName = annotation.value(); } }
@Override public void getFieldInfo(final BeanInfo beanInfo, final FieldInfo fieldInfo, final Class objectClass, final Method method) { if (objectClass.getName().startsWith("java.lang") || BeanUtils.isRecord(objectClass)) { processor.getFieldInfo(beanInfo, fieldInfo, objectClass, method); return; } final Field field = BeanUtils.getField(objectClass, method); if (field == null) { processor.getFieldInfo(beanInfo, fieldInfo, objectClass, method); return; } fieldInfo.features |= FieldInfo.FIELD_MASK; this.getFieldInfo(beanInfo, fieldInfo, objectClass, field); } }; }
@Override public ObjectWriterAnnotationProcessor getAnnotationProcessor() { return this.processor; } }
|