001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2011 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.messaging.amf.io.util;
022    
023    import java.lang.reflect.Field;
024    import java.lang.reflect.Modifier;
025    import java.util.HashMap;
026    
027    import org.granite.util.TypeUtil;
028    import org.granite.util.Introspector;
029    import org.granite.util.PropertyDescriptor;
030    
031    /**
032     * @author Franck WOLFF
033     */
034    public class DefaultActionScriptClassDescriptor extends ActionScriptClassDescriptor {
035    
036        public DefaultActionScriptClassDescriptor(String type, byte encoding) {
037            super(type, encoding);
038        }
039    
040        @Override
041        public void defineProperty(String name) {
042    
043            if (type.length() == 0 || instantiator != null)
044                properties.add(new MapProperty(converters, name));
045            else {
046                try {
047                    Class<?> clazz = TypeUtil.forName(type);
048    
049                    // Try to find public getter/setter.
050                    PropertyDescriptor[] props = Introspector.getPropertyDescriptors(clazz);
051                    for (PropertyDescriptor prop : props) {
052                        if (name.equals(prop.getName()) && prop.getWriteMethod() != null && prop.getReadMethod() != null) {
053                            properties.add(new MethodProperty(converters, name, prop.getWriteMethod(), prop.getReadMethod()));
054                            return;
055                        }
056                    }
057    
058                    // Try to find public field.
059                    Field field = clazz.getField(name);
060                    if (!Modifier.isStatic(field.getModifiers()) && !Modifier.isTransient(field.getModifiers()))
061                        properties.add(new FieldProperty(converters, field));
062    
063                }
064                catch (NoSuchFieldException e) {
065                    if ("uid".equals(name)) // ObjectProxy specific property...
066                        properties.add(new UIDProperty(converters));
067                    else
068                            throw new RuntimeException(e);
069                }
070                catch (Exception e) {
071                    throw new RuntimeException(e);
072                }
073            }
074        }
075    
076        @Override
077        public Object newJavaInstance() {
078    
079            if (type.length() == 0)
080                return new HashMap<String, Object>();
081    
082            String className = (instantiator != null ? instantiator : type);
083            try {
084                return TypeUtil.newInstance(className);
085            } catch (Exception e) {
086                throw new RuntimeException("Could not create instance of: " + className, e);
087            }
088        }
089    }