< prev index next >

src/java.xml/share/classes/com/sun/org/apache/bcel/internal/generic/BranchHandle.java

Print this page


   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 }
   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 package com.sun.org.apache.bcel.internal.generic;
  22 

  23 /**
  24  * BranchHandle is returned by specialized InstructionList.append() whenever a
  25  * BranchInstruction is appended. This is useful when the target of this
  26  * instruction is not known at time of creation and must be set later via
  27  * setTarget().
  28  *
  29  * @see InstructionHandle
  30  * @see Instruction
  31  * @see InstructionList
  32  * @version $Id: BranchHandle.java 1749603 2016-06-21 20:50:19Z ggregory $
  33  */
  34 public final class BranchHandle extends InstructionHandle {
  35 
  36     // This is also a cache in case the InstructionHandle#swapInstruction() method is used
  37     // See BCEL-273
  38     private BranchInstruction bi; // An alias in fact, but saves lots of casts
  39 
  40     private BranchHandle(final BranchInstruction i) {
  41         super(i);
  42         bi = i;
  43     }
  44 
  45     /**
  46      * Factory methods.
  47      */
  48     private static BranchHandle bh_list = null; // List of reusable handles
  49 
  50     static BranchHandle getBranchHandle(final BranchInstruction i) {
  51         if (bh_list == null) {
  52             return new BranchHandle(i);
  53         }
  54         final BranchHandle bh = bh_list;
  55         bh_list = (BranchHandle) bh.getNext();

  56         bh.setInstruction(i);

  57         return bh;
  58     }

  59 
  60     /**
  61      * Handle adds itself to the list of resuable handles.
  62      */
  63     @Override
  64     protected void addHandle() {
  65         super.setNext(bh_list);
  66         bh_list = this;
  67     }
  68 
  69 
  70     /* Override InstructionHandle methods: delegate to branch instruction.
  71      * Through this overriding all access to the private i_position field should
  72      * be prevented.
  73      */
  74     @Override
  75     public int getPosition() {
  76         return bi.getPosition();
  77     }
  78 
  79     @Override
  80     void setPosition(final int pos) {
  81         // Original code: i_position = bi.position = pos;
  82         bi.setPosition(pos);
  83         super.setPosition(pos);
  84     }
  85 
  86     @Override
  87     protected int updatePosition(final int offset, final int max_offset) {
  88         final int x = bi.updatePosition(offset, max_offset);
  89         super.setPosition(bi.getPosition());
  90         return x;
  91     }
  92 
  93     /**
  94      * Pass new target to instruction.
  95      */
  96     public void setTarget(final InstructionHandle ih) {
  97         bi.setTarget(ih);
  98     }
  99 
 100     /**
 101      * Update target of instruction.
 102      */
 103     public void updateTarget(final InstructionHandle old_ih, final InstructionHandle new_ih) {
 104         bi.updateTarget(old_ih, new_ih);
 105     }
 106 
 107     /**
 108      * @return target of instruction.
 109      */
 110     public InstructionHandle getTarget() {
 111         return bi.getTarget();
 112     }
 113 
 114     /**
 115      * Set new contents. Old instruction is disposed and may not be used
 116      * anymore.
 117      */
 118     @Override // This is only done in order to apply the additional type check; could be merged with super impl.
 119     public void setInstruction(final Instruction i) { // TODO could be package-protected?
 120         super.setInstruction(i);
 121         if (!(i instanceof BranchInstruction)) {
 122             throw new ClassGenException("Assigning " + i
 123                     + " to branch handle which is not a branch instruction");
 124         }
 125         bi = (BranchInstruction) i;

 126     }
 127 }
< prev index next >