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