1
2
3
4
5 package gboat2.base.dao.util;
6
7 import java.lang.reflect.Field;
8 import java.util.LinkedList;
9 import java.util.List;
10
11
12
13
14
15 public final class ClassHelper {
16
17
18
19
20 private ClassHelper() {
21 }
22
23
24
25
26
27
28
29 public static boolean checkField(String clazz, String fieldName) {
30 try {
31 Class<?> c = Class.forName(clazz);
32 if (c.getDeclaredField(fieldName) != null) {
33 return true;
34 }
35 } catch (Exception e) {
36 return false;
37 }
38 return false;
39 }
40
41
42
43
44
45
46
47 public static boolean checkField(Class<?> clazz, String fieldName) {
48 try {
49 if (clazz.getDeclaredField(fieldName) != null) {
50 return true;
51 }
52 } catch (Exception e) {
53 return false;
54 }
55 return false;
56 }
57
58
59
60
61
62
63
64 public static Field getField(String clazz, String fieldName) {
65 Field field = null;
66 try {
67 Class<?> c = Class.forName(clazz);
68 try {
69 field = c.getDeclaredField(fieldName);
70 } catch (NoSuchFieldException e) {
71 Field[] fields = c.getDeclaredFields();
72 for (Field f : fields) {
73 if (f.getName().equalsIgnoreCase(fieldName)) {
74 field = f;
75 break;
76 }
77 }
78 }
79 } catch (SecurityException e) {
80 e.printStackTrace();
81 } catch (ClassNotFoundException e) {
82 e.printStackTrace();
83 }
84
85 return field;
86 }
87
88
89
90
91
92
93
94 public static Field getField(Class<?> clazz, String fieldName) {
95 Field field = null;
96 try {
97 field = clazz.getDeclaredField(fieldName);
98 } catch (NoSuchFieldException e) {
99 Field[] fields = clazz.getDeclaredFields();
100 for (Field f : fields) {
101 if (f.getName().equalsIgnoreCase(fieldName)) {
102 field = f;
103 break;
104 }
105 }
106
107 if (!clazz.getSuperclass().getSimpleName().equals("Object")) {
108 fields = clazz.getSuperclass().getDeclaredFields();
109 for (Field f : fields) {
110 if (f.getName().equalsIgnoreCase(fieldName)) {
111 field = f;
112 break;
113 }
114 }
115 }
116 }
117
118 return field;
119 }
120
121
122
123
124
125
126
127 public static String getFieldIgnoreCase(String clazz, String fieldName) {
128 if (checkField(clazz, fieldName)) {
129 return fieldName;
130 } else {
131 try {
132 Class<?> c = Class.forName(clazz);
133 Field[] fields = c.getDeclaredFields();
134 for (Field field : fields) {
135 if (field.getName().equalsIgnoreCase(fieldName)) {
136 return field.getName();
137 }
138 }
139 } catch (SecurityException e) {
140 e.printStackTrace();
141 } catch (ClassNotFoundException e) {
142 e.printStackTrace();
143 }
144
145 return null;
146 }
147 }
148
149
150
151
152
153
154
155 public static String getFieldNameIgnoreCase(Class<?> clazz, String fieldName) {
156 if (checkField(clazz, fieldName)) {
157 return fieldName;
158 } else {
159 Field[] fields = clazz.getDeclaredFields();
160 for (Field field : fields) {
161 if (field.getName().equalsIgnoreCase(fieldName)) {
162 return field.getName();
163 }
164 }
165
166 if (!clazz.getSuperclass().getSimpleName().equals("Object")) {
167 fields = clazz.getSuperclass().getDeclaredFields();
168 for (Field f : fields) {
169 if (f.getName().equalsIgnoreCase(fieldName)) {
170 return f.getName();
171 }
172 }
173 }
174 return null;
175 }
176 }
177
178
179
180
181
182
183 public static List<String> getFieldNames(String clazz) {
184 List<String> fieldNames = new LinkedList<String>();
185 try {
186 Class<?> c = Class.forName(clazz);
187 fieldNames.addAll(getFieldNames(c));
188 } catch (SecurityException e) {
189 e.printStackTrace();
190 } catch (ClassNotFoundException e) {
191 e.printStackTrace();
192 }
193
194 return fieldNames;
195 }
196
197
198
199
200
201
202 public static List<String> getFieldNames(Class<?> clazz) {
203 List<String> fieldNames = new LinkedList<String>();
204 Field[] fields = clazz.getDeclaredFields();
205 for (Field field : fields) {
206 fieldNames.add(field.getName());
207 }
208
209 return fieldNames;
210 }
211
212
213
214
215
216
217 public static String getNameWithoutPackage(Class<?> clazz) {
218 String name = clazz.getName();
219 if (name.indexOf(".") != -1) {
220 name = name.substring(name.lastIndexOf(".") + 1);
221 }
222
223 return name;
224 }
225 }