翰林优商.apk(点击下载) / ColumnEntity.java


package sjm.xuitls.db.table;

import android.database.Cursor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import sjm.xuitls.common.util.LogUtil;
import sjm.xuitls.db.annotation.Column;
import sjm.xuitls.db.converter.ColumnConverter;
import sjm.xuitls.db.converter.ColumnConverterFactory;
import sjm.xuitls.db.sqlite.ColumnDbType;

public final class ColumnEntity {
    protected final ColumnConverter columnConverter;
    protected final Field columnField;
    protected final Method getMethod;
    private final boolean isAutoId;
    private final boolean isId;
    protected final String name;
    private final String property;
    protected final Method setMethod;

    ColumnEntity(Class<?> cls, Field field, Column column) {
        field.setAccessible(true);
        this.columnField = field;
        this.name = column.name();
        this.property = column.property();
        boolean isId2 = column.isId();
        this.isId = isId2;
        Class<?> type = field.getType();
        this.isAutoId = isId2 && column.autoGen() && ColumnUtils.isAutoIdType(type);
        this.columnConverter = ColumnConverterFactory.getColumnConverter(type);
        Method findGetMethod = ColumnUtils.findGetMethod(cls, field);
        this.getMethod = findGetMethod;
        if (findGetMethod != null && !findGetMethod.isAccessible()) {
            findGetMethod.setAccessible(true);
        }
        Method findSetMethod = ColumnUtils.findSetMethod(cls, field);
        this.setMethod = findSetMethod;
        if (findSetMethod != null && !findSetMethod.isAccessible()) {
            findSetMethod.setAccessible(true);
        }
    }

    public void setValueFromCursor(Object obj, Cursor cursor, int i) {
        Object fieldValue = this.columnConverter.getFieldValue(cursor, i);
        if (fieldValue != null) {
            Method method = this.setMethod;
            if (method != null) {
                try {
                    method.invoke(obj, fieldValue);
                } catch (Throwable th) {
                    LogUtil.e(th.getMessage(), th);
                }
            } else {
                try {
                    this.columnField.set(obj, fieldValue);
                } catch (Throwable th2) {
                    LogUtil.e(th2.getMessage(), th2);
                }
            }
        }
    }

    public Object getColumnValue(Object obj) {
        Object fieldValue = getFieldValue(obj);
        if (!this.isAutoId || (!fieldValue.equals(0L) && !fieldValue.equals(0))) {
            return this.columnConverter.fieldValue2DbValue(fieldValue);
        }
        return null;
    }

    public void setAutoIdValue(Object obj, long j) {
        Object valueOf = Long.valueOf(j);
        if (ColumnUtils.isInteger(this.columnField.getType())) {
            valueOf = Integer.valueOf((int) j);
        }
        Method method = this.setMethod;
        if (method != null) {
            try {
                method.invoke(obj, valueOf);
            } catch (Throwable th) {
                LogUtil.e(th.getMessage(), th);
            }
        } else {
            try {
                this.columnField.set(obj, valueOf);
            } catch (Throwable th2) {
                LogUtil.e(th2.getMessage(), th2);
            }
        }
    }

    public Object getFieldValue(Object obj) {
        if (obj != null) {
            Method method = this.getMethod;
            if (method != null) {
                try {
                    return method.invoke(obj, new Object[0]);
                } catch (Throwable th) {
                    LogUtil.e(th.getMessage(), th);
                }
            } else {
                try {
                    return this.columnField.get(obj);
                } catch (Throwable th2) {
                    LogUtil.e(th2.getMessage(), th2);
                }
            }
        }
        return null;
    }

    public String getName() {
        return this.name;
    }

    public String getProperty() {
        return this.property;
    }

    public boolean isId() {
        return this.isId;
    }

    public boolean isAutoId() {
        return this.isAutoId;
    }

    public Field getColumnField() {
        return this.columnField;
    }

    public ColumnConverter getColumnConverter() {
        return this.columnConverter;
    }

    public ColumnDbType getColumnDbType() {
        return this.columnConverter.getColumnDbType();
    }

    public String toString() {
        return this.name;
    }
}