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   */
18  package org.apache.bcel.generic;
19  
20  import java.io.DataOutputStream;
21  import java.io.IOException;
22  
23  import org.apache.bcel.ExceptionConst;
24  import org.apache.bcel.util.ByteSequence;
25  
26  /**
27   * LDC - Push item from constant pool.
28   *
29   * <PRE>Stack: ... -&gt; ..., item</PRE>
30   *
31   */
32  public class LDC extends CPInstruction implements PushInstruction, ExceptionThrower {
33  
34      /**
35       * Empty constructor needed for Instruction.readInstruction.
36       * Not to be used otherwise.
37       */
38      LDC() {
39      }
40  
41  
42      public LDC(final int index) {
43          super(org.apache.bcel.Const.LDC_W, index);
44          setSize();
45      }
46  
47  
48      // Adjust to proper size
49      protected final void setSize() {
50          if (super.getIndex() <= org.apache.bcel.Const.MAX_BYTE) { // Fits in one byte?
51              super.setOpcode(org.apache.bcel.Const.LDC);
52              super.setLength(2);
53          } else {
54              super.setOpcode(org.apache.bcel.Const.LDC_W);
55              super.setLength(3);
56          }
57      }
58  
59  
60      /**
61       * Dump instruction as byte code to stream out.
62       * @param out Output stream
63       */
64      @Override
65      public void dump( final DataOutputStream out ) throws IOException {
66          out.writeByte(super.getOpcode());
67          if (super.getLength() == 2) { // TODO useless check?
68              out.writeByte(super.getIndex());
69          } else {
70              out.writeShort(super.getIndex());
71          }
72      }
73  
74  
75      /**
76       * Set the index to constant pool and adjust size.
77       */
78      @Override
79      public final void setIndex( final int index ) {
80          super.setIndex(index);
81          setSize();
82      }
83  
84  
85      /**
86       * Read needed data (e.g. index) from file.
87       */
88      @Override
89      protected void initFromFile( final ByteSequence bytes, final boolean wide ) throws IOException {
90          super.setLength(2);
91          super.setIndex(bytes.readUnsignedByte());
92      }
93  
94  
95      public Object getValue( final ConstantPoolGen cpg ) {
96          org.apache.bcel.classfile.Constant c = cpg.getConstantPool().getConstant(super.getIndex());
97          switch (c.getTag()) {
98              case org.apache.bcel.Const.CONSTANT_String:
99                  final int i = ((org.apache.bcel.classfile.ConstantString) c).getStringIndex();
100                 c = cpg.getConstantPool().getConstant(i);
101                 return ((org.apache.bcel.classfile.ConstantUtf8) c).getBytes();
102             case org.apache.bcel.Const.CONSTANT_Float:
103                 return new Float(((org.apache.bcel.classfile.ConstantFloat) c).getBytes());
104             case org.apache.bcel.Const.CONSTANT_Integer:
105                 return Integer.valueOf(((org.apache.bcel.classfile.ConstantInteger) c).getBytes());
106             case org.apache.bcel.Const.CONSTANT_Class:
107                 final int nameIndex = ((org.apache.bcel.classfile.ConstantClass) c).getNameIndex();
108                 c = cpg.getConstantPool().getConstant(nameIndex);
109                 return new ObjectType(((org.apache.bcel.classfile.ConstantUtf8) c).getBytes());
110             default: // Never reached
111                 throw new IllegalArgumentException("Unknown or invalid constant type at " + super.getIndex());
112         }
113     }
114 
115 
116     @Override
117     public Type getType( final ConstantPoolGen cpg ) {
118         switch (cpg.getConstantPool().getConstant(super.getIndex()).getTag()) {
119             case org.apache.bcel.Const.CONSTANT_String:
120                 return Type.STRING;
121             case org.apache.bcel.Const.CONSTANT_Float:
122                 return Type.FLOAT;
123             case org.apache.bcel.Const.CONSTANT_Integer:
124                 return Type.INT;
125             case org.apache.bcel.Const.CONSTANT_Class:
126                 return Type.CLASS;
127             default: // Never reached
128                 throw new IllegalArgumentException("Unknown or invalid constant type at " + super.getIndex());
129         }
130     }
131 
132 
133     @Override
134     public Class<?>[] getExceptions() {
135         return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_STRING_RESOLUTION);
136     }
137 
138 
139     /**
140      * Call corresponding visitor method(s). The order is:
141      * Call visitor methods of implemented interfaces first, then
142      * call methods according to the class hierarchy in descending order,
143      * i.e., the most specific visitXXX() call comes last.
144      *
145      * @param v Visitor object
146      */
147     @Override
148     public void accept( final Visitor v ) {
149         v.visitStackProducer(this);
150         v.visitPushInstruction(this);
151         v.visitExceptionThrower(this);
152         v.visitTypedInstruction(this);
153         v.visitCPInstruction(this);
154         v.visitLDC(this);
155     }
156 }