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