1 package spoon.examples.delegate.template; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 import java.util.TreeSet ; 6 7 import spoon.reflect.Factory; 8 import spoon.reflect.declaration.CtClass; 9 import spoon.reflect.declaration.CtMethod; 10 import spoon.reflect.declaration.CtParameter; 11 import spoon.reflect.reference.CtTypeReference; 12 import spoon.template.Local; 13 import spoon.template.Parameter; 14 import spoon.template.Substitution; 15 import spoon.template.Template; 16 17 public class DelegateTemplate implements Template { 18 19 @Parameter 20 CtTypeReference _DelegateType_; 21 22 @Parameter 23 CtTypeReference _ReturnType_; 24 25 @Parameter 26 String _delegate_; 27 28 @Parameter 29 String _voidDelegate_; 30 31 @Parameter 32 List <CtParameter> _args_; 33 34 @Local 35 public DelegateTemplate(CtTypeReference<?> delegateType) { 36 this._DelegateType_ = delegateType; 37 } 38 39 @Local 40 public CtMethod<?> getDelegateMethod(CtClass<?> target, CtMethod<?> method) { 41 Factory f = target.getFactory(); 42 boolean voidMethod = method.getType().equals( 43 f.Type().createReference(void.class)); 44 if (voidMethod) { 45 _voidDelegate_ = method.getSimpleName(); 46 } else { 47 this._ReturnType_ = method.getType(); 48 _delegate_ = method.getSimpleName(); 49 } 50 _args_ = new ArrayList <CtParameter>(method.getParameters()); 51 CtMethod<?> delegateMethod; 52 if (voidMethod) { 53 delegateMethod = Substitution.substitute(target, this, f.Template() 54 .get(DelegateTemplate.class).getMethod( 55 "_voidDelegate_", 56 f.Type().createReference(List .class))); 57 } else { 58 delegateMethod = Substitution.substitute(target, this, f.Template() 59 .get(DelegateTemplate.class).getMethod( 60 "_delegate_", f.Type().createReference(List .class))); 61 } 62 delegateMethod 63 .setThrownTypes(new TreeSet <CtTypeReference<? extends Throwable >>( 64 method.getThrownTypes())); 65 return delegateMethod; 66 } 67 68 _DelegateType_ delegate; 69 70 public DelegateTemplate(_DelegateType_ delegate) { 71 this.delegate = delegate; 72 } 73 74 @Local 75 public _ReturnType_ _delegate_(List <CtParameter> _args_) { 76 return delegate._delegate_(_args_); 77 } 78 79 @Local 80 public void _voidDelegate_(List <CtParameter> _args_) { 81 delegate._voidDelegate_(_args_); 82 } 83 84 } 85 86 interface _ReturnType_ { 87 } 88 89 interface _DelegateType_ { 90 91 _ReturnType_ _delegate_(List <CtParameter> _args_); 92 93 void _voidDelegate_(List <CtParameter> _args_); 94 95 } | Popular Tags |