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.As3Type;
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 As3Type as3Type;
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 if (provider == null || type == null)
066 throw new IllegalArgumentException("Parameter provider and type cannot be null");
067
068 this.provider = provider;
069 this.type = type;
070 this.url = url;
071 this.as3Type = provider.getAs3Type(type);
072 this.kind = provider.getKind(type);
073 this.generationType = provider.getGenerationType(kind, type);
074 }
075
076 ///////////////////////////////////////////////////////////////////////////
077 // Properties.
078
079 protected JavaTypeFactory getProvider() {
080 return provider;
081 }
082
083 public Class<?> getType() {
084 return type;
085 }
086
087 public String getName() {
088 if (type.isMemberClass())
089 return type.getEnclosingClass().getSimpleName() + '$' + type.getSimpleName();
090 return type.getSimpleName();
091 }
092
093 public Package getPackage() {
094 return type.getPackage();
095 }
096
097 public String getPackageName() {
098 return (type.getPackage() != null ? type.getPackage().getName() : "");
099 }
100
101 public String getQualifiedName() {
102 if (type.getPackage() == null)
103 return getName();
104 return new StringBuilder().append(getPackageName()).append('.').append(getName()).toString();
105 }
106
107 public URL getUrl() {
108 return url;
109 }
110
111 public boolean isBean() {
112 return kind == Kind.BEAN;
113 }
114
115 public boolean isEntity() {
116 return kind == Kind.ENTITY;
117 }
118
119 public boolean isEnum() {
120 return kind == Kind.ENUM;
121 }
122
123 public boolean isInterface() {
124 return kind == Kind.INTERFACE;
125 }
126
127 public boolean isRemoteDestination() {
128 return kind == Kind.REMOTE_DESTINATION;
129 }
130
131 public boolean isGenerated() {
132 return generationType != GenerationType.NOT_GENERATED;
133 }
134
135 public boolean isWithBase() {
136 return generationType == GenerationType.GENERATED_WITH_BASE;
137 }
138
139 public GenerationType getGenerationType() {
140 return generationType;
141 }
142
143 public Kind getKind() {
144 return kind;
145 }
146
147 public long getLastModified() {
148 if (lastModified == Long.MIN_VALUE) {
149 try {
150 lastModified = URIUtil.lastModified(url);
151 } catch (IOException e) {
152 lastModified = -1L;
153 }
154 }
155 return lastModified;
156 }
157
158 public As3Type getAs3Type() {
159 return as3Type;
160 }
161
162 ///////////////////////////////////////////////////////////////////////////
163 // Utility.
164
165 protected <T extends Collection<?>> T removeNull(T coll) {
166 coll.remove(null);
167 return coll;
168 }
169
170 protected PropertyDescriptor[] getPropertyDescriptors(Class<?> type) {
171 PropertyDescriptor[] propertyDescriptors = ClassUtil.getProperties(type);
172 return (propertyDescriptors != null ? propertyDescriptors : new PropertyDescriptor[0]);
173 }
174
175 protected List<JavaProperty> getSortedUnmodifiableList(Collection<JavaProperty> coll) {
176 List<JavaProperty> list = (coll instanceof List<?> ? (List<JavaProperty>)coll : new ArrayList<JavaProperty>(coll));
177 Collections.sort(list);
178 return Collections.unmodifiableList(list);
179 }
180 }