Coverage report

  %line %branch
org.apache.portals.graffito.jcr.reflection.ReflectionUtils
0% 
0% 

 1  
 /*
 2  
  * Copyright 2000-2004 The Apache Software Foundation.
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.apache.portals.graffito.jcr.reflection;
 17  
 
 18  
 import java.lang.reflect.InvocationTargetException;
 19  
 
 20  
 import org.apache.commons.beanutils.ConstructorUtils;
 21  
 import org.apache.commons.beanutils.PropertyUtils;
 22  
 import org.apache.portals.graffito.jcr.exception.JcrMappingException;
 23  
 import org.apache.portals.graffito.jcr.persistence.collectionconverter.CollectionConverter;
 24  
 
 25  
 
 26  
 /**
 27  
  * Utility class for handling reflection using BeanUtils.
 28  
  * 
 29  
  * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
 30  
  */
 31  0
 abstract public class ReflectionUtils {
 32  
     public static Object getNestedProperty(Object object, String fieldName) {
 33  0
         if (null == object) {
 34  0
             return null;
 35  
         }
 36  
         
 37  
         try {
 38  0
             return PropertyUtils.getNestedProperty(object, fieldName);
 39  
         }
 40  0
         catch(IllegalAccessException e) {
 41  0
             throw new JcrMappingException("Cannot access property "
 42  
                     + fieldName,
 43  
                     e);
 44  
         }
 45  0
         catch(InvocationTargetException e) {
 46  0
             throw new JcrMappingException("Cannot access property "
 47  
                     + fieldName,
 48  
                     e);
 49  
         }
 50  0
         catch(NoSuchMethodException e) {
 51  0
             throw new JcrMappingException("Cannot access property "
 52  
                     + fieldName,
 53  
                     e);
 54  
         }
 55  
     }
 56  
     
 57  
     public static Class getPropertyType(Object object, String fieldName) {
 58  
         try {
 59  0
             return PropertyUtils.getPropertyType(object, fieldName);
 60  
         }
 61  0
         catch(Exception ex) {
 62  0
             throw new JcrMappingException("Cannot access property "
 63  
                     + fieldName,
 64  
                     ex);
 65  
         }
 66  
     }
 67  
 
 68  
     public static Object newInstance(Class clazz) {
 69  
         try {
 70  0
             return clazz.newInstance();
 71  
         }
 72  0
         catch(Exception ex) {
 73  0
             throw new JcrMappingException("Cannot create instance for class "
 74  
                     + clazz,
 75  
                     ex);
 76  
         }
 77  
     }
 78  
     
 79  
     /**
 80  
      * @param className
 81  
      * @param objects
 82  
      * @return
 83  
      */
 84  
     public static CollectionConverter invokeConstructor(String className, 
 85  
                                                         Object[] params) {
 86  
         try {
 87  0
             Class converterClass= Class.forName(className);
 88  
     
 89  0
             return (CollectionConverter) ConstructorUtils.invokeConstructor(converterClass, params);
 90  
         }
 91  0
         catch(Exception ex) {
 92  0
             throw new JcrMappingException("Cannot create instance for class "
 93  
                     + className,
 94  
                     ex);
 95  
         }
 96  
     }
 97  
 
 98  
     /**
 99  
      * @param object
 100  
      * @param fieldName
 101  
      * @param path
 102  
      */
 103  
     public static void setNestedProperty(Object object, String fieldName, Object value) {
 104  
         try {
 105  0
             PropertyUtils.setNestedProperty(object, fieldName, value);
 106  
         }
 107  0
         catch(Exception ex) {
 108  0
             throw new JcrMappingException("Cannot set the field " + fieldName,
 109  
                     ex);
 110  0
         }
 111  0
     }
 112  
 
 113  
     /**
 114  
      * @param string
 115  
      * @return
 116  
      */
 117  
     public static Object newInstance(String clazz) {
 118  
         try {
 119  0
             return Class.forName(clazz).newInstance();
 120  
         }
 121  0
         catch(Exception ex) {
 122  0
             throw new JcrMappingException("Cannot create instance for class "
 123  
                     + clazz,
 124  
                     ex);
 125  
         }
 126  
     }
 127  
 
 128  
     /**
 129  
      * @param elementClassName
 130  
      * @return
 131  
      */
 132  
     public static Class forName(String clazz) {
 133  
         try {
 134  0
             return Class.forName(clazz);
 135  
         }
 136  0
         catch(Exception ex) {
 137  0
             throw new JcrMappingException("Cannot load class " + clazz, ex);
 138  
         }
 139  
     }
 140  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.