EnumElementValue.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. /**
  21.  * @since 6.0
  22.  */
  23. public class EnumElementValue extends ElementValue {
  24.     // For enum types, these two indices point to the type and value
  25.     private final int typeIdx;

  26.     private final int valueIdx;

  27.     public EnumElementValue(final int type, final int typeIdx, final int valueIdx, final ConstantPool cpool) {
  28.         super(type, cpool);
  29.         if (type != ENUM_CONSTANT) {
  30.             throw new ClassFormatException("Only element values of type enum can be built with this ctor - type specified: " + type);
  31.         }
  32.         this.typeIdx = typeIdx;
  33.         this.valueIdx = valueIdx;
  34.     }

  35.     @Override
  36.     public void dump(final DataOutputStream dos) throws IOException {
  37.         dos.writeByte(super.getType()); // u1 type of value (ENUM_CONSTANT == 'e')
  38.         dos.writeShort(typeIdx); // u2
  39.         dos.writeShort(valueIdx); // u2
  40.     }

  41.     public String getEnumTypeString() {
  42.         return super.getConstantPool().getConstantUtf8(typeIdx).getBytes();
  43.     }

  44.     public String getEnumValueString() {
  45.         return super.getConstantPool().getConstantUtf8(valueIdx).getBytes();
  46.     }

  47.     public int getTypeIndex() {
  48.         return typeIdx;
  49.     }

  50.     public int getValueIndex() {
  51.         return valueIdx;
  52.     }

  53.     @Override
  54.     public String stringifyValue() {
  55.         return super.getConstantPool().getConstantUtf8(valueIdx).getBytes();
  56.     }
  57. }