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 edu.ksu.cis.bandera.jjjc.node.*;
00036 import edu.ksu.cis.bandera.jjjc.util.*;
00037 import ca.mcgill.sable.soot.*;
00038 import ca.mcgill.sable.soot.jimple.*;
00039 import ca.mcgill.sable.util.*;
00040 import java.util.*;
00041 public class ForStmtAnnotation extends ControlFlowAnnotation {
00042 private Annotation initAnnotation = null;
00043 private Annotation updateAnnotation = null;
00044 private Hashtable declaredLocals = new Hashtable();
00045 private Type type = null;
00046 private int modifiers = 0;
00047
00048
00049
00050
00051 public ForStmtAnnotation(edu.ksu.cis.bandera.jjjc.node.Node node) {
00052 super(node);
00053 }
00054
00055
00056
00057
00058
00059 public void addDeclaredLocal(String actualName, Local local) {
00060 declaredLocals.put(actualName, local);
00061 }
00062 public void apply(Switch sw)
00063 {
00064 ((AnnotationSwitch) sw).caseForStmtAnnotation(this);
00065 }
00066
00067
00068
00069
00070 public Object clone() {
00071 ForStmtAnnotation result = new ForStmtAnnotation((Node) node.clone());
00072
00073
00074
00075
00076
00077
00078
00079
00080 if (initAnnotation != null) result.setInitAnnotation((Annotation) initAnnotation.clone());
00081 if (blockAnnotation != null) result.setBlockAnnotation((Annotation) blockAnnotation.clone());
00082 if (updateAnnotation != null) result.setUpdateAnnotation((Annotation) updateAnnotation.clone());
00083 result.setDeclaredLocals((Hashtable) declaredLocals.clone());
00084 result.setIndefinite(indefinite);
00085
00086 return result;
00087 }
00088
00089
00090
00091
00092
00093 public Vector getAllAnnotations(boolean includeSequential) {
00094 Vector result = new Vector();
00095 result.addElement(this);
00096 for (Enumeration e = blockAnnotation.getAllAnnotations(includeSequential).elements(); e.hasMoreElements();) {
00097 result.addElement(e.nextElement());
00098 }
00099 return result;
00100 }
00101
00102
00103
00104
00105 public Stmt getBackpointStmt() {
00106 Stmt result = null;
00107
00108 if (updateAnnotation != null) {
00109 Stmt[] stmts = updateAnnotation.getStatements();
00110 if (stmts.length > 0) result = stmts[stmts.length - 1];
00111 }
00112 return result;
00113 }
00114
00115
00116
00117
00118
00119 public Annotation getContainingAnnotation(Stmt stmt) throws AnnotationException {
00120 Vector result = new Vector();
00121
00122 if (initAnnotation != null) {
00123 Annotation a = initAnnotation.getContainingAnnotation(stmt);
00124 if (a != null) result.addElement(a);
00125 }
00126
00127 Annotation a = super.getContainingAnnotation(stmt);
00128 if (a != null) result.addElement(a);
00129
00130 if (updateAnnotation != null) {
00131 a = updateAnnotation.getContainingAnnotation(stmt);
00132 if (a != null) result.addElement(a);
00133 }
00134
00135 int size = result.size();
00136 if (size == 0) return null;
00137 else if (size == 1) return (Annotation) result.elementAt(0);
00138 throw new AnnotationException("Statement " + stmt + " is contained in two or more annotations");
00139 }
00140
00141
00142
00143
00144 public Hashtable getDeclaredLocal() {
00145 return declaredLocals;
00146 }
00147
00148
00149
00150
00151 public Hashtable getDeclaredLocals() {
00152 return declaredLocals;
00153 }
00154
00155
00156
00157
00158 public Annotation getInitAnnotation() {
00159 return initAnnotation;
00160 }
00161
00162
00163
00164
00165 public int getModifiers() {
00166 return modifiers;
00167 }
00168
00169
00170
00171
00172 public Stmt[] getStatements() {
00173 Stmt[] initStmts = (initAnnotation != null) ? initAnnotation.getStatements() : new Stmt[0];
00174 Stmt[] testStmts = toArray();
00175 Stmt[] blockStmts = (blockAnnotation != null)? blockAnnotation.getStatements() : new Stmt[0];
00176 Stmt[] updateStmts = (updateAnnotation != null) ? updateAnnotation.getStatements() : new Stmt[0];
00177
00178 Stmt[] result = new Stmt[initStmts.length + testStmts.length + blockStmts.length + updateStmts.length];
00179
00180 int index = initStmts.length;
00181
00182 for (int i = 0; i < index; i++) {
00183 result[i] = initStmts[i];
00184 }
00185
00186 for (int i = 0; i < testStmts.length; i++) {
00187 result[index + i] = testStmts[i];
00188 }
00189
00190 index += testStmts.length;
00191
00192 for (int i = 0; i < blockStmts.length; i++) {
00193 result[index + i] = blockStmts[i];
00194 }
00195
00196 index += blockStmts.length;
00197
00198 for (int i = 0; i < updateStmts.length; i++) {
00199 result[index + i] = updateStmts[i];
00200 }
00201
00202 return result;
00203 }
00204
00205
00206
00207
00208 public Type getType() {
00209 return type;
00210 }
00211
00212
00213
00214
00215 public Annotation getUpdateAnnotation() {
00216 return updateAnnotation;
00217 }
00218
00219
00220
00221
00222
00223 public boolean removeStmt(Stmt stmt) throws AnnotationException {
00224 return super.removeStmt(stmt) || initAnnotation.removeStmt(stmt)
00225 || updateAnnotation.removeStmt(stmt);
00226 }
00227
00228
00229
00230
00231
00232 public boolean removeStmtByMark(Stmt stmt) throws AnnotationException {
00233 return super.removeStmtByMark(stmt) || initAnnotation.removeStmtByMark(stmt)
00234 || updateAnnotation.removeStmtByMark(stmt);
00235 }
00236
00237
00238
00239
00240
00241
00242 public boolean replaceStmt(Stmt oldStmt, Stmt newStmt) throws AnnotationException {
00243 return super.replaceStmt(oldStmt, newStmt) || initAnnotation.replaceStmt(oldStmt, newStmt)
00244 || updateAnnotation.replaceStmt(oldStmt, newStmt);
00245 }
00246
00247
00248
00249
00250
00251
00252 public boolean replaceStmtByMark(Stmt oldStmt, Stmt newStmt) throws AnnotationException {
00253 return super.replaceStmtByMark(oldStmt, newStmt) || initAnnotation.replaceStmtByMark(oldStmt, newStmt)
00254 || updateAnnotation.replaceStmtByMark(oldStmt, newStmt);
00255 }
00256
00257
00258
00259
00260 public void setDeclaredLocals(Hashtable table) {
00261 declaredLocals = table;
00262 }
00263
00264
00265
00266
00267 public void setInitAnnotation(Annotation annotation) {
00268 initAnnotation = annotation;
00269 initAnnotation.setParent(this);
00270 }
00271
00272
00273
00274
00275 public void setModifiers(int modifiers) {
00276 this.modifiers = modifiers;
00277 }
00278
00279
00280
00281
00282 public void setType(Type type) {
00283 this.type = type;
00284 }
00285
00286
00287
00288
00289 public void setUpdateAnnotation(Annotation annotation) {
00290 updateAnnotation = annotation;
00291 updateAnnotation.setParent(this);
00292 }
00293
00294
00295
00296
00297 public String toString() {
00298 AForStmt node = (AForStmt) this.node;
00299 String result = "for (";
00300 if(node.getForInit() != null) result += node.getForInit().toString().trim();
00301
00302 result += "; ";
00303
00304 if(node.getExp() != null) result += node.getExp().toString().trim();
00305
00306 result += "; ";
00307
00308 Object temp[] = node.getForUpdate().toArray();
00309 for(int i = 0; i < temp.length - 1; i++) {
00310 result += (temp[i].toString() + ", ");
00311 }
00312
00313 if (temp.length > 0) result += temp[temp.length - 1].toString().trim();
00314
00315 result += ")";
00316
00317 return result;
00318 }
00319
00320
00321
00322
00323 public void validate(JimpleBody body) {
00324 if (initAnnotation != null) initAnnotation.validate(body);
00325
00326 super.validate(body);
00327
00328 if (updateAnnotation != null) updateAnnotation.validate(body);
00329
00330 for (Enumeration e = declaredLocals.keys(); e.hasMoreElements();) {
00331 String actualName = (String) e.nextElement();
00332 String jimpleName = ((Local) declaredLocals.get(actualName)).getName().trim();
00333 if (!body.declaresLocal(jimpleName)) {
00334 declaredLocals.remove(actualName);
00335 }
00336 }
00337 }
00338 }