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 IfStmtAnnotation extends ConditionalAnnotation {
00041 private Annotation thenAnnotation = null;
00042 private Annotation annotation = null;
00043 private Annotation elseAnnotation = null;
00044
00045
00046
00047
00048 public IfStmtAnnotation(edu.ksu.cis.bandera.jjjc.node.Node node) {
00049 super(node);
00050 }
00051 public void apply(Switch sw)
00052 {
00053 ((AnnotationSwitch) sw).caseIfStmtAnnotation(this);
00054 }
00055
00056
00057
00058
00059 public Object clone() {
00060 IfStmtAnnotation result = new IfStmtAnnotation((Node) node.clone());
00061
00062
00063
00064
00065
00066
00067
00068 if (thenAnnotation != null) result.setThenAnnotation((Annotation) thenAnnotation.clone());
00069 if (annotation != null) result.setAnnotation((Annotation) annotation.clone());
00070 if (elseAnnotation != null) result.setElseAnnotation((Annotation) elseAnnotation.clone());
00071
00072 return result;
00073 }
00074
00075
00076
00077
00078
00079 public Vector getAllAnnotations(boolean includeSequential) {
00080 Vector result = new Vector();
00081 result.addElement(this);
00082 for (Enumeration e = thenAnnotation.getAllAnnotations(includeSequential).elements(); e.hasMoreElements();) {
00083 result.addElement(e.nextElement());
00084 }
00085 for (Enumeration e = elseAnnotation.getAllAnnotations(includeSequential).elements(); e.hasMoreElements();) {
00086 result.addElement(e.nextElement());
00087 }
00088 return result;
00089 }
00090
00091
00092
00093
00094 public Annotation getAnnotation() {
00095 return annotation;
00096 }
00097
00098
00099
00100
00101
00102 public Annotation getContainingAnnotation(Stmt stmt) throws AnnotationException {
00103 Vector result = new Vector();
00104
00105 Annotation a = super.getContainingAnnotation(stmt);
00106 if (a != null) result.addElement(a);
00107
00108 if (thenAnnotation != null) {
00109 a = thenAnnotation.getContainingAnnotation(stmt);
00110 if (a != null) result.addElement(a);
00111 }
00112
00113 if (annotation != null) {
00114 a = annotation.getContainingAnnotation(stmt);
00115 if (a != null) result.addElement(a);
00116 }
00117
00118 if (elseAnnotation != null) {
00119 a = elseAnnotation.getContainingAnnotation(stmt);
00120 if (a != null) result.addElement(a);
00121 }
00122
00123 int size = result.size();
00124 if (size == 0) return null;
00125 else if (size == 1) return (Annotation) result.elementAt(0);
00126 throw new AnnotationException("Statement " + stmt + " is contained in two or more annotations");
00127 }
00128
00129
00130
00131
00132 public Annotation getElseAnnotation() {
00133 return elseAnnotation;
00134 }
00135
00136
00137
00138
00139 public Stmt[] getStatements() {
00140 Stmt[] testStmts = toArray();
00141 Stmt[] thenStmts = (thenAnnotation != null) ? thenAnnotation.getStatements() : new Stmt[0];
00142 Stmt[] stmts = (annotation != null) ? annotation.getStatements() : new Stmt[0];
00143 Stmt[] elseStmts = (elseAnnotation != null) ? elseAnnotation.getStatements() : new Stmt[0];
00144
00145 Stmt[] result = new Stmt[testStmts.length + thenStmts.length + stmts.length + elseStmts.length];
00146
00147 for (int i = 0; i < testStmts.length; i++) {
00148 result[i] = testStmts[i];
00149 }
00150
00151 int index = testStmts.length;
00152
00153 for (int i = 0; i < thenStmts.length; i++) {
00154 result[index + i] = thenStmts[i];
00155 }
00156
00157 index += thenStmts.length;
00158
00159 for (int i = 0; i < stmts.length; i++) {
00160 result[index + i] = stmts[i];
00161 }
00162
00163 index += stmts.length;
00164
00165 for (int i = 0; i < elseStmts.length; i++) {
00166 result[index + i] = elseStmts[i];
00167 }
00168
00169 return result;
00170 }
00171
00172
00173
00174
00175 public Annotation getThenAnnotation() {
00176 return thenAnnotation;
00177 }
00178
00179
00180
00181
00182
00183 public boolean removeStmt(Stmt stmt) throws AnnotationException {
00184 Annotation a = getContainingAnnotation(stmt);
00185 if (a == null) return false;
00186 else if (a == this) return remove(stmt);
00187 else return a.removeStmt(stmt);
00188 }
00189
00190
00191
00192
00193
00194 public boolean removeStmtByMark(Stmt stmt) throws AnnotationException {
00195 Annotation a = getContainingAnnotation(stmt);
00196 if (a == null) return false;
00197 else if (a == this) return removeByMark(stmt);
00198 else return a.removeStmtByMark(stmt);
00199 }
00200
00201
00202
00203
00204
00205
00206 public boolean replaceStmt(Stmt oldStmt, Stmt newStmt) throws AnnotationException {
00207 Annotation a = getContainingAnnotation(oldStmt);
00208 if (a == null) return false;
00209 else if (a == this) return replace(oldStmt, newStmt);
00210 else return a.replaceStmt(oldStmt, newStmt);
00211 }
00212
00213
00214
00215
00216
00217
00218 public boolean replaceStmtByMark(Stmt oldStmt, Stmt newStmt) throws AnnotationException {
00219 Annotation a = getContainingAnnotation(oldStmt);
00220 if (a == null) return false;
00221 else if (a == this) return replaceByMark(oldStmt, newStmt);
00222 else return a.replaceStmtByMark(oldStmt, newStmt);
00223 }
00224
00225
00226
00227
00228 public void setAnnotation(Annotation annotation) {
00229 this.annotation = annotation;
00230 annotation.setParent(this);
00231 }
00232
00233
00234
00235
00236 public void setElseAnnotation(Annotation annotation) {
00237 elseAnnotation = annotation;
00238 elseAnnotation.setParent(this);
00239 }
00240
00241
00242
00243
00244 public void setThenAnnotation(Annotation annotation) {
00245 thenAnnotation = annotation;
00246 thenAnnotation.setParent(this);
00247 }
00248
00249
00250
00251
00252 public String toString() {
00253 return "if (" + ((AIfStmt) node).getExp().toString().trim() + ")";
00254 }
00255
00256
00257
00258
00259 public void validate(JimpleBody body) {
00260 super.validate(body);
00261 if (thenAnnotation != null) thenAnnotation.validate(body);
00262 if (annotation != null) annotation.validate(body);
00263 if (elseAnnotation != null) elseAnnotation.validate(body);
00264 }
00265 }