Search in sources :

Example 1 with ApiListField

use of com.alipay.api.internal.mapping.ApiListField in project synergic-developing by zeemood.

the class JSONWriter method model.

private void model(Object object) {
    add("{");
    boolean addedSomething = false;
    Field[] ff = object.getClass().getDeclaredFields();
    try {
        for (int i = 0; i < ff.length; ++i) {
            Field field = ff[i];
            // 获取注解
            ApiField jsonField = field.getAnnotation(ApiField.class);
            ApiListField listField = field.getAnnotation(ApiListField.class);
            // 优先处理列表类型注解,非列表类型才处理字段注解
            if (listField != null) {
                PropertyDescriptor pd = new PropertyDescriptor(field.getName(), object.getClass());
                Method accessor = pd.getReadMethod();
                if (!accessor.isAccessible())
                    accessor.setAccessible(true);
                Object value = accessor.invoke(object, (Object[]) null);
                if (value == null)
                    continue;
                if (addedSomething)
                    add(',');
                add(listField.value(), value, true);
                addedSomething = true;
            } else if (jsonField != null) {
                PropertyDescriptor pd = new PropertyDescriptor(field.getName(), object.getClass());
                Method accessor = pd.getReadMethod();
                if (!accessor.isAccessible())
                    accessor.setAccessible(true);
                Object value = accessor.invoke(object, (Object[]) null);
                if (value == null)
                    continue;
                if (addedSomething)
                    add(',');
                add(jsonField.value(), value, true);
                addedSomething = true;
            }
        }
    } catch (IntrospectionException e1) {
        AlipayLogger.logBizError(e1);
    } catch (IllegalAccessException e2) {
        AlipayLogger.logBizError(e2);
    } catch (IllegalArgumentException e3) {
        AlipayLogger.logBizError(e3);
    } catch (InvocationTargetException e4) {
        AlipayLogger.logBizError(e4);
    }
    add("}");
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ApiField(com.alipay.api.internal.mapping.ApiField) Field(java.lang.reflect.Field) ApiListField(com.alipay.api.internal.mapping.ApiListField) ApiListField(com.alipay.api.internal.mapping.ApiListField) ApiField(com.alipay.api.internal.mapping.ApiField)

Aggregations

ApiField (com.alipay.api.internal.mapping.ApiField)1 ApiListField (com.alipay.api.internal.mapping.ApiListField)1 IntrospectionException (java.beans.IntrospectionException)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1