KickJava   Java API By Example, From Geeks To Geeks.

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


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 /* Modified By Marc Berndl May 17th */
27
28
29 package soot.jimple.parser;
30
31 import soot.baf.*;
32 import soot.*;
33 import soot.jimple.*;
34
35 import soot.jimple.parser.parser.*;
36 import soot.jimple.parser.lexer.*;
37 import soot.jimple.parser.node.*;
38 import soot.jimple.parser.analysis.*;
39
40 import java.io.*;
41 import java.util.*;
42
43
44 /*
45    Walks a jimple AST and extracts the fields, and method signatures and produces
46    a new squeleton SootClass instance.
47 */

48 public class SkeletonExtractorWalker extends Walker
49 {
50            
51     public SkeletonExtractorWalker(SootResolver aResolver, SootClass aSootClass)
52     {
53         super(aSootClass, aResolver);
54     }
55
56     public SkeletonExtractorWalker(SootResolver aResolver)
57     {
58         super(aResolver);
59     }
60
61
62     public void caseAFile(AFile node)
63     {
64         inAFile(node);
65         {
66             Object JavaDoc temp[] = node.getModifier().toArray();
67             for(int i = 0; i < temp.length; i++)
68             {
69                 ((PModifier) temp[i]).apply(this);
70             }
71         }
72         if(node.getFileType() != null)
73         {
74             node.getFileType().apply(this);
75         }
76         if(node.getClassName() != null)
77         {
78             node.getClassName().apply(this);
79         }
80         
81
82     String JavaDoc className = (String JavaDoc) mProductions.removeLast();
83         
84         if(mSootClass == null) {
85             mSootClass = new SootClass(className);
86             mSootClass.setResolvingLevel(SootClass.SIGNATURES);
87         } else {
88             if(!className.equals(mSootClass.getName()))
89                 throw new RuntimeException JavaDoc("expected: " + className + ", but got: " + mSootClass.getName());
90         }
91         
92         if(node.getExtendsClause() != null)
93         {
94             node.getExtendsClause().apply(this);
95         }
96         if(node.getImplementsClause() != null)
97         {
98             node.getImplementsClause().apply(this);
99         }
100         if(node.getFileBody() != null)
101         {
102             node.getFileBody().apply(this);
103         }
104         outAFile(node);
105     }
106
107
108     public void outAFile(AFile node)
109     {
110         List implementsList = null;
111         String JavaDoc superClass = null;
112
113         String JavaDoc classType = null;
114         
115         if(node.getImplementsClause() != null) {
116         implementsList = (List) mProductions.removeLast();
117         }
118         if(node.getExtendsClause() != null) {
119         superClass = (String JavaDoc) mProductions.removeLast();
120
121         }
122     classType = (String JavaDoc) mProductions.removeLast();
123                 
124         int modifierFlags = processModifiers(node.getModifier());
125
126         
127         if(classType.equals("interface"))
128             modifierFlags |= Modifier.INTERFACE;
129
130         mSootClass.setModifiers(modifierFlags);
131                 
132         if(superClass != null) {
133             mSootClass.setSuperclass(mResolver.makeClassRef(superClass));
134         }
135         
136         if(implementsList != null) {
137             Iterator implIt = implementsList.iterator();
138             while(implIt.hasNext()) {
139                 SootClass interfaceClass = mResolver.makeClassRef((String JavaDoc) implIt.next());
140                 mSootClass.addInterface(interfaceClass);
141             }
142         }
143         
144     mProductions.addLast(mSootClass);
145     }
146
147
148
149     /*
150       member =
151       {field} modifier* type name semicolon |
152       {method} modifier* type name l_paren parameter_list? r_paren throws_clause? method_body;
153     */

154     
155        
156     public void caseAMethodMember(AMethodMember node)
157     {
158         inAMethodMember(node);
159         {
160             Object JavaDoc temp[] = node.getModifier().toArray();
161             for(int i = 0; i < temp.length; i++)
162                 {
163                     ((PModifier) temp[i]).apply(this);
164                 }
165         }
166         if(node.getType() != null)
167             {
168                 node.getType().apply(this);
169             }
170         if(node.getName() != null)
171             {
172                 node.getName().apply(this);
173             }
174         if(node.getLParen() != null)
175             {
176                 node.getLParen().apply(this);
177             }
178         if(node.getParameterList() != null)
179             {
180                 node.getParameterList().apply(this);
181             }
182         if(node.getRParen() != null)
183             {
184                 node.getRParen().apply(this);
185             }
186         if(node.getThrowsClause() != null)
187             {
188                 node.getThrowsClause().apply(this);
189             }
190         /*if(node.getMethodBody() != null)
191           {
192           node.getMethodBody().apply(this);
193           }*/

194         outAMethodMember(node);
195     }
196       public void outAMethodMember(AMethodMember node)
197     {
198         int modifier = 0;
199         Type type;
200         String JavaDoc name;
201         List parameterList = null;
202         List throwsClause = null;
203         JimpleBody methodBody = null;
204
205         /* if(node.getMethodBody() instanceof AFullMethodBody)
206             methodBody = (JimpleBody) mProductions.pop();
207         */

208         
209         if(node.getThrowsClause() != null)
210         throwsClause = (List) mProductions.removeLast();
211         
212         if(node.getParameterList() != null) {
213         parameterList = (List) mProductions.removeLast();
214         }
215         else {
216             parameterList = new ArrayList();
217         }
218
219     Object JavaDoc o = mProductions.removeLast();
220
221
222         name = (String JavaDoc) o;
223     type = (Type) mProductions.removeLast();
224         modifier = processModifiers(node.getModifier());
225
226         SootMethod method;
227
228         if(throwsClause != null)
229             method = new SootMethod(name, parameterList, type, modifier, throwsClause);
230         else
231             method = new SootMethod(name, parameterList, type, modifier);
232
233         mSootClass.addMethod(method);
234     }
235
236
237     /*
238       throws_clause =
239       throws class_name_list;
240     */

241     public void outAThrowsClause(AThrowsClause node)
242     {
243     List l = (List) mProductions.removeLast();
244
245         Iterator it = l.iterator();
246         List exceptionClasses = new ArrayList(l.size());
247       
248         while(it.hasNext()) {
249             String JavaDoc className = (String JavaDoc) it.next();
250           
251             exceptionClasses.add(mResolver.makeClassRef(className));
252         }
253
254     mProductions.addLast(exceptionClasses);
255     }
256 }
257
Popular Tags