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