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.ClassElementValue;
25 import org.apache.bcel.classfile.ConstantUtf8;
26 import org.apache.bcel.classfile.ElementValue;
27
28 /**
29 * Generates class element values in annotations.
30 *
31 * @since 6.0
32 */
33 public class ClassElementValueGen extends ElementValueGen {
34 // For primitive types and string type, this points to the value entry in
35 // the cpool
36 // For 'class' this points to the class entry in the cpool
37 private final int idx;
38
39 /**
40 * Constructs a ClassElementValueGen.
41 *
42 * @param value the class element value.
43 * @param cpool the constant pool generator.
44 * @param copyPoolEntries whether to copy pool entries.
45 */
46 public ClassElementValueGen(final ClassElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
47 super(CLASS, cpool);
48 if (copyPoolEntries) {
49 // idx = cpool.addClass(value.getClassString());
50 idx = cpool.addUtf8(value.getClassString());
51 } else {
52 idx = value.getIndex();
53 }
54 }
55
56 /**
57 * Constructs a ClassElementValueGen.
58 *
59 * @param typeIdx the type index.
60 * @param cpool the constant pool generator.
61 */
62 protected ClassElementValueGen(final int typeIdx, final ConstantPoolGen cpool) {
63 super(CLASS, cpool);
64 this.idx = typeIdx;
65 }
66
67 /**
68 * Constructs a ClassElementValueGen.
69 *
70 * @param t the object type.
71 * @param cpool the constant pool generator.
72 */
73 public ClassElementValueGen(final ObjectType t, final ConstantPoolGen cpool) {
74 super(CLASS, cpool);
75 // this.idx = cpool.addClass(t);
76 idx = cpool.addUtf8(t.getSignature());
77 }
78
79 @Override
80 public void dump(final DataOutputStream dos) throws IOException {
81 dos.writeByte(super.getElementValueType()); // u1 kind of value
82 dos.writeShort(idx);
83 }
84
85 /**
86 * Gets the class string.
87 *
88 * @return the class string.
89 */
90 public String getClassString() {
91 final ConstantUtf8 cu8 = (ConstantUtf8) getConstantPool().getConstant(idx);
92 return cu8.getBytes();
93 // ConstantClass c = (ConstantClass) getConstantPool().getConstant(idx);
94 // ConstantUtf8 utf8 =
95 // (ConstantUtf8) getConstantPool().getConstant(c.getNameIndex());
96 // return utf8.getBytes();
97 }
98
99 /**
100 * Return immutable variant of this ClassElementValueGen.
101 *
102 * @return immutable variant of this ClassElementValueGen.
103 */
104 @Override
105 public ElementValue getElementValue() {
106 return new ClassElementValue(super.getElementValueType(), idx, getConstantPool().getConstantPool());
107 }
108
109 /**
110 * Gets the index.
111 *
112 * @return the index.
113 */
114 public int getIndex() {
115 return idx;
116 }
117
118 @Override
119 public String stringifyValue() {
120 return getClassString();
121 }
122 }