View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.bcel.generic;
18  
19  import java.io.DataOutputStream;
20  import java.io.IOException;
21  
22  import org.apache.bcel.ExceptionConst;
23  import org.apache.bcel.util.ByteSequence;
24  
25  /**
26   * LDC - Push item from constant pool.
27   *
28   * <PRE>
29   * Stack: ... -&gt; ..., item
30   * </PRE>
31   */
32  public class LDC extends CPInstruction implements PushInstruction, ExceptionThrower {
33  
34      /**
35       * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
36       */
37      LDC() {
38      }
39  
40      public LDC(final int index) {
41          super(org.apache.bcel.Const.LDC_W, index);
42          setSize();
43      }
44  
45      /**
46       * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
47       * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last.
48       *
49       * @param v Visitor object
50       */
51      @Override
52      public void accept(final Visitor v) {
53          v.visitStackProducer(this);
54          v.visitPushInstruction(this);
55          v.visitExceptionThrower(this);
56          v.visitTypedInstruction(this);
57          v.visitCPInstruction(this);
58          v.visitLDC(this);
59      }
60  
61      /**
62       * Dump instruction as byte code to stream out.
63       *
64       * @param out Output stream
65       */
66      @Override
67      public void dump(final DataOutputStream out) throws IOException {
68          out.writeByte(super.getOpcode());
69          if (super.getLength() == 2) { // TODO useless check?
70              out.writeByte(super.getIndex());
71          } else {
72              out.writeShort(super.getIndex());
73          }
74      }
75  
76      @Override
77      public Class<?>[] getExceptions() {
78          return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_STRING_RESOLUTION);
79      }
80  
81      @Override
82      public Type getType(final ConstantPoolGen cpg) {
83          switch (cpg.getConstantPool().getConstant(super.getIndex()).getTag()) {
84          case org.apache.bcel.Const.CONSTANT_String:
85              return Type.STRING;
86          case org.apache.bcel.Const.CONSTANT_Float:
87              return Type.FLOAT;
88          case org.apache.bcel.Const.CONSTANT_Integer:
89              return Type.INT;
90          case org.apache.bcel.Const.CONSTANT_Class:
91              return Type.CLASS;
92          case org.apache.bcel.Const.CONSTANT_Dynamic:
93              return Type.OBJECT;
94          default: // Never reached
95              throw new IllegalArgumentException("Unknown or invalid constant type at " + super.getIndex());
96          }
97      }
98  
99      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 }