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.generic;
20  
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  import org.apache.bcel.classfile.ConstantUtf8;
25  import org.apache.bcel.classfile.ElementValue;
26  import org.apache.bcel.classfile.EnumElementValue;
27  
28  /**
29   * Generates enum element values in annotations.
30   *
31   * @since 6.0
32   */
33  public class EnumElementValueGen extends ElementValueGen {
34      // For enum types, these two indices point to the type and value
35      private final int typeIdx;
36  
37      private final int valueIdx;
38  
39      /**
40       * Constructs an EnumElementValueGen from an EnumElementValue.
41       *
42       * @param value the enum element value.
43       * @param cpool the constant pool.
44       * @param copyPoolEntries whether to copy pool entries.
45       */
46      public EnumElementValueGen(final EnumElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
47          super(ENUM_CONSTANT, cpool);
48          if (copyPoolEntries) {
49              typeIdx = cpool.addUtf8(value.getEnumTypeString()); // was addClass(value.getEnumTypeString());
50              valueIdx = cpool.addUtf8(value.getEnumValueString()); // was addString(value.getEnumValueString());
51          } else {
52              typeIdx = value.getTypeIndex();
53              valueIdx = value.getValueIndex();
54          }
55      }
56  
57      /**
58       * This constructor assumes the constant pool already contains the right type and value - as indicated by typeIdx and valueIdx.
59       * This constructor is used for deserialization.
60       *
61       * @param typeIdx the type index.
62       * @param valueIdx the value index.
63       * @param cpool the constant pool.
64       */
65      protected EnumElementValueGen(final int typeIdx, final int valueIdx, final ConstantPoolGen cpool) {
66          super(ENUM_CONSTANT, cpool);
67          if (super.getElementValueType() != ENUM_CONSTANT) {
68              throw new IllegalArgumentException("Only element values of type enum can be built with this ctor - type specified: " + super.getElementValueType());
69          }
70          this.typeIdx = typeIdx;
71          this.valueIdx = valueIdx;
72      }
73  
74      /**
75       * Constructs an EnumElementValueGen.
76       *
77       * @param t the object type.
78       * @param value the value.
79       * @param cpool the constant pool.
80       */
81      public EnumElementValueGen(final ObjectType t, final String value, final ConstantPoolGen cpool) {
82          super(ENUM_CONSTANT, cpool);
83          typeIdx = cpool.addUtf8(t.getSignature()); // was addClass(t);
84          valueIdx = cpool.addUtf8(value); // was addString(value);
85      }
86  
87      @Override
88      public void dump(final DataOutputStream dos) throws IOException {
89          dos.writeByte(super.getElementValueType()); // u1 type of value (ENUM_CONSTANT == 'e')
90          dos.writeShort(typeIdx); // u2
91          dos.writeShort(valueIdx); // u2
92      }
93  
94      /**
95       * Returns immutable variant of this EnumElementValueGen.
96       *
97       * @return immutable variant.
98       */
99      @Override
100     public ElementValue getElementValue() {
101         System.err.println("Duplicating value: " + getEnumTypeString() + ":" + getEnumValueString());
102         return new EnumElementValue(super.getElementValueType(), typeIdx, valueIdx, getConstantPool().getConstantPool());
103     }
104 
105     /**
106      * Gets the enum type string.
107      *
108      * @return the enum type string.
109      */
110     public String getEnumTypeString() {
111         // ...existing code...
112         return ((ConstantUtf8) getConstantPool().getConstant(typeIdx)).getBytes();
113         // ...existing code...
114     }
115 
116     /**
117      * Gets the enum value string.
118      *
119      * @return the enum value string.
120      */
121     public String getEnumValueString() {
122         return ((ConstantUtf8) getConstantPool().getConstant(valueIdx)).getBytes();
123         // ...existing code...
124     }
125 
126     /**
127      * Gets the type index.
128      *
129      * @return the type index.
130      */
131     public int getTypeIndex() {
132         return typeIdx;
133     }
134 
135     /**
136      * Gets the value index.
137      *
138      * @return the value index.
139      */
140     public int getValueIndex() {
141         return valueIdx;
142     }
143 
144     @Override
145     public String stringifyValue() {
146         final ConstantUtf8 cu8 = (ConstantUtf8) getConstantPool().getConstant(valueIdx);
147         return cu8.getBytes();
148         // ConstantString cu8 =
149         // (ConstantString) getConstantPool().getConstant(valueIdx);
150         // return
151         // ((ConstantUtf8) getConstantPool().getConstant(cu8.getStringIndex())).getBytes();
152     }
153 }