001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2012 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.client.tide.impl;
022    
023    import java.lang.reflect.Method;
024    
025    import org.granite.client.tide.BeanManager;
026    import org.granite.logging.Logger;
027    
028    /**
029     * @author William DRAI
030     */
031    public class SimpleBeanManager implements BeanManager {
032        
033        @SuppressWarnings("unused")
034            private static final Logger log = Logger.getLogger(SimpleBeanManager.class);
035    
036        @Override
037        public void setProperty(Object bean, String propertyName, Object value) {
038            try {
039                    boolean found = false;
040                    for (Method m : bean.getClass().getMethods()) {
041                            if (m.getName().equals("set" + propertyName.substring(0, 1).toString() + propertyName.substring(1))
042                                            && m.getParameterTypes().length == 1 && m.getParameterTypes()[0].isInstance(value)) {
043                                    m.invoke(bean, value);
044                                    found = true;
045                                    break;
046                            }
047                    }
048                    if (!found)
049                            throw new RuntimeException("Could not find setter for bean property " + bean + "." + propertyName);
050            }
051            catch (Exception e) {
052                throw new RuntimeException("Could not write bean property " + bean + "." + propertyName, e);
053            }
054        }
055    
056        @Override
057        public Object getProperty(Object bean, String propertyName) {
058            try {
059                    for (Method m : bean.getClass().getMethods()) {
060                            if ((m.getName().equals("get" + propertyName.substring(0, 1).toString() + propertyName.substring(1))
061                                            || m.getName().equals("is" + propertyName.substring(0, 1).toString() + propertyName.substring(1)))
062                                            && m.getParameterTypes().length == 0) {
063                                    return m.invoke(bean);
064                            }
065                    }
066                    throw new RuntimeException("Could not find getter for bean property " + bean + "." + propertyName);
067            }
068            catch (Exception e) {
069                throw new RuntimeException("Could not read bean property " + bean + "." + propertyName, e);
070            }
071        }
072    
073    }