001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 */
018 package org.apache.bcel.classfile;
019
020 import java.io.DataInput;
021 import java.io.DataOutputStream;
022 import java.io.IOException;
023
024 import org.apache.bcel.Constants;
025
026 /**
027 * This class is derived from the abstract
028 * <A HREF="org.apache.bcel.classfile.Constant.html">Constant</A> class
029 * and represents a reference to a (external) class.
030 *
031 * @version $Id: ConstantClass.java 1152072 2011-07-29 01:54:05Z dbrosius $
032 * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
033 * @see Constant
034 */
035 public final class ConstantClass extends Constant implements ConstantObject {
036
037 private static final long serialVersionUID = -1083450233715258720L;
038 private int name_index; // Identical to ConstantString except for the name
039
040
041 /**
042 * Initialize from another object.
043 */
044 public ConstantClass(ConstantClass c) {
045 this(c.getNameIndex());
046 }
047
048
049 /**
050 * Initialize instance from file data.
051 *
052 * @param file Input stream
053 * @throws IOException
054 */
055 ConstantClass(DataInput file) throws IOException {
056 this(file.readUnsignedShort());
057 }
058
059
060 /**
061 * @param name_index Name index in constant pool. Should refer to a
062 * ConstantUtf8.
063 */
064 public ConstantClass(int name_index) {
065 super(Constants.CONSTANT_Class);
066 this.name_index = name_index;
067 }
068
069
070 /**
071 * Called by objects that are traversing the nodes of the tree implicitely
072 * defined by the contents of a Java class. I.e., the hierarchy of methods,
073 * fields, attributes, etc. spawns a tree of objects.
074 *
075 * @param v Visitor object
076 */
077 @Override
078 public void accept( Visitor v ) {
079 v.visitConstantClass(this);
080 }
081
082
083 /**
084 * Dump constant class to file stream in binary format.
085 *
086 * @param file Output file stream
087 * @throws IOException
088 */
089 @Override
090 public final void dump( DataOutputStream file ) throws IOException {
091 file.writeByte(tag);
092 file.writeShort(name_index);
093 }
094
095
096 /**
097 * @return Name index in constant pool of class name.
098 */
099 public final int getNameIndex() {
100 return name_index;
101 }
102
103
104 /**
105 * @param name_index the name index in the constant pool of this Constant Class
106 */
107 public final void setNameIndex( int name_index ) {
108 this.name_index = name_index;
109 }
110
111
112 /** @return String object
113 */
114 public Object getConstantValue( ConstantPool cp ) {
115 Constant c = cp.getConstant(name_index, Constants.CONSTANT_Utf8);
116 return ((ConstantUtf8) c).getBytes();
117 }
118
119
120 /** @return dereferenced string
121 */
122 public String getBytes( ConstantPool cp ) {
123 return (String) getConstantValue(cp);
124 }
125
126
127 /**
128 * @return String representation.
129 */
130 @Override
131 public final String toString() {
132 return super.toString() + "(name_index = " + name_index + ")";
133 }
134 }