1 package spoon.reflect.factory; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 import java.util.StringTokenizer ; 6 7 import spoon.reflect.Factory; 8 import spoon.reflect.code.CtBlock; 9 import spoon.reflect.declaration.CtAnonymousExecutable; 10 import spoon.reflect.declaration.CtClass; 11 import spoon.reflect.declaration.CtConstructor; 12 import spoon.reflect.declaration.CtExecutable; 13 import spoon.reflect.declaration.CtMethod; 14 import spoon.reflect.declaration.CtParameter; 15 import spoon.reflect.reference.CtExecutableReference; 16 import spoon.reflect.reference.CtParameterReference; 17 import spoon.reflect.reference.CtTypeReference; 18 19 22 public class ExecutableFactory extends SubFactory { 23 24 private static final long serialVersionUID = 1L; 25 26 32 public ExecutableFactory(Factory factory) { 33 super(factory); 34 } 35 36 39 public CtAnonymousExecutable createAnonymous(CtClass<?> target, CtBlock body) { 40 CtAnonymousExecutable a = factory.Core().createAnonymousExecutable(); 41 target.getAnonymousExecutables().add(a); 42 a.setParent(target); 43 a.setBody(body); 44 body.setParent(a); 45 return a; 46 } 47 48 51 public <T> CtParameter<T> createParameter(CtExecutable<?> parent, 52 CtTypeReference<T> type, String name) { 53 CtParameter<T> parameter = factory.Core().createParameter(); 54 parameter.setType(type); 55 parameter.setSimpleName(name); 56 if (parent != null) { 57 parent.getParameters().add(parameter); 58 parameter.setParent(parent); 59 } 60 return parameter; 61 } 62 63 71 @SuppressWarnings ("unchecked") 72 public <T> CtParameterReference<T> createParameterReference( 73 CtParameter<T> parameter) { 74 CtParameterReference<T> ref = factory.Core().createParameterReference(); 75 if (parameter.getParent() != null) 76 ref.setDeclaringExecutable(factory.Executable().createReference( 77 (CtExecutable) parameter.getParent())); 78 ref.setSimpleName(parameter.getSimpleName()); 79 ref.setType(parameter.getType()); 80 return ref; 81 } 82 83 86 public <T> CtExecutableReference<T> createReference(CtExecutable<T> e) { 87 CtTypeReference<?> refs[] = new CtTypeReference[e.getParameters() 88 .size()]; 89 int i = 0; 90 for (CtParameter<?> param : e.getParameters()) { 91 refs[i++] = param.getType(); 92 } 93 if (e instanceof CtMethod) { 94 return createReference(e.getDeclaringType().getReference(), 95 ((CtMethod<T>) e).getType(), e.getSimpleName(), refs); 96 } else { 97 return createReference(e.getDeclaringType().getReference(), 98 ((CtConstructor<T>) e).getType(), e.getSimpleName(), refs); 99 } 100 } 101 102 114 public <T> CtExecutableReference<T> createReference( 115 CtTypeReference<?> declaringType, CtTypeReference<T> type, 116 String methodName, CtTypeReference<?>... parameterTypes) { 117 CtExecutableReference<T> methodRef = factory.Core() 118 .createExecutableReference(); 119 methodRef.setDeclaringType(declaringType); 120 methodRef.setSimpleName(methodName); 121 methodRef.setType(type); 122 List <CtTypeReference<?>> l = new ArrayList <CtTypeReference<?>>(); 123 for (CtTypeReference<?> ref : parameterTypes) 124 l.add(ref); 125 methodRef.setParameterTypes(l); 126 return methodRef; 127 } 128 129 143 public <T> CtExecutableReference<T> createReference( 144 CtTypeReference<?> declaringType, boolean isStatic, 145 CtTypeReference<T> type, String methodName, 146 CtTypeReference<?>... parameterTypes) { 147 CtExecutableReference<T> methodRef = factory.Core() 148 .createExecutableReference(); 149 methodRef.setStatic(isStatic); 150 methodRef.setDeclaringType(declaringType); 151 methodRef.setSimpleName(methodName); 152 methodRef.setType(type); 153 List <CtTypeReference<?>> l = new ArrayList <CtTypeReference<?>>(); 154 for (CtTypeReference<?> ref : parameterTypes) 155 l.add(ref); 156 methodRef.setParameterTypes(l); 157 return methodRef; 158 } 159 160 174 public <T> CtExecutableReference<T> createReference( 175 CtTypeReference<?> declaringType, boolean isStatic, 176 CtTypeReference<T> type, String methodName, 177 List <CtTypeReference<?>> parameterTypes) { 178 CtExecutableReference<T> methodRef = factory.Core() 179 .createExecutableReference(); 180 methodRef.setStatic(isStatic); 181 methodRef.setDeclaringType(declaringType); 182 methodRef.setSimpleName(methodName); 183 methodRef.setType(type); 184 List <CtTypeReference<?>> l = new ArrayList <CtTypeReference<?>>(); 185 for (CtTypeReference<?> ref : parameterTypes) 186 l.add(ref); 187 methodRef.setParameterTypes(l); 188 return methodRef; 189 } 190 191 203 public <T> CtExecutableReference<T> createReference( 204 CtTypeReference<?> declaringType, CtTypeReference<T> type, 205 String methodName, List <CtTypeReference<?>> parameterTypes) { 206 CtExecutableReference<T> methodRef = factory.Core() 207 .createExecutableReference(); 208 methodRef.setDeclaringType(declaringType); 209 methodRef.setSimpleName(methodName); 210 methodRef.setType(type); 211 List <CtTypeReference<?>> l = new ArrayList <CtTypeReference<?>>(); 212 for (CtTypeReference<?> ref : parameterTypes) 213 l.add(ref); 214 methodRef.setParameterTypes(l); 215 return methodRef; 216 } 217 218 222 public <T> CtExecutableReference<T> createReference(String signature) { 223 CtExecutableReference<T> executableRef = factory.Core().createExecutableReference(); 224 String type = signature.substring(0, signature.indexOf(" ")); 225 String declaringType = signature.substring(signature.indexOf(" ") + 1, 226 signature.indexOf(CtExecutable.EXECUTABLE_SEPARATOR)); 227 String executableName = signature.substring(signature 228 .indexOf(CtExecutable.EXECUTABLE_SEPARATOR) + 1,signature.indexOf("(")); 229 executableRef.setSimpleName(executableName); 230 executableRef 231 .setDeclaringType(factory.Type().createReference(declaringType)); 232 CtTypeReference<T> typeRef = factory.Type().createReference(type); 233 executableRef.setType(typeRef); 234 String parameters=signature.substring(signature.indexOf("(")+1, signature.indexOf(")")); 235 StringTokenizer t=new StringTokenizer (parameters,","); 236 while(t.hasMoreTokens()) { 237 String paramType=t.nextToken(); 238 executableRef.getParameterTypes().add(factory.Type().createReference(paramType)); 239 } 240 return executableRef; 241 } 242 243 } 244 | Popular Tags |