KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > reflect > visitor > ModelConsistencyChecker


1 package spoon.reflect.visitor;
2
3 import java.util.Stack JavaDoc;
4
5 import spoon.processing.Environment;
6 import spoon.processing.Severity;
7 import spoon.reflect.declaration.CtElement;
8
9 /**
10  * This scanner checks that a program model is consistent whith regards to the
11  * parent elements (children must have the right parent). This class can be used
12  * to validate that a program transformation does not harm the model integrity,
13  * and also to automatically fix it when possible.
14  */

15 public class ModelConsistencyChecker extends CtScanner {
16
17     boolean fixInconsistencies = false;
18
19     Environment environment;
20
21     Stack JavaDoc<CtElement> stack = new Stack JavaDoc<CtElement>();
22
23     /**
24      * Creates a new model consistency checker.
25      *
26      * @param environment
27      * the environment where to report errors
28      * @param fixInconsistencies
29      * automatically fix the inconsitencies rather than reporting
30      * warnings (to report warnings, set this to false)
31      */

32     public ModelConsistencyChecker(Environment environment,
33             boolean fixInconsistencies) {
34         this.fixInconsistencies = fixInconsistencies;
35         this.environment = environment;
36     }
37
38     /**
39      * Enters an element.
40      */

41     @Override JavaDoc
42     public void enter(CtElement element) {
43         if (!stack.isEmpty()) {
44             if (element.getParent() != stack.peek()) {
45                 if (fixInconsistencies) {
46                     element.setParent(stack.peek());
47                 } else {
48                     environment.report(null, Severity.WARNING,
49                             "inconsistent parent for " + element);
50                 }
51             }
52         }
53         stack.push(element);
54     }
55
56     /**
57      * Exits an element.
58      */

59     @Override JavaDoc
60     protected void exit(CtElement e) {
61         stack.pop();
62     }
63
64 }
65
Popular Tags