001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * https://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.bcel.generic; 020 021import java.io.DataOutputStream; 022import java.io.IOException; 023 024import org.apache.bcel.ExceptionConst; 025import org.apache.bcel.util.ByteSequence; 026 027/** 028 * LDC - Push item from constant pool. 029 * 030 * <PRE> 031 * Stack: ... -> ..., item 032 * </PRE> 033 */ 034public class LDC extends CPInstruction implements PushInstruction, ExceptionThrower { 035 036 /** 037 * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise. 038 */ 039 LDC() { 040 } 041 042 public LDC(final int index) { 043 super(org.apache.bcel.Const.LDC_W, index); 044 setSize(); 045 } 046 047 /** 048 * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call 049 * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last. 050 * 051 * @param v Visitor object 052 */ 053 @Override 054 public void accept(final Visitor v) { 055 v.visitStackProducer(this); 056 v.visitPushInstruction(this); 057 v.visitExceptionThrower(this); 058 v.visitTypedInstruction(this); 059 v.visitCPInstruction(this); 060 v.visitLDC(this); 061 } 062 063 /** 064 * Dump instruction as byte code to stream out. 065 * 066 * @param out Output stream 067 */ 068 @Override 069 public void dump(final DataOutputStream out) throws IOException { 070 out.writeByte(super.getOpcode()); 071 if (super.getLength() == 2) { // TODO useless check? 072 out.writeByte(super.getIndex()); 073 } else { 074 out.writeShort(super.getIndex()); 075 } 076 } 077 078 @Override 079 public Class<?>[] getExceptions() { 080 return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_STRING_RESOLUTION); 081 } 082 083 @Override 084 public Type getType(final ConstantPoolGen cpg) { 085 switch (cpg.getConstantPool().getConstant(super.getIndex()).getTag()) { 086 case org.apache.bcel.Const.CONSTANT_String: 087 return Type.STRING; 088 case org.apache.bcel.Const.CONSTANT_Float: 089 return Type.FLOAT; 090 case org.apache.bcel.Const.CONSTANT_Integer: 091 return Type.INT; 092 case org.apache.bcel.Const.CONSTANT_Class: 093 return Type.CLASS; 094 case org.apache.bcel.Const.CONSTANT_Dynamic: 095 return Type.OBJECT; 096 default: // Never reached 097 throw new IllegalArgumentException("Unknown or invalid constant type at " + super.getIndex()); 098 } 099 } 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}