KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > AttributesUnitPrinter


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Ondrej 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;
21 import soot.jimple.*;
22 import java.util.*;
23 import soot.tagkit.*;
24
25 /**
26  * Adds PositionTags to ValueBoxes to identify their position in the output.
27  */

28 public class AttributesUnitPrinter {
29
30     private int startOffset;
31     private Stack startOffsets;
32     private int endOffset;
33     private int startStmtOffset;
34     private int startLn;
35     private int currentLn;
36     private int lastNewline;
37     private UnitPrinter printer;
38     
39     public AttributesUnitPrinter( int currentLnNum ) {
40         this.currentLn = currentLnNum;
41     }
42     public void startUnit( Unit u ) {
43         startLn = currentLn;
44         startStmtOffset = output().length() - lastNewline;
45     }
46     public void endUnit( Unit u ) {
47         int endStmtOffset = output().length() - lastNewline;
48         //G.v().out.println("u: "+u.toString());
49
if (hasTag(u)){
50             //G.v().out.println("u: "+u.toString()+" has tag");
51
u.addTag( new JimpleLineNumberTag( startLn, currentLn ));
52         }
53         if (hasColorTag(u)) {
54             u.addTag( new PositionTag(startStmtOffset, endStmtOffset) );
55         }
56     }
57     public void startValueBox( ValueBox u ) {
58         if (startOffsets == null) {
59             startOffsets = new Stack();
60         }
61         startOffsets.push(new Integer JavaDoc(output().length() - lastNewline));
62     }
63     public void endValueBox( ValueBox u ) {
64         endOffset = output().length() - lastNewline;
65         if (hasColorTag(u)) {
66             u.addTag(new PositionTag(((Integer JavaDoc)startOffsets.pop()).intValue(), endOffset));
67         }
68     }
69
70     private boolean hasTag(Host h) {
71         if (h instanceof Unit) {
72             Iterator usesAndDefsIt = ((Unit)h).getUseAndDefBoxes().iterator();
73             while (usesAndDefsIt.hasNext()){
74                 if (hasTag((ValueBox)usesAndDefsIt.next())) return true;
75             }
76         }
77         if (h.getTags().isEmpty()) return false;
78         return true;
79     }
80     
81     private boolean hasColorTag(Host h) {
82         Iterator it = h.getTags().iterator();
83         while (it.hasNext()){
84             if (it.next() instanceof ColorTag) return true;
85         }
86         return false;
87     }
88     
89     public void setEndLn(int ln){
90         currentLn = ln;
91     }
92     public int getEndLn() {
93         return currentLn;
94     }
95     public void newline() {
96         currentLn++;
97         lastNewline = output().length();
98     }
99     private StringBuffer JavaDoc output() {
100         return printer.output();
101     }
102     public void setUnitPrinter( UnitPrinter up ) {
103         printer = up;
104     }
105 }
106
107
108
Popular Tags