1 package spoon.reflect.factory; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 import java.util.Set ; 6 import java.util.TreeSet ; 7 8 import spoon.reflect.Factory; 9 import spoon.reflect.code.BinaryOperatorKind; 10 import spoon.reflect.code.CtAssignment; 11 import spoon.reflect.code.CtBinaryOperator; 12 import spoon.reflect.code.CtBlock; 13 import spoon.reflect.code.CtExpression; 14 import spoon.reflect.code.CtFieldAccess; 15 import spoon.reflect.code.CtInvocation; 16 import spoon.reflect.code.CtLiteral; 17 import spoon.reflect.code.CtLocalVariable; 18 import spoon.reflect.code.CtNewArray; 19 import spoon.reflect.code.CtStatement; 20 import spoon.reflect.code.CtStatementList; 21 import spoon.reflect.code.CtVariableAccess; 22 import spoon.reflect.declaration.CtNamedElement; 23 import spoon.reflect.declaration.CtVariable; 24 import spoon.reflect.declaration.ModifierKind; 25 import spoon.reflect.reference.CtExecutableReference; 26 import spoon.reflect.reference.CtFieldReference; 27 import spoon.reflect.reference.CtLocalVariableReference; 28 import spoon.reflect.reference.CtReference; 29 import spoon.reflect.reference.CtTypeReference; 30 import spoon.reflect.reference.CtVariableReference; 31 32 36 public class CodeFactory extends SubFactory { 37 38 private static final long serialVersionUID = 1L; 39 40 43 public CodeFactory(Factory factory) { 44 super(factory); 45 } 46 47 60 public <T> CtBinaryOperator<T> createBinaryOperator(CtExpression<?> left, 61 CtExpression<?> right, BinaryOperatorKind kind) { 62 CtBinaryOperator<T> op = factory.Core().createBinaryOperator(); 63 op.setLeftHandOperand(left); 64 op.setRightHandOperand(right); 65 setParent(op, left, right); 66 op.setKind(kind); 67 return op; 68 } 69 70 79 @SuppressWarnings ("unchecked") 80 public <T> CtFieldAccess<Class <T>> createClassAccess(CtTypeReference<T> type) { 81 CtFieldAccess<Class <T>> ca = factory.Core().createFieldAccess(); 82 CtTypeReference classType = factory.Type().createReference(Class .class); 83 84 ca.setType(classType); 85 CtFieldReference field = factory.Core().createFieldReference(); 86 field.setDeclaringType(type); 87 field.setType(classType); 88 field.setSimpleName("class"); 89 ca.setVariable(field); 90 return ca; 91 } 92 93 106 public <T> CtInvocation<T> createInvocation(CtExpression<?> target, 107 CtExecutableReference<T> executable, CtExpression<?>... arguments) { 108 List <CtExpression<?>> ext = new ArrayList <CtExpression<?>>(); 109 for (CtExpression<?> arg : arguments) { 110 ext.add(arg); 111 } 112 return createInvocation(target, executable, ext); 113 } 114 115 128 public <T> CtInvocation<T> createInvocation(CtExpression<?> target, 129 CtExecutableReference<T> executable, List <CtExpression<?>> arguments) { 130 CtInvocation<T> invocation = factory.Core().createInvocation(); 131 invocation.setTarget(target); 132 invocation.setExecutable(executable); 133 invocation.setArguments(arguments); 134 return invocation; 135 } 136 137 146 public <T> CtLiteral<T> createLiteral(T value) { 147 CtLiteral<T> l = factory.Core().createLiteral(); 148 l.setValue(value); 149 return l; 150 } 151 152 155 @SuppressWarnings ("unchecked") 156 public <T> CtNewArray<T[]> createLiteralArray(T[] value) { 157 if (!value.getClass().isArray()) 158 throw new RuntimeException ("value is not an array"); 159 if (value.getClass().getComponentType().isArray()) 160 throw new RuntimeException ("can only create one-dimension arrays"); 161 CtNewArray<T[]> array = factory.Core().createNewArray(); 162 array.setType(factory.Type().createArrayReference( 163 factory.Type().createReference( 164 (Class <T>)value.getClass().getComponentType()))); 165 for (T e : value) { 166 CtLiteral<T> l = factory.Core().createLiteral(); 167 l.setValue(e); 168 l.setParent(array); 169 array.getElements().add(l); 170 } 171 return array; 172 } 173 174 187 public <T> CtLocalVariable<T> createLocalVariable(CtTypeReference<T> type, 188 String name, CtExpression<T> defaultExpression) { 189 CtLocalVariable<T> var = factory.Core().createLocalVariable(); 190 var.setSimpleName(name); 191 var.setType(type); 192 var.setDefaultExpression(defaultExpression); 193 return var; 194 } 195 196 200 public <T> CtLocalVariableReference<T> createLocalVariableReference( 201 CtLocalVariable<T> localVariable) { 202 CtLocalVariableReference<T> ref = factory.Core() 203 .createLocalVariableReference(); 204 ref.setType(localVariable.getType()); 205 ref.setSimpleName(localVariable.getSimpleName()); 206 ref.setDeclaration(localVariable); 207 return ref; 208 } 209 210 214 public <T> CtLocalVariableReference<T> createLocalVariableReference( 215 CtTypeReference<T> type, String name) { 216 CtLocalVariableReference<T> ref = factory.Core() 217 .createLocalVariableReference(); 218 ref.setType(type); 219 ref.setSimpleName(name); 220 return ref; 221 } 222 223 226 @SuppressWarnings ("unchecked") 227 public CtStatementList createStatementList(CtBlock<?> block) { 228 CtStatementList l = factory.Core().createStatementList(); 229 for (CtStatement s : block.getStatements()) { 230 l.getStatements().add(factory.Core().clone(s)); 231 } 232 return l; 233 } 234 235 246 public <T> CtFieldAccess<T> createThisAccess(CtTypeReference<T> type) { 247 CtFieldAccess<T> fa = factory.Core().createFieldAccess(); 248 fa.setType(type); 249 CtFieldReference<T> field = factory.Core().createFieldReference(); 250 field.setDeclaringType(type); 251 field.setType(type); 252 field.setSimpleName("this"); 253 fa.setVariable(field); 254 return fa; 255 } 256 257 260 public <T> CtVariableAccess<T> createVariableAccess( 261 CtVariableReference<T> variable, boolean isStatic) { 262 CtVariableAccess<T> va; 263 if (variable instanceof CtFieldReference) { 264 va = factory.Core().createFieldAccess(); 265 if (!isStatic) { 268 ((CtFieldAccess<T>) va) 269 .setTarget(createThisAccess(((CtFieldReference<T>) variable) 270 .getDeclaringType())); 271 } 272 } else { 273 va = factory.Core().createVariableAccess(); 274 } 275 va.setVariable(variable); 276 va.setType(variable.getType()); 277 return va; 278 } 279 280 286 public List <CtExpression<?>> createVariableAccesses( 287 List <? extends CtVariable<?>> variables) { 288 List <CtExpression<?>> result = new ArrayList <CtExpression<?>>(); 289 for (CtVariable<?> v : variables) { 290 result.add(createVariableAccess(v.getReference(), v.getModifiers() 291 .contains(ModifierKind.STATIC))); 292 } 293 return result; 294 } 295 296 309 public <T> CtAssignment createVariableAssignment( 310 CtVariableReference<T> variable, boolean isStatic, 311 CtExpression<T> expression) { 312 CtAssignment<T, T> va = factory.Core().createAssignment(); 313 va.setAssignment(expression); 314 expression.setParent(va); 315 CtVariableAccess<T> vaccess = createVariableAccess(variable, isStatic); 316 va.setAssigned(vaccess); 317 vaccess.setParent(va); 318 return va; 319 } 320 321 331 @SuppressWarnings ("unchecked") 332 public CtStatementList createVariableAssignments( 333 List <? extends CtVariable> variables, 334 List <? extends CtExpression> expressions) { 335 CtStatementList<?> result = factory.Core().createStatementList(); 336 for (int i = 0; i < variables.size(); i++) { 337 result.getStatements().add( 338 createVariableAssignment(variables.get(i).getReference(), 339 variables.get(i).getModifiers().contains( 340 ModifierKind.STATIC), expressions.get(i))); 341 } 342 return result; 343 } 344 345 356 @SuppressWarnings ("unchecked") 357 public <R extends CtReference, E extends CtNamedElement> List <R> getReferences( 358 List <E> elements) { 359 List <R> refs = new ArrayList <R>(); 360 for (E e : elements) { 361 refs.add((R) e.getReference()); 362 } 363 return refs; 364 } 365 366 373 public Set <ModifierKind> modifiers(ModifierKind... modifiers) { 374 Set <ModifierKind> ret = new TreeSet <ModifierKind>(); 375 for (ModifierKind m : modifiers) 376 ret.add(m); 377 return ret; 378 } 379 380 } 381 | Popular Tags |