1 package spoon.template; 2 3 import java.util.List ; 4 5 import spoon.reflect.code.CtBlock; 6 import spoon.reflect.code.CtExpression; 7 import spoon.reflect.declaration.CtAnonymousExecutable; 8 import spoon.reflect.declaration.CtClass; 9 import spoon.reflect.declaration.CtConstructor; 10 import spoon.reflect.declaration.CtElement; 11 import spoon.reflect.declaration.CtExecutable; 12 import spoon.reflect.declaration.CtField; 13 import spoon.reflect.declaration.CtInterface; 14 import spoon.reflect.declaration.CtMethod; 15 import spoon.reflect.declaration.CtSimpleType; 16 import spoon.reflect.declaration.CtType; 17 import spoon.reflect.reference.CtPackageReference; 18 import spoon.reflect.reference.CtTypeReference; 19 import spoon.reflect.visitor.Query; 20 import spoon.support.query.ReferenceTypeFilter; 21 import spoon.support.template.Parameters; 22 import spoon.support.template.SubstitutionVisitor; 23 24 27 public abstract class Substitution { 28 29 private Substitution() { 30 } 31 32 44 @SuppressWarnings ("unchecked") 45 public static void insertAll(CtType<?> targetType, Template template) { 46 47 CtClass<? extends Template> sourceClass = targetType.getFactory() 48 .Template().get(template.getClass()); 49 for (CtTypeReference<?> t : sourceClass.getSuperInterfaces()) { 51 if (!t.equals(targetType.getFactory().Type().createReference( 52 Template.class))) { 53 CtTypeReference<?> t1 = t; 54 if (Parameters.getNames(sourceClass) 56 .contains(t.getSimpleName())) { 57 Object o = Parameters.getValue(template, t.getSimpleName(), 58 null); 59 if (o instanceof CtTypeReference) { 60 t1 = (CtTypeReference) o; 61 } else if (o instanceof Class ) { 62 t1 = targetType.getFactory().Type().createReference( 63 (Class ) o); 64 } else if (o instanceof String ) { 65 t1 = targetType.getFactory().Type().createReference( 66 (String ) o); 67 } 68 } 69 if(!t1.equals(targetType.getReference())) { 70 Class c=t1.getActualClass(); 71 if(c!=null && c.isInterface()) { 72 targetType.getSuperInterfaces().add(t1); 73 } 74 if(c==null) { 75 targetType.getSuperInterfaces().add(t1); 76 } 77 } 78 } 79 } 80 for (CtMethod<?> m : sourceClass.getMethods()) { 82 if (m.getAnnotation(Local.class) != null) 83 continue; 84 if (m.getAnnotation(Parameter.class) != null) 85 continue; 86 insertMethod(targetType, template, m); 87 } 88 if (targetType instanceof CtClass) { 90 for (CtConstructor c : sourceClass.getConstructors()) { 91 if (c.isImplicitDefault()) 92 continue; 93 if (c.getAnnotation(Local.class) != null) 94 continue; 95 insertConstructor((CtClass<?>) targetType, template, c); 96 } 97 } 98 if (targetType instanceof CtClass) { 100 for (CtAnonymousExecutable e : sourceClass 101 .getAnonymousExecutables()) { 102 ((CtClass<?>) targetType).getAnonymousExecutables().add( 103 substitute(targetType, template, e)); 104 } 105 } 106 for (CtField<?> f : sourceClass.getFields()) { 108 if (f.getAnnotation(Local.class) != null) 109 continue; 110 if (Parameters.isParameterSource(f.getReference())) 111 continue; 112 113 insertField(targetType, template, f); 114 } 115 for (CtSimpleType<?> t : sourceClass.getNestedTypes()) { 117 if (t.getAnnotation(Local.class) != null) 118 continue; 119 CtSimpleType<?> result = substitute(sourceClass, template, t); 120 targetType.getNestedTypes().add(result); 121 result.setParent(targetType); 122 } 123 124 } 125 126 135 @SuppressWarnings ("unchecked") 136 public static void insertAllSuperInterfaces(CtType<?> targetType, 137 Template template) { 138 139 CtClass<? extends Template> sourceClass = targetType.getFactory() 140 .Template().get(template.getClass()); 141 for (CtTypeReference<?> t : sourceClass.getSuperInterfaces()) { 143 if (!t.equals(targetType.getFactory().Type().createReference( 144 Template.class))) { 145 CtTypeReference<?> t1 = t; 146 if (Parameters.getNames(sourceClass) 148 .contains(t.getSimpleName())) { 149 Object o = Parameters.getValue(template, t.getSimpleName(), 150 null); 151 if (o instanceof CtTypeReference) { 152 t1 = (CtTypeReference) o; 153 } else if (o instanceof Class ) { 154 t1 = targetType.getFactory().Type().createReference( 155 (Class ) o); 156 } else if (o instanceof String ) { 157 t1 = targetType.getFactory().Type().createReference( 158 (String ) o); 159 } 160 } 161 if(!t1.equals(targetType.getReference())) { 162 Class c=t1.getActualClass(); 163 if(c!=null && c.isInterface()) { 164 targetType.getSuperInterfaces().add(t1); 165 } 166 if(c==null) { 167 targetType.getSuperInterfaces().add(t1); 168 } 169 } 170 } 171 } 172 } 173 174 184 @SuppressWarnings ("unchecked") 185 public static void insertAllMethods(CtType<?> targetType, Template template) { 186 187 CtClass<?> sourceClass = targetType.getFactory().Template().get( 188 template.getClass()); 189 for (CtMethod<?> m : sourceClass.getMethods()) { 191 if (m.getAnnotation(Local.class) != null) 192 continue; 193 if (m.getAnnotation(Parameter.class) != null) 194 continue; 195 insertMethod(targetType, template, m); 196 } 197 } 198 199 209 @SuppressWarnings ("unchecked") 210 public static void insertAllFields(CtType<?> targetType, Template template) { 211 212 CtClass<?> sourceClass = targetType.getFactory().Template().get( 213 template.getClass()); 214 for (CtField<?> f : sourceClass.getFields()) { 216 if (f.getAnnotation(Local.class) != null) 217 continue; 218 if (Parameters.isParameterSource(f.getReference())) 219 continue; 220 221 insertField(targetType, template, f); 222 } 223 } 224 225 236 @SuppressWarnings ("unchecked") 237 public static void insertAllConstructors(CtType<?> targetType, 238 Template template) { 239 240 CtClass<?> sourceClass = targetType.getFactory().Template().get( 241 template.getClass()); 242 if (targetType instanceof CtClass) { 244 for (CtConstructor c : sourceClass.getConstructors()) { 245 if (c.isImplicitDefault()) 246 continue; 247 if (c.getAnnotation(Local.class) != null) 248 continue; 249 insertConstructor((CtClass<?>) targetType, template, c); 250 } 251 } 252 if (targetType instanceof CtClass) { 254 for (CtAnonymousExecutable e : sourceClass 255 .getAnonymousExecutables()) { 256 ((CtClass<?>) targetType).getAnonymousExecutables().add( 257 substitute(targetType, template, e)); 258 } 259 } 260 } 261 262 275 public static CtConstructor insertConstructor(CtClass<?> targetClass, 276 Template template, CtMethod<Void > sourceMethod) { 277 278 if (targetClass instanceof CtInterface) 279 return null; 280 CtConstructor newConstructor = targetClass.getFactory().Constructor() 281 .create(targetClass, sourceMethod); 282 newConstructor = substitute(targetClass, template, newConstructor); 283 targetClass.getConstructors().add(newConstructor); 284 newConstructor.setParent(targetClass); 285 return newConstructor; 286 } 287 288 301 public static <T> CtMethod<T> insertMethod(CtType<?> targetType, 302 Template template, CtMethod<T> sourceMethod) { 303 304 CtMethod<T> newMethod = substitute(targetType, template, sourceMethod); 305 if (targetType instanceof CtInterface) 306 newMethod.setBody(null); 307 targetType.getMethods().add(newMethod); 308 newMethod.setParent(targetType); 309 return newMethod; 310 } 311 312 325 public static <T> CtConstructor insertConstructor(CtClass<?> targetClass, 326 Template template, CtConstructor sourceConstructor) { 327 328 CtConstructor newConstrutor = substitute(targetClass, template, 329 sourceConstructor); 330 newConstrutor.setParent(targetClass); 331 targetClass.getConstructors().add(newConstrutor); 332 return newConstrutor; 333 } 334 335 350 351 public static CtBlock<?> substituteMethodBody(CtClass<?> targetClass, 352 Template template, String executableName, 353 CtTypeReference<?>... parameterTypes) { 354 CtClass<?> sourceClass = targetClass.getFactory().Template().get( 355 template.getClass()); 356 CtExecutable<?> sourceExecutable = executableName.equals(template 357 .getClass().getSimpleName()) ? sourceClass 358 .getConstructor(parameterTypes) : sourceClass.getMethod( 359 executableName, parameterTypes); 360 return substitute(targetClass, template, sourceExecutable.getBody()); 361 } 362 363 376 377 public static CtExpression<?> substituteFieldDefaultExpression( 378 CtSimpleType<?> targetType, Template template, String fieldName) { 379 CtClass<?> sourceClass = targetType.getFactory().Template().get( 380 template.getClass()); 381 CtField<?> sourceField = sourceClass.getField(fieldName); 382 return substitute(targetType, template, sourceField 383 .getDefaultExpression()); 384 } 385 386 398 public static <E extends CtElement> E substitute( 399 CtSimpleType<?> targetType, Template template, E code) { 400 if (code == null) 401 return null; 402 if (targetType == null) 403 throw new RuntimeException ("target is null in substitution"); 404 E result = targetType.getFactory().Core().clone(code); 405 new SubstitutionVisitor(targetType.getFactory(), targetType, template) 406 .scan(result); 407 return result; 408 } 409 410 423 449 public static <T extends CtSimpleType> T substitute(Template template, 450 T templateType) { 451 T result = templateType.getFactory().Core().clone(templateType); 452 result.setParent(templateType.getParent()); 453 new SubstitutionVisitor(templateType.getFactory(), result, template) 454 .scan(result); 455 return result; 456 } 457 458 472 public static <T> CtField<T> insertField(CtType<?> targetType, 473 Template template, CtField<T> sourceField) { 474 CtField<T> field = substitute(targetType, template, sourceField); 475 targetType.getFields().add(field); 476 field.setParent(targetType); 477 return field; 478 } 479 480 484 public static void redirectTypeReferences(CtElement element, 485 CtTypeReference<?> source, CtTypeReference<?> target) { 486 487 List <CtTypeReference> refs = Query 488 .getReferences(element, 489 new ReferenceTypeFilter<CtTypeReference>( 490 CtTypeReference.class)); 491 492 String srcName = source.getQualifiedName(); 493 String targetName = target.getSimpleName(); 494 CtPackageReference targetPackage = target.getPackage(); 495 496 for (CtTypeReference ref : refs) { 497 if (ref.getQualifiedName().equals(srcName)) { 498 ref.setSimpleName(targetName); 499 ref.setPackage(targetPackage); 500 } 501 } 502 } 503 } 504 | Popular Tags |