| 1 | |
package org.jtheque.utils.bean; |
| 2 | |
|
| 3 | |
import org.slf4j.LoggerFactory; |
| 4 | |
|
| 5 | |
import java.lang.reflect.Field; |
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
public final class BeanUtils { |
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
private BeanUtils() { |
| 28 | 0 | super(); |
| 29 | 0 | } |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 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 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 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 | |
} |