SimpleElementValue.java

  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. import java.io.DataOutputStream;
  19. import java.io.IOException;

  20. import org.apache.bcel.Const;

  21. /**
  22.  * @since 6.0
  23.  */
  24. public class SimpleElementValue extends ElementValue {
  25.     private int index;

  26.     public SimpleElementValue(final int type, final int index, final ConstantPool cpool) {
  27.         super(type, cpool);
  28.         this.index = index;
  29.     }

  30.     @Override
  31.     public void dump(final DataOutputStream dos) throws IOException {
  32.         final int type = super.getType();
  33.         dos.writeByte(type); // u1 kind of value
  34.         switch (type) {
  35.         case PRIMITIVE_INT:
  36.         case PRIMITIVE_BYTE:
  37.         case PRIMITIVE_CHAR:
  38.         case PRIMITIVE_FLOAT:
  39.         case PRIMITIVE_LONG:
  40.         case PRIMITIVE_BOOLEAN:
  41.         case PRIMITIVE_SHORT:
  42.         case PRIMITIVE_DOUBLE:
  43.         case STRING:
  44.             dos.writeShort(getIndex());
  45.             break;
  46.         default:
  47.             throw new ClassFormatException("SimpleElementValue doesn't know how to write out type " + type);
  48.         }
  49.     }

  50.     /**
  51.      * @return Value entry index in the cpool
  52.      */
  53.     public int getIndex() {
  54.         return index;
  55.     }

  56.     public boolean getValueBoolean() {
  57.         if (super.getType() != PRIMITIVE_BOOLEAN) {
  58.             throw new IllegalStateException("Don't call getValueBoolean() on a non BOOLEAN ElementValue");
  59.         }
  60.         final ConstantInteger bo = (ConstantInteger) super.getConstantPool().getConstant(getIndex());
  61.         return bo.getBytes() != 0;
  62.     }

  63.     public byte getValueByte() {
  64.         if (super.getType() != PRIMITIVE_BYTE) {
  65.             throw new IllegalStateException("Don't call getValueByte() on a non BYTE ElementValue");
  66.         }
  67.         return (byte) super.getConstantPool().getConstantInteger(getIndex()).getBytes();
  68.     }

  69.     public char getValueChar() {
  70.         if (super.getType() != PRIMITIVE_CHAR) {
  71.             throw new IllegalStateException("Don't call getValueChar() on a non CHAR ElementValue");
  72.         }
  73.         return (char) super.getConstantPool().getConstantInteger(getIndex()).getBytes();
  74.     }

  75.     public double getValueDouble() {
  76.         if (super.getType() != PRIMITIVE_DOUBLE) {
  77.             throw new IllegalStateException("Don't call getValueDouble() on a non DOUBLE ElementValue");
  78.         }
  79.         final ConstantDouble d = (ConstantDouble) super.getConstantPool().getConstant(getIndex());
  80.         return d.getBytes();
  81.     }

  82.     public float getValueFloat() {
  83.         if (super.getType() != PRIMITIVE_FLOAT) {
  84.             throw new IllegalStateException("Don't call getValueFloat() on a non FLOAT ElementValue");
  85.         }
  86.         final ConstantFloat f = (ConstantFloat) super.getConstantPool().getConstant(getIndex());
  87.         return f.getBytes();
  88.     }

  89.     public int getValueInt() {
  90.         if (super.getType() != PRIMITIVE_INT) {
  91.             throw new IllegalStateException("Don't call getValueInt() on a non INT ElementValue");
  92.         }
  93.         return super.getConstantPool().getConstantInteger(getIndex()).getBytes();
  94.     }

  95.     public long getValueLong() {
  96.         if (super.getType() != PRIMITIVE_LONG) {
  97.             throw new IllegalStateException("Don't call getValueLong() on a non LONG ElementValue");
  98.         }
  99.         final ConstantLong j = (ConstantLong) super.getConstantPool().getConstant(getIndex());
  100.         return j.getBytes();
  101.     }

  102.     public short getValueShort() {
  103.         if (super.getType() != PRIMITIVE_SHORT) {
  104.             throw new IllegalStateException("Don't call getValueShort() on a non SHORT ElementValue");
  105.         }
  106.         final ConstantInteger s = (ConstantInteger) super.getConstantPool().getConstant(getIndex());
  107.         return (short) s.getBytes();
  108.     }

  109.     public String getValueString() {
  110.         if (super.getType() != STRING) {
  111.             throw new IllegalStateException("Don't call getValueString() on a non STRING ElementValue");
  112.         }
  113.         return super.getConstantPool().getConstantUtf8(getIndex()).getBytes();
  114.     }

  115.     public void setIndex(final int index) {
  116.         this.index = index;
  117.     }

  118.     // Whatever kind of value it is, return it as a string
  119.     @Override
  120.     public String stringifyValue() {
  121.         final ConstantPool cpool = super.getConstantPool();
  122.         final int type = super.getType();
  123.         switch (type) {
  124.         case PRIMITIVE_INT:
  125.             return Integer.toString(cpool.getConstantInteger(getIndex()).getBytes());
  126.         case PRIMITIVE_LONG:
  127.             final ConstantLong j = cpool.getConstant(getIndex(), Const.CONSTANT_Long, ConstantLong.class);
  128.             return Long.toString(j.getBytes());
  129.         case PRIMITIVE_DOUBLE:
  130.             final ConstantDouble d = cpool.getConstant(getIndex(), Const.CONSTANT_Double, ConstantDouble.class);
  131.             return Double.toString(d.getBytes());
  132.         case PRIMITIVE_FLOAT:
  133.             final ConstantFloat f = cpool.getConstant(getIndex(), Const.CONSTANT_Float, ConstantFloat.class);
  134.             return Float.toString(f.getBytes());
  135.         case PRIMITIVE_SHORT:
  136.             final ConstantInteger s = cpool.getConstantInteger(getIndex());
  137.             return Integer.toString(s.getBytes());
  138.         case PRIMITIVE_BYTE:
  139.             final ConstantInteger b = cpool.getConstantInteger(getIndex());
  140.             return Integer.toString(b.getBytes());
  141.         case PRIMITIVE_CHAR:
  142.             final ConstantInteger ch = cpool.getConstantInteger(getIndex());
  143.             return String.valueOf((char) ch.getBytes());
  144.         case PRIMITIVE_BOOLEAN:
  145.             final ConstantInteger bo = cpool.getConstantInteger(getIndex());
  146.             if (bo.getBytes() == 0) {
  147.                 return "false";
  148.             }
  149.             return "true";
  150.         case STRING:
  151.             return cpool.getConstantUtf8(getIndex()).getBytes();
  152.         default:
  153.             throw new IllegalStateException("SimpleElementValue class does not know how to stringify type " + type);
  154.         }
  155.     }

  156.     @Override
  157.     public String toString() {
  158.         return stringifyValue();
  159.     }
  160. }