KickJava   Java API By Example, From Geeks To Geeks.

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


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 package soot.jimple.parser;
27 import soot.options.*;
28
29 import soot.baf.*;
30 import soot.*;
31 import soot.jimple.*;
32
33 import soot.jimple.parser.parser.*;
34 import soot.jimple.parser.lexer.*;
35 import soot.jimple.parser.node.*;
36 import soot.jimple.parser.analysis.*;
37
38 import java.io.*;
39 import java.util.*;
40
41
42 /**
43  * Walks a jimple AST and constructs the method bodies for all the methods of
44  * the SootClass associated with this walker (see constructor).
45  * note: Contrary to the plain "Walker", this walker does not create a SootClass,
46  * or interact with the scene. It merely adds method bodies for each of the methods of
47  * the SootClass it was initialized with.
48  */

49
50 /* Modified By Marc Berndl May 17th */
51    
52 public class BodyExtractorWalker extends Walker
53 {
54     Map methodToParsedBodyMap;
55
56     /** Constructs a walker, and attaches it to the given SootClass, sending bodies to
57      * the given methodToParsedBodyMap. */

58     public BodyExtractorWalker(SootClass sc, SootResolver resolver, Map methodToParsedBodyMap)
59     {
60         super(sc, resolver);
61         this.methodToParsedBodyMap = methodToParsedBodyMap;
62     }
63     
64     /*
65       file =
66       modifier* file_type class_name extends_clause? implements_clause? file_body;
67     */

68     public void caseAFile(AFile node)
69     {
70         inAFile(node);
71         {
72             Object JavaDoc temp[] = node.getModifier().toArray();
73             for(int i = 0; i < temp.length; i++)
74             {
75                 ((PModifier) temp[i]).apply(this);
76             }
77         }
78         if(node.getFileType() != null)
79         {
80             node.getFileType().apply(this);
81         }
82         if(node.getClassName() != null)
83         {
84             node.getClassName().apply(this);
85         }
86         
87     String JavaDoc className = (String JavaDoc) mProductions.removeLast();
88         if(!className.equals(mSootClass.getName()))
89             throw new RuntimeException JavaDoc("expected: " + className + ", but got: " + mSootClass.getName());
90
91         if(node.getExtendsClause() != null)
92         {
93             node.getExtendsClause().apply(this);
94         }
95         if(node.getImplementsClause() != null)
96         {
97             node.getImplementsClause().apply(this);
98         }
99         if(node.getFileBody() != null)
100         {
101             node.getFileBody().apply(this);
102         }
103         outAFile(node);
104     }
105
106     public void outAFile(AFile node)
107     {
108         if(node.getImplementsClause() != null)
109         mProductions.removeLast(); // implements_clause
110

111         if(node.getExtendsClause() != null)
112         mProductions.removeLast(); // extends_clause
113

114     mProductions.removeLast(); // file_type
115
mProductions.addLast(mSootClass);
116     }
117
118
119     /*
120       member =
121       {field} modifier* type name semicolon |
122       {method} modifier* type name l_paren parameter_list? r_paren throws_clause? method_body;
123     */

124     public void outAFieldMember(AFieldMember node)
125     {
126     mProductions.removeLast(); // name
127
mProductions.removeLast(); // type
128
}
129
130     public void outAMethodMember(AMethodMember node)
131     {
132         int modifier = 0;
133         Type type;
134         String JavaDoc name;
135         List parameterList = new ArrayList();
136         List throwsClause = null;
137         JimpleBody methodBody = null;
138
139         if(node.getMethodBody() instanceof AFullMethodBody)
140         methodBody = (JimpleBody) mProductions.removeLast();
141         
142         if(node.getThrowsClause() != null)
143         throwsClause = (List) mProductions.removeLast();
144         
145         if(node.getParameterList() != null)
146         parameterList = (List) mProductions.removeLast();
147
148     name = (String JavaDoc) mProductions.removeLast(); // name
149
type = (Type) mProductions.removeLast(); // type
150
SootMethod sm = null;
151         if (mSootClass.declaresMethod(SootMethod.getSubSignature(name, parameterList, type)))
152         {
153             sm = mSootClass.getMethod(SootMethod.getSubSignature(name, parameterList, type));
154             if (Options.v().verbose())
155                 G.v().out.println("[Jimple parser] " + SootMethod.getSubSignature(name, parameterList, type));
156         }
157         else
158         {
159             G.v().out.println("[!!! Couldn't parse !!] " + SootMethod.getSubSignature(name, parameterList, type));
160
161         
162             G.v().out.println("[!] Methods in class are:");
163             Iterator it = mSootClass.methodIterator();
164             while(it.hasNext()) {
165                 SootMethod next = (SootMethod) it.next();
166                 G.v().out.println(next.getSubSignature());
167             }
168             
169         }
170
171         if(sm.isConcrete())
172         {
173           if (Options.v().verbose())
174               G.v().out.println("[Parsed] "+sm.getDeclaration());
175
176           methodBody.setMethod(sm);
177           methodToParsedBodyMap.put(sm, methodBody);
178         }
179         else if(node.getMethodBody() instanceof AFullMethodBody) {
180             if(sm.isPhantom() && Options.v().verbose())
181                G.v().out.println("[jimple parser] phantom method!");
182             throw new RuntimeException JavaDoc("Impossible: !concrete => ! instanceof " + sm.getName() );
183         }
184     }
185   
186 }
187
Popular Tags