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 */
017package org.apache.bcel.generic;
018
019/**
020 * LDC2_W - Push long or double from constant pool
021 *
022 * <PRE>
023 * Stack: ... -&gt; ..., item.word1, item.word2
024 * </PRE>
025 */
026public class LDC2_W extends CPInstruction implements PushInstruction {
027
028    /**
029     * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
030     */
031    LDC2_W() {
032    }
033
034    public LDC2_W(final int index) {
035        super(org.apache.bcel.Const.LDC2_W, index);
036    }
037
038    /**
039     * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
040     * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last.
041     *
042     * @param v Visitor object
043     */
044    @Override
045    public void accept(final Visitor v) {
046        v.visitStackProducer(this);
047        v.visitPushInstruction(this);
048        v.visitTypedInstruction(this);
049        v.visitCPInstruction(this);
050        v.visitLDC2_W(this);
051    }
052
053    @Override
054    public Type getType(final ConstantPoolGen cpg) {
055        switch (cpg.getConstantPool().getConstant(super.getIndex()).getTag()) {
056        case org.apache.bcel.Const.CONSTANT_Long:
057            return Type.LONG;
058        case org.apache.bcel.Const.CONSTANT_Double:
059            return Type.DOUBLE;
060        default: // Never reached
061            throw new IllegalArgumentException("Unknown constant type " + super.getOpcode());
062        }
063    }
064
065    public Number getValue(final ConstantPoolGen cpg) {
066        final org.apache.bcel.classfile.Constant c = cpg.getConstantPool().getConstant(super.getIndex());
067        switch (c.getTag()) {
068        case org.apache.bcel.Const.CONSTANT_Long:
069            return Long.valueOf(((org.apache.bcel.classfile.ConstantLong) c).getBytes());
070        case org.apache.bcel.Const.CONSTANT_Double:
071            return Double.valueOf(((org.apache.bcel.classfile.ConstantDouble) c).getBytes());
072        default: // Never reached
073            throw new IllegalArgumentException("Unknown or invalid constant type at " + super.getIndex());
074        }
075    }
076}