KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > xml > TagCollector


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2004 Jennifer Lhotak
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 package soot.xml;
21
22 import soot.*;
23 import soot.tagkit.*;
24 import java.util.*;
25 import java.io.*;
26
27 public class TagCollector {
28
29     private ArrayList attributes;
30     private ArrayList keys;
31     
32     public TagCollector(){
33         attributes = new ArrayList();
34         keys = new ArrayList();
35     }
36
37     public void collectTags(SootClass sc){
38     
39         
40         // tag fields
41
Iterator fit = sc.getFields().iterator();
42         while (fit.hasNext()){
43             SootField sf = (SootField)fit.next();
44             collectFieldTags(sf);
45         }
46         
47         // tag methods
48
Iterator it = sc.getMethods().iterator();
49         while (it.hasNext()) {
50             SootMethod sm = (SootMethod)it.next();
51             collectMethodTags(sm);
52         
53             if (!sm.hasActiveBody()) continue;
54             Body b = sm.getActiveBody();
55             collectBodyTags(b);
56         }
57     }
58
59     public void collectKeyTags(SootClass sc){
60         Iterator it = sc.getTags().iterator();
61         while (it.hasNext()){
62             Object JavaDoc next = it.next();
63             if (next instanceof KeyTag){
64                 KeyTag kt = (KeyTag)next;
65                 Key k = new Key(kt.red(), kt.green(), kt.blue(), kt.key());
66                 k.aType(kt.analysisType());
67                 keys.add(k);
68             }
69         }
70     }
71
72     public void printKeys(PrintWriter writerOut){
73         Iterator it = keys.iterator();
74         while (it.hasNext()){
75             Key k = (Key)it.next();
76             k.print(writerOut);
77         }
78     }
79     
80             
81     public void collectFieldTags(SootField sf){
82         Iterator fTags = sf.getTags().iterator();
83         Attribute fa = new Attribute();
84         while (fTags.hasNext()){
85             Tag t = (Tag)fTags.next();
86             fa.addTag(t);
87             //System.out.println("field tag: "+t);
88
}
89         attributes.add(fa);
90     }
91
92     public void collectMethodTags(SootMethod sm){
93         if (!sm.hasActiveBody()) {
94             return;
95         }
96         if (!sm.getTags().isEmpty()){
97             Iterator mTags = sm.getTags().iterator();
98             Attribute ma = new Attribute();
99             while (mTags.hasNext()){
100                 Tag t = (Tag)mTags.next();
101                 ma.addTag(t);
102                 //System.out.println("method tag: "+t);
103
}
104             attributes.add(ma);
105         }
106             
107     }
108     
109     public void collectBodyTags(Body b){
110         Iterator itUnits = b.getUnits().iterator();
111         while (itUnits.hasNext()) {
112             Unit u = (Unit)itUnits.next();
113             Iterator itTags = u.getTags().iterator();
114             Attribute ua = new Attribute();
115             JimpleLineNumberTag jlnt = null;
116             while (itTags.hasNext()) {
117                 Tag t = (Tag)itTags.next();
118                 ua.addTag(t);
119                 if (t instanceof JimpleLineNumberTag){
120                     jlnt = (JimpleLineNumberTag)t;
121                 }
122                 //System.out.println("adding unit tag: "+t);
123
}
124             attributes.add(ua);
125             Iterator valBoxIt = u.getUseAndDefBoxes().iterator();
126             while (valBoxIt.hasNext()){
127                 ValueBox vb = (ValueBox)valBoxIt.next();
128                 //PosColorAttribute attr = new PosColorAttribute();
129
if (!vb.getTags().isEmpty()){
130                     Iterator tagsIt = vb.getTags().iterator();
131                     Attribute va = new Attribute();
132                     while (tagsIt.hasNext()) {
133                         Tag t = (Tag)tagsIt.next();
134                         //System.out.println("adding vb tag: "+t);
135
va.addTag(t);
136                         //System.out.println("vb: "+vb.getValue()+" tag: "+t);
137
if (jlnt != null) {
138                             va.addTag(jlnt);
139                         }
140                     }
141                     // also here add line tags of the unit
142
attributes.add(va);
143                     //System.out.println("added att: "+va);
144
}
145             }
146         }
147     }
148     
149     public void printTags(PrintWriter writerOut){
150         
151         Iterator it = attributes.iterator();
152         while (it.hasNext()){
153             Attribute a = (Attribute)it.next();
154             //System.out.println("will print attr: "+a);
155
a.print(writerOut);
156         }
157     }
158 }
159
Popular Tags