1 package com.sun.org.apache.bcel.internal.verifier.structurals; 2 3 56 57 import com.sun.org.apache.bcel.internal.generic.Type; 58 import com.sun.org.apache.bcel.internal.generic.ReferenceType; 59 import com.sun.org.apache.bcel.internal.verifier.exc.*; 60 61 68 public class LocalVariables{ 69 70 private Type[] locals; 71 72 75 public LocalVariables(int maxLocals){ 76 locals = new Type[maxLocals]; 77 for (int i=0; i<maxLocals; i++){ 78 locals[i] = Type.UNKNOWN; 79 } 80 } 81 82 87 protected Object clone(){ 88 LocalVariables lvs = new LocalVariables(locals.length); 89 for (int i=0; i<locals.length; i++){ 90 lvs.locals[i] = this.locals[i]; 91 } 92 return lvs; 93 } 94 95 98 public Type get(int i){ 99 return locals[i]; 100 } 101 102 106 public LocalVariables getClone(){ 107 return (LocalVariables) this.clone(); 108 } 109 110 114 public int maxLocals(){ 115 return locals.length; 116 } 117 118 121 public void set(int i, Type type){ 122 if (type == Type.BYTE || type == Type.SHORT || type == Type.BOOLEAN || type == Type.CHAR){ 123 throw new AssertionViolatedException("LocalVariables do not know about '"+type+"'. Use Type.INT instead."); 124 } 125 locals[i] = type; 126 } 127 128 131 public boolean equals(Object o){ 132 if (!(o instanceof LocalVariables)) return false; 133 LocalVariables lv = (LocalVariables) o; 134 if (this.locals.length != lv.locals.length) return false; 135 for (int i=0; i<this.locals.length; i++){ 136 if (!this.locals[i].equals(lv.locals[i])){ 137 return false; 139 } 140 } 141 return true; 142 } 143 144 148 public void merge(LocalVariables lv){ 149 150 if (this.locals.length != lv.locals.length){ 151 throw new AssertionViolatedException("Merging LocalVariables of different size?!? From different methods or what?!?"); 152 } 153 154 for (int i=0; i<locals.length; i++){ 155 merge(lv, i); 156 } 157 } 158 159 164 private void merge(LocalVariables lv, int i){ 165 166 if ( (!(locals[i] instanceof UninitializedObjectType)) && (lv.locals[i] instanceof UninitializedObjectType) ){ 169 throw new StructuralCodeConstraintException("Backwards branch with an uninitialized object in the local variables detected."); 170 } 171 if ( (!(locals[i].equals(lv.locals[i]))) && (locals[i] instanceof UninitializedObjectType) && (lv.locals[i] instanceof UninitializedObjectType) ){ 173 throw new StructuralCodeConstraintException("Backwards branch with an uninitialized object in the local variables detected."); 174 } 175 if (locals[i] instanceof UninitializedObjectType){ 177 if (! (lv.locals[i] instanceof UninitializedObjectType)){ 178 locals[i] = ((UninitializedObjectType) locals[i]).getInitialized(); 179 } 180 } 181 if ((locals[i] instanceof ReferenceType) && (lv.locals[i] instanceof ReferenceType)){ 182 if (! locals[i].equals(lv.locals[i])){ Type sup = ((ReferenceType) locals[i]).firstCommonSuperclass((ReferenceType) (lv.locals[i])); 184 185 if (sup != null){ 186 locals[i] = sup; 187 } 188 else{ 189 throw new AssertionViolatedException("Could not load all the super classes of '"+locals[i]+"' and '"+lv.locals[i]+"'."); 191 } 192 } 193 } 194 else{ 195 if (! (locals[i].equals(lv.locals[i])) ){ 196 202 locals[i] = Type.UNKNOWN; 203 } 204 } 205 } 206 207 210 public String toString(){ 211 String s = new String (); 212 for (int i=0; i<locals.length; i++){ 213 s += Integer.toString(i)+": "+locals[i]+"\n"; 214 } 215 return s; 216 } 217 218 222 public void initializeObject(UninitializedObjectType u){ 223 for (int i=0; i<locals.length; i++){ 224 if (locals[i] == u){ 225 locals[i] = u.getInitialized(); 226 } 227 } 228 } 229 } 230 | Popular Tags |