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         // Constant cc = getConstantPool().getConstant(typeIdx);
112         // ConstantClass cu8 =
113         // (ConstantClass) getConstantPool().getConstant(typeIdx);
114         // return
115         // ((ConstantUtf8) getConstantPool().getConstant(cu8.getNameIndex())).getBytes();
116         return ((ConstantUtf8) getConstantPool().getConstant(typeIdx)).getBytes();
117         // return Utility.signatureToString(cu8.getBytes());
118     }
119 
120     /**
121      * Gets the enum value string.
122      *
123      * @return The enum value string.
124      */
125     public String getEnumValueString() {
126         return ((ConstantUtf8) getConstantPool().getConstant(valueIdx)).getBytes();
127         // ConstantString cu8 =
128         // (ConstantString) getConstantPool().getConstant(valueIdx);
129         // return
130         // ((ConstantUtf8) getConstantPool().getConstant(cu8.getStringIndex())).getBytes();
131     }
132 
133     /**
134      * Gets the type index.
135      *
136      * @return The type index.
137      */
138     public int getTypeIndex() {
139         return typeIdx;
140     }
141 
142     /**
143      * Gets the value index.
144      *
145      * @return The value index.
146      */
147     public int getValueIndex() {
148         return valueIdx;
149     }
150 
151     @Override
152     public String stringifyValue() {
153         final ConstantUtf8 cu8 = (ConstantUtf8) getConstantPool().getConstant(valueIdx);
154         return cu8.getBytes();
155         // ConstantString cu8 =
156         // (ConstantString) getConstantPool().getConstant(valueIdx);
157         // return
158         // ((ConstantUtf8) getConstantPool().getConstant(cu8.getStringIndex())).getBytes();
159     }
160 }