1 package spoon.reflect.factory; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 6 import spoon.reflect.Factory; 7 import spoon.reflect.declaration.CtElement; 8 import spoon.reflect.declaration.CtPackage; 9 import spoon.reflect.declaration.CtSimpleType; 10 import spoon.reflect.declaration.CtType; 11 import spoon.reflect.declaration.CtTypeParameter; 12 import spoon.reflect.reference.CtArrayTypeReference; 13 import spoon.reflect.reference.CtTypeParameterReference; 14 import spoon.reflect.reference.CtTypeReference; 15 16 19 public class TypeFactory extends SubFactory { 20 21 private static final long serialVersionUID = 1L; 22 23 29 public TypeFactory(Factory factory) { 30 super(factory); 31 } 32 33 41 public <T> CtArrayTypeReference<T[]> createArrayReference(CtSimpleType<T> type) { 42 CtArrayTypeReference<T[]> array = factory.Core() 43 .createArrayTypeReference(); 44 array.setComponentType(createReference(type)); 45 return array; 46 } 47 48 51 public <T> CtArrayTypeReference<T[]> createArrayReference( 52 CtTypeReference<T> reference) { 53 CtArrayTypeReference<T[]> array = factory.Core() 54 .createArrayTypeReference(); 55 array.setComponentType(reference); 56 return array; 57 } 58 59 62 @SuppressWarnings ("unchecked") 63 public <T> CtArrayTypeReference<T> createArrayReference(String qualifiedName) { 64 CtArrayTypeReference<T> array = factory.Core() 65 .createArrayTypeReference(); 66 array 67 .setComponentType((CtTypeReference<T>) createReference(qualifiedName)); 68 return array; 69 } 70 71 74 public <T> CtTypeReference<T> createReference(Class <T> type) { 75 if (type.isArray()) { 76 CtArrayTypeReference<T> array = factory.Core() 77 .createArrayTypeReference(); 78 array.setComponentType((CtTypeReference<?>) createReference(type 79 .getComponentType())); 80 return array; 81 } 82 return createReference(type.getName()); 83 } 84 85 88 public <T> CtTypeReference<T> createReference(CtSimpleType<T> type) { 89 CtTypeReference<T> ref = factory.Core().createTypeReference(); 90 if (type.isTopLevel()) { 91 ref 92 .setPackage(factory.Package().createReference( 93 type.getPackage())); 94 } else { 95 if (type.getDeclaringType() != null) { 96 ref.setDeclaringType(createReference(type.getDeclaringType())); 97 } 98 } 99 ref.setSimpleName(type.getSimpleName()); 100 return ref; 101 } 102 103 106 @SuppressWarnings ("unchecked") 107 public <T> CtTypeReference<T> createReference(String qualifiedName) { 108 if(qualifiedName.endsWith("[]")) { 109 return createArrayReference(qualifiedName.substring(0,qualifiedName.length()-2)); 110 } 111 CtTypeReference ref = factory.Core().createTypeReference(); 112 if (hasInnerType(qualifiedName) > 0) { 113 ref 114 .setDeclaringType(createReference(getDeclaringTypeName(qualifiedName))); 115 } else if (hasPackage(qualifiedName) > 0) { 116 ref.setPackage(factory.Package().createReference( 117 getPackageName(qualifiedName))); 118 } 119 ref.setSimpleName(getSimpleName(qualifiedName)); 120 return ref; 121 } 122 123 128 @SuppressWarnings ("unchecked") 129 public <T> CtSimpleType<T> get(String qualifiedName) { 130 int inertTypeIndex = qualifiedName 131 .lastIndexOf(CtType.INNERTTYPE_SEPARATOR); 132 if (inertTypeIndex > 0) { 133 String s = qualifiedName.substring(0, inertTypeIndex); 134 CtSimpleType t = (CtSimpleType<?>) get(s); 135 if (t == null) 136 return null; 137 return t.getNestedType(qualifiedName.substring(inertTypeIndex + 1)); 138 } 139 140 int packageIndex = qualifiedName 141 .lastIndexOf(CtPackage.PACKAGE_SEPARATOR); 142 CtPackage pack; 143 if (packageIndex > 0) 144 pack = factory.Package().get( 145 qualifiedName.substring(0, packageIndex)); 146 else 147 pack = factory.Package().get( 148 CtPackage.TOP_LEVEL_PACKAGE_NAME); 149 150 if (pack == null) 151 return null; 152 153 return (CtSimpleType<T>)pack.getType(qualifiedName.substring(packageIndex + 1)); 154 } 155 156 159 public List <CtSimpleType> getAll() { 160 List <CtSimpleType> types = new ArrayList <CtSimpleType>(); 161 for (CtPackage pack : factory.Package().getAll()) { 162 types.addAll(pack.getTypes()); 163 } 164 return types; 165 } 166 167 175 @SuppressWarnings ("unchecked") 176 public <T> CtSimpleType<T> get(Class <T> cl) { 177 return (CtSimpleType<T>)get(cl.getName()); 178 } 179 180 181 184 protected String getDeclaringTypeName(String qualifiedName) { 185 return qualifiedName.substring(0, hasInnerType(qualifiedName)); 186 } 187 188 191 public List <CtTypeReference<?>> createReferences(List <Class <?>> classes) { 192 List <CtTypeReference<?>> refs = new ArrayList <CtTypeReference<?>>(); 193 for (Class <?> c : classes) { 194 refs.add(createReference(c)); 195 } 196 return refs; 197 } 198 199 202 protected String getPackageName(String qualifiedName) { 203 if (hasPackage(qualifiedName) >= 0) 204 return qualifiedName.substring(0, hasPackage(qualifiedName)); 205 else 206 return CtPackage.TOP_LEVEL_PACKAGE_NAME; 207 } 208 209 212 protected String getSimpleName(String qualifiedName) { 213 if (hasInnerType(qualifiedName) > 0) { 214 return qualifiedName.substring(hasInnerType(qualifiedName) + 1); 215 } else if (hasPackage(qualifiedName) > 0) { 216 return qualifiedName.substring(hasPackage(qualifiedName) + 1); 217 } else { 218 return qualifiedName; 219 } 220 } 221 222 225 protected int hasInnerType(String qualifiedName) { 226 int ret = qualifiedName.lastIndexOf(CtType.INNERTTYPE_SEPARATOR); 227 return ret; 238 } 239 240 243 protected int hasPackage(String qualifiedName) { 244 return qualifiedName.lastIndexOf(CtPackage.PACKAGE_SEPARATOR); 245 } 246 247 255 public CtTypeParameter createTypeParameter(CtElement owner, String name) { 256 CtTypeParameter typeParam = factory.Core().createTypeParameter(); 257 typeParam.setParent(owner); 258 typeParam.setName(name); 259 return typeParam; 260 } 261 262 272 public CtTypeParameter createTypeParameter(CtElement owner, String name, 273 List <CtTypeReference> bounds) { 274 CtTypeParameter typeParam = factory.Core().createTypeParameter(); 275 typeParam.setParent(owner); 276 typeParam.setName(name); 277 typeParam.setBounds(bounds); 278 return typeParam; 279 } 280 281 287 public CtTypeParameterReference createTypeParameterReference(String name) { 288 CtTypeParameterReference typeParam = factory.Core() 289 .createTypeParameterReference(); 290 typeParam.setSimpleName(name); 291 return typeParam; 292 } 293 294 302 public CtTypeParameterReference createTypeParameterReference(String name, 303 List <CtTypeReference> bounds) { 304 CtTypeParameterReference typeParam = factory.Core() 305 .createTypeParameterReference(); 306 typeParam.setSimpleName(name); 307 typeParam.setBounds(bounds); 308 return typeParam; 309 } 310 311 } 312 | Popular Tags |