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