1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 /*
6  * Licensed to the Apache Software Foundation (ASF) under one or more
7  * contributor license agreements.  See the NOTICE file distributed with
8  * this work for additional information regarding copyright ownership.
9  * The ASF licenses this file to You under the Apache License, Version 2.0
10  * (the "License"); you may not use this file except in compliance with
11  * the License.  You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 package com.sun.org.apache.bcel.internal.generic;
23 
24 import java.io.DataOutputStream;
25 import java.io.IOException;
26 
27 import com.sun.org.apache.bcel.internal.classfile.ClassElementValue;
28 import com.sun.org.apache.bcel.internal.classfile.ConstantUtf8;
29 import com.sun.org.apache.bcel.internal.classfile.ElementValue;
30 
31 /**
32  * @since 6.0
33  */
34 public class ClassElementValueGen extends ElementValueGen
35 {
36     // For primitive types and string type, this points to the value entry in
37     // the cpool
38     // For 'class' this points to the class entry in the cpool
39     private int idx;
40 
ClassElementValueGen(final int typeIdx, final ConstantPoolGen cpool)41     protected ClassElementValueGen(final int typeIdx, final ConstantPoolGen cpool)
42     {
43         super(ElementValueGen.CLASS, cpool);
44         this.idx = typeIdx;
45     }
46 
ClassElementValueGen(final ObjectType t, final ConstantPoolGen cpool)47     public ClassElementValueGen(final ObjectType t, final ConstantPoolGen cpool)
48     {
49         super(ElementValueGen.CLASS, cpool);
50         // this.idx = cpool.addClass(t);
51         idx = cpool.addUtf8(t.getSignature());
52     }
53 
54     /**
55      * Return immutable variant of this ClassElementValueGen
56      */
57     @Override
getElementValue()58     public ElementValue getElementValue()
59     {
60         return new ClassElementValue(super.getElementValueType(),
61                 idx,
62                 getConstantPool().getConstantPool());
63     }
64 
ClassElementValueGen(final ClassElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries)65     public ClassElementValueGen(final ClassElementValue value, final ConstantPoolGen cpool,
66             final boolean copyPoolEntries)
67     {
68         super(CLASS, cpool);
69         if (copyPoolEntries)
70         {
71             // idx = cpool.addClass(value.getClassString());
72             idx = cpool.addUtf8(value.getClassString());
73         }
74         else
75         {
76             idx = value.getIndex();
77         }
78     }
79 
getIndex()80     public int getIndex()
81     {
82         return idx;
83     }
84 
getClassString()85     public String getClassString()
86     {
87         final ConstantUtf8 cu8 = (ConstantUtf8) getConstantPool().getConstant(idx);
88         return cu8.getBytes();
89         // ConstantClass c = (ConstantClass)getConstantPool().getConstant(idx);
90         // ConstantUtf8 utf8 =
91         // (ConstantUtf8)getConstantPool().getConstant(c.getNameIndex());
92         // return utf8.getBytes();
93     }
94 
95     @Override
stringifyValue()96     public String stringifyValue()
97     {
98         return getClassString();
99     }
100 
101     @Override
dump(final DataOutputStream dos)102     public void dump(final DataOutputStream dos) throws IOException
103     {
104         dos.writeByte(super.getElementValueType()); // u1 kind of value
105         dos.writeShort(idx);
106     }
107 }
108