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