1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 /*
6  * Licensed to the Apache Software Foundation (ASF) under one or more
7  * contributor license agreements.  See the NOTICE file distributed with
8  * this work for additional information regarding copyright ownership.
9  * The ASF licenses this file to You under the Apache License, Version 2.0
10  * (the "License"); you may not use this file except in compliance with
11  * the License.  You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 package com.sun.org.apache.bcel.internal.generic;
23 
24 import java.io.DataOutputStream;
25 import java.io.IOException;
26 
27 import com.sun.org.apache.bcel.internal.classfile.ConstantUtf8;
28 import com.sun.org.apache.bcel.internal.classfile.ElementValue;
29 import com.sun.org.apache.bcel.internal.classfile.ElementValuePair;
30 
31 /**
32  * @since 6.0
33  */
34 public class ElementValuePairGen
35 {
36     private int nameIdx;
37 
38     private final ElementValueGen value;
39 
40     private final ConstantPoolGen cpool;
41 
ElementValuePairGen(final ElementValuePair nvp, final ConstantPoolGen cpool, final boolean copyPoolEntries)42     public ElementValuePairGen(final ElementValuePair nvp, final ConstantPoolGen cpool,
43             final boolean copyPoolEntries)
44     {
45         this.cpool = cpool;
46         // J5ASSERT:
47         // Could assert nvp.getNameString() points to the same thing as
48         // cpool.getConstant(nvp.getNameIndex())
49         // if
50         // (!nvp.getNameString().equals(((ConstantUtf8)cpool.getConstant(nvp.getNameIndex())).getBytes()))
51         // {
52         // throw new RuntimeException("envp buggered");
53         // }
54         if (copyPoolEntries)
55         {
56             nameIdx = cpool.addUtf8(nvp.getNameString());
57         }
58         else
59         {
60             nameIdx = nvp.getNameIndex();
61         }
62         value = ElementValueGen.copy(nvp.getValue(), cpool, copyPoolEntries);
63     }
64 
65     /**
66      * Retrieve an immutable version of this ElementNameValuePairGen
67      */
getElementNameValuePair()68     public ElementValuePair getElementNameValuePair()
69     {
70         final ElementValue immutableValue = value.getElementValue();
71         return new ElementValuePair(nameIdx, immutableValue, cpool
72                 .getConstantPool());
73     }
74 
ElementValuePairGen(final int idx, final ElementValueGen value, final ConstantPoolGen cpool)75     protected ElementValuePairGen(final int idx, final ElementValueGen value,
76             final ConstantPoolGen cpool)
77     {
78         this.nameIdx = idx;
79         this.value = value;
80         this.cpool = cpool;
81     }
82 
ElementValuePairGen(final String name, final ElementValueGen value, final ConstantPoolGen cpool)83     public ElementValuePairGen(final String name, final ElementValueGen value,
84             final ConstantPoolGen cpool)
85     {
86         this.nameIdx = cpool.addUtf8(name);
87         this.value = value;
88         this.cpool = cpool;
89     }
90 
dump(final DataOutputStream dos)91     protected void dump(final DataOutputStream dos) throws IOException
92     {
93         dos.writeShort(nameIdx); // u2 name of the element
94         value.dump(dos);
95     }
96 
getNameIndex()97     public int getNameIndex()
98     {
99         return nameIdx;
100     }
101 
getNameString()102     public final String getNameString()
103     {
104         // ConstantString cu8 = (ConstantString)cpool.getConstant(nameIdx);
105         return ((ConstantUtf8) cpool.getConstant(nameIdx)).getBytes();
106     }
107 
getValue()108     public final ElementValueGen getValue()
109     {
110         return value;
111     }
112 
113     @Override
toString()114     public String toString()
115     {
116         return "ElementValuePair:[" + getNameString() + "="
117                 + value.stringifyValue() + "]";
118     }
119 }
120