1 package com.sun.org.apache.bcel.internal.classfile; 2 3 56 import java.util.Stack ; 57 58 67 public class DescendingVisitor implements Visitor { 68 private JavaClass clazz; 69 private Visitor visitor; 70 private Stack stack = new Stack (); 71 72 74 public Object predecessor() { 75 return predecessor(0); 76 } 77 78 82 public Object predecessor(int level) { 83 int size = stack.size(); 84 85 if((size < 2) || (level < 0)) 86 return null; 87 else 88 return stack.elementAt(size - (level + 2)); } 90 91 93 public Object current() { 94 return stack.peek(); 95 } 96 97 101 public DescendingVisitor(JavaClass clazz, Visitor visitor) { 102 this.clazz = clazz; 103 this.visitor = visitor; 104 } 105 106 109 public void visit() { clazz.accept(this); } 110 111 public void visitJavaClass(JavaClass clazz) { 112 stack.push(clazz); 113 clazz.accept(visitor); 114 115 Field[] fields = clazz.getFields(); 116 for(int i=0; i < fields.length; i++) 117 fields[i].accept(this); 118 119 Method[] methods = clazz.getMethods(); 120 for(int i=0; i < methods.length; i++) 121 methods[i].accept(this); 122 123 Attribute[] attributes = clazz.getAttributes(); 124 for(int i=0; i < attributes.length; i++) 125 attributes[i].accept(this); 126 127 clazz.getConstantPool().accept(this); 128 stack.pop(); 129 } 130 131 public void visitField(Field field) { 132 stack.push(field); 133 field.accept(visitor); 134 135 Attribute[] attributes = field.getAttributes(); 136 for(int i=0; i < attributes.length; i++) 137 attributes[i].accept(this); 138 stack.pop(); 139 } 140 141 public void visitConstantValue(ConstantValue cv) { 142 stack.push(cv); 143 cv.accept(visitor); 144 stack.pop(); 145 } 146 147 public void visitMethod(Method method) { 148 stack.push(method); 149 method.accept(visitor); 150 151 Attribute[] attributes = method.getAttributes(); 152 for(int i=0; i < attributes.length; i++) 153 attributes[i].accept(this); 154 155 stack.pop(); 156 } 157 158 public void visitExceptionTable(ExceptionTable table) { 159 stack.push(table); 160 table.accept(visitor); 161 stack.pop(); 162 } 163 164 public void visitCode(Code code) { 165 stack.push(code); 166 code.accept(visitor); 167 168 CodeException[] table = code.getExceptionTable(); 169 for(int i=0; i < table.length; i++) 170 table[i].accept(this); 171 172 Attribute[] attributes = code.getAttributes(); 173 for(int i=0; i < attributes.length; i++) 174 attributes[i].accept(this); 175 stack.pop(); 176 } 177 178 public void visitCodeException(CodeException ce) { 179 stack.push(ce); 180 ce.accept(visitor); 181 stack.pop(); 182 } 183 184 public void visitLineNumberTable(LineNumberTable table) { 185 stack.push(table); 186 table.accept(visitor); 187 188 LineNumber[] numbers = table.getLineNumberTable(); 189 for(int i=0; i < numbers.length; i++) 190 numbers[i].accept(this); 191 stack.pop(); 192 } 193 194 public void visitLineNumber(LineNumber number) { 195 stack.push(number); 196 number.accept(visitor); 197 stack.pop(); 198 } 199 200 public void visitLocalVariableTable(LocalVariableTable table) { 201 stack.push(table); 202 table.accept(visitor); 203 204 LocalVariable[] vars = table.getLocalVariableTable(); 205 for(int i=0; i < vars.length; i++) 206 vars[i].accept(this); 207 stack.pop(); 208 } 209 210 public void visitStackMap(StackMap table) { 211 stack.push(table); 212 table.accept(visitor); 213 214 StackMapEntry[] vars = table.getStackMap(); 215 216 for(int i=0; i < vars.length; i++) 217 vars[i].accept(this); 218 stack.pop(); 219 } 220 221 public void visitStackMapEntry(StackMapEntry var) { 222 stack.push(var); 223 var.accept(visitor); 224 stack.pop(); 225 } 226 227 public void visitLocalVariable(LocalVariable var) { 228 stack.push(var); 229 var.accept(visitor); 230 stack.pop(); 231 } 232 233 public void visitConstantPool(ConstantPool cp) { 234 stack.push(cp); 235 cp.accept(visitor); 236 237 Constant[] constants = cp.getConstantPool(); 238 for(int i=1; i < constants.length; i++) { 239 if(constants[i] != null) 240 constants[i].accept(this); 241 } 242 243 stack.pop(); 244 } 245 246 public void visitConstantClass(ConstantClass constant) { 247 stack.push(constant); 248 constant.accept(visitor); 249 stack.pop(); 250 } 251 252 public void visitConstantDouble(ConstantDouble constant) { 253 stack.push(constant); 254 constant.accept(visitor); 255 stack.pop(); 256 } 257 258 public void visitConstantFieldref(ConstantFieldref constant) { 259 stack.push(constant); 260 constant.accept(visitor); 261 stack.pop(); 262 } 263 264 public void visitConstantFloat(ConstantFloat constant) { 265 stack.push(constant); 266 constant.accept(visitor); 267 stack.pop(); 268 } 269 270 public void visitConstantInteger(ConstantInteger constant) { 271 stack.push(constant); 272 constant.accept(visitor); 273 stack.pop(); 274 } 275 276 public void visitConstantInterfaceMethodref(ConstantInterfaceMethodref constant) { 277 stack.push(constant); 278 constant.accept(visitor); 279 stack.pop(); 280 } 281 282 public void visitConstantLong(ConstantLong constant) { 283 stack.push(constant); 284 constant.accept(visitor); 285 stack.pop(); 286 } 287 288 public void visitConstantMethodref(ConstantMethodref constant) { 289 stack.push(constant); 290 constant.accept(visitor); 291 stack.pop(); 292 } 293 294 public void visitConstantNameAndType(ConstantNameAndType constant) { 295 stack.push(constant); 296 constant.accept(visitor); 297 stack.pop(); 298 } 299 300 public void visitConstantString(ConstantString constant) { 301 stack.push(constant); 302 constant.accept(visitor); 303 stack.pop(); 304 } 305 306 public void visitConstantUtf8(ConstantUtf8 constant) { 307 stack.push(constant); 308 constant.accept(visitor); 309 stack.pop(); 310 } 311 312 public void visitInnerClasses(InnerClasses ic) { 313 stack.push(ic); 314 ic.accept(visitor); 315 316 InnerClass[] ics = ic.getInnerClasses(); 317 for(int i=0; i < ics.length; i++) 318 ics[i].accept(this); 319 stack.pop(); 320 } 321 322 public void visitInnerClass(InnerClass inner) { 323 stack.push(inner); 324 inner.accept(visitor); 325 stack.pop(); 326 } 327 328 public void visitDeprecated(Deprecated attribute) { 329 stack.push(attribute); 330 attribute.accept(visitor); 331 stack.pop(); 332 } 333 334 public void visitSourceFile(SourceFile attribute) { 335 stack.push(attribute); 336 attribute.accept(visitor); 337 stack.pop(); 338 } 339 340 public void visitSynthetic(Synthetic attribute) { 341 stack.push(attribute); 342 attribute.accept(visitor); 343 stack.pop(); 344 } 345 346 public void visitUnknown(Unknown attribute) { 347 stack.push(attribute); 348 attribute.accept(visitor); 349 stack.pop(); 350 } 351 } 352 | Popular Tags |