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