KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > RadioScenePack


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
22 import soot.util.*;
23 import java.util.*;
24
25 /** A wrapper object for a pack of optimizations.
26  * Provides chain-like operations, except that the key is the phase name. */

27 public class RadioScenePack extends ScenePack
28 {
29     public RadioScenePack(String JavaDoc name) {
30         super(name);
31     }
32
33     protected void internalApply()
34     {
35         LinkedList enableds = new LinkedList();
36
37         for( Iterator tIt = this.iterator(); tIt.hasNext(); ) {
38
39             final Transform t = (Transform) tIt.next();
40             Map opts = PhaseOptions.v().getPhaseOptions( t );
41             if( !PhaseOptions.getBoolean( opts, "enabled" ) ) continue;
42             enableds.add( t );
43         }
44         if( enableds.size() == 0 ) {
45             G.v().out.println( "Exactly one phase in the pack "+getPhaseName()+
46                     " must be enabled. Currently, none of them are." );
47             throw new CompilationDeathException( CompilationDeathException.COMPILATION_ABORTED );
48         }
49         if( enableds.size() > 1 ) {
50             G.v().out.println( "Only one phase in the pack "+getPhaseName()+
51                     " may be enabled. The following are enabled currently: " );
52             for( Iterator tIt = enableds.iterator(); tIt.hasNext(); ) {
53                 final Transform t = (Transform) tIt.next();
54                 G.v().out.println( " "+t.getPhaseName() );
55             }
56             throw new CompilationDeathException( CompilationDeathException.COMPILATION_ABORTED );
57         }
58         for( Iterator tIt = enableds.iterator(); tIt.hasNext(); ) {
59             final Transform t = (Transform) tIt.next();
60             t.apply();
61         }
62     }
63
64     public void add(Transform t) {
65         super.add(t);
66         checkEnabled(t);
67     }
68     public void insertAfter(Transform t, String JavaDoc phaseName) {
69         super.insertAfter(t, phaseName);
70         checkEnabled(t);
71     }
72     public void insertBefore(Transform t, String JavaDoc phaseName) {
73         super.insertBefore(t, phaseName);
74         checkEnabled(t);
75     }
76     private void checkEnabled(Transform t) {
77         Map options = PhaseOptions.v().getPhaseOptions(t);
78         if( PhaseOptions.v().getBoolean( options, "enabled" ) ) {
79             // Enabling this one will disable all the others
80
PhaseOptions.v().setPhaseOption( t, "enabled:true" );
81         }
82     }
83 }
84
Popular Tags