KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > PhaseOptions


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 java.util.*;
22
23 /** Manages the phase options of the various soot phases. */
24 public class PhaseOptions {
25     /** Needed for preventing infinite recursion in constructor.
26      * Termination is assured: each constructor is called exactly once.
27      * Here is a case analysis.
28      * a. PackManager used first. Then its constructor needs PhaseOptions,
29             which also needs a PackManager; OK because we store the
30             PackManager being initialized in a field.
31          b. PhaseOptions used first. Then getPM() calls PackManager.v(),
32             which calls the constr, which sets the .pm field here, uses
33             PhaseOptions (which uses PackManager), and returns. OK. */

34     private PackManager pm;
35     public void setPackManager(PackManager m) { this.pm = m; }
36     PackManager getPM()
37     {
38         if (pm == null)
39             PackManager.v();
40         return pm;
41     }
42
43     public PhaseOptions( Singletons.Global g ) { }
44     public static PhaseOptions v() { return G.v().soot_PhaseOptions(); }
45
46     private Map phaseToOptionMap = new HashMap();
47
48     public Map getPhaseOptions(String JavaDoc phaseName) {
49         return getPhaseOptions(getPM().getPhase(phaseName));
50     }
51
52     public Map getPhaseOptions(HasPhaseOptions phase) {
53         Map ret = (Map) phaseToOptionMap.get(phase);
54         if( ret == null ) ret = new HashMap();
55         else ret = new HashMap( ret );
56         StringTokenizer st = new StringTokenizer( phase.getDefaultOptions() );
57         while( st.hasMoreTokens() ) {
58             String JavaDoc opt = st.nextToken();
59             String JavaDoc key = getKey( opt );
60             String JavaDoc value = getValue( opt );
61             if( !ret.containsKey( key ) ) ret.put( key, value );
62         }
63         return Collections.unmodifiableMap(ret);
64     }
65
66     public boolean processPhaseOptions(String JavaDoc phaseName, String JavaDoc option) {
67         StringTokenizer st = new StringTokenizer(option, ",");
68         while (st.hasMoreTokens()) {
69             if( !setPhaseOption( phaseName, st.nextToken() ) ) {
70                 return false;
71             }
72         }
73         return true;
74     }
75
76     /** This method returns true iff key "name" is in options
77         and maps to "true". */

78     public static boolean getBoolean(Map options, String JavaDoc name)
79     {
80         return options.containsKey(name) &&
81             options.get(name).equals("true");
82     }
83
84
85
86     /** This method returns the value of "name" in options
87         or "" if "name" is not found. */

88     public static String JavaDoc getString(Map options, String JavaDoc name)
89     {
90         return options.containsKey(name) ?
91             (String JavaDoc)options.get(name) : "";
92     }
93
94
95
96     /** This method returns the float value of "name" in options
97         or 1.0 if "name" is not found. */

98     public static float getFloat(Map options, String JavaDoc name)
99     {
100         return options.containsKey(name) ?
101             new Float JavaDoc((String JavaDoc)options.get(name)).floatValue() : 1.0f;
102     }
103
104
105
106     /** This method returns the integer value of "name" in options
107         or 0 if "name" is not found. */

108     public static int getInt(Map options, String JavaDoc name)
109     {
110         return options.containsKey(name) ?
111             new Integer JavaDoc((String JavaDoc)options.get(name)).intValue() : 0;
112     }
113
114
115     private Map mapForPhase( String JavaDoc phaseName ) {
116         HasPhaseOptions phase = getPM().getPhase( phaseName );
117         if( phase == null ) return null;
118         return mapForPhase( phase );
119     }
120
121     private Map mapForPhase( HasPhaseOptions phase ) {
122         Map optionMap = (Map) phaseToOptionMap.get( phase );
123         if( optionMap == null ) {
124             phaseToOptionMap.put( phase, optionMap = new HashMap() );
125         }
126         return optionMap;
127     }
128
129     private String JavaDoc getKey( String JavaDoc option ) {
130         int delimLoc = option.indexOf(":");
131         if (delimLoc < 0) {
132             if( option.equals("on") || option.equals("off") ) return "enabled";
133             return option;
134         } else {
135             return option.substring(0, delimLoc);
136         }
137     }
138     private String JavaDoc getValue( String JavaDoc option ) {
139         int delimLoc = option.indexOf(":");
140         if (delimLoc < 0) {
141             if( option.equals("off") ) return "false";
142             return "true";
143         } else {
144             return option.substring(delimLoc+1);
145         }
146     }
147     private void resetRadioPack( String JavaDoc phaseName ) {
148         for( Iterator pIt = getPM().allPacks().iterator(); pIt.hasNext(); ) {
149             final Pack p = (Pack) pIt.next();
150             if( !(p instanceof RadioScenePack) ) continue;
151             if( p.get(phaseName) == null ) continue;
152             for( Iterator tIt = p.iterator(); tIt.hasNext(); ) {
153                 final Transform t = (Transform) tIt.next();
154                 setPhaseOption( t.getPhaseName(), "enabled:false" );
155             }
156         }
157     }
158     private boolean checkParentEnabled( String JavaDoc phaseName ) {
159         if( true ) return true;
160         // This check for the parent being enabled
161
// has been taken out, because it caused problems with the order in
162
// which the options are specified.
163
for( Iterator pIt = getPM().allPacks().iterator(); pIt.hasNext(); ) {
164             final Pack p = (Pack) pIt.next();
165             if( getBoolean( getPhaseOptions( p ), "enabled" ) ) continue;
166             for( Iterator tIt = p.iterator(); tIt.hasNext(); ) {
167                 final Transform t = (Transform) tIt.next();
168                 if( t.getPhaseName().equals( phaseName ) ) {
169                     G.v().out.println( "Attempt to set option for phase "+phaseName+" of disabled pack "+p.getPhaseName() );
170                     return false;
171
172                 }
173             }
174         }
175         return true;
176     }
177     public boolean setPhaseOption( String JavaDoc phaseName, String JavaDoc option ) {
178         HasPhaseOptions phase = getPM().getPhase( phaseName );
179         if( phase == null ) {
180             G.v().out.println( "Option "+option+" given for nonexistent"
181                     +" phase "+phaseName );
182             return false;
183         }
184         return setPhaseOption( phase, option );
185     }
186     public boolean setPhaseOption( HasPhaseOptions phase, String JavaDoc option ) {
187         Map optionMap = mapForPhase( phase );
188         if( !checkParentEnabled( phase.getPhaseName() ) ) return false;
189         if( optionMap == null ) {
190             G.v().out.println( "Option "+option+" given for nonexistent"
191                     +" phase "+phase.getPhaseName() );
192             return false;
193         }
194         String JavaDoc key = getKey( option );
195         if( key.equals( "enabled" ) && getValue( option ).equals( "true" ) ) {
196             resetRadioPack( phase.getPhaseName() );
197         }
198         if( declaresOption( phase, key ) ) {
199             optionMap.put( key, getValue( option ) );
200             return true;
201         }
202         G.v().out.println( "Invalid option "+option+" for phase "+phase.getPhaseName() );
203         return false;
204     }
205
206     private boolean declaresOption( String JavaDoc phaseName, String JavaDoc option ) {
207         HasPhaseOptions phase = getPM().getPhase( phaseName );
208         return declaresOption( phase, option );
209     }
210     private boolean declaresOption( HasPhaseOptions phase, String JavaDoc option ) {
211         String JavaDoc declareds = phase.getDeclaredOptions();
212         for( StringTokenizer st = new StringTokenizer( declareds );
213                 st.hasMoreTokens(); ) {
214             if( st.nextToken().equals( option ) ) {
215                 return true;
216             }
217         }
218         return false;
219     }
220
221     public void setPhaseOptionIfUnset( String JavaDoc phaseName, String JavaDoc option ) {
222         Map optionMap = mapForPhase( phaseName );
223         if( optionMap == null )
224             throw new RuntimeException JavaDoc( "No such phase "+phaseName );
225         if( optionMap.containsKey( getKey( option ) ) ) return;
226         if( !declaresOption( phaseName, getKey( option ) ) )
227             throw new RuntimeException JavaDoc( "No option "+option+" for phase "+phaseName );
228         optionMap.put( getKey( option ), getValue( option ) );
229     }
230
231 }
232
Popular Tags