View Javadoc
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   * @since 6.0
30   */
31  public class ClassElementValueGen extends ElementValueGen {
32      // For primitive types and string type, this points to the value entry in
33      // the cpool
34      // For 'class' this points to the class entry in the cpool
35      private final int idx;
36  
37      public ClassElementValueGen(final ClassElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
38          super(CLASS, cpool);
39          if (copyPoolEntries) {
40              // idx = cpool.addClass(value.getClassString());
41              idx = cpool.addUtf8(value.getClassString());
42          } else {
43              idx = value.getIndex();
44          }
45      }
46  
47      protected ClassElementValueGen(final int typeIdx, final ConstantPoolGen cpool) {
48          super(CLASS, cpool);
49          this.idx = typeIdx;
50      }
51  
52      public ClassElementValueGen(final ObjectType t, final ConstantPoolGen cpool) {
53          super(CLASS, cpool);
54          // this.idx = cpool.addClass(t);
55          idx = cpool.addUtf8(t.getSignature());
56      }
57  
58      @Override
59      public void dump(final DataOutputStream dos) throws IOException {
60          dos.writeByte(super.getElementValueType()); // u1 kind of value
61          dos.writeShort(idx);
62      }
63  
64      public String getClassString() {
65          final ConstantUtf8 cu8 = (ConstantUtf8) getConstantPool().getConstant(idx);
66          return cu8.getBytes();
67          // ConstantClass c = (ConstantClass) getConstantPool().getConstant(idx);
68          // ConstantUtf8 utf8 =
69          // (ConstantUtf8) getConstantPool().getConstant(c.getNameIndex());
70          // return utf8.getBytes();
71      }
72  
73      /**
74       * Return immutable variant of this ClassElementValueGen
75       */
76      @Override
77      public ElementValue getElementValue() {
78          return new ClassElementValue(super.getElementValueType(), idx, getConstantPool().getConstantPool());
79      }
80  
81      public int getIndex() {
82          return idx;
83      }
84  
85      @Override
86      public String stringifyValue() {
87          return getClassString();
88      }
89  }