1 17 package org.apache.bcel.generic; 18 19 import java.io.DataOutputStream ; 20 import java.io.IOException ; 21 import java.io.Serializable ; 22 import java.util.Locale ; 23 import org.apache.bcel.Constants; 24 import org.apache.bcel.classfile.ConstantPool; 25 import org.apache.bcel.util.ByteSequence; 26 27 33 public abstract class Instruction implements Cloneable , Serializable { 34 35 protected short length = 1; protected short opcode = -1; private static InstructionComparator cmp = InstructionComparator.DEFAULT; 38 39 40 44 Instruction() { 45 } 46 47 48 public Instruction(short opcode, short length) { 49 this.length = length; 50 this.opcode = opcode; 51 } 52 53 54 58 public void dump( DataOutputStream out ) throws IOException { 59 out.writeByte(opcode); } 61 62 63 65 public String getName() { 66 return Constants.OPCODE_NAMES[opcode]; 67 } 68 69 70 79 public String toString( boolean verbose ) { 80 if (verbose) { 81 return getName() + "[" + opcode + "](" + length + ")"; 82 } else { 83 return getName(); 84 } 85 } 86 87 88 91 public String toString() { 92 return toString(true); 93 } 94 95 96 99 public String toString( ConstantPool cp ) { 100 return toString(false); 101 } 102 103 104 112 public Instruction copy() { 113 Instruction i = null; 114 if (InstructionConstants.INSTRUCTIONS[this.getOpcode()] != null) { 116 i = this; 117 } else { 118 try { 119 i = (Instruction) clone(); 120 } catch (CloneNotSupportedException e) { 121 System.err.println(e); 122 } 123 } 124 return i; 125 } 126 127 128 134 protected void initFromFile( ByteSequence bytes, boolean wide ) throws IOException { 135 } 136 137 138 145 public static final Instruction readInstruction( ByteSequence bytes ) throws IOException { 146 boolean wide = false; 147 short opcode = (short) bytes.readUnsignedByte(); 148 Instruction obj = null; 149 if (opcode == Constants.WIDE) { wide = true; 151 opcode = (short) bytes.readUnsignedByte(); 152 } 153 if (InstructionConstants.INSTRUCTIONS[opcode] != null) { 154 return InstructionConstants.INSTRUCTIONS[opcode]; } 156 159 Class clazz; 160 try { 161 clazz = Class.forName(className(opcode)); 162 } catch (ClassNotFoundException cnfe) { 163 throw new ClassGenException("Illegal opcode detected."); 166 } 167 try { 168 obj = (Instruction) clazz.newInstance(); 169 if (wide 170 && !((obj instanceof LocalVariableInstruction) || (obj instanceof IINC) || (obj instanceof RET))) { 171 throw new Exception ("Illegal opcode after wide: " + opcode); 172 } 173 obj.setOpcode(opcode); 174 obj.initFromFile(bytes, wide); } catch (Exception e) { 177 throw new ClassGenException(e.toString()); 178 } 179 return obj; 180 } 181 182 183 private static final String className( short opcode ) { 184 String name = Constants.OPCODE_NAMES[opcode].toUpperCase(Locale.ENGLISH); 185 188 try { 189 int len = name.length(); 190 char ch1 = name.charAt(len - 2), ch2 = name.charAt(len - 1); 191 if ((ch1 == '_') && (ch2 >= '0') && (ch2 <= '5')) { 192 name = name.substring(0, len - 2); 193 } 194 if (name.equals("ICONST_M1")) { 195 name = "ICONST"; 196 } 197 } catch (StringIndexOutOfBoundsException e) { 198 System.err.println(e); 199 } 200 return "org.apache.bcel.generic." + name; 201 } 202 203 204 211 public int consumeStack( ConstantPoolGen cpg ) { 212 return Constants.CONSUME_STACK[opcode]; 213 } 214 215 216 223 public int produceStack( ConstantPoolGen cpg ) { 224 return Constants.PRODUCE_STACK[opcode]; 225 } 226 227 228 231 public short getOpcode() { 232 return opcode; 233 } 234 235 236 239 public int getLength() { 240 return length; 241 } 242 243 244 247 private void setOpcode( short opcode ) { 248 this.opcode = opcode; 249 } 250 251 252 254 void dispose() { 255 } 256 257 258 266 public abstract void accept( Visitor v ); 267 268 269 274 public static InstructionComparator getComparator() { 275 return cmp; 276 } 277 278 279 281 public static void setComparator( InstructionComparator c ) { 282 cmp = c; 283 } 284 285 286 289 public boolean equals( Object that ) { 290 return (that instanceof Instruction) ? cmp.equals(this, (Instruction) that) : false; 291 } 292 } 293 | Popular Tags |