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 * Generates element value pairs in annotations.
30 *
31 * @since 6.0
32 */
33 public class ElementValuePairGen {
34 private final int nameIdx;
35
36 private final ElementValueGen value;
37
38 private final ConstantPoolGen constantPoolGen;
39
40 /**
41 * Constructs an ElementValuePairGen from an ElementValuePair.
42 *
43 * @param nvp the element value pair.
44 * @param cpool the constant pool.
45 * @param copyPoolEntries whether to copy pool entries.
46 */
47 public ElementValuePairGen(final ElementValuePair nvp, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
48 this.constantPoolGen = cpool;
49 // J5ASSERT:
50 // Could assert nvp.getNameString() points to the same thing as
51 // constantPoolGen.getConstant(nvp.getNameIndex())
52 // if
53 // (!nvp.getNameString().equals(((ConstantUtf8) constantPoolGen.getConstant(nvp.getNameIndex())).getBytes()))
54 // {
55 // throw new IllegalArgumentException("envp buggered");
56 // }
57 if (copyPoolEntries) {
58 nameIdx = cpool.addUtf8(nvp.getNameString());
59 } else {
60 nameIdx = nvp.getNameIndex();
61 }
62 value = ElementValueGen.copy(nvp.getValue(), cpool, copyPoolEntries);
63 }
64
65 /**
66 * Constructs an ElementValuePairGen.
67 *
68 * @param idx the name index.
69 * @param value the element value.
70 * @param cpool the constant pool.
71 */
72 protected ElementValuePairGen(final int idx, final ElementValueGen value, final ConstantPoolGen cpool) {
73 this.nameIdx = idx;
74 this.value = value;
75 this.constantPoolGen = cpool;
76 }
77
78 /**
79 * Constructs an ElementValuePairGen.
80 *
81 * @param name the name.
82 * @param value the element value.
83 * @param cpool the constant pool.
84 */
85 public ElementValuePairGen(final String name, final ElementValueGen value, final ConstantPoolGen cpool) {
86 this.nameIdx = cpool.addUtf8(name);
87 this.value = value;
88 this.constantPoolGen = cpool;
89 }
90
91 /**
92 * Dumps this element value pair to a DataOutputStream.
93 *
94 * @param dos the output stream.
95 * @throws IOException if an I/O error occurs.
96 */
97 protected void dump(final DataOutputStream dos) throws IOException {
98 dos.writeShort(nameIdx); // u2 name of the element
99 value.dump(dos);
100 }
101
102 /**
103 * Retrieves an immutable version of this ElementValuePairGen.
104 *
105 * @return an immutable ElementValuePair.
106 */
107 public ElementValuePair getElementNameValuePair() {
108 final ElementValue immutableValue = value.getElementValue();
109 return new ElementValuePair(nameIdx, immutableValue, constantPoolGen.getConstantPool());
110 }
111
112 /**
113 * Gets the name index.
114 *
115 * @return the name index.
116 */
117 public int getNameIndex() {
118 return nameIdx;
119 }
120
121 /**
122 * Gets the name string.
123 *
124 * @return the name string.
125 */
126 public final String getNameString() {
127 // ConstantString cu8 = (ConstantString) constantPoolGen.getConstant(nameIdx);
128 return ((ConstantUtf8) constantPoolGen.getConstant(nameIdx)).getBytes();
129 }
130
131 /**
132 * Gets the value.
133 *
134 * @return the element value.
135 */
136 public final ElementValueGen getValue() {
137 return value;
138 }
139
140 @Override
141 public String toString() {
142 return "ElementValuePair:[" + getNameString() + "=" + value.stringifyValue() + "]";
143 }
144 }