• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.classfile;
19 
20 import java.io.DataInput;
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23 
24 import org.apache.bcel.Const;
25 
26 /**
27  * This class is derived from the abstract {@link Constant}
28  * and represents a reference to a (external) class.
29  *
30  * @version $Id$
31  * @see     Constant
32  */
33 public final class ConstantClass extends Constant implements ConstantObject {
34 
35     private int name_index; // Identical to ConstantString except for the name
36 
37 
38     /**
39      * Initialize from another object.
40      */
ConstantClass(final ConstantClass c)41     public ConstantClass(final ConstantClass c) {
42         this(c.getNameIndex());
43     }
44 
45 
46     /**
47      * Initialize instance from file data.
48      *
49      * @param file Input stream
50      * @throws IOException
51      */
ConstantClass(final DataInput file)52     ConstantClass(final DataInput file) throws IOException {
53         this(file.readUnsignedShort());
54     }
55 
56 
57     /**
58      * @param name_index Name index in constant pool.  Should refer to a
59      * ConstantUtf8.
60      */
ConstantClass(final int name_index)61     public ConstantClass(final int name_index) {
62         super(Const.CONSTANT_Class);
63         this.name_index = name_index;
64     }
65 
66 
67     /**
68      * Called by objects that are traversing the nodes of the tree implicitely
69      * defined by the contents of a Java class. I.e., the hierarchy of methods,
70      * fields, attributes, etc. spawns a tree of objects.
71      *
72      * @param v Visitor object
73      */
74     @Override
accept( final Visitor v )75     public void accept( final Visitor v ) {
76         v.visitConstantClass(this);
77     }
78 
79 
80     /**
81      * Dump constant class to file stream in binary format.
82      *
83      * @param file Output file stream
84      * @throws IOException
85      */
86     @Override
dump( final DataOutputStream file )87     public final void dump( final DataOutputStream file ) throws IOException {
88         file.writeByte(super.getTag());
89         file.writeShort(name_index);
90     }
91 
92 
93     /**
94      * @return Name index in constant pool of class name.
95      */
getNameIndex()96     public final int getNameIndex() {
97         return name_index;
98     }
99 
100 
101     /**
102      * @param name_index the name index in the constant pool of this Constant Class
103      */
setNameIndex( final int name_index )104     public final void setNameIndex( final int name_index ) {
105         this.name_index = name_index;
106     }
107 
108 
109     /** @return String object
110      */
111     @Override
getConstantValue( final ConstantPool cp )112     public Object getConstantValue( final ConstantPool cp ) {
113         final Constant c = cp.getConstant(name_index, Const.CONSTANT_Utf8);
114         return ((ConstantUtf8) c).getBytes();
115     }
116 
117 
118     /** @return dereferenced string
119      */
getBytes( final ConstantPool cp )120     public String getBytes( final ConstantPool cp ) {
121         return (String) getConstantValue(cp);
122     }
123 
124 
125     /**
126      * @return String representation.
127      */
128     @Override
toString()129     public final String toString() {
130         return super.toString() + "(name_index = " + name_index + ")";
131     }
132 }
133