001    /*
002            Copyright (c) 2009 Olivier Chafik, All Rights Reserved
003            
004            This file is part of JNAerator (http://jnaerator.googlecode.com/).
005            
006            JNAerator is free software: you can redistribute it and/or modify
007            it under the terms of the GNU General Public License as published by
008            the Free Software Foundation, either version 3 of the License, or
009            (at your option) any later version.
010            
011            JNAerator is distributed in the hope that it will be useful,
012            but WITHOUT ANY WARRANTY; without even the implied warranty of
013            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014            GNU General Public License for more details.
015            
016            You should have received a copy of the GNU General Public License
017            along with JNAerator.  If not, see <http://www.gnu.org/licenses/>.
018    */
019    package com.ochafik.beans;
020    import java.beans.BeanInfo;
021    import java.beans.IntrospectionException;
022    import java.beans.Introspector;
023    import java.beans.PropertyChangeEvent;
024    import java.beans.PropertyChangeListener;
025    import java.beans.PropertyDescriptor;
026    import java.lang.reflect.Method;
027    
028    import com.ochafik.util.string.StringUtils;
029    public class BeansUtils {
030            public static final void markTransientProperty(String name,Class<?> classe) throws IntrospectionException {       
031                    BeanInfo info = Introspector.getBeanInfo(classe);
032                    PropertyDescriptor[] propertyDescriptors =
033                                                 info.getPropertyDescriptors();
034                    for (int i = 0; i < propertyDescriptors.length; ++i) {
035                        PropertyDescriptor pd = propertyDescriptors[i];
036                        if (pd.getName().equals(name)) {
037                            pd.setValue("transient", Boolean.TRUE);
038                        }
039                    }
040            }
041    
042            public static void addPropertyChangeListener(Object bean, final String propertyName, final PropertyChangeListener listener) {
043                    if (bean == null)
044                            throw new NullPointerException("Null bean !");
045                    
046                    try {
047                            bean.getClass().getMethod("addPropertyChangeListener", String.class, PropertyChangeListener.class).invoke(bean, propertyName, listener);
048                    } catch (Exception ex) {
049                            try {
050                                    bean.getClass().getMethod("addPropertyChangeListener", PropertyChangeListener.class).invoke(bean, new PropertyChangeListener() {
051                                            
052                                            public void propertyChange(PropertyChangeEvent evt) {
053                                                    if (propertyName.equals(evt.getPropertyName()))
054                                                            listener.propertyChange(evt);
055                                            }
056                                    });
057                            } catch (Exception ex2) {
058                                    throw new RuntimeException(ex);
059                            }
060                    }
061            }
062            public static void removePropertyChangeListener(Object bean, String propertyName, PropertyChangeListener listener) {
063                    if (bean == null)
064                            throw new NullPointerException("Null bean !");
065                    
066                    try {
067                            bean.getClass().getMethod("removePropertyChangeListener", PropertyChangeListener.class).invoke(bean, listener);
068                    } catch (Exception ex) {
069                            throw new RuntimeException(ex);
070                    }
071            }
072            
073            public static Object get(Object bean, String propertyName) {
074                    return get(bean, propertyName, Object.class);
075            }
076                    
077            public static <T> T get(Object bean, String propertyName, Class<T> propertyClass) {
078                    if (bean == null)
079                            throw new NullPointerException("Null bean !");
080                    
081                    try {
082                            String cap  = StringUtils.capitalize(propertyName);
083                            Method m;
084                            if (propertyClass == Boolean.class) {
085                                    try {
086                                            m = bean.getClass().getMethod("is" + cap);
087                                    } catch (NoSuchMethodException ex) {
088                                            m = bean.getClass().getMethod("get" + cap);
089                                    }
090                            } else {
091                                    m = bean.getClass().getMethod("get" + cap);
092                            }
093                            return propertyClass.cast(m.invoke(bean));
094                    } catch (Exception ex) {
095                            throw new RuntimeException(ex);
096                    }
097            }
098            
099            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    }