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

MethodDeclarationAnnotation.java

00001 package edu.ksu.cis.bandera.annotation;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 1999, 2000   Robby (robby@cis.ksu.edu)              *
00006  * All rights reserved.                                              *
00007  *                                                                   *
00008  * This work was done as a project in the SAnToS Laboratory,         *
00009  * Department of Computing and Information Sciences, Kansas State    *
00010  * University, USA (http://www.cis.ksu.edu/santos).                  *
00011  * It is understood that any modification not identified as such is  *
00012  * not covered by the 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 toolkit; 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 SAnToS projects, please visit the web-site *
00033  *                http://www.cis.ksu.edu/santos                      *
00034  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00035 import ca.mcgill.sable.soot.*;
00036 import ca.mcgill.sable.soot.jimple.*;
00037 import edu.ksu.cis.bandera.jjjc.symboltable.*;
00038 import java.util.*;
00039 import ca.mcgill.sable.util.*;
00040 public class MethodDeclarationAnnotation extends BlockStmtAnnotation implements java.lang.Comparable {
00041     private SootMethod sm;
00042     private Method m;
00043     private Vector annotations = null;
00044 /**
00045  * MethodDeclarationAnnotation constructor comment.
00046  * @param node edu.ksu.cis.bandera.jjjc.node.Node
00047  */
00048 public MethodDeclarationAnnotation(edu.ksu.cis.bandera.jjjc.node.Node node) {
00049     super(node);
00050 }
00051 public void apply(Switch sw)
00052 {
00053     ((AnnotationSwitch) sw).caseMethodDeclarationAnnotation(this);
00054 }
00055 /**
00056  * 
00057  * @return java.lang.Object
00058  */
00059 public Object clone() {
00060     MethodDeclarationAnnotation result = new MethodDeclarationAnnotation((edu.ksu.cis.bandera.jjjc.node.Node) node.clone());
00061 
00062     for (Enumeration e = containedAnnotations.elements(); e.hasMoreElements();) {
00063         result.addAnnotation((Annotation) ((Annotation) e.nextElement()).clone());
00064     }
00065 
00066     result.sm = sm;
00067     result.m = m;
00068     
00069     return result;
00070 }
00071 /**
00072  * 
00073  * @return int
00074  * @param o java.lang.Object
00075  */
00076 public int compareTo(Object o) {
00077     if (o instanceof FieldDeclarationAnnotation) {
00078         return 1;
00079     } else if (o instanceof ConstructorDeclarationAnnotation) {
00080         return 1;
00081     } else if (o instanceof MethodDeclarationAnnotation) {
00082         MethodDeclarationAnnotation mda = (MethodDeclarationAnnotation) o;
00083 
00084         int i = getMethod().getName().compareTo(mda.getMethod().getName());
00085         
00086         if (i != 0) return i;
00087         
00088         i = getSootMethod().getParameterCount() - mda.getSootMethod().getParameterCount();
00089 
00090         if (i != 0) return i;
00091         
00092         return getSootMethod().toString().compareTo(getSootMethod().toString());
00093     } else {
00094         return -1;
00095     }
00096 }
00097 /**
00098  * 
00099  * @return edu.ksu.cis.bandera.annotation.Annotation
00100  * @param index int
00101  */
00102 public Annotation getAnnotationAt(int index) {
00103     if (annotations == null) {
00104         annotations = getAllAnnotations(false);
00105     }
00106     return (Annotation) annotations.elementAt(index - 1);
00107 }
00108 /**
00109  * 
00110  * @return java.util.Hashtable
00111  * @param annotation edu.ksu.cis.bandera.annotation.Annotation
00112  */
00113 public Hashtable getDeclaredLocalVariablesUntil(Annotation annotation) {
00114     Hashtable result = new Hashtable();
00115     JimpleBody body = (JimpleBody) sm.getBody(Jimple.v());
00116     for (Enumeration e = m.getParameters().elements(); e.hasMoreElements();) {
00117         try {
00118             String name = ((Variable) e.nextElement()).getName().toString().trim();
00119             result.put(name, body.getLocal(name));
00120         } catch (Exception ex) {}
00121     }
00122     Hashtable table = super.getDeclaredLocalVariablesUntil(annotation);
00123     for (Enumeration e = table.keys(); e.hasMoreElements();) {
00124         Object key = e.nextElement();
00125         result.put(key, table.get(key));
00126     }
00127     return result;
00128 }
00129 /**
00130  * 
00131  * @return edu.ksu.cis.bandera.jjjc.symboltable.Method
00132  */
00133 public Method getMethod() {
00134     return m;
00135 }
00136 /**
00137  * 
00138  * @return java.util.Hashtable
00139  */
00140 public Hashtable getParameterLocals() {
00141     Hashtable result = new Hashtable();
00142     JimpleBody body = (JimpleBody) sm.getBody(Jimple.v());
00143     for (Enumeration e = m.getParameters().elements(); e.hasMoreElements();) {
00144         try {
00145             String name = ((Variable) e.nextElement()).getName().toString().trim();
00146             result.put(name, body.getLocal(name));
00147         } catch (Exception ex) {}
00148     }
00149     return result;
00150 }
00151 /**
00152  * 
00153  * @return ca.mcgill.sable.soot.SootMethod
00154  */
00155 public SootMethod getSootMethod() {
00156     return sm;
00157 }
00158 /**
00159  * 
00160  * @return int
00161  * @param annotation edu.ksu.cis.bandera.annotation.Annotation
00162  */
00163 public int indexOf(Annotation annotation) {
00164     if (annotations == null) {
00165         annotations = getAllAnnotations(false);
00166     }
00167     return annotations.indexOf(annotation) + 1;
00168 }
00169 /**
00170  * 
00171  * @param method edu.ksu.cis.bandera.jjjc.symboltable.Method
00172  */
00173 public void setMethod(Method method) {
00174     m = method;
00175 }
00176 /**
00177  * 
00178  * @param sootMethod ca.mcgill.sable.soot.SootMethod
00179  */
00180 public void setSootMethod(SootMethod sootMethod) {
00181     sm = sootMethod;
00182 }
00183 /**
00184  * 
00185  * @return java.lang.String
00186  */
00187 public String toString() {
00188     String result = "";
00189     try {
00190         boolean isAbstractNative = Modifier.isAbstract(sm.getModifiers()) || Modifier.isNative(sm.getModifiers());
00191         result = Modifier.toString(sm.getModifiers()) + " " + sm.getReturnType().toString().trim()
00192                 + " " + sm.getName().trim() + "(";
00193         JimpleBody body = (JimpleBody) sm.getBody(Jimple.v());
00194         String parm = "";
00195         for (Enumeration e = m.getParameters().elements(); e.hasMoreElements();) {
00196             Variable v = (Variable) e.nextElement();
00197             if (body.declaresLocal(v.getName().toString().trim()) || isAbstractNative) {
00198                 parm += (v.toString() + ", ");
00199             }
00200         }
00201         if (parm.length() > 1) 
00202             parm = parm.substring(0, parm.length() - 2);
00203         result += (parm + ")");
00204         String exceptions = sm.getExceptions().toString().trim();
00205         exceptions = exceptions.substring(1, exceptions.length() - 1);
00206         if (sm.getExceptions().size() > 0) {
00207             result += (" throws " + exceptions);
00208         }
00209     } catch (Exception e) {}
00210     return result;
00211 }
00212 }

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