EnumElementValueGen.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.ConstantUtf8;
  21. import org.apache.bcel.classfile.ElementValue;
  22. import org.apache.bcel.classfile.EnumElementValue;

  23. /**
  24.  * @since 6.0
  25.  */
  26. public class EnumElementValueGen extends ElementValueGen {
  27.     // For enum types, these two indices point to the type and value
  28.     private final int typeIdx;

  29.     private final int valueIdx;

  30.     public EnumElementValueGen(final EnumElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
  31.         super(ENUM_CONSTANT, cpool);
  32.         if (copyPoolEntries) {
  33.             typeIdx = cpool.addUtf8(value.getEnumTypeString()); // was addClass(value.getEnumTypeString());
  34.             valueIdx = cpool.addUtf8(value.getEnumValueString()); // was addString(value.getEnumValueString());
  35.         } else {
  36.             typeIdx = value.getTypeIndex();
  37.             valueIdx = value.getValueIndex();
  38.         }
  39.     }

  40.     /**
  41.      * This ctor assumes the constant pool already contains the right type and value - as indicated by typeIdx and valueIdx.
  42.      * This ctor is used for deserialization
  43.      */
  44.     protected EnumElementValueGen(final int typeIdx, final int valueIdx, final ConstantPoolGen cpool) {
  45.         super(ENUM_CONSTANT, cpool);
  46.         if (super.getElementValueType() != ENUM_CONSTANT) {
  47.             throw new IllegalArgumentException("Only element values of type enum can be built with this ctor - type specified: " + super.getElementValueType());
  48.         }
  49.         this.typeIdx = typeIdx;
  50.         this.valueIdx = valueIdx;
  51.     }

  52.     public EnumElementValueGen(final ObjectType t, final String value, final ConstantPoolGen cpool) {
  53.         super(ENUM_CONSTANT, cpool);
  54.         typeIdx = cpool.addUtf8(t.getSignature()); // was addClass(t);
  55.         valueIdx = cpool.addUtf8(value); // was addString(value);
  56.     }

  57.     @Override
  58.     public void dump(final DataOutputStream dos) throws IOException {
  59.         dos.writeByte(super.getElementValueType()); // u1 type of value (ENUM_CONSTANT == 'e')
  60.         dos.writeShort(typeIdx); // u2
  61.         dos.writeShort(valueIdx); // u2
  62.     }

  63.     /**
  64.      * Return immutable variant of this EnumElementValue
  65.      */
  66.     @Override
  67.     public ElementValue getElementValue() {
  68.         System.err.println("Duplicating value: " + getEnumTypeString() + ":" + getEnumValueString());
  69.         return new EnumElementValue(super.getElementValueType(), typeIdx, valueIdx, getConstantPool().getConstantPool());
  70.     }

  71.     // BCELBUG: Should we need to call utility.signatureToString() on the output
  72.     // here?
  73.     public String getEnumTypeString() {
  74.         // Constant cc = getConstantPool().getConstant(typeIdx);
  75.         // ConstantClass cu8 =
  76.         // (ConstantClass) getConstantPool().getConstant(typeIdx);
  77.         // return
  78.         // ((ConstantUtf8) getConstantPool().getConstant(cu8.getNameIndex())).getBytes();
  79.         return ((ConstantUtf8) getConstantPool().getConstant(typeIdx)).getBytes();
  80.         // return Utility.signatureToString(cu8.getBytes());
  81.     }

  82.     public String getEnumValueString() {
  83.         return ((ConstantUtf8) getConstantPool().getConstant(valueIdx)).getBytes();
  84.         // ConstantString cu8 =
  85.         // (ConstantString) getConstantPool().getConstant(valueIdx);
  86.         // return
  87.         // ((ConstantUtf8) getConstantPool().getConstant(cu8.getStringIndex())).getBytes();
  88.     }

  89.     public int getTypeIndex() {
  90.         return typeIdx;
  91.     }

  92.     public int getValueIndex() {
  93.         return valueIdx;
  94.     }

  95.     @Override
  96.     public String stringifyValue() {
  97.         final ConstantUtf8 cu8 = (ConstantUtf8) getConstantPool().getConstant(valueIdx);
  98.         return cu8.getBytes();
  99.         // ConstantString cu8 =
  100.         // (ConstantString) getConstantPool().getConstant(valueIdx);
  101.         // return
  102.         // ((ConstantUtf8) getConstantPool().getConstant(cu8.getStringIndex())).getBytes();
  103.     }
  104. }