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.classfile; 19 20 import java.io.DataOutputStream; 21 import java.io.IOException; 22 23 import org.apache.bcel.Const; 24 25 /** 26 * @since 6.0 27 */ 28 public class SimpleElementValue extends ElementValue 29 { 30 private int index; 31 32 public SimpleElementValue(final int type, final int index, final ConstantPool cpool) 33 { 34 super(type, cpool); 35 this.index = index; 36 } 37 38 /** 39 * @return Value entry index in the cpool 40 */ 41 public int getIndex() 42 { 43 return index; 44 } 45 46 public void setIndex(final int index) 47 { 48 this.index = index; 49 } 50 51 public String getValueString() 52 { 53 if (super.getType() != STRING) { 54 throw new RuntimeException( 55 "Dont call getValueString() on a non STRING ElementValue"); 56 } 57 final ConstantUtf8 c = (ConstantUtf8) super.getConstantPool().getConstant(getIndex(), 58 Const.CONSTANT_Utf8); 59 return c.getBytes(); 60 } 61 62 public int getValueInt() 63 { 64 if (super.getType() != PRIMITIVE_INT) { 65 throw new RuntimeException( 66 "Dont call getValueString() on a non STRING ElementValue"); 67 } 68 final ConstantInteger c = (ConstantInteger) super.getConstantPool().getConstant(getIndex(), 69 Const.CONSTANT_Integer); 70 return c.getBytes(); 71 } 72 73 public byte getValueByte() 74 { 75 if (super.getType() != PRIMITIVE_BYTE) { 76 throw new RuntimeException( 77 "Dont call getValueByte() on a non BYTE ElementValue"); 78 } 79 final ConstantInteger c = (ConstantInteger) super.getConstantPool().getConstant(getIndex(), 80 Const.CONSTANT_Integer); 81 return (byte) c.getBytes(); 82 } 83 84 public char getValueChar() 85 { 86 if (super.getType() != PRIMITIVE_CHAR) { 87 throw new RuntimeException( 88 "Dont call getValueChar() on a non CHAR ElementValue"); 89 } 90 final ConstantInteger c = (ConstantInteger) super.getConstantPool().getConstant(getIndex(), 91 Const.CONSTANT_Integer); 92 return (char) c.getBytes(); 93 } 94 95 public long getValueLong() 96 { 97 if (super.getType() != PRIMITIVE_LONG) { 98 throw new RuntimeException( 99 "Dont call getValueLong() on a non LONG ElementValue"); 100 } 101 final ConstantLong j = (ConstantLong) super.getConstantPool().getConstant(getIndex()); 102 return j.getBytes(); 103 } 104 105 public float getValueFloat() 106 { 107 if (super.getType() != PRIMITIVE_FLOAT) { 108 throw new RuntimeException( 109 "Dont call getValueFloat() on a non FLOAT ElementValue"); 110 } 111 final ConstantFloat f = (ConstantFloat) super.getConstantPool().getConstant(getIndex()); 112 return f.getBytes(); 113 } 114 115 public double getValueDouble() 116 { 117 if (super.getType() != PRIMITIVE_DOUBLE) { 118 throw new RuntimeException( 119 "Dont call getValueDouble() on a non DOUBLE ElementValue"); 120 } 121 final ConstantDouble d = (ConstantDouble) super.getConstantPool().getConstant(getIndex()); 122 return d.getBytes(); 123 } 124 125 public boolean getValueBoolean() 126 { 127 if (super.getType() != PRIMITIVE_BOOLEAN) { 128 throw new RuntimeException( 129 "Dont call getValueBoolean() on a non BOOLEAN ElementValue"); 130 } 131 final ConstantInteger bo = (ConstantInteger) super.getConstantPool().getConstant(getIndex()); 132 return bo.getBytes() != 0; 133 } 134 135 public short getValueShort() 136 { 137 if (super.getType() != PRIMITIVE_SHORT) { 138 throw new RuntimeException( 139 "Dont call getValueShort() on a non SHORT ElementValue"); 140 } 141 final ConstantInteger s = (ConstantInteger) super.getConstantPool().getConstant(getIndex()); 142 return (short) s.getBytes(); 143 } 144 145 @Override 146 public String toString() 147 { 148 return stringifyValue(); 149 } 150 151 // Whatever kind of value it is, return it as a string 152 @Override 153 public String stringifyValue() 154 { 155 final ConstantPool cpool = super.getConstantPool(); 156 final int _type = super.getType(); 157 switch (_type) 158 { 159 case PRIMITIVE_INT: 160 final ConstantInteger c = (ConstantInteger) cpool.getConstant(getIndex(), 161 Const.CONSTANT_Integer); 162 return Integer.toString(c.getBytes()); 163 case PRIMITIVE_LONG: 164 final ConstantLong j = (ConstantLong) cpool.getConstant(getIndex(), 165 Const.CONSTANT_Long); 166 return Long.toString(j.getBytes()); 167 case PRIMITIVE_DOUBLE: 168 final ConstantDouble d = (ConstantDouble) cpool.getConstant(getIndex(), 169 Const.CONSTANT_Double); 170 return Double.toString(d.getBytes()); 171 case PRIMITIVE_FLOAT: 172 final ConstantFloat f = (ConstantFloat) cpool.getConstant(getIndex(), 173 Const.CONSTANT_Float); 174 return Float.toString(f.getBytes()); 175 case PRIMITIVE_SHORT: 176 final ConstantInteger s = (ConstantInteger) cpool.getConstant(getIndex(), 177 Const.CONSTANT_Integer); 178 return Integer.toString(s.getBytes()); 179 case PRIMITIVE_BYTE: 180 final ConstantInteger b = (ConstantInteger) cpool.getConstant(getIndex(), 181 Const.CONSTANT_Integer); 182 return Integer.toString(b.getBytes()); 183 case PRIMITIVE_CHAR: 184 final ConstantInteger ch = (ConstantInteger) cpool.getConstant( 185 getIndex(), Const.CONSTANT_Integer); 186 return String.valueOf((char)ch.getBytes()); 187 case PRIMITIVE_BOOLEAN: 188 final ConstantInteger bo = (ConstantInteger) cpool.getConstant( 189 getIndex(), Const.CONSTANT_Integer); 190 if (bo.getBytes() == 0) { 191 return "false"; 192 } 193 return "true"; 194 case STRING: 195 final ConstantUtf8 cu8 = (ConstantUtf8) cpool.getConstant(getIndex(), 196 Const.CONSTANT_Utf8); 197 return cu8.getBytes(); 198 default: 199 throw new RuntimeException("SimpleElementValue class does not know how to stringify type " + _type); 200 } 201 } 202 203 @Override 204 public void dump(final DataOutputStream dos) throws IOException 205 { 206 final int _type = super.getType(); 207 dos.writeByte(_type); // u1 kind of value 208 switch (_type) 209 { 210 case PRIMITIVE_INT: 211 case PRIMITIVE_BYTE: 212 case PRIMITIVE_CHAR: 213 case PRIMITIVE_FLOAT: 214 case PRIMITIVE_LONG: 215 case PRIMITIVE_BOOLEAN: 216 case PRIMITIVE_SHORT: 217 case PRIMITIVE_DOUBLE: 218 case STRING: 219 dos.writeShort(getIndex()); 220 break; 221 default: 222 throw new RuntimeException("SimpleElementValue doesnt know how to write out type " + _type); 223 } 224 } 225 } 226