SimpleElementValueGen.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.generic;

  18. import java.io.DataOutputStream;
  19. import java.io.IOException;

  20. import org.apache.bcel.classfile.ConstantDouble;
  21. import org.apache.bcel.classfile.ConstantFloat;
  22. import org.apache.bcel.classfile.ConstantInteger;
  23. import org.apache.bcel.classfile.ConstantLong;
  24. import org.apache.bcel.classfile.ConstantUtf8;
  25. import org.apache.bcel.classfile.ElementValue;
  26. import org.apache.bcel.classfile.SimpleElementValue;

  27. /**
  28.  * @since 6.0
  29.  */
  30. public class SimpleElementValueGen extends ElementValueGen {
  31.     // For primitive types and string type, this points to the value entry in
  32.     // the cpGen
  33.     // For 'class' this points to the class entry in the cpGen
  34.     private final int idx;

  35.     public SimpleElementValueGen(final int type, final ConstantPoolGen cpGen, final boolean value) {
  36.         super(type, cpGen);
  37.         if (value) {
  38.             idx = getConstantPool().addInteger(1);
  39.         } else {
  40.             idx = getConstantPool().addInteger(0);
  41.         }
  42.     }

  43.     public SimpleElementValueGen(final int type, final ConstantPoolGen cpGen, final byte value) {
  44.         super(type, cpGen);
  45.         idx = getConstantPool().addInteger(value);
  46.     }

  47.     public SimpleElementValueGen(final int type, final ConstantPoolGen cpGen, final char value) {
  48.         super(type, cpGen);
  49.         idx = getConstantPool().addInteger(value);
  50.     }

  51.     public SimpleElementValueGen(final int type, final ConstantPoolGen cpGen, final double value) {
  52.         super(type, cpGen);
  53.         idx = getConstantPool().addDouble(value);
  54.     }

  55.     public SimpleElementValueGen(final int type, final ConstantPoolGen cpGen, final float value) {
  56.         super(type, cpGen);
  57.         idx = getConstantPool().addFloat(value);
  58.     }

  59.     public SimpleElementValueGen(final int type, final ConstantPoolGen cpGen, final int value) {
  60.         super(type, cpGen);
  61.         idx = getConstantPool().addInteger(value);
  62.     }

  63.     public SimpleElementValueGen(final int type, final ConstantPoolGen cpGen, final long value) {
  64.         super(type, cpGen);
  65.         idx = getConstantPool().addLong(value);
  66.     }

  67.     public SimpleElementValueGen(final int type, final ConstantPoolGen cpGen, final short value) {
  68.         super(type, cpGen);
  69.         idx = getConstantPool().addInteger(value);
  70.     }

  71.     public SimpleElementValueGen(final int type, final ConstantPoolGen cpGen, final String value) {
  72.         super(type, cpGen);
  73.         idx = getConstantPool().addUtf8(value);
  74.     }

  75.     // ctors for each supported type... type could be inferred but for now lets
  76.     // force it to be passed
  77.     /**
  78.      * Protected ctor used for deserialization, doesn't *put* an entry in the constant pool, assumes the one at the supplied
  79.      * index is correct.
  80.      */
  81.     protected SimpleElementValueGen(final int type, final int idx, final ConstantPoolGen cpGen) {
  82.         super(type, cpGen);
  83.         this.idx = idx;
  84.     }

  85.     /**
  86.      * The boolean controls whether we copy info from the 'old' constant pool to the 'new'. You need to use this ctor if the
  87.      * annotation is being copied from one file to another.
  88.      */
  89.     public SimpleElementValueGen(final SimpleElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
  90.         super(value.getElementValueType(), cpool);
  91.         if (!copyPoolEntries) {
  92.             // J5ASSERT: Could assert value.stringifyValue() is the same as
  93.             // cpool.getConstant(SimpleElementValuevalue.getIndex())
  94.             idx = value.getIndex();
  95.         } else {
  96.             switch (value.getElementValueType()) {
  97.             case STRING:
  98.                 idx = cpool.addUtf8(value.getValueString());
  99.                 break;
  100.             case PRIMITIVE_INT:
  101.                 idx = cpool.addInteger(value.getValueInt());
  102.                 break;
  103.             case PRIMITIVE_BYTE:
  104.                 idx = cpool.addInteger(value.getValueByte());
  105.                 break;
  106.             case PRIMITIVE_CHAR:
  107.                 idx = cpool.addInteger(value.getValueChar());
  108.                 break;
  109.             case PRIMITIVE_LONG:
  110.                 idx = cpool.addLong(value.getValueLong());
  111.                 break;
  112.             case PRIMITIVE_FLOAT:
  113.                 idx = cpool.addFloat(value.getValueFloat());
  114.                 break;
  115.             case PRIMITIVE_DOUBLE:
  116.                 idx = cpool.addDouble(value.getValueDouble());
  117.                 break;
  118.             case PRIMITIVE_BOOLEAN:
  119.                 if (value.getValueBoolean()) {
  120.                     idx = cpool.addInteger(1);
  121.                 } else {
  122.                     idx = cpool.addInteger(0);
  123.                 }
  124.                 break;
  125.             case PRIMITIVE_SHORT:
  126.                 idx = cpool.addInteger(value.getValueShort());
  127.                 break;
  128.             default:
  129.                 throw new IllegalArgumentException("SimpleElementValueGen class does not know how to copy this type " + super.getElementValueType());
  130.             }
  131.         }
  132.     }

  133.     @Override
  134.     public void dump(final DataOutputStream dos) throws IOException {
  135.         dos.writeByte(super.getElementValueType()); // u1 kind of value
  136.         switch (super.getElementValueType()) {
  137.         case PRIMITIVE_INT:
  138.         case PRIMITIVE_BYTE:
  139.         case PRIMITIVE_CHAR:
  140.         case PRIMITIVE_FLOAT:
  141.         case PRIMITIVE_LONG:
  142.         case PRIMITIVE_BOOLEAN:
  143.         case PRIMITIVE_SHORT:
  144.         case PRIMITIVE_DOUBLE:
  145.         case STRING:
  146.             dos.writeShort(idx);
  147.             break;
  148.         default:
  149.             throw new IllegalStateException("SimpleElementValueGen doesn't know how to write out type " + super.getElementValueType());
  150.         }
  151.     }

  152.     /**
  153.      * Return immutable variant
  154.      */
  155.     @Override
  156.     public ElementValue getElementValue() {
  157.         return new SimpleElementValue(super.getElementValueType(), idx, getConstantPool().getConstantPool());
  158.     }

  159.     public int getIndex() {
  160.         return idx;
  161.     }

  162.     public int getValueInt() {
  163.         if (super.getElementValueType() != PRIMITIVE_INT) {
  164.             throw new IllegalStateException("Don't call getValueString() on a non STRING ElementValue");
  165.         }
  166.         final ConstantInteger c = (ConstantInteger) getConstantPool().getConstant(idx);
  167.         return c.getBytes();
  168.     }

  169.     public String getValueString() {
  170.         if (super.getElementValueType() != STRING) {
  171.             throw new IllegalStateException("Don't call getValueString() on a non STRING ElementValue");
  172.         }
  173.         final ConstantUtf8 c = (ConstantUtf8) getConstantPool().getConstant(idx);
  174.         return c.getBytes();
  175.     }

  176.     // Whatever kind of value it is, return it as a string
  177.     @Override
  178.     public String stringifyValue() {
  179.         switch (super.getElementValueType()) {
  180.         case PRIMITIVE_INT:
  181.             final ConstantInteger c = (ConstantInteger) getConstantPool().getConstant(idx);
  182.             return Integer.toString(c.getBytes());
  183.         case PRIMITIVE_LONG:
  184.             final ConstantLong j = (ConstantLong) getConstantPool().getConstant(idx);
  185.             return Long.toString(j.getBytes());
  186.         case PRIMITIVE_DOUBLE:
  187.             final ConstantDouble d = (ConstantDouble) getConstantPool().getConstant(idx);
  188.             return Double.toString(d.getBytes());
  189.         case PRIMITIVE_FLOAT:
  190.             final ConstantFloat f = (ConstantFloat) getConstantPool().getConstant(idx);
  191.             return Float.toString(f.getBytes());
  192.         case PRIMITIVE_SHORT:
  193.             final ConstantInteger s = (ConstantInteger) getConstantPool().getConstant(idx);
  194.             return Integer.toString(s.getBytes());
  195.         case PRIMITIVE_BYTE:
  196.             final ConstantInteger b = (ConstantInteger) getConstantPool().getConstant(idx);
  197.             return Integer.toString(b.getBytes());
  198.         case PRIMITIVE_CHAR:
  199.             final ConstantInteger ch = (ConstantInteger) getConstantPool().getConstant(idx);
  200.             return Integer.toString(ch.getBytes());
  201.         case PRIMITIVE_BOOLEAN:
  202.             final ConstantInteger bo = (ConstantInteger) getConstantPool().getConstant(idx);
  203.             if (bo.getBytes() == 0) {
  204.                 return "false";
  205.             }
  206.             return "true";
  207.         case STRING:
  208.             final ConstantUtf8 cu8 = (ConstantUtf8) getConstantPool().getConstant(idx);
  209.             return cu8.getBytes();
  210.         default:
  211.             throw new IllegalStateException("SimpleElementValueGen class does not know how to stringify type " + super.getElementValueType());
  212.         }
  213.     }
  214. }