KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > support > visitor > ElementReplacer


1 package spoon.support.visitor;
2
3 import java.lang.reflect.Field JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.List JavaDoc;
6
7 import spoon.processing.FactoryAccessor;
8 import spoon.reflect.declaration.CtElement;
9 import spoon.reflect.reference.CtReference;
10 import spoon.reflect.visitor.CtScanner;
11 import spoon.support.util.RtHelper;
12
13 /**
14  * This scanner replaces a program element with another one in a program element
15  * hierarchy.
16  */

17 public class ElementReplacer<T extends FactoryAccessor> extends CtScanner {
18
19     T replacement;
20
21     T toReplace;
22
23     /**
24      * Creates a new replacer.
25      *
26      * @param toReplace the element to be replaced
27      * @param replacement the replacing element
28      */

29     public ElementReplacer(T toReplace, T replacement) {
30         super();
31         this.replacement = replacement;
32         this.toReplace = toReplace;
33     }
34
35     private T getReplacement(Object JavaDoc parent) {
36         T ret = replacement.getFactory().Core().clone(replacement);
37         if (ret instanceof CtElement && parent instanceof CtElement) {
38             ((CtElement) ret).setParent((CtElement) parent);
39         }
40         return ret;
41     }
42
43     @SuppressWarnings JavaDoc("unchecked")
44     private void replaceIn(Object JavaDoc parent) throws Exception JavaDoc {
45
46 // List<Field> fields = Arrays.asList(RtHelper.getAllFields(parent.getClass()));
47

48 // Class current = parent.getClass();
49
// for (Field f : current.getDeclaredFields())
50
// fields.add(f);
51
//
52
// while (current.getSuperclass() != null) {
53
// current = current.getSuperclass();
54
// for (Field f : current.getDeclaredFields())
55
// fields.add(f);
56
// }
57

58         // Field[] fields = parent.getClass().getFields();
59
for (Field JavaDoc f : RtHelper.getAllFields(parent.getClass())) {
60             f.setAccessible(true);
61             Object JavaDoc tmp = f.get(parent);
62
63             if (tmp != null) {
64                 if (tmp instanceof List JavaDoc) {
65                     List JavaDoc<T> lst = (List JavaDoc<T>) tmp;
66
67                     for (int i = 0; i < lst.size(); i++) {
68                         if (lst.get(i)!=null && lst.get(i).equals(toReplace)) {
69                             lst.remove(i);
70                             if(replacement!=null)
71                                 lst.add(i, getReplacement(parent));
72                         }
73                     }
74                 } else if (tmp instanceof Collection JavaDoc) {
75                     Collection JavaDoc<T> collect = (Collection JavaDoc<T>) tmp;
76                     Object JavaDoc[] array = collect.toArray();
77                     for (Object JavaDoc obj : array) {
78                         if (obj.equals(toReplace)) {
79                             collect.remove(obj);
80                             collect.add(getReplacement(parent));
81                         }
82                     }
83                 } else if (tmp.equals(toReplace)) {
84                     f.set(parent, getReplacement(parent));
85                 }
86             }
87         }
88     }
89
90     /**
91      * Do the replacement of the given element if needed.
92      */

93     @Override JavaDoc
94     public void enter(CtElement e) {
95         try {
96             replaceIn(e);
97         } catch (Exception JavaDoc e1) {
98             e1.printStackTrace();
99         }
100         super.enter(e);
101     }
102
103     /**
104      * Do the replacement of the given element reference if needed.
105      */

106     @Override JavaDoc
107     public void enterReference(CtReference e) {
108         try {
109             replaceIn(e);
110         } catch (Exception JavaDoc e1) {
111             e1.printStackTrace();
112         }
113         super.enterReference(e);
114     }
115
116 }
117
Popular Tags