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