Home | History | Annotate | Download | only in generic
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  */
     18 package org.apache.bcel.generic;
     19 
     20 import java.io.DataOutputStream;
     21 import java.io.IOException;
     22 
     23 /**
     24  * JSR - Jump to subroutine
     25  *
     26  * @version $Id$
     27  */
     28 public class JSR extends JsrInstruction implements VariableLengthInstruction {
     29 
     30     /**
     31      * Empty constructor needed for Instruction.readInstruction.
     32      * Not to be used otherwise.
     33      */
     34     JSR() {
     35     }
     36 
     37 
     38     public JSR(final InstructionHandle target) {
     39         super(org.apache.bcel.Const.JSR, target);
     40     }
     41 
     42 
     43     /**
     44      * Dump instruction as byte code to stream out.
     45      * @param out Output stream
     46      */
     47     @Override
     48     public void dump( final DataOutputStream out ) throws IOException {
     49         super.setIndex(getTargetOffset());
     50         if (super.getOpcode() == org.apache.bcel.Const.JSR) {
     51             super.dump(out);
     52         } else { // JSR_W
     53             super.setIndex(getTargetOffset());
     54             out.writeByte(super.getOpcode());
     55             out.writeInt(super.getIndex());
     56         }
     57     }
     58 
     59 
     60     @Override
     61     protected int updatePosition( final int offset, final int max_offset ) {
     62         final int i = getTargetOffset(); // Depending on old position value
     63         setPosition(getPosition() + offset); // Position may be shifted by preceding expansions
     64         if (Math.abs(i) >= (Short.MAX_VALUE - max_offset)) { // to large for short (estimate)
     65             super.setOpcode(org.apache.bcel.Const.JSR_W);
     66             final short old_length = (short) super.getLength();
     67             super.setLength(5);
     68             return super.getLength() - old_length;
     69         }
     70         return 0;
     71     }
     72 
     73 
     74     /**
     75      * Call corresponding visitor method(s). The order is:
     76      * Call visitor methods of implemented interfaces first, then
     77      * call methods according to the class hierarchy in descending order,
     78      * i.e., the most specific visitXXX() call comes last.
     79      *
     80      * @param v Visitor object
     81      */
     82     @Override
     83     public void accept( final Visitor v ) {
     84         v.visitStackProducer(this);
     85         v.visitVariableLengthInstruction(this);
     86         v.visitBranchInstruction(this);
     87         v.visitJsrInstruction(this);
     88         v.visitJSR(this);
     89     }
     90 }
     91