Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

SootMethod.java

00001 package ca.mcgill.sable.soot;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Soot, a Java(TM) classfile optimization framework.                *
00005  * Copyright (C) 1997, 1998 Raja Vallee-Rai (kor@sable.mcgill.ca)    *
00006  * All rights reserved.                                              *
00007  *                                                                   *
00008  * This work was done as a project of the Sable Research Group,      *
00009  * School of Computer Science, McGill University, Canada             *
00010  * (http://www.sable.mcgill.ca/).  It is understood that any         *
00011  * modification not identified as such is not covered by the         *
00012  * preceding statement.                                              *
00013  *                                                                   *
00014  * This work is free software; you can redistribute it and/or        *
00015  * modify it under the terms of the GNU Library General Public       *
00016  * License as published by the Free Software Foundation; either      *
00017  * version 2 of the License, or (at your option) any later version.  *
00018  *                                                                   *
00019  * This work is distributed in the hope that it will be useful,      *
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00022  * Library General Public License for more details.                  *
00023  *                                                                   *
00024  * You should have received a copy of the GNU Library General Public *
00025  * License along with this library; if not, write to the             *
00026  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00027  * Boston, MA  02111-1307, USA.                                      *
00028  *                                                                   *
00029  * Java is a trademark of Sun Microsystems, Inc.                     *
00030  *                                                                   *
00031  * To submit a bug report, send a comment, or get the latest news on *
00032  * this project and other Sable Research Group projects, please      *
00033  * visit the web site: http://www.sable.mcgill.ca/                   *
00034  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00035 
00036 /*
00037  Reference Version
00038  -----------------
00039  This is the latest official version on which this file is based.
00040  The reference version is: $SootVersion: 1.beta.4 $
00041 
00042  Change History
00043  --------------
00044  A) Notes:
00045 
00046  Please use the following template.  Most recent changes should
00047  appear at the top of the list.
00048 
00049  - Modified on [date (March 1, 1900)] by [name]. [(*) if appropriate]
00050    [description of modification].
00051 
00052  Any Modification flagged with "(*)" was done as a project of the
00053  Sable Research Group, School of Computer Science,
00054  McGill University, Canada (http://www.sable.mcgill.ca/).
00055 
00056  You should add your copyright, using the following template, at
00057  the top of this file, along with other copyrights.
00058 
00059  *                                                                   *
00060  * Modifications by [name] are                                       *
00061  * Copyright (C) [year(s)] [your name (or company)].  All rights     *
00062  * reserved.                                                         *
00063  *                                                                   *
00064 
00065  B) Changes:
00066 
00067  - Modified on November 21, 1998 by Raja Vallee-Rai (kor@sable.mcgill.ca) (*)
00068    Added the isBodyStored method.
00069    
00070  - Modified on November 2, 1998 by Raja Vallee-Rai (kor@sable.mcgill.ca) (*)
00071    Repackaged all source files and performed extensive modifications.
00072    First initial release of Soot.
00073 
00074  - Modified on 15-Jun-1998 by Raja Vallee-Rai (kor@sable.mcgill.ca). (*)
00075    First internal release (Version 0.1).
00076 */
00077 
00078 import ca.mcgill.sable.util.*;
00079 import ca.mcgill.sable.soot.baf.*;
00080 
00081 public class SootMethod
00082 {
00083     String name;
00084     List parameterTypes;
00085     Type returnType;
00086 
00087     boolean isDeclared;
00088     SootClass declaringClass;
00089 
00090     int modifiers;
00091 
00092     List exceptions = new ArrayList();
00093 
00094     Map repToBody = new HashMap();
00095 
00096     /**
00097      * Hooks for coffi.  Do not use!
00098      */
00099 
00100     public ca.mcgill.sable.soot.coffi.ClassFile coffiClass;
00101 
00102     /**
00103      * Hooks for coffi.  Do not use!
00104      */
00105 
00106     public ca.mcgill.sable.soot.coffi.method_info coffiMethod;
00107 
00108 
00109     public SootMethod(String name, List parameterTypes, Type returnType)
00110     {
00111         this.name = name;
00112         this.parameterTypes = new ArrayList();
00113         this.parameterTypes.addAll(parameterTypes);
00114         this.returnType = returnType;
00115     }
00116     public SootMethod(String name, List parameterTypes, Type returnType, int modifiers)
00117     {
00118         this.name = name;
00119         this.parameterTypes = new ArrayList();
00120         this.parameterTypes.addAll(parameterTypes);
00121 
00122         this.returnType = returnType;
00123         this.modifiers = modifiers;
00124     }
00125     /*
00126     public void setInstListBody(InstListBody instListBody)
00127     {
00128         this.instListBody = instListBody;
00129     }
00130 
00131     public InstListBody getInstListBody()
00132     {
00133         if(instListBody == null)
00134         {
00135             instListBody = new InstListBody(this, coffiClass, coffiMethod);
00136             coffiClass = null;
00137             coffiMethod = null;
00138         }
00139 
00140         return instListBody;
00141     }
00142 */
00143     public void addException(SootClass e) throws AlreadyThrowsException
00144     {
00145         if(exceptions.contains(e))
00146             throw new AlreadyThrowsException(e.getName());
00147 
00148         exceptions.add(e);
00149     }
00150     /**
00151         Retrieves a stored representation for the body of the method.
00152      */
00153 
00154     public Body getBody(BodyRepresentation bodyRep)
00155     {
00156         if(bodyRep.equals(ClassFile.v()))
00157             return new ClassFileBody(this);
00158         else if(repToBody.containsKey(bodyRep))
00159             return (Body) repToBody.get(bodyRep);
00160         else
00161             throw new RuntimeException("Method does not have a stored representation for" + bodyRep);
00162     }
00163     /**
00164         Returns the declaration of this method.  Used at the tops of textual body representations (before the {}'s containing the code
00165         for representation.)
00166      */
00167 
00168     public String getDeclaration()
00169     {
00170         StringBuffer buffer = new StringBuffer();
00171 
00172         buffer.append(Modifier.toString(this.getModifiers()));
00173 
00174         if(buffer.length() != 0)
00175             buffer.append(" ");
00176 
00177         buffer.append(this.getReturnType().toString() + " " + this.getName());
00178         buffer.append("(");
00179 
00180         Iterator typeIt = this.getParameterTypes().iterator();
00181 
00182         if(typeIt.hasNext())
00183         {
00184             buffer.append(typeIt.next());
00185 
00186             while(typeIt.hasNext())
00187             {
00188                 buffer.append(", ");
00189                 buffer.append(typeIt.next());
00190             }
00191         }
00192 
00193         buffer.append(")");
00194 
00195         // Print exceptions
00196         {
00197             Iterator exceptionIt = this.getExceptions().iterator();
00198 
00199             if(exceptionIt.hasNext())
00200             {
00201                 buffer.append(" throws ");
00202                 buffer.append(((SootClass) exceptionIt.next()).getName());
00203 
00204                 while(exceptionIt.hasNext())
00205                 {
00206                     buffer.append(", ");
00207                     buffer.append(((SootClass) exceptionIt.next()).getName());
00208                 }
00209             }
00210 
00211         }
00212 
00213         return buffer.toString();
00214     }
00215     public SootClass getDeclaringClass() throws NotDeclaredException
00216     {
00217         if(!isDeclared)
00218             throw new NotDeclaredException(getName());
00219 
00220         return declaringClass;
00221     }
00222     /**
00223      * Returns a backed list of the exceptions thrown by this method.
00224      */
00225 
00226     public List getExceptions()
00227     {
00228         return exceptions;
00229     }
00230     public int getModifiers()
00231     {
00232         return modifiers;
00233     }
00234     public String getName()
00235     {
00236         return name;
00237     }
00238     public int getParameterCount()
00239     {
00240         return parameterTypes.size();
00241     }
00242     public Type getParameterType(int n)
00243     {
00244         return (Type) parameterTypes.get(n);
00245     }
00246     /**
00247      * Returns a backed list of the parameter types of this method.
00248      */
00249 
00250     public List getParameterTypes()
00251     {
00252         return parameterTypes;
00253     }
00254     public Type getReturnType()
00255     {
00256         return returnType;
00257     }
00258     /**
00259         Returns the Soot signature of this method.  Used to refer to methods unambiguously.
00260      */
00261 
00262     public String getSignature()
00263     {
00264         StringBuffer buffer = new StringBuffer();
00265 
00266         buffer.append(getDeclaringClass().getName());
00267         buffer.append(".");
00268         buffer.append(getName());
00269         buffer.append("(");
00270 
00271         Iterator typeIt = getParameterTypes().iterator();
00272 
00273         if(typeIt.hasNext())
00274         {
00275             buffer.append(typeIt.next());
00276 
00277             while(typeIt.hasNext())
00278             {
00279                 buffer.append(",");
00280                 buffer.append(typeIt.next());
00281             }
00282         }
00283 
00284         buffer.append(")");
00285 
00286         buffer.append(":" + getReturnType().toString());
00287 
00288         return buffer.toString();
00289     }
00290     /**
00291         Determines if a representation of the given type is stored for the body.
00292      */
00293 
00294     public boolean isBodyStored(BodyRepresentation bodyRep)
00295     {
00296         return repToBody.containsKey(bodyRep);
00297     }
00298     public boolean isDeclared()
00299     {
00300         return isDeclared;
00301     }
00302     /**
00303      * For convenience.
00304      */
00305 
00306     public boolean isStatic()
00307     {
00308         return Modifier.isStatic(this.getModifiers());
00309     }
00310     public void removeException(SootClass e) throws DoesNotThrowException
00311     {
00312         if(!exceptions.contains(e))
00313             throw new DoesNotThrowException(e.getName());
00314     }
00315     public void setModifiers(int modifiers)
00316     {
00317         this.modifiers = modifiers;
00318     }
00319     public void setName(String name)
00320     {
00321         this.name = name;
00322     }
00323     public void setParameterTypes(List parameterTypes)
00324     {
00325         this.parameterTypes = new ArrayList();
00326         this.parameterTypes.addAll(parameterTypes);
00327     }
00328     public void setReturnType(Type t)
00329     {
00330         returnType = t;
00331     }
00332     /*
00333     public SootMethod(String name, List parameterTypes, Type returnType, int modifiers,
00334         InstListBody instListBody)
00335     {
00336         this.name = name;
00337         this.parameterTypes = new ArrayList();
00338         this.parameterTypes.addAll(parameterTypes);
00339         this.returnType = returnType;
00340         this.modifiers = modifiers;
00341 
00342         this.instListBody = instListBody;
00343     }
00344     */
00345 
00346     /*
00347     public SootMethod(String name, List parameterTypes, Type returnType, InstListBody instListBody)
00348     {
00349         this.name = name;
00350         this.parameterTypes = new ArrayList();
00351         this.parameterTypes.addAll(parameterTypes);
00352         this.returnType = returnType;
00353         this.instListBody = instListBody;
00354     }
00355  */
00356 
00357     public void setSource(ca.mcgill.sable.soot.coffi.ClassFile coffiClass,
00358         ca.mcgill.sable.soot.coffi.method_info coffiMethod)
00359     {
00360         this.coffiClass = coffiClass;
00361         this.coffiMethod = coffiMethod;
00362     }
00363     /**
00364         Stores a representation for the body of the method.
00365      */
00366 
00367     public void storeBody(BodyRepresentation r, Body b)
00368     {
00369         repToBody.put(r, b);
00370     }
00371     public boolean throwsException(SootClass e)
00372     {
00373         return exceptions.contains(e);
00374     }
00375     public String toString()
00376     {
00377         return getSignature();
00378     }
00379 }

Generated at Thu Feb 7 06:55:20 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001