1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.ochafik.beans;
20 import java.beans.BeanInfo;
21 import java.beans.IntrospectionException;
22 import java.beans.Introspector;
23 import java.beans.PropertyChangeEvent;
24 import java.beans.PropertyChangeListener;
25 import java.beans.PropertyDescriptor;
26 import java.lang.reflect.Method;
27
28 import com.ochafik.util.string.StringUtils;
29 public class BeansUtils {
30 public static final void markTransientProperty(String name,Class<?> classe) throws IntrospectionException {
31 BeanInfo info = Introspector.getBeanInfo(classe);
32 PropertyDescriptor[] propertyDescriptors =
33 info.getPropertyDescriptors();
34 for (int i = 0; i < propertyDescriptors.length; ++i) {
35 PropertyDescriptor pd = propertyDescriptors[i];
36 if (pd.getName().equals(name)) {
37 pd.setValue("transient", Boolean.TRUE);
38 }
39 }
40 }
41
42 public static void addPropertyChangeListener(Object bean, final String propertyName, final PropertyChangeListener listener) {
43 if (bean == null)
44 throw new NullPointerException("Null bean !");
45
46 try {
47 bean.getClass().getMethod("addPropertyChangeListener", String.class, PropertyChangeListener.class).invoke(bean, propertyName, listener);
48 } catch (Exception ex) {
49 try {
50 bean.getClass().getMethod("addPropertyChangeListener", PropertyChangeListener.class).invoke(bean, new PropertyChangeListener() {
51
52 public void propertyChange(PropertyChangeEvent evt) {
53 if (propertyName.equals(evt.getPropertyName()))
54 listener.propertyChange(evt);
55 }
56 });
57 } catch (Exception ex2) {
58 throw new RuntimeException(ex);
59 }
60 }
61 }
62 public static void removePropertyChangeListener(Object bean, String propertyName, PropertyChangeListener listener) {
63 if (bean == null)
64 throw new NullPointerException("Null bean !");
65
66 try {
67 bean.getClass().getMethod("removePropertyChangeListener", PropertyChangeListener.class).invoke(bean, listener);
68 } catch (Exception ex) {
69 throw new RuntimeException(ex);
70 }
71 }
72
73 public static Object get(Object bean, String propertyName) {
74 return get(bean, propertyName, Object.class);
75 }
76
77 public static <T> T get(Object bean, String propertyName, Class<T> propertyClass) {
78 if (bean == null)
79 throw new NullPointerException("Null bean !");
80
81 try {
82 String cap = StringUtils.capitalize(propertyName);
83 Method m;
84 if (propertyClass == Boolean.class) {
85 try {
86 m = bean.getClass().getMethod("is" + cap);
87 } catch (NoSuchMethodException ex) {
88 m = bean.getClass().getMethod("get" + cap);
89 }
90 } else {
91 m = bean.getClass().getMethod("get" + cap);
92 }
93 return propertyClass.cast(m.invoke(bean));
94 } catch (Exception ex) {
95 throw new RuntimeException(ex);
96 }
97 }
98
99 public static <T> void set(Object bean, String propertyName, Class<T> propertyClass, T value) {
100 if (bean == null)
101 throw new NullPointerException("Null bean !");
102
103 try {
104 bean.getClass().getMethod("set" + StringUtils.capitalize(propertyName), propertyClass).invoke(bean, value);
105 } catch (Exception ex) {
106 throw new RuntimeException(ex);
107 }
108 }
109 }