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