KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > parser > JimpleAST


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2000 Patrice Pominville
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 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27 package soot.jimple.parser;
28
29 import soot.jimple.parser.parser.*;
30 import soot.jimple.parser.lexer.*;
31 import soot.jimple.parser.node.*;
32 import soot.jimple.parser.analysis.*;
33 import java.io.*;
34 import java.util.*;
35
36 import soot.util.*;
37 import soot.*;
38
39 /**
40     This class encapsulates a JimpleAST instance and provides methods
41     to act on it.
42 */

43 public class JimpleAST
44 {
45     private Start mTree = null;
46     private HashMap methodToParsedBodyMap = null;
47
48     /** Constructs a JimpleAST and generates its parse tree from the given InputStream.
49      *
50      * @param aInputStream The InputStream to parse.
51      */

52     public JimpleAST(InputStream aJIS)
53     {
54         Parser p =
55             new Parser(new Lexer(
56                     new PushbackReader(new BufferedReader(
57                     new InputStreamReader(aJIS)), 1024)));
58         try {
59             mTree = p.parse();
60         } catch(ParserException e) {
61             throw new RuntimeException JavaDoc("Parser exception occurred: " + e);
62         } catch(LexerException e) {
63             throw new RuntimeException JavaDoc("Lexer exception occurred: " + e);
64         } catch(IOException e) {
65             throw new RuntimeException JavaDoc("IOException occurred: " + e);
66         }
67     }
68
69     /** Reads an entire class from jimple, creates the Soot objects &
70      * returns it. */

71     public SootClass createSootClass()
72     {
73         Walker w = new Walker(SootResolver.v());
74         mTree.apply(w);
75         return w.getSootClass();
76     }
77
78     /**
79      * Applies a SkeletonExtractorWalker to the given SootClass, using the given Resolver to resolve
80      * the reference types it contains. The given SootClass instance will be filled to contain
81      * a class skeleton: that is no Body instances will be created for the class' methods.
82      * @param sc a SootClass to fill in.
83      * @param resolver used to resolve the reference types found the given SootClass instance.
84      */

85     public void getSkeleton(SootClass sc)
86     {
87         Walker w = new SkeletonExtractorWalker(SootResolver.v(), sc);
88         mTree.apply(w);
89     }
90     
91
92     /** Returns a body corresponding to the parsed jimple for m.
93      * If necessary, applies the BodyExtractorWalker to initialize the bodies map.
94      * @param m the method we want to get a body for.
95      * @return the actual body for the given method.
96      */

97     public Body getBody(SootMethod m)
98     {
99         if (methodToParsedBodyMap == null)
100             stashBodiesForClass(m.getDeclaringClass());
101         return (Body)methodToParsedBodyMap.get(m);
102     }
103
104
105     /**
106      * Extracts the reference constant pool for this JimpleAST.
107      * @return the Set of RefTypes for the reference types contained this AST.
108      */

109     public Set getCstPool()
110     {
111         CstPoolExtractor cpe = new CstPoolExtractor(mTree);
112         return cpe.getCstPool();
113     }
114
115     /** Returns the SootResolver currently in use. */
116     public SootResolver getResolver()
117     {
118         return SootResolver.v();
119     }
120
121     /* Runs a Walker on the InputStream associated to this object.
122      * The SootClass which we want bodies for is passed as the argument.
123      */

124     private void stashBodiesForClass(SootClass sc)
125     {
126         methodToParsedBodyMap = new HashMap();
127
128         Walker w = new BodyExtractorWalker(sc, SootResolver.v(), methodToParsedBodyMap);
129
130         boolean oldPhantomValue = Scene.v().getPhantomRefs();
131
132         Scene.v().setPhantomRefs(true);
133         mTree.apply(w);
134         Scene.v().setPhantomRefs(oldPhantomValue);
135     }
136 } // Parse
137
Popular Tags