1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.bcel.internal.generic;
  23 
  24 
  25 /**
  26  * BranchHandle is returned by specialized InstructionList.append() whenever a
  27  * BranchInstruction is appended. This is useful when the target of this
  28  * instruction is not known at time of creation and must be set later
  29  * via setTarget().
  30  *
  31  * @see InstructionHandle
  32  * @see Instruction
  33  * @see InstructionList
  34  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  35  */
  36 public final class BranchHandle extends InstructionHandle {
  37   private BranchInstruction bi; // An alias in fact, but saves lots of casts
  38 
  39   private BranchHandle(BranchInstruction i) {
  40     super(i);
  41     bi = i;
  42   }
  43 
  44   /** Factory methods.
  45    */
  46   private static BranchHandle bh_list = null; // List of reusable handles
  47 
  48   static final BranchHandle getBranchHandle(BranchInstruction i) {
  49     if(bh_list == null)
  50       return new BranchHandle(i);
  51     else {
  52       BranchHandle bh = bh_list;
  53       bh_list = (BranchHandle)bh.next;
  54 
  55       bh.setInstruction(i);
  56 
  57       return bh;
  58     }
  59   }
  60 
  61   /** Handle adds itself to the list of resuable handles.
  62    */
  63   protected void addHandle() {
  64     next    = bh_list;
  65     bh_list = this;
  66   }
  67 
  68   /* Override InstructionHandle methods: delegate to branch instruction.
  69    * Through this overriding all access to the private i_position field should
  70    * be prevented.
  71    */
  72   public int getPosition() { return bi.position; }
  73 
  74   void setPosition(int pos) {
  75     i_position = bi.position = pos;
  76   }
  77 
  78   protected int updatePosition(int offset, int max_offset) {
  79     int x = bi.updatePosition(offset, max_offset);
  80     i_position = bi.position;
  81     return x;
  82   }
  83 
  84   /**
  85    * Pass new target to instruction.
  86    */
  87   public void setTarget(InstructionHandle ih) {
  88     bi.setTarget(ih);
  89   }
  90 
  91   /**
  92    * Update target of instruction.
  93    */
  94   public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) {
  95     bi.updateTarget(old_ih, new_ih);
  96   }
  97 
  98   /**
  99    * @return target of instruction.
 100    */
 101   public InstructionHandle getTarget() {
 102     return bi.getTarget();
 103   }
 104 
 105   /**
 106    * Set new contents. Old instruction is disposed and may not be used anymore.
 107    */
 108   public void setInstruction(Instruction i) {
 109     super.setInstruction(i);
 110 
 111     if(!(i instanceof BranchInstruction))
 112       throw new ClassGenException("Assigning " + i +
 113                                   " to branch handle which is not a branch instruction");
 114 
 115     bi = (BranchInstruction)i;
 116   }
 117 }