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