View Javadoc

1   /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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   * Offers static methods for directly manipulating static fields.
26   *
27   * @author Ben Alex
28   * @version $Id: FieldUtils.java 1784 2007-02-24 21:00:24Z luke_t $
29   */
30  public final class FieldUtils {
31      //~ Constructors ===================================================================================================
32  
33      private FieldUtils() {
34      }
35  
36      //~ Methods ========================================================================================================
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       * Attempts to locate the specified field on the class.
51       *
52       * @param clazz the class definition containing the field
53       * @param fieldName the name of the field to locate
54       *
55       * @return the Field (never null)
56       *
57       * @throws IllegalStateException if field could not be found
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              // Try superclass
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; // unreachable - previous line throws exception
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 }