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.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   * @see     Constant
31   */
32  public final class ConstantClass extends Constant implements ConstantObject {
33  
34      private int nameIndex; // Identical to ConstantString except for the name
35  
36  
37      /**
38       * Initialize from another object.
39       */
40      public ConstantClassnstantClass.html#ConstantClass">ConstantClass(final ConstantClass c) {
41          this(c.getNameIndex());
42      }
43  
44  
45      /**
46       * Constructs an instance from file data.
47       *
48       * @param dataInput Input stream
49       * @throws IOException if an I/O error occurs reading from the given {@code dataInput}.
50       */
51      ConstantClass(final DataInput dataInput) throws IOException {
52          this(dataInput.readUnsignedShort());
53      }
54  
55  
56      /**
57       * @param nameIndex Name index in constant pool.  Should refer to a
58       * ConstantUtf8.
59       */
60      public ConstantClass(final int nameIndex) {
61          super(Const.CONSTANT_Class);
62          this.nameIndex = nameIndex;
63      }
64  
65  
66      /**
67       * Called by objects that are traversing the nodes of the tree implicitely
68       * defined by the contents of a Java class. I.e., the hierarchy of methods,
69       * fields, attributes, etc. spawns a tree of objects.
70       *
71       * @param v Visitor object
72       */
73      @Override
74      public void accept( final Visitor v ) {
75          v.visitConstantClass(this);
76      }
77  
78  
79      /**
80       * Dumps constant class to file stream in binary format.
81       *
82       * @param file Output file stream
83       * @throws IOException if an I/O error occurs writing to the DataOutputStream.
84       */
85      @Override
86      public void dump( final DataOutputStream file ) throws IOException {
87          file.writeByte(super.getTag());
88          file.writeShort(nameIndex);
89      }
90  
91  
92      /**
93       * @return Name index in constant pool of class name.
94       */
95      public int getNameIndex() {
96          return nameIndex;
97      }
98  
99  
100     /**
101      * @param nameIndex the name index in the constant pool of this Constant Class
102      */
103     public void setNameIndex( final int nameIndex ) {
104         this.nameIndex = nameIndex;
105     }
106 
107 
108     /** @return String object
109      */
110     @Override
111     public Object getConstantValue( final ConstantPool cp ) {
112         final Constant c = cp.getConstant(nameIndex, Const.CONSTANT_Utf8);
113         return ((ConstantUtf8) c).getBytes();
114     }
115 
116 
117     /** @return dereferenced string
118      */
119     public String getBytes( final ConstantPool cp ) {
120         return (String) getConstantValue(cp);
121     }
122 
123 
124     /**
125      * @return String representation.
126      */
127     @Override
128     public String toString() {
129         return super.toString() + "(nameIndex = " + nameIndex + ")";
130     }
131 }