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