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 */ 018package org.apache.bcel.generic; 019 020import java.io.DataOutputStream; 021import java.io.IOException; 022 023import org.apache.bcel.ExceptionConst; 024import org.apache.bcel.util.ByteSequence; 025 026/** 027 * LDC - Push item from constant pool. 028 * 029 * <PRE>Stack: ... -> ..., item</PRE> 030 * 031 */ 032public class LDC extends CPInstruction implements PushInstruction, ExceptionThrower { 033 034 /** 035 * Empty constructor needed for Instruction.readInstruction. 036 * Not to be used otherwise. 037 */ 038 LDC() { 039 } 040 041 042 public LDC(final int index) { 043 super(org.apache.bcel.Const.LDC_W, index); 044 setSize(); 045 } 046 047 048 // Adjust to proper size 049 protected final void setSize() { 050 if (super.getIndex() <= org.apache.bcel.Const.MAX_BYTE) { // Fits in one byte? 051 super.setOpcode(org.apache.bcel.Const.LDC); 052 super.setLength(2); 053 } else { 054 super.setOpcode(org.apache.bcel.Const.LDC_W); 055 super.setLength(3); 056 } 057 } 058 059 060 /** 061 * Dump instruction as byte code to stream out. 062 * @param out Output stream 063 */ 064 @Override 065 public void dump( final DataOutputStream out ) throws IOException { 066 out.writeByte(super.getOpcode()); 067 if (super.getLength() == 2) { // TODO useless check? 068 out.writeByte(super.getIndex()); 069 } else { 070 out.writeShort(super.getIndex()); 071 } 072 } 073 074 075 /** 076 * Set the index to constant pool and adjust size. 077 */ 078 @Override 079 public final void setIndex( final int index ) { 080 super.setIndex(index); 081 setSize(); 082 } 083 084 085 /** 086 * Read needed data (e.g. index) from file. 087 */ 088 @Override 089 protected void initFromFile( final ByteSequence bytes, final boolean wide ) throws IOException { 090 super.setLength(2); 091 super.setIndex(bytes.readUnsignedByte()); 092 } 093 094 095 public Object getValue( final ConstantPoolGen cpg ) { 096 org.apache.bcel.classfile.Constant c = cpg.getConstantPool().getConstant(super.getIndex()); 097 switch (c.getTag()) { 098 case org.apache.bcel.Const.CONSTANT_String: 099 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 RuntimeException("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 RuntimeException("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}