    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 */
    018package org.apache.bcel.generic;
    019
    020import java.io.DataOutputStream;
    021import java.io.IOException;
    022
    023import org.apache.bcel.ExceptionConst;
    024import org.apache.bcel.util.ByteSequence;
    025
    026/**
    027 * LDC - Push item from constant pool.
    028 *
    029 * <PRE>
    030 * Stack: ... -&gt; ..., item
    031 * </PRE>
    032 *
    033 */
    034public class LDC extends CPInstruction implements PushInstruction, ExceptionThrower {
    035
    036    /**
    037     * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
    038     */
    039    LDC() {
    040    }
    041
    042    public LDC(final int index) {
    043        super(org.apache.bcel.Const.LDC_W, index);
    044        setSize();
    045    }
    046
    047    /**
    048     * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
    049     * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last.
    050     *
    051     * @param v Visitor object
    052     */
    053    @Override
    054    public void accept(final Visitor v) {
    055        v.visitStackProducer(this);
    056        v.visitPushInstruction(this);
    057        v.visitExceptionThrower(this);
    058        v.visitTypedInstruction(this);
    059        v.visitCPInstruction(this);
    060        v.visitLDC(this);
    061    }
    062
    063    /**
    064     * Dump instruction as byte code to stream out.
    065     *
    066     * @param out Output stream
    067     */
    068    @Override
    069    public void dump(final DataOutputStream out) throws IOException {
    070        out.writeByte(super.getOpcode());
    071        if (super.getLength() == 2) { // TODO useless check?
    072            out.writeByte(super.getIndex());
    073        } else {
    074            out.writeShort(super.getIndex());
    075        }
    076    }
    077
    078    @Override
    079    public Class<?>[] getExceptions() {
    080        return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_STRING_RESOLUTION);
    081    }
    082
    083    @Override
    084    public Type getType(final ConstantPoolGen cpg) {
    085        switch (cpg.getConstantPool().getConstant(super.getIndex()).getTag()) {
    086        case org.apache.bcel.Const.CONSTANT_String:
    087            return Type.STRING;
    088        case org.apache.bcel.Const.CONSTANT_Float:
    089            return Type.FLOAT;
    090        case org.apache.bcel.Const.CONSTANT_Integer:
    091            return Type.INT;
    092        case org.apache.bcel.Const.CONSTANT_Class:
    093            return Type.CLASS;
    094        default: // Never reached
    095            throw new IllegalArgumentException("Unknown or invalid constant type at " + super.getIndex());
    096        }
    097    }
    098
    099    public Object getValue(final ConstantPoolGen cpg) {
    100        org.apache.bcel.classfile.Constant c = cpg.getConstantPool().getConstant(super.getIndex());
    101        switch (c.getTag()) {
    102        case org.apache.bcel.Const.CONSTANT_String:
    103            final int i = ((org.apache.bcel.classfile.ConstantString) c).getStringIndex();
    104            c = cpg.getConstantPool().getConstant(i);
    105            return ((org.apache.bcel.classfile.ConstantUtf8) c).getBytes();
    106        case org.apache.bcel.Const.CONSTANT_Float:
    107            return Float.valueOf(((org.apache.bcel.classfile.ConstantFloat) c).getBytes());
    108        case org.apache.bcel.Const.CONSTANT_Integer:
    109            return Integer.valueOf(((org.apache.bcel.classfile.ConstantInteger) c).getBytes());
    110        case org.apache.bcel.Const.CONSTANT_Class:
    111            final int nameIndex = ((org.apache.bcel.classfile.ConstantClass) c).getNameIndex();
    112            c = cpg.getConstantPool().getConstant(nameIndex);
    113            return new ObjectType(((org.apache.bcel.classfile.ConstantUtf8) c).getBytes());
    114        default: // Never reached
    115            throw new IllegalArgumentException("Unknown or invalid constant type at " + super.getIndex());
    116        }
    117    }
    118
    119    /**
    120     * Read needed data (e.g. index) from file.
    121     */
    122    @Override
    123    protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
    124        super.setLength(2);
    125        super.setIndex(bytes.readUnsignedByte());
    126    }
    127
    128    /**
    129     * Set the index to constant pool and adjust size.
    130     */
    131    @Override
    132    public final void setIndex(final int index) {
    133        super.setIndex(index);
    134        setSize();
    135    }
    136
    137    // Adjust to proper size
    138    protected final void setSize() {
    139        if (super.getIndex() <= org.apache.bcel.Const.MAX_BYTE) { // Fits in one byte?
    140            super.setOpcode(org.apache.bcel.Const.LDC);
    141            super.setLength(2);
    142        } else {
    143            super.setOpcode(org.apache.bcel.Const.LDC_W);
    144            super.setLength(3);
    145        }
    146    }
    147}