1 package com.sun.org.apache.bcel.internal.generic; 2 3 56 import java.io.*; 57 import com.sun.org.apache.bcel.internal.util.ByteSequence; 58 59 68 public abstract class Select extends BranchInstruction 69 implements VariableLengthInstruction, StackProducer 70 { 71 protected int[] match; protected int[] indices; protected InstructionHandle[] targets; protected int fixed_length; protected int match_length; protected int padding = 0; 78 82 Select() {} 83 84 92 Select(short opcode, int[] match, InstructionHandle[] targets, 93 InstructionHandle target) { 94 super(opcode, target); 95 96 this.targets = targets; 97 for(int i=0; i < targets.length; i++) 98 notifyTarget(null, targets[i], this); 99 100 this.match = match; 101 102 if((match_length = match.length) != targets.length) 103 throw new ClassGenException("Match and target array have not the same length"); 104 105 indices = new int[match_length]; 106 } 107 108 121 protected int updatePosition(int offset, int max_offset) { 122 position += offset; 124 short old_length = length; 125 126 128 padding = (4 - ((position + 1) % 4)) % 4; 129 length = (short)(fixed_length + padding); 131 return length - old_length; 132 } 133 134 138 public void dump(DataOutputStream out) throws IOException { 139 out.writeByte(opcode); 140 141 for(int i=0; i < padding; i++) out.writeByte(0); 143 144 index = getTargetOffset(); out.writeInt(index); 146 } 147 148 151 protected void initFromFile(ByteSequence bytes, boolean wide) throws IOException 152 { 153 padding = (4 - (bytes.getIndex() % 4)) % 4; 155 for(int i=0; i < padding; i++) { 156 byte b; 157 if((b=bytes.readByte()) != 0) 158 throw new ClassGenException("Padding byte != 0: " + b); 159 } 160 161 index = bytes.readInt(); 163 } 164 165 168 public String toString(boolean verbose) { 169 StringBuffer buf = new StringBuffer (super.toString(verbose)); 170 171 if(verbose) { 172 for(int i=0; i < match_length; i++) { 173 String s = "null"; 174 175 if(targets[i] != null) 176 s = targets[i].getInstruction().toString(); 177 178 buf.append("(" + match[i] + ", " + s + " = {" + indices[i] + "})"); 179 } 180 } 181 else 182 buf.append(" ..."); 183 184 return buf.toString(); 185 } 186 187 190 public void setTarget(int i, InstructionHandle target) { 191 notifyTarget(targets[i], target, this); 192 targets[i] = target; 193 } 194 195 199 public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) { 200 boolean targeted = false; 201 202 if(target == old_ih) { 203 targeted = true; 204 setTarget(new_ih); 205 } 206 207 for(int i=0; i < targets.length; i++) { 208 if(targets[i] == old_ih) { 209 targeted = true; 210 setTarget(i, new_ih); 211 } 212 } 213 214 if(!targeted) 215 throw new ClassGenException("Not targeting " + old_ih); 216 } 217 218 221 public boolean containsTarget(InstructionHandle ih) { 222 if(target == ih) 223 return true; 224 225 for(int i=0; i < targets.length; i++) 226 if(targets[i] == ih) 227 return true; 228 229 return false; 230 } 231 232 235 void dispose() { 236 super.dispose(); 237 238 for(int i=0; i < targets.length; i++) 239 targets[i].removeTargeter(this); 240 } 241 242 245 public int[] getMatchs() { return match; } 246 247 250 public int[] getIndices() { return indices; } 251 252 255 public InstructionHandle[] getTargets() { return targets; } 256 } 257 | Popular Tags |