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