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