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.generator.as3.reflect;
022    
023    import java.beans.PropertyDescriptor;
024    import java.lang.reflect.Method;
025    import java.net.URL;
026    import java.util.ArrayList;
027    import java.util.Collection;
028    import java.util.Collections;
029    import java.util.HashSet;
030    import java.util.List;
031    import java.util.Set;
032    
033    import org.granite.generator.as3.reflect.JavaMethod.MethodType;
034    
035    /**
036     * @author Franck WOLFF
037     */
038    public class JavaInterface extends JavaAbstractType {
039    
040        protected final Set<JavaType> imports;
041    
042        protected final List<JavaInterface> interfaces;
043    
044        protected final List<JavaProperty> properties;
045    
046        public JavaInterface(JavaTypeFactory provider, Class<?> type, URL url) {
047            super(provider, type, url);
048    
049            if (!type.isInterface())
050                throw new IllegalArgumentException("type should be an interface: " + type);
051    
052            // Find superclass (controller filtered).
053            this.interfaces = Collections.unmodifiableList(provider.getJavaTypeInterfaces(type));
054    
055            // Collect bean properties.
056            this.properties = getSortedUnmodifiableList(initProperties());
057    
058            // Collect imports.
059            Set<JavaType> tmpImports = new HashSet<JavaType>();
060            for (JavaInterface interfaze : interfaces)
061                    tmpImports.add(provider.getJavaImport(interfaze.getType()));
062            for (JavaProperty property : properties)
063                tmpImports.add(provider.getJavaImport(property.getType()));
064            this.imports = Collections.unmodifiableSet(removeNull(tmpImports));
065        }
066    
067        public Set<JavaType> getImports() {
068            return imports;
069        }
070    
071        public boolean hasSuperInterfaces() {
072            return interfaces != null && !interfaces.isEmpty();
073        }
074        public List<JavaInterface> getSuperInterfaces() {
075            return interfaces;
076        }
077    
078        public Collection<JavaProperty> getProperties() {
079            return properties;
080        }
081    
082        protected Collection<JavaProperty> initProperties() {
083            // Find (non static, non transient) declared fields or getter/setter for interfaces.
084            List<JavaProperty> properties = new ArrayList<JavaProperty>();
085            for (PropertyDescriptor propertyDescriptor : getPropertyDescriptors(getType())) {
086                String name = propertyDescriptor.getName();
087                JavaMethod readMethod = null;
088                JavaMethod writeMethod = null;
089    
090                Method method = propertyDescriptor.getReadMethod();
091                if (method != null)
092                    readMethod = new JavaMethod(method, MethodType.GETTER);
093    
094                method = propertyDescriptor.getWriteMethod();
095                if (method != null)
096                    writeMethod = new JavaMethod(method, MethodType.SETTER);
097    
098                if (readMethod != null || writeMethod != null)
099                    properties.add(new JavaMethodProperty(provider, name, readMethod, writeMethod));
100            }
101            return properties;
102        }
103    }