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 ConstructorDeclarationAnnotation extends BlockStmtAnnotation implements java.lang.Comparable {
00041 private SootMethod sm = null;
00042 private Method m = null;
00043 private Vector annotations = null;
00044
00045
00046
00047
00048 public ConstructorDeclarationAnnotation(edu.ksu.cis.bandera.jjjc.node.Node node) {
00049 super(node);
00050 }
00051 public void apply(Switch sw)
00052 {
00053 ((AnnotationSwitch) sw).caseConstructorDeclarationAnnotation(this);
00054 }
00055
00056
00057
00058
00059 public Object clone() {
00060 ConstructorDeclarationAnnotation result = new ConstructorDeclarationAnnotation((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 ConstructorDeclarationAnnotation cda = (ConstructorDeclarationAnnotation) o;
00081 int i = getSootMethod().getParameterCount() - cda.getSootMethod().getParameterCount();
00082 if (i == 0) {
00083 return getSootMethod().toString().compareTo(getSootMethod().toString());
00084 } else {
00085 return i;
00086 }
00087 } else {
00088 return -1;
00089 }
00090 }
00091
00092
00093
00094
00095
00096 public Annotation getAnnotationAt(int index) {
00097 if (annotations == null) {
00098 annotations = getAllAnnotations(false);
00099 }
00100 return (Annotation) annotations.elementAt(index + 1);
00101 }
00102
00103
00104
00105
00106 public Method getConstructor() {
00107 return m;
00108 }
00109
00110
00111
00112
00113
00114 public Hashtable getDeclaredLocalVariablesUntil(Annotation annotation) {
00115 Hashtable result = new Hashtable();
00116 JimpleBody body = (JimpleBody) sm.getBody(Jimple.v());
00117 for (Enumeration e = m.getParameters().elements(); e.hasMoreElements();) {
00118 try {
00119 String name = ((Variable) e.nextElement()).getName().toString().trim();
00120 result.put(name, body.getLocal(name));
00121 } catch (Exception ex) {}
00122 }
00123 Hashtable table = super.getDeclaredLocalVariablesUntil(annotation);
00124 for (Enumeration e = table.keys(); e.hasMoreElements();) {
00125 Object key = e.nextElement();
00126 result.put(key, table.get(key));
00127 }
00128 return result;
00129 }
00130
00131
00132
00133
00134 public Hashtable getParameterLocals() {
00135 Hashtable result = new Hashtable();
00136 JimpleBody body = (JimpleBody) sm.getBody(Jimple.v());
00137 for (Enumeration e = m.getParameters().elements(); e.hasMoreElements();) {
00138 try {
00139 String name = ((Variable) e.nextElement()).getName().toString().trim();
00140 result.put(name, body.getLocal(name));
00141 } catch (Exception ex) {}
00142 }
00143 return result;
00144 }
00145
00146
00147
00148
00149 public SootMethod getSootMethod() {
00150 return sm;
00151 }
00152
00153
00154
00155
00156
00157 public int indexOf(Annotation annotation) {
00158 if (annotations == null) {
00159 annotations = getAllAnnotations(false);
00160 }
00161 return annotations.indexOf(annotation) + 1;
00162 }
00163
00164
00165
00166
00167 public void setConstructor(Method constructor) {
00168 m = constructor;
00169 }
00170
00171
00172
00173
00174 public void setSootMethod(SootMethod sootMethod) {
00175 sm = sootMethod;
00176 }
00177
00178
00179
00180
00181 public String toString() {
00182 String result = "";
00183 try {
00184 boolean isAbstractNative = Modifier.isAbstract(sm.getModifiers()) || Modifier.isNative(sm.getModifiers());
00185 result = Modifier.toString(sm.getModifiers()) + " " + m.getDeclaringClassOrInterface().getName().getLastIdentifier().trim() + "(";
00186 JimpleBody body = (JimpleBody) sm.getBody(Jimple.v());
00187 String parm = "";
00188 for (Enumeration e = m.getParameters().elements(); e.hasMoreElements();) {
00189 Variable v = (Variable) e.nextElement();
00190 if (body.declaresLocal(v.getName().toString().trim()) || isAbstractNative) {
00191 parm += (v.toString() + ", ");
00192 }
00193 }
00194 if (parm.length() > 1)
00195 parm = parm.substring(0, parm.length() - 2);
00196 result += (parm + ")");
00197 String exceptions = sm.getExceptions().toString().trim();
00198 exceptions = exceptions.substring(1, exceptions.length() - 1);
00199 if (sm.getExceptions().size() > 0) {
00200 result += (" throws " + exceptions);
00201 }
00202 } catch (Exception e) {}
00203 return result;
00204 }
00205 }