001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004     *
005     *   This file is part of the Granite Data Services Platform.
006     *
007     *   Granite Data Services is free software; you can redistribute it and/or
008     *   modify it under the terms of the GNU Lesser General Public
009     *   License as published by the Free Software Foundation; either
010     *   version 2.1 of the License, or (at your option) any later version.
011     *
012     *   Granite Data Services is distributed in the hope that it will be useful,
013     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015     *   General Public License for more details.
016     *
017     *   You should have received a copy of the GNU Lesser General Public
018     *   License along with this library; if not, write to the Free Software
019     *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020     *   USA, or see <http://www.gnu.org/licenses/>.
021     */
022    package org.granite.messaging.amf.io.util;
023    
024    import java.lang.reflect.Field;
025    import java.lang.reflect.Modifier;
026    import java.util.ArrayList;
027    import java.util.HashSet;
028    import java.util.Hashtable;
029    import java.util.List;
030    import java.util.Map;
031    import java.util.Set;
032    
033    import org.granite.util.Introspector;
034    import org.granite.util.PropertyDescriptor;
035    
036    /**
037     * @author Franck WOLFF
038     */
039    public class DefaultJavaClassDescriptor extends JavaClassDescriptor {
040    
041        public DefaultJavaClassDescriptor(Class<?> type) {
042            super(type);
043        }
044    
045        @Override
046        protected List<Property> introspectProperties() {
047            List<Property> properties = null;
048            Class<?> type = getType();
049    
050            if (!isExternalizable() && !Map.class.isAssignableFrom(type) && !Hashtable.class.isAssignableFrom(type)) {
051                properties = new ArrayList<Property>();
052    
053                try {
054                    Set<String> propertyNames = new HashSet<String>();
055    
056                    // Add read/write properties (ie: public getter/setter).
057                    PropertyDescriptor[] descs = Introspector.getPropertyDescriptors(type);
058                    for (PropertyDescriptor property : descs) {
059                        String propertyName = property.getName();
060                        if (property.getWriteMethod() != null && property.getReadMethod() != null) {
061                            properties.add(new MethodProperty(converters, propertyName, property.getWriteMethod(), property.getReadMethod()));
062                            propertyNames.add(propertyName);
063                        }
064                    }
065    
066                    // Add other public fields.
067                    Field[] fields = type.getFields();
068                    for (Field field : fields) {
069                        String propertyName = field.getName();
070                        if (!propertyNames.contains(propertyName) &&
071                            !Modifier.isStatic(field.getModifiers()) &&
072                            !Modifier.isTransient(field.getModifiers())) {
073                            properties.add(new FieldProperty(converters, field));
074                            propertyNames.add(propertyName);
075                        }
076                    }
077                }
078                catch (RuntimeException e) {
079                    throw e;
080                }
081                catch (Exception e) {
082                    throw new RuntimeException(e);
083                }
084            }
085    
086            return properties;
087        }
088    }