001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License. 
016     *
017     */
018    package org.apache.bcel.generic;
019    
020    import java.io.DataOutputStream;
021    import java.io.IOException;
022    
023    /** 
024     * GOTO - Branch always (to relative offset, not absolute address)
025     *
026     * @version $Id: GOTO.java 1152072 2011-07-29 01:54:05Z dbrosius $
027     * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
028     */
029    public class GOTO extends GotoInstruction implements VariableLengthInstruction {
030    
031        private static final long serialVersionUID = 6106731367505145625L;
032    
033    
034        /**
035         * Empty constructor needed for the Class.newInstance() statement in
036         * Instruction.readInstruction(). Not to be used otherwise.
037         */
038        GOTO() {
039        }
040    
041    
042        public GOTO(InstructionHandle target) {
043            super(org.apache.bcel.Constants.GOTO, target);
044        }
045    
046    
047        /**
048         * Dump instruction as byte code to stream out.
049         * @param out Output stream
050         */
051        @Override
052        public void dump( DataOutputStream out ) throws IOException {
053            index = getTargetOffset();
054            if (opcode == org.apache.bcel.Constants.GOTO) {
055                super.dump(out);
056            } else { // GOTO_W
057                index = getTargetOffset();
058                out.writeByte(opcode);
059                out.writeInt(index);
060            }
061        }
062    
063    
064        /** Called in pass 2 of InstructionList.setPositions() in order to update
065         * the branch target, that may shift due to variable length instructions.
066         */
067        @Override
068        protected int updatePosition( int offset, int max_offset ) {
069            int i = getTargetOffset(); // Depending on old position value
070            position += offset; // Position may be shifted by preceding expansions
071            if (Math.abs(i) >= (32767 - max_offset)) { // to large for short (estimate)
072                opcode = org.apache.bcel.Constants.GOTO_W;
073                short old_length = length;
074                length = 5;
075                return length - old_length;
076            }
077            return 0;
078        }
079    
080    
081        /**
082         * Call corresponding visitor method(s). The order is:
083         * Call visitor methods of implemented interfaces first, then
084         * call methods according to the class hierarchy in descending order,
085         * i.e., the most specific visitXXX() call comes last.
086         *
087         * @param v Visitor object
088         */
089        @Override
090        public void accept( Visitor v ) {
091            v.visitVariableLengthInstruction(this);
092            v.visitUnconditionalBranch(this);
093            v.visitBranchInstruction(this);
094            v.visitGotoInstruction(this);
095            v.visitGOTO(this);
096        }
097    }