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        case org.apache.bcel.Const.CONSTANT_Dynamic:
093            return Type.OBJECT;
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 Type.getType(Type.internalTypeNameToSignature(((org.apache.bcel.classfile.ConstantUtf8) c).getBytes()));
114        case org.apache.bcel.Const.CONSTANT_Dynamic:
115            // Really not sure what to return here, maybe a BootstrapMethod instance but how do we get it?
116            return c;
117        default: // Never reached
118            throw new IllegalArgumentException("Unknown or invalid constant type at " + super.getIndex());
119        }
120    }
121
122    /**
123     * Read needed data (e.g. index) from file.
124     */
125    @Override
126    protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
127        super.setLength(2);
128        super.setIndex(bytes.readUnsignedByte());
129    }
130
131    /**
132     * Sets the index to constant pool and adjust size.
133     */
134    @Override
135    public final void setIndex(final int index) {
136        super.setIndex(index);
137        setSize();
138    }
139
140    // Adjust to proper size
141    protected final void setSize() {
142        if (super.getIndex() <= org.apache.bcel.Const.MAX_BYTE) { // Fits in one byte?
143            super.setOpcode(org.apache.bcel.Const.LDC);
144            super.setLength(2);
145        } else {
146            super.setOpcode(org.apache.bcel.Const.LDC_W);
147            super.setLength(3);
148        }
149    }
150}