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.io.Serializable;
025import java.lang.reflect.Field;
026import java.lang.reflect.Modifier;
027import java.lang.reflect.Proxy;
028import java.util.ArrayList;
029import java.util.List;
030
031/**
032 * @author Franck WOLFF
033 */
034public class DefaultClassGetter implements ClassGetter {
035
036    public Class<?> getClass(Object o) {
037        if (o == null)
038            return null;
039
040        Class<?> clazz = o.getClass();
041        if (Proxy.isProxyClass(clazz))
042            clazz = clazz.getInterfaces()[0];
043        
044        if (Enum.class.isAssignableFrom(clazz) && clazz.isAnonymousClass() && Enum.class.isAssignableFrom(clazz.getSuperclass()))
045                clazz = clazz.getSuperclass();
046
047        return clazz;
048    }
049    
050    public boolean isEntity(Object o) {
051        return false;
052    }
053    
054    public Serializable getIdentifier(Object o) {
055        return null;
056    }
057    
058    public boolean isInitialized(Object owner, String propertyName, Object propertyValue) {
059        return true;
060    }
061    
062    public void initialize(Object owner, String propertyName, Object propertyValue) {
063    }
064
065    public List<Object[]> getFieldValues(Object obj) {
066        return getFieldValues(obj, null);
067    }
068
069    public List<Object[]> getFieldValues(Object obj, Object dest) {
070        return defaultGetFieldValues(obj, dest);
071    }
072    
073    public static List<Object[]> defaultGetFieldValues(Object obj, Object dest) {
074        List<Object[]> fieldValues = new ArrayList<Object[]>();
075        
076        // Merges field values
077        try {
078            Class<?> clazz = obj.getClass();
079            while (clazz != null) {
080                Field[] fields = clazz.getDeclaredFields();
081                for (Field field : fields) {
082                    if ((field.getModifiers() & Modifier.STATIC) != 0 
083                        || (field.getModifiers() & Modifier.FINAL) != 0 
084                        || (field.getModifiers() & Modifier.VOLATILE) != 0 
085                        || (field.getModifiers() & Modifier.NATIVE) != 0 
086                        || (field.getModifiers() & Modifier.TRANSIENT) != 0)
087                        continue;
088                    field.setAccessible(true);
089                    Object o = field.get(obj);
090                    if (dest != null) {
091                        Object d = field.get(dest);
092                        fieldValues.add(new Object[] { field, o, d });
093                    }
094                    else
095                        fieldValues.add(new Object[] { field, o });
096                }
097                clazz = clazz.getSuperclass();
098            }
099        }
100        catch (Exception e) {
101            throw new RuntimeException("Could not get entity fields ", e);
102        }
103        
104        return fieldValues;
105    }
106}