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