1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.util;
17
18 import org.springframework.util.Assert;
19 import org.springframework.util.ReflectionUtils;
20
21 import java.lang.reflect.Field;
22
23
24
25
26
27
28
29
30 public final class FieldUtils {
31
32
33 private FieldUtils() {
34 }
35
36
37
38 public static String getAccessorName(String fieldName, Class type) {
39 Assert.hasText(fieldName, "FieldName required");
40 Assert.notNull(type, "Type required");
41
42 if (type.getName().equals("boolean")) {
43 return "is" + org.springframework.util.StringUtils.capitalize(fieldName);
44 } else {
45 return "get" + org.springframework.util.StringUtils.capitalize(fieldName);
46 }
47 }
48
49
50
51
52
53
54
55
56
57
58
59 public static Field getField(Class clazz, String fieldName)
60 throws IllegalStateException {
61 Assert.notNull(clazz, "Class required");
62 Assert.hasText(fieldName, "Field name required");
63
64 try {
65 return clazz.getDeclaredField(fieldName);
66 } catch (NoSuchFieldException nsf) {
67
68 if (clazz.getSuperclass() != null) {
69 return getField(clazz.getSuperclass(), fieldName);
70 }
71
72 throw new IllegalStateException("Could not locate field '" + fieldName + "' on class " + clazz);
73 }
74 }
75
76 public static String getMutatorName(String fieldName) {
77 Assert.hasText(fieldName, "FieldName required");
78
79 return "set" + org.springframework.util.StringUtils.capitalize(fieldName);
80 }
81
82 public static Object getProtectedFieldValue(String protectedField, Object object) {
83 Field field = FieldUtils.getField(object.getClass(), protectedField);
84
85 try {
86 field.setAccessible(true);
87
88 return field.get(object);
89 } catch (Exception ex) {
90 ReflectionUtils.handleReflectionException(ex);
91
92 return null;
93 }
94 }
95
96 public static void setProtectedFieldValue(String protectedField, Object object, Object newValue) {
97 Field field = FieldUtils.getField(object.getClass(), protectedField);
98
99 try {
100 field.setAccessible(true);
101 field.set(object, newValue);
102 } catch (Exception ex) {
103 ReflectionUtils.handleReflectionException(ex);
104 }
105 }
106 }