1 package spoon.reflect.factory; 2 3 import java.util.ArrayList ; 4 import java.util.Collection ; 5 import java.util.HashMap ; 6 import java.util.List ; 7 import java.util.Map ; 8 9 import spoon.reflect.Factory; 10 import spoon.reflect.declaration.CtField; 11 import spoon.reflect.declaration.CtMethod; 12 import spoon.reflect.declaration.CtParameter; 13 import spoon.reflect.declaration.CtType; 14 import spoon.reflect.declaration.ModifierKind; 15 import spoon.reflect.eval.PartialEvaluator; 16 import spoon.reflect.eval.SymbolicEvaluationPath; 17 import spoon.reflect.eval.SymbolicEvaluator; 18 import spoon.reflect.eval.SymbolicInstance; 19 import spoon.reflect.reference.CtTypeReference; 20 import spoon.reflect.reference.CtVariableReference; 21 import spoon.support.reflect.eval.VisitorPartialEvaluator; 22 import spoon.support.reflect.eval.VisitorSymbolicEvaluator; 23 24 27 public class EvalFactory extends SubFactory { 28 29 private static final long serialVersionUID = 1L; 30 31 34 public EvalFactory(Factory factory) { 35 super(factory); 36 } 37 38 41 public PartialEvaluator createPartialEvaluator() { 42 return new VisitorPartialEvaluator(); 43 } 44 45 48 public SymbolicEvaluator createSymbolicEvaluator() { 49 return new VisitorSymbolicEvaluator(); 50 } 51 52 62 public <T> SymbolicInstance<T> createSymbolicInstance( 63 SymbolicEvaluator evaluator, CtTypeReference<T> concreteType, 64 boolean isType) { 65 return new SymbolicInstance<T>(evaluator, concreteType, isType); 66 } 67 68 76 @SuppressWarnings ("unchecked") 77 public Map <CtMethod, List <SymbolicEvaluationPath>> createEvaluationPaths( 78 Collection <CtMethod> entryPoints) { 79 Map <CtMethod, List <SymbolicEvaluationPath>> paths = new HashMap <CtMethod, List <SymbolicEvaluationPath>>(); 80 for (CtMethod<?> m : entryPoints) { 81 SymbolicEvaluator evaluator = createSymbolicEvaluator(); 82 List <SymbolicInstance> args = new ArrayList <SymbolicInstance>(); 83 for (CtParameter<?> p : m.getParameters()) { 84 SymbolicInstance arg = createSymbolicInstance(evaluator, p 85 .getType(), false); 86 evaluator.getHeap().store(arg); 87 args.add(arg); 88 } 89 SymbolicInstance target = createSymbolicInstance(evaluator, m 91 .getDeclaringType().getReference(), m.getModifiers() 92 .contains(ModifierKind.STATIC)); 93 CtType<?> targetType = m.getDeclaringType(); 95 for (CtField field : targetType.getFields()) { 96 if (!field.getModifiers().contains(ModifierKind.STATIC) 97 && m.getModifiers().contains(ModifierKind.STATIC)) { 98 continue; 99 } 100 101 CtVariableReference<?> fref = field.getReference(); 102 SymbolicInstance si = createSymbolicInstance(evaluator, fref 103 .getType(), false); 104 target.setFieldValue(evaluator.getHeap(), fref, si); 105 } 106 107 evaluator.getHeap().store(target); 108 try { 109 evaluator.invoke(target, m, args); 110 } catch (Throwable th) { 111 th.printStackTrace(); 112 } 113 paths.put(m, evaluator.getPaths()); 114 } 115 return paths; 116 } 117 118 } 119 | Popular Tags |