sujinwen
2017-07-21 9f86ecd1435f386b778538df5eb0d769c3171db0
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
 
package com.awsle.aibatis.reflect;
 
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
/**
 * 
 * @author 席有芳
 * @url http://code.awsle.com/index.php/p/aibatis/
 * @mail 951868171@qq.com
 * @version 1.0
 * @since aibatis-Alpha1.0.zip
 */
public class FieldUtils {
    
    public static Method getFieldGetMethod(Class<?> clazz, Field f) {
        String fn = f.getName();
        Method m = getFieldGetMethod(clazz, fn);
        if(m == null && f.getType() == boolean.class){
            m = getBooleanFieldGetMethod(clazz, fn);
        }
        return m;
    }
    
    public static Method getBooleanFieldGetMethod(Class<?> clazz, String fieldName) {
        String mn = "is" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
        if(isISStart(fieldName)){
            mn = fieldName;
        }
        try {
            return clazz.getDeclaredMethod(mn);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    
    public static Method getBooleanFieldSetMethod(Class<?> clazz, Field f) {
        String fn = f.getName();
        String mn = "set" + fn.substring(0, 1).toUpperCase() + fn.substring(1);
        if(isISStart(f.getName())){
            mn = "set" + fn.substring(2, 3).toUpperCase() + fn.substring(3);
        }
        try {
            return clazz.getDeclaredMethod(mn, f.getType());
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    
    private static boolean isISStart(String fieldName){
        if(fieldName==null || fieldName.trim().length()==0)
            return false;
        //is开头,并且is之后第一个字母是大写 比如 isAdmin
        return fieldName.startsWith("is") && !Character.isLowerCase(fieldName.charAt(2));
    }
    
    
    
    
    public static Method getFieldGetMethod(Class<?> clazz, String fieldName) {
        String mn = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
        try {
            return clazz.getDeclaredMethod(mn);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            return null;
        }
    }
 
    public static Method getFieldSetMethod(Class<?> clazz, Field f) {
        String fn = f.getName();
        String mn = "set" + fn.substring(0, 1).toUpperCase() + fn.substring(1);
        try {
            return clazz.getDeclaredMethod(mn, f.getType());
        } catch (NoSuchMethodException e) {
            if(f.getType() == boolean.class){
                return getBooleanFieldSetMethod(clazz, f);
            }
        }
        return null;
    }
    
    public static Method getFieldSetMethod(Class<?> clazz, String fieldName) {
        try {
            return getFieldSetMethod(clazz, clazz.getDeclaredField(fieldName));
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 获取某个字段的值
     * @param entity
     * @param fieldName
     * @return
     */
    public static Object getFieldValue(Object entity,Field field){
        Method method = getFieldGetMethod(entity.getClass(), field);
        return invoke(entity, method);
    }
    
    /**
     * 获取某个字段的值
     * @param entity
     * @param fieldName
     * @return
     */
    public static Object getFieldValue(Object entity,String fieldName){
        Method method = getFieldGetMethod(entity.getClass(), fieldName);
        return invoke(entity, method);
    }
    
    /**
     * 设置某个字段的值
     * @param entity
     * @param fieldName
     * @return
     */
    public static void setFieldValue(Object entity,Field field,Object value){
        try {
            Method set = getFieldSetMethod(entity.getClass(), field);
            if (set != null && value != null) {
                set.setAccessible(true);
                Class<?> type = field.getType();
                if (type == String.class) {
                    set.invoke(entity, value.toString());
                } else if (type == int.class || type == Integer.class) {
                    set.invoke(entity, value == null ? (Integer) null : Integer.parseInt(value.toString()));
                } else if (type == float.class || type == Float.class) {
                    set.invoke(entity, value == null ? (Float) null: Float.parseFloat(value.toString()));
                } else if (type == long.class || type == Long.class) {
                    set.invoke(entity, value == null ? (Long) null: Long.parseLong(value.toString()));
                } else if (type == Date.class) {
                    set.invoke(entity, value == null ? (Date) null: stringToDateTime(value.toString()));
                } else {
                    set.invoke(entity, value);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
 
    
    /**
     * 获取某个字段的值
     * @param entity
     * @param fieldName
     * @return
     */
    public static Field getFieldByName(Class<?> clazz,String fieldName){
        Field field = null;
        if(fieldName!=null){
            try {
                field = clazz.getDeclaredField(fieldName);
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        return field;
    }
    
 
 
    /**
     * 获取某个实体执行某个方法的结果
     * @param obj
     * @param method
     * @return
     */
    private static Object invoke(Object obj , Method method){
        if(obj == null || method == null) return null;
        try {
            return method.invoke(obj);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    
    public static boolean isBaseDateType(Field field){
        Class<?> clazz = field.getType();
        return   clazz.equals(String.class) ||  
                 clazz.equals(Integer.class)||  
                 clazz.equals(Byte.class) ||  
                 clazz.equals(Long.class) ||  
                 clazz.equals(Double.class) ||  
                 clazz.equals(Float.class) ||  
                 clazz.equals(Character.class) ||  
                 clazz.equals(Short.class) ||  
                 clazz.equals(Boolean.class) ||  
                 clazz.equals(Date.class) ||  
                 clazz.equals(java.util.Date.class) ||
                 clazz.equals(java.sql.Date.class) ||
                 clazz.isPrimitive();
    }
    
    private static Date stringToDateTime(String strDate) {
        if (strDate != null) {
            try {
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(strDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}