Coverage Report - org.acegisecurity.util.FieldUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
FieldUtils
10% 
0% 
2.667
 
 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  0
     private FieldUtils() {
 34  0
     }
 35  
 
 36  
     //~ Methods ========================================================================================================
 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  
      * 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  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  
             // Try superclass
 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; // unreachable - previous line throws exception
 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  
 }