| 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 |
0 |
private FieldUtils() { |
| 34 |
0 |
} |
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
public static String getAccessorName(String fieldName, Class type) { |
| 39 |
0 |
Assert.hasText(fieldName, "FieldName required"); |
| 40 |
0 |
Assert.notNull(type, "Type required"); |
| 41 |
|
|
| 42 |
0 |
if (type.getName().equals("boolean")) { |
| 43 |
0 |
return "is" + org.springframework.util.StringUtils.capitalize(fieldName); |
| 44 |
|
} else { |
| 45 |
0 |
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 |
29 |
Assert.notNull(clazz, "Class required"); |
| 62 |
29 |
Assert.hasText(fieldName, "Field name required"); |
| 63 |
|
|
| 64 |
|
try { |
| 65 |
29 |
return clazz.getDeclaredField(fieldName); |
| 66 |
0 |
} catch (NoSuchFieldException nsf) { |
| 67 |
|
|
| 68 |
0 |
if (clazz.getSuperclass() != null) { |
| 69 |
0 |
return getField(clazz.getSuperclass(), fieldName); |
| 70 |
|
} |
| 71 |
|
|
| 72 |
0 |
throw new IllegalStateException("Could not locate field '" + fieldName + "' on class " + clazz); |
| 73 |
|
} |
| 74 |
|
} |
| 75 |
|
|
| 76 |
|
public static String getMutatorName(String fieldName) { |
| 77 |
0 |
Assert.hasText(fieldName, "FieldName required"); |
| 78 |
|
|
| 79 |
0 |
return "set" + org.springframework.util.StringUtils.capitalize(fieldName); |
| 80 |
|
} |
| 81 |
|
|
| 82 |
|
public static Object getProtectedFieldValue(String protectedField, Object object) { |
| 83 |
0 |
Field field = FieldUtils.getField(object.getClass(), protectedField); |
| 84 |
|
|
| 85 |
|
try { |
| 86 |
0 |
field.setAccessible(true); |
| 87 |
|
|
| 88 |
0 |
return field.get(object); |
| 89 |
0 |
} catch (Exception ex) { |
| 90 |
0 |
ReflectionUtils.handleReflectionException(ex); |
| 91 |
|
|
| 92 |
0 |
return null; |
| 93 |
|
} |
| 94 |
|
} |
| 95 |
|
|
| 96 |
|
public static void setProtectedFieldValue(String protectedField, Object object, Object newValue) { |
| 97 |
0 |
Field field = FieldUtils.getField(object.getClass(), protectedField); |
| 98 |
|
|
| 99 |
|
try { |
| 100 |
0 |
field.setAccessible(true); |
| 101 |
0 |
field.set(object, newValue); |
| 102 |
0 |
} catch (Exception ex) { |
| 103 |
0 |
ReflectionUtils.handleReflectionException(ex); |
| 104 |
0 |
} |
| 105 |
0 |
} |
| 106 |
|
} |