KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > AbstractUnitPrinter


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003, 2004 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
24 /**
25 * Partial default UnitPrinter implementation.
26 */

27 public abstract class AbstractUnitPrinter implements UnitPrinter {
28     public void setPositionTagger( AttributesUnitPrinter pt ) {
29         this.pt = pt;
30         pt.setUnitPrinter( this );
31     }
32     public AttributesUnitPrinter getPositionTagger() {
33         return pt;
34     }
35     public void startUnit( Unit u ) {
36         handleIndent();
37         if( pt != null ) pt.startUnit( u );
38     }
39     public void endUnit( Unit u ) {
40         if( pt != null ) pt.endUnit( u );
41     }
42     public void startUnitBox( UnitBox ub ) { handleIndent(); }
43     public void endUnitBox( UnitBox ub ) {}
44     public void startValueBox( ValueBox vb ) {
45         handleIndent();
46         if( pt != null ) pt.startValueBox( vb );
47     }
48     public void endValueBox( ValueBox vb ) {
49         if( pt != null ) pt.endValueBox( vb );
50     }
51
52     public void noIndent() { startOfLine = false; }
53     public void incIndent() { indent = indent + " "; }
54     public void decIndent() {
55         if( indent.length() >= 4 ) indent = indent.substring(4);
56     }
57     public void setIndent(String JavaDoc indent) { this.indent = indent; }
58     public String JavaDoc getIndent() { return indent; }
59     
60     public abstract void literal( String JavaDoc s );
61     public abstract void type( Type t );
62     public abstract void methodRef( SootMethodRef m );
63     public abstract void fieldRef( SootFieldRef f );
64     public abstract void identityRef( IdentityRef r );
65     public abstract void unitRef( Unit u, boolean branchTarget );
66
67     public void newline() {
68         output.append("\n");
69         startOfLine = true;
70         if( pt != null ) pt.newline();
71     }
72     public void local( Local l ) {
73         handleIndent();
74         if( quotableLocals == null )
75             initializeQuotableLocals();
76         if( quotableLocals.contains(l.getName()) )
77             output.append ( "'" + l.getName() + "'");
78         else
79             output.append( l.getName() );
80     }
81     public void constant( Constant c ) {
82         handleIndent();
83         output.append( c.toString() );
84     }
85     public String JavaDoc toString() {
86         String JavaDoc ret = output.toString();
87         output = new StringBuffer JavaDoc();
88         return ret;
89     }
90     public StringBuffer JavaDoc output() {
91         return output;
92     }
93
94     protected void handleIndent() {
95         if( startOfLine ) output.append( indent );
96         startOfLine = false;
97     }
98
99     protected void initializeQuotableLocals() {
100         quotableLocals = new HashSet();
101         quotableLocals.addAll (Jimple.jimpleKeywordList());
102     }
103
104     protected boolean startOfLine = true;
105     protected String JavaDoc indent = " ";
106     protected StringBuffer JavaDoc output = new StringBuffer JavaDoc();
107     protected AttributesUnitPrinter pt;
108     protected HashSet quotableLocals;
109 }
110
111
Popular Tags