001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.util;
017:
018: import org.springframework.util.Assert;
019: import org.springframework.util.ReflectionUtils;
020:
021: import java.lang.reflect.Field;
022:
023: /**
024: * Offers static methods for directly manipulating static fields.
025: *
026: * @author Ben Alex
027: * @version $Id: FieldUtils.java 1784 2007-02-24 21:00:24Z luke_t $
028: */
029: public final class FieldUtils {
030: //~ Constructors ===================================================================================================
031:
032: private FieldUtils() {
033: }
034:
035: //~ Methods ========================================================================================================
036:
037: public static String getAccessorName(String fieldName, Class type) {
038: Assert.hasText(fieldName, "FieldName required");
039: Assert.notNull(type, "Type required");
040:
041: if (type.getName().equals("boolean")) {
042: return "is"
043: + org.springframework.util.StringUtils
044: .capitalize(fieldName);
045: } else {
046: return "get"
047: + org.springframework.util.StringUtils
048: .capitalize(fieldName);
049: }
050: }
051:
052: /**
053: * Attempts to locate the specified field on the class.
054: *
055: * @param clazz the class definition containing the field
056: * @param fieldName the name of the field to locate
057: *
058: * @return the Field (never null)
059: *
060: * @throws IllegalStateException if field could not be found
061: */
062: public static Field getField(Class clazz, String fieldName)
063: throws IllegalStateException {
064: Assert.notNull(clazz, "Class required");
065: Assert.hasText(fieldName, "Field name required");
066:
067: try {
068: return clazz.getDeclaredField(fieldName);
069: } catch (NoSuchFieldException nsf) {
070: // Try superclass
071: if (clazz.getSuperclass() != null) {
072: return getField(clazz.getSuperclass(), fieldName);
073: }
074:
075: throw new IllegalStateException("Could not locate field '"
076: + fieldName + "' on class " + clazz);
077: }
078: }
079:
080: public static String getMutatorName(String fieldName) {
081: Assert.hasText(fieldName, "FieldName required");
082:
083: return "set"
084: + org.springframework.util.StringUtils
085: .capitalize(fieldName);
086: }
087:
088: public static Object getProtectedFieldValue(String protectedField,
089: Object object) {
090: Field field = FieldUtils.getField(object.getClass(),
091: protectedField);
092:
093: try {
094: field.setAccessible(true);
095:
096: return field.get(object);
097: } catch (Exception ex) {
098: ReflectionUtils.handleReflectionException(ex);
099:
100: return null; // unreachable - previous line throws exception
101: }
102: }
103:
104: public static void setProtectedFieldValue(String protectedField,
105: Object object, Object newValue) {
106: Field field = FieldUtils.getField(object.getClass(),
107: protectedField);
108:
109: try {
110: field.setAccessible(true);
111: field.set(object, newValue);
112: } catch (Exception ex) {
113: ReflectionUtils.handleReflectionException(ex);
114: }
115: }
116: }
|