Coverage Report - org.jtheque.utils.bean.BeanUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
BeanUtils
0%
0/45
0%
0/4
3
 
 1  
 package org.jtheque.utils.bean;
 2  
 
 3  
 import org.slf4j.LoggerFactory;
 4  
 
 5  
 import java.lang.reflect.Field;
 6  
 
 7  
 /*
 8  
  * This file is part of JTheque.
 9  
  *
 10  
  * JTheque is free software: you can redistribute it and/or modify
 11  
  * it under the terms of the GNU General Public License as published by
 12  
  * the Free Software Foundation, either version 3 of the License.
 13  
  *
 14  
  * JTheque is distributed in the hope that it will be useful,
 15  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17  
  * GNU General Public License for more details.
 18  
  *
 19  
  * You should have received a copy of the GNU General Public License
 20  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 21  
  */
 22  
 
 23  
 public final class BeanUtils {
 24  
     /**
 25  
      * Utility class, not instanciable.
 26  
      */
 27  
     private BeanUtils() {
 28  0
         super();
 29  0
     }
 30  
 
 31  
     /**
 32  
      * Create a quick memento using the specified fields.
 33  
      *
 34  
      * @param bean The bean to create the memento from.
 35  
      * @param fields The fields to use.
 36  
      *
 37  
      * @param <T> The type of data.
 38  
      *
 39  
      * @return A memento of the bean.
 40  
      */
 41  
     public static <T> T createQuickMemento(T bean, String... fields) {
 42  0
         T instance = null;
 43  
 
 44  
         try {
 45  0
             instance = (T) bean.getClass().newInstance();
 46  
 
 47  0
             for (String field : fields) {
 48  0
                 Object value = ReflectionUtils.getPropertyValue(bean, field);
 49  
 
 50  0
                 ReflectionUtils.getSetterMethod(instance, field).invoke(instance, value);
 51  
             }
 52  0
         } catch (Exception e) {
 53  0
             LoggerFactory.getLogger(ReflectionUtils.class).error(e.getMessage(), e);
 54  0
         }
 55  
 
 56  0
         return instance;
 57  
     }
 58  
 
 59  
     /**
 60  
      * Restore the quick memento to the bean.
 61  
      *
 62  
      * @param bean The bean to restore.
 63  
      * @param memento The memento to restore.
 64  
      * @param fields The fields to use.
 65  
      */
 66  
     public static void restoreQuickMemento(Object bean, Object memento, String... fields) {
 67  
         try {
 68  0
             for (String field : fields) {
 69  0
                 Object value = ReflectionUtils.getPropertyValue(memento, field);
 70  
 
 71  0
                 ReflectionUtils.getSetterMethod(bean, field).invoke(bean, value);
 72  
             }
 73  0
         } catch (Exception e) {
 74  0
             LoggerFactory.getLogger(ReflectionUtils.class).error(e.getMessage(), e);
 75  0
         }
 76  0
     }
 77  
 
 78  
     public static void set(Object bean, String field, Object value){
 79  
         try {
 80  0
             Field f = bean.getClass().getDeclaredField(field);
 81  0
             f.setAccessible(true);
 82  0
             f.set(bean, value);
 83  0
         } catch (NoSuchFieldException e) {
 84  0
             LoggerFactory.getLogger(ReflectionUtils.class).error(e.getMessage(), e);
 85  0
         } catch (IllegalAccessException e) {
 86  0
             LoggerFactory.getLogger(ReflectionUtils.class).error(e.getMessage(), e);
 87  0
         }
 88  0
     }
 89  
 
 90  
     public static <T> T get(Object bean, String field) {
 91  
         try {
 92  0
             Field f = bean.getClass().getDeclaredField(field);
 93  0
             f.setAccessible(true);
 94  0
             return (T) f.get(bean);
 95  0
         } catch (NoSuchFieldException e) {
 96  0
             LoggerFactory.getLogger(ReflectionUtils.class).error(e.getMessage(), e);
 97  0
         } catch (IllegalAccessException e) {
 98  0
             LoggerFactory.getLogger(ReflectionUtils.class).error(e.getMessage(), e);
 99  0
         }
 100  
 
 101  0
         return null;
 102  
     }
 103  
 
 104  
     public static <T> T getStatic(Class<?> aClass, String field) {
 105  
         try {
 106  0
             Field f = aClass.getDeclaredField(field);
 107  0
             f.setAccessible(true);
 108  0
             return (T) f.get(null);
 109  0
         } catch (NoSuchFieldException e) {
 110  0
             LoggerFactory.getLogger(ReflectionUtils.class).error(e.getMessage(), e);
 111  0
         } catch (IllegalAccessException e) {
 112  0
             LoggerFactory.getLogger(ReflectionUtils.class).error(e.getMessage(), e);
 113  0
         }
 114  
 
 115  0
         return null;
 116  
     }
 117  
 }