View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.bcel.generic;
20  
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  import org.apache.bcel.ExceptionConst;
25  import org.apache.bcel.util.ByteSequence;
26  
27  /**
28   * LDC - Push item from constant pool.
29   *
30   * <PRE>
31   * Stack: ... -&gt; ..., item
32   * </PRE>
33   */
34  public class LDC extends CPInstruction implements PushInstruction, ExceptionThrower {
35  
36      /**
37       * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
38       */
39      LDC() {
40      }
41  
42      public LDC(final int index) {
43          super(org.apache.bcel.Const.LDC_W, index);
44          setSize();
45      }
46  
47      /**
48       * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
49       * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last.
50       *
51       * @param v Visitor object
52       */
53      @Override
54      public void accept(final Visitor v) {
55          v.visitStackProducer(this);
56          v.visitPushInstruction(this);
57          v.visitExceptionThrower(this);
58          v.visitTypedInstruction(this);
59          v.visitCPInstruction(this);
60          v.visitLDC(this);
61      }
62  
63      /**
64       * Dump instruction as byte code to stream out.
65       *
66       * @param out Output stream
67       */
68      @Override
69      public void dump(final DataOutputStream out) throws IOException {
70          out.writeByte(super.getOpcode());
71          if (super.getLength() == 2) { // TODO useless check?
72              out.writeByte(super.getIndex());
73          } else {
74              out.writeShort(super.getIndex());
75          }
76      }
77  
78      @Override
79      public Class<?>[] getExceptions() {
80          return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_STRING_RESOLUTION);
81      }
82  
83      @Override
84      public Type getType(final ConstantPoolGen cpg) {
85          switch (cpg.getConstantPool().getConstant(super.getIndex()).getTag()) {
86          case org.apache.bcel.Const.CONSTANT_String:
87              return Type.STRING;
88          case org.apache.bcel.Const.CONSTANT_Float:
89              return Type.FLOAT;
90          case org.apache.bcel.Const.CONSTANT_Integer:
91              return Type.INT;
92          case org.apache.bcel.Const.CONSTANT_Class:
93              return Type.CLASS;
94          case org.apache.bcel.Const.CONSTANT_Dynamic:
95              return Type.OBJECT;
96          default: // Never reached
97              throw new IllegalArgumentException("Unknown or invalid constant type at " + super.getIndex());
98          }
99      }
100 
101     public Object getValue(final ConstantPoolGen cpg) {
102         org.apache.bcel.classfile.Constant c = cpg.getConstantPool().getConstant(super.getIndex());
103         switch (c.getTag()) {
104         case org.apache.bcel.Const.CONSTANT_String:
105             final int i = ((org.apache.bcel.classfile.ConstantString) c).getStringIndex();
106             c = cpg.getConstantPool().getConstant(i);
107             return ((org.apache.bcel.classfile.ConstantUtf8) c).getBytes();
108         case org.apache.bcel.Const.CONSTANT_Float:
109             return Float.valueOf(((org.apache.bcel.classfile.ConstantFloat) c).getBytes());
110         case org.apache.bcel.Const.CONSTANT_Integer:
111             return Integer.valueOf(((org.apache.bcel.classfile.ConstantInteger) c).getBytes());
112         case org.apache.bcel.Const.CONSTANT_Class:
113             final int nameIndex = ((org.apache.bcel.classfile.ConstantClass) c).getNameIndex();
114             c = cpg.getConstantPool().getConstant(nameIndex);
115             return Type.getType(Type.internalTypeNameToSignature(((org.apache.bcel.classfile.ConstantUtf8) c).getBytes()));
116         case org.apache.bcel.Const.CONSTANT_Dynamic:
117             // Really not sure what to return here, maybe a BootstrapMethod instance but how do we get it?
118             return c;
119         default: // Never reached
120             throw new IllegalArgumentException("Unknown or invalid constant type at " + super.getIndex());
121         }
122     }
123 
124     /**
125      * Reads needed data (for example index) from file.
126      */
127     @Override
128     protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
129         super.setLength(2);
130         super.setIndex(bytes.readUnsignedByte());
131     }
132 
133     /**
134      * Sets the index to constant pool and adjust size.
135      */
136     @Override
137     public final void setIndex(final int index) {
138         super.setIndex(index);
139         setSize();
140     }
141 
142     // Adjust to proper size
143     protected final void setSize() {
144         if (super.getIndex() <= org.apache.bcel.Const.MAX_BYTE) { // Fits in one byte?
145             super.setOpcode(org.apache.bcel.Const.LDC);
146             super.setLength(2);
147         } else {
148             super.setOpcode(org.apache.bcel.Const.LDC_W);
149             super.setLength(3);
150         }
151     }
152 }