View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.bcel.classfile;
20  
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  import org.apache.bcel.Const;
25  
26  /**
27   * @since 6.0
28   */
29  public class SimpleElementValue extends ElementValue {
30      private int index;
31  
32      public SimpleElementValue(final int type, final int index, final ConstantPool cpool) {
33          super(type, cpool);
34          this.index = index;
35      }
36  
37      @Override
38      public void dump(final DataOutputStream dos) throws IOException {
39          final int type = super.getType();
40          dos.writeByte(type); // u1 kind of value
41          switch (type) {
42          case PRIMITIVE_INT:
43          case PRIMITIVE_BYTE:
44          case PRIMITIVE_CHAR:
45          case PRIMITIVE_FLOAT:
46          case PRIMITIVE_LONG:
47          case PRIMITIVE_BOOLEAN:
48          case PRIMITIVE_SHORT:
49          case PRIMITIVE_DOUBLE:
50          case STRING:
51              dos.writeShort(getIndex());
52              break;
53          default:
54              throw new ClassFormatException("SimpleElementValue doesn't know how to write out type " + type);
55          }
56      }
57  
58      /**
59       * @return Value entry index in the cpool
60       */
61      public int getIndex() {
62          return index;
63      }
64  
65      public boolean getValueBoolean() {
66          if (super.getType() != PRIMITIVE_BOOLEAN) {
67              throw new IllegalStateException("Don't call getValueBoolean() on a non BOOLEAN ElementValue");
68          }
69          final ConstantInteger bo = (ConstantInteger) super.getConstantPool().getConstant(getIndex());
70          return bo.getBytes() != 0;
71      }
72  
73      public byte getValueByte() {
74          if (super.getType() != PRIMITIVE_BYTE) {
75              throw new IllegalStateException("Don't call getValueByte() on a non BYTE ElementValue");
76          }
77          return (byte) super.getConstantPool().getConstantInteger(getIndex()).getBytes();
78      }
79  
80      public char getValueChar() {
81          if (super.getType() != PRIMITIVE_CHAR) {
82              throw new IllegalStateException("Don't call getValueChar() on a non CHAR ElementValue");
83          }
84          return (char) super.getConstantPool().getConstantInteger(getIndex()).getBytes();
85      }
86  
87      public double getValueDouble() {
88          if (super.getType() != PRIMITIVE_DOUBLE) {
89              throw new IllegalStateException("Don't call getValueDouble() on a non DOUBLE ElementValue");
90          }
91          final ConstantDouble d = (ConstantDouble) super.getConstantPool().getConstant(getIndex());
92          return d.getBytes();
93      }
94  
95      public float getValueFloat() {
96          if (super.getType() != PRIMITIVE_FLOAT) {
97              throw new IllegalStateException("Don't call getValueFloat() on a non FLOAT ElementValue");
98          }
99          final ConstantFloat f = (ConstantFloat) super.getConstantPool().getConstant(getIndex());
100         return f.getBytes();
101     }
102 
103     public int getValueInt() {
104         if (super.getType() != PRIMITIVE_INT) {
105             throw new IllegalStateException("Don't call getValueInt() on a non INT ElementValue");
106         }
107         return super.getConstantPool().getConstantInteger(getIndex()).getBytes();
108     }
109 
110     public long getValueLong() {
111         if (super.getType() != PRIMITIVE_LONG) {
112             throw new IllegalStateException("Don't call getValueLong() on a non LONG ElementValue");
113         }
114         final ConstantLong j = (ConstantLong) super.getConstantPool().getConstant(getIndex());
115         return j.getBytes();
116     }
117 
118     public short getValueShort() {
119         if (super.getType() != PRIMITIVE_SHORT) {
120             throw new IllegalStateException("Don't call getValueShort() on a non SHORT ElementValue");
121         }
122         final ConstantInteger s = (ConstantInteger) super.getConstantPool().getConstant(getIndex());
123         return (short) s.getBytes();
124     }
125 
126     public String getValueString() {
127         if (super.getType() != STRING) {
128             throw new IllegalStateException("Don't call getValueString() on a non STRING ElementValue");
129         }
130         return super.getConstantPool().getConstantUtf8(getIndex()).getBytes();
131     }
132 
133     public void setIndex(final int index) {
134         this.index = index;
135     }
136 
137     // Whatever kind of value it is, return it as a string
138     @Override
139     public String stringifyValue() {
140         final ConstantPool cpool = super.getConstantPool();
141         final int type = super.getType();
142         switch (type) {
143         case PRIMITIVE_INT:
144             return Integer.toString(cpool.getConstantInteger(getIndex()).getBytes());
145         case PRIMITIVE_LONG:
146             final ConstantLong j = cpool.getConstant(getIndex(), Const.CONSTANT_Long, ConstantLong.class);
147             return Long.toString(j.getBytes());
148         case PRIMITIVE_DOUBLE:
149             final ConstantDouble d = cpool.getConstant(getIndex(), Const.CONSTANT_Double, ConstantDouble.class);
150             return Double.toString(d.getBytes());
151         case PRIMITIVE_FLOAT:
152             final ConstantFloat f = cpool.getConstant(getIndex(), Const.CONSTANT_Float, ConstantFloat.class);
153             return Float.toString(f.getBytes());
154         case PRIMITIVE_SHORT:
155             final ConstantInteger s = cpool.getConstantInteger(getIndex());
156             return Integer.toString(s.getBytes());
157         case PRIMITIVE_BYTE:
158             final ConstantInteger b = cpool.getConstantInteger(getIndex());
159             return Integer.toString(b.getBytes());
160         case PRIMITIVE_CHAR:
161             final ConstantInteger ch = cpool.getConstantInteger(getIndex());
162             return String.valueOf((char) ch.getBytes());
163         case PRIMITIVE_BOOLEAN:
164             final ConstantInteger bo = cpool.getConstantInteger(getIndex());
165             if (bo.getBytes() == 0) {
166                 return "false";
167             }
168             return "true";
169         case STRING:
170             return cpool.getConstantUtf8(getIndex()).getBytes();
171         default:
172             throw new IllegalStateException("SimpleElementValue class does not know how to stringify type " + type);
173         }
174     }
175 
176     @Override
177     public String toString() {
178         return stringifyValue();
179     }
180 }