FieldParserUtil.java 2.94 KB
Newer Older
薛涛 Toby Xue's avatar
薛涛 Toby Xue committed
1 2
package logwire.web.bo.field.parse;

薛涛 Toby Xue's avatar
薛涛 Toby Xue committed
3 4
import logwire.core.bo.annotation.Choice;
import logwire.core.bo.annotation.ForeignKey;
薛涛 Toby Xue's avatar
薛涛 Toby Xue committed
5
import logwire.core.bo.field.BizArray;
6
import logwire.core.bo.field.BizMany;
薛涛 Toby Xue's avatar
薛涛 Toby Xue committed
7 8
import logwire.core.bo.field.BizOne;
import logwire.core.exceptions.ApplicationException;
薛涛 Toby Xue's avatar
薛涛 Toby Xue committed
9

10
import java.lang.reflect.Field;
薛涛 Toby Xue's avatar
薛涛 Toby Xue committed
11
import java.lang.reflect.Type;
12
import java.math.BigDecimal;
薛涛 Toby Xue's avatar
薛涛 Toby Xue committed
13 14 15 16 17 18 19 20 21 22 23 24 25
import java.time.OffsetDateTime;

public abstract class FieldParserUtil {

    private FieldParserUtil() {
    }

    /**
     * 获取解析注解的 parser
     *
     * @param field
     * @return
     */
26
    public static FieldParser getParser(Field field) {
薛涛 Toby Xue's avatar
薛涛 Toby Xue committed
27 28 29 30 31 32 33 34 35 36
        if (field.getAnnotation(Choice.class) != null) {
            if (field.getType().equals(String.class)) {
                return ChoiceFieldParser.getInstance();
            } else if (field.getType().equals(BizArray.class)) {
                return BizArrayFieldParser.getInstance();
            } else {
                throw new ApplicationException("@Choice 只能用与 String/BizArray 类型");
            }
        } else if (field.getAnnotation(ForeignKey.class) != null) {
            //弱关联
37
            if (field.getType().equals(BizOne.class)||field.getType().equals(BizMany.class)) {
薛涛 Toby Xue's avatar
薛涛 Toby Xue committed
38 39
                return ForeignKeyFieldParser.getInstance();
            } else {
40
                throw new ApplicationException("@ForeignKey 只能用与 BizOne/BizMany 类型");
薛涛 Toby Xue's avatar
薛涛 Toby Xue committed
41 42 43 44
            }

        }
        return getParserByFieldType(field.getType());
薛涛 Toby Xue's avatar
薛涛 Toby Xue committed
45 46
    }

薛涛 Toby Xue's avatar
薛涛 Toby Xue committed
47
    public static FieldParser getParserByFieldType(Type clazz) {
薛涛 Toby Xue's avatar
薛涛 Toby Xue committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61
        // 此时一定是存在 Field 类型的 annotation
        // 根据字段类型来推断 Parser
        if (clazz.equals(String.class)) {
            return StringFieldParser.getInstance();
        } else if (clazz.equals(Integer.class) || clazz.equals(int.class)) {
            return IntegerFieldParser.getInstance();
        } else if (clazz.equals(Short.class) || clazz.equals(short.class)) {
            return SmallIntegerFieldParser.getInstance();
        } else if (clazz.equals(Long.class) || clazz.equals(long.class)) {
            return BigIntegerFieldParser.getInstance();
        } else if (clazz.equals(OffsetDateTime.class) || clazz.equals(java.util.Date.class)) {
            return DateTimeFieldParser.getInstance();
        } else if (clazz.equals(Boolean.class) || clazz.equals(boolean.class)) {
            return BooleanFieldParser.getInstance();
62
        } else if (clazz.equals(Double.class) || clazz.equals(double.class) || clazz.equals(float.class)||clazz.equals(Float.class)||clazz.equals(BigDecimal.class)) {
薛涛 Toby Xue's avatar
薛涛 Toby Xue committed
63 64 65
            return DecimalFieldParser.getInstance();
        } else if (clazz.equals(BizArray.class)) {
            return BizArrayFieldParser.getInstance();
66
        } else if (clazz.equals(BizOne.class)||clazz.equals(BizMany.class)) {
薛涛 Toby Xue's avatar
薛涛 Toby Xue committed
67
            return ForeignKeyFieldParser.getInstance();
薛涛 Toby Xue's avatar
薛涛 Toby Xue committed
68
        }
薛涛 Toby Xue's avatar
薛涛 Toby Xue committed
69
        throw new ApplicationException("该字段类型无法识别");
薛涛 Toby Xue's avatar
薛涛 Toby Xue committed
70 71
    }
}