00001 package edu.ksu.cis.bandera.annotation;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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
00046
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
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
00074
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
00100
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
00111
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
00132
00133 public Method getMethod() {
00134 return m;
00135 }
00136
00137
00138
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
00154
00155 public SootMethod getSootMethod() {
00156 return sm;
00157 }
00158
00159
00160
00161
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
00172
00173 public void setMethod(Method method) {
00174 m = method;
00175 }
00176
00177
00178
00179
00180 public void setSootMethod(SootMethod sootMethod) {
00181 sm = sootMethod;
00182 }
00183
00184
00185
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 }