KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > reflect > factory > EvalFactory


1 package spoon.reflect.factory;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
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 /**
25  * A factory to create some evaluation utilities on the Spoon metamodel.
26  */

27 public class EvalFactory extends SubFactory {
28
29     private static final long serialVersionUID = 1L;
30
31     /**
32      * Creates the evaluation factory.
33      */

34     public EvalFactory(Factory factory) {
35         super(factory);
36     }
37
38     /**
39      * Creates a partial evaluator on the Spoon meta-model.
40      */

41     public PartialEvaluator createPartialEvaluator() {
42         return new VisitorPartialEvaluator();
43     }
44
45     /**
46      * Creates a symbolic evaluator on the Spoon meta-model.
47      */

48     public SymbolicEvaluator createSymbolicEvaluator() {
49         return new VisitorSymbolicEvaluator();
50     }
51
52     /**
53      * Creates a new symbolic instance.
54      *
55      * @param evaluator
56      * the evaluator
57      * @param concreteType
58      * the type of the instance
59      * @param isType
60      * tells if it is a type instance or a regular instance
61      */

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     /**
69      * Gets the symbolic evaluation paths of the program, as calculated by
70      * {@link spoon.reflect.eval.SymbolicEvaluator}.
71      *
72      * @param entryPoints
73      * the entry point methods
74      * @return a map containing the paths for each entry point
75      */

76     @SuppressWarnings JavaDoc("unchecked")
77     public Map JavaDoc<CtMethod, List JavaDoc<SymbolicEvaluationPath>> createEvaluationPaths(
78             Collection JavaDoc<CtMethod> entryPoints) {
79         Map JavaDoc<CtMethod, List JavaDoc<SymbolicEvaluationPath>> paths = new HashMap JavaDoc<CtMethod, List JavaDoc<SymbolicEvaluationPath>>();
80         for (CtMethod<?> m : entryPoints) {
81             SymbolicEvaluator evaluator = createSymbolicEvaluator();
82             List JavaDoc<SymbolicInstance> args = new ArrayList JavaDoc<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             // Create target(this) for the invocation
90
SymbolicInstance target = createSymbolicInstance(evaluator, m
91                     .getDeclaringType().getReference(), m.getModifiers()
92                     .contains(ModifierKind.STATIC));
93             // Seed the fields of the class
94
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 JavaDoc th) {
111                 th.printStackTrace();
112             }
113             paths.put(m, evaluator.getPaths());
114         }
115         return paths;
116     }
117
118 }
119
Popular Tags