View Javadoc

1   /*
2   	Copyright (c) 2009 Olivier Chafik, All Rights Reserved
3   	
4   	This file is part of JNAerator (http://jnaerator.googlecode.com/).
5   	
6   	JNAerator is free software: you can redistribute it and/or modify
7   	it under the terms of the GNU General Public License as published by
8   	the Free Software Foundation, either version 3 of the License, or
9   	(at your option) any later version.
10  	
11  	JNAerator is distributed in the hope that it will be useful,
12  	but WITHOUT ANY WARRANTY; without even the implied warranty of
13  	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  	GNU General Public License for more details.
15  	
16  	You should have received a copy of the GNU General Public License
17  	along with JNAerator.  If not, see <http://www.gnu.org/licenses/>.
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 }