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