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