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