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.io.IOException;
025    import java.net.URL;
026    import java.util.ArrayList;
027    import java.util.Collection;
028    import java.util.Collections;
029    import java.util.List;
030    
031    import org.granite.generator.as3.ClientType;
032    import org.granite.util.ClassUtil;
033    import org.granite.util.URIUtil;
034    
035    /**
036     * @author Franck WOLFF
037     */
038    public abstract class JavaAbstractType implements JavaType {
039    
040        ///////////////////////////////////////////////////////////////////////////
041        // Generation type.
042            
043            public static enum GenerationType {
044                    NOT_GENERATED,
045                    GENERATED_SINGLE,
046                    GENERATED_WITH_BASE
047            }
048    
049        ///////////////////////////////////////////////////////////////////////////
050        // Fields.
051    
052        protected final JavaTypeFactory provider;
053        protected final Class<?> type;
054        protected final URL url;
055        protected final ClientType clientType;
056        protected final Kind kind;
057        protected final GenerationType generationType;
058    
059        private long lastModified = Long.MIN_VALUE;
060    
061        ///////////////////////////////////////////////////////////////////////////
062        // Constructor.
063    
064        protected JavaAbstractType(JavaTypeFactory provider, Class<?> type, URL url) {
065            this(provider, type, url, false);
066        }
067        
068        protected JavaAbstractType(JavaTypeFactory provider, Class<?> type, URL url, boolean property) {
069            if (provider == null || type == null)
070                throw new IllegalArgumentException("Parameter provider and type cannot be null");
071    
072            this.provider = provider;
073            this.type = type;
074            this.url = url;
075            this.clientType = provider.getClientType(type, null, null, property);
076            this.kind = provider.getKind(type);
077            this.generationType = provider.getGenerationType(kind, type);
078        }
079    
080        ///////////////////////////////////////////////////////////////////////////
081        // Properties.
082    
083        protected JavaTypeFactory getProvider() {
084            return provider;
085        }
086    
087        @Override
088            public Class<?> getType() {
089            return type;
090        }
091    
092            @Override
093            public String getName() {
094            if (type.isMemberClass())
095                    return type.getEnclosingClass().getSimpleName() + '$' + type.getSimpleName();
096            return type.getSimpleName();
097        }
098    
099        @Override
100            public Package getPackage() {
101            return type.getPackage();
102        }
103    
104        @Override
105            public String getPackageName() {
106            return (type.getPackage() != null ? type.getPackage().getName() : "");
107        }
108    
109        public String getQualifiedName() {
110            if (type.getPackage() == null)
111                return getName();
112            return new StringBuilder().append(getPackageName()).append('.').append(getName()).toString();
113        }
114    
115        @Override
116            public URL getUrl() {
117            return url;
118        }
119    
120        @Override
121            public boolean isBean() {
122                    return kind == Kind.BEAN;
123            }
124    
125            @Override
126            public boolean isEntity() {
127                    return kind == Kind.ENTITY;
128            }
129    
130            @Override
131            public boolean isEnum() {
132                    return kind == Kind.ENUM;
133            }
134    
135            @Override
136            public boolean isInterface() {
137                    return kind == Kind.INTERFACE;
138            }
139            
140            @Override
141            public boolean isRemoteDestination() {
142                    return kind == Kind.REMOTE_DESTINATION;
143            }
144    
145            @Override
146            public boolean isGenerated() {
147                    return generationType != GenerationType.NOT_GENERATED;
148            }
149    
150            @Override
151            public boolean isWithBase() {
152                    return generationType == GenerationType.GENERATED_WITH_BASE;
153            }
154    
155            @Override
156            public GenerationType getGenerationType() {
157                    return generationType;
158            }
159    
160            @Override
161            public Kind getKind() {
162                    return kind;
163            }
164    
165            @Override
166            public long getLastModified() {
167            if (lastModified == Long.MIN_VALUE) {
168                try {
169                    lastModified = URIUtil.lastModified(url);
170                } catch (IOException e) {
171                    lastModified = -1L;
172                }
173            }
174            return lastModified;
175        }
176    
177        public ClientType getAs3Type() {
178            return clientType;
179        }
180    
181        @Override
182            public ClientType getClientType() {
183            return clientType;
184        }
185    
186        ///////////////////////////////////////////////////////////////////////////
187        // Utility.
188    
189        protected <T extends Collection<?>> T removeNull(T coll) {
190            coll.remove(null);
191            return coll;
192        }
193    
194        protected PropertyDescriptor[] getPropertyDescriptors(Class<?> type) {
195            PropertyDescriptor[] propertyDescriptors = ClassUtil.getProperties(type);
196            return (propertyDescriptors != null ? propertyDescriptors : new PropertyDescriptor[0]);
197        }
198    
199        protected List<JavaProperty> getSortedUnmodifiableList(Collection<JavaProperty> coll) {
200            List<JavaProperty> list = (coll instanceof List<?> ? (List<JavaProperty>)coll : new ArrayList<JavaProperty>(coll));
201            Collections.sort(list);
202            return Collections.unmodifiableList(list);
203        }
204    }