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