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.ElementValuePair;
27  
28  /**
29   * @since 6.0
30   */
31  public class ElementValuePairGen {
32      private final int nameIdx;
33  
34      private final ElementValueGen value;
35  
36      private final ConstantPoolGen constantPoolGen;
37  
38      public ElementValuePairGen(final ElementValuePair nvp, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
39          this.constantPoolGen = cpool;
40          // J5ASSERT:
41          // Could assert nvp.getNameString() points to the same thing as
42          // constantPoolGen.getConstant(nvp.getNameIndex())
43          // if
44          // (!nvp.getNameString().equals(((ConstantUtf8) constantPoolGen.getConstant(nvp.getNameIndex())).getBytes()))
45          // {
46          // throw new IllegalArgumentException("envp buggered");
47          // }
48          if (copyPoolEntries) {
49              nameIdx = cpool.addUtf8(nvp.getNameString());
50          } else {
51              nameIdx = nvp.getNameIndex();
52          }
53          value = ElementValueGen.copy(nvp.getValue(), cpool, copyPoolEntries);
54      }
55  
56      protected ElementValuePairGen(final int idx, final ElementValueGen value, final ConstantPoolGen cpool) {
57          this.nameIdx = idx;
58          this.value = value;
59          this.constantPoolGen = cpool;
60      }
61  
62      public ElementValuePairGen(final String name, final ElementValueGen value, final ConstantPoolGen cpool) {
63          this.nameIdx = cpool.addUtf8(name);
64          this.value = value;
65          this.constantPoolGen = cpool;
66      }
67  
68      protected void dump(final DataOutputStream dos) throws IOException {
69          dos.writeShort(nameIdx); // u2 name of the element
70          value.dump(dos);
71      }
72  
73      /**
74       * Retrieve an immutable version of this ElementNameValuePairGen
75       */
76      public ElementValuePair getElementNameValuePair() {
77          final ElementValue immutableValue = value.getElementValue();
78          return new ElementValuePair(nameIdx, immutableValue, constantPoolGen.getConstantPool());
79      }
80  
81      public int getNameIndex() {
82          return nameIdx;
83      }
84  
85      public final String getNameString() {
86          // ConstantString cu8 = (ConstantString) constantPoolGen.getConstant(nameIdx);
87          return ((ConstantUtf8) constantPoolGen.getConstant(nameIdx)).getBytes();
88      }
89  
90      public final ElementValueGen getValue() {
91          return value;
92      }
93  
94      @Override
95      public String toString() {
96          return "ElementValuePair:[" + getNameString() + "=" + value.stringifyValue() + "]";
97      }
98  }