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