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.ArrayList; 027import java.util.HashSet; 028import java.util.Hashtable; 029import java.util.List; 030import java.util.Map; 031import java.util.Set; 032 033import org.granite.util.Introspector; 034import org.granite.util.PropertyDescriptor; 035 036/** 037 * @author Franck WOLFF 038 */ 039public class DefaultGroovyClassDescriptor extends JavaClassDescriptor { 040 041 public DefaultGroovyClassDescriptor(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 Introspector.flushFromCaches(type); // Ensure that we don't get a cached reference, the Groovy class could have been modified 058 PropertyDescriptor[] descs = Introspector.getPropertyDescriptors(type); 059 for (PropertyDescriptor property : descs) { 060 String propertyName = property.getName(); 061 if (property.getWriteMethod() != null && property.getReadMethod() != null) { 062 properties.add(new MethodProperty(converters, propertyName, property.getWriteMethod(), property.getReadMethod())); 063 propertyNames.add(propertyName); 064 } 065 } 066 067 // Add other public fields. 068 Field[] fields = type.getFields(); 069 for (Field field : fields) { 070 String propertyName = field.getName(); 071 if (!propertyNames.contains(propertyName) && 072 !Modifier.isStatic(field.getModifiers()) && 073 !Modifier.isTransient(field.getModifiers())) { 074 properties.add(new FieldProperty(converters, field)); 075 propertyNames.add(propertyName); 076 } 077 } 078 } 079 catch (RuntimeException e) { 080 throw e; 081 } 082 catch (Exception e) { 083 throw new RuntimeException(e); 084 } 085 } 086 087 return properties; 088 } 089}