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 */ 018package org.apache.bcel.classfile; 019 020import java.io.DataInput; 021import java.io.DataOutputStream; 022import java.io.IOException; 023 024import org.apache.bcel.Const; 025 026/** 027 * This class is derived from the abstract {@link Constant} 028 * and represents a reference to a (external) class. 029 * 030 * @see Constant 031 */ 032public final class ConstantClass extends Constant implements ConstantObject { 033 034 private int name_index; // Identical to ConstantString except for the name 035 036 037 /** 038 * Initialize from another object. 039 */ 040 public ConstantClass(final ConstantClass c) { 041 this(c.getNameIndex()); 042 } 043 044 045 /** 046 * Constructs an instance from file data. 047 * 048 * @param dataInput Input stream 049 * @throws IOException if an I/O error occurs reading from the given {@code dataInput}. 050 */ 051 ConstantClass(final DataInput dataInput) throws IOException { 052 this(dataInput.readUnsignedShort()); 053 } 054 055 056 /** 057 * @param name_index Name index in constant pool. Should refer to a 058 * ConstantUtf8. 059 */ 060 public ConstantClass(final int name_index) { 061 super(Const.CONSTANT_Class); 062 this.name_index = name_index; 063 } 064 065 066 /** 067 * Called by objects that are traversing the nodes of the tree implicitely 068 * defined by the contents of a Java class. I.e., the hierarchy of methods, 069 * fields, attributes, etc. spawns a tree of objects. 070 * 071 * @param v Visitor object 072 */ 073 @Override 074 public void accept( final Visitor v ) { 075 v.visitConstantClass(this); 076 } 077 078 079 /** 080 * Dumps constant class to file stream in binary format. 081 * 082 * @param file Output file stream 083 * @throws IOException if an I/O error occurs writing to the DataOutputStream. 084 */ 085 @Override 086 public void dump( final DataOutputStream file ) throws IOException { 087 file.writeByte(super.getTag()); 088 file.writeShort(name_index); 089 } 090 091 092 /** 093 * @return Name index in constant pool of class name. 094 */ 095 public int getNameIndex() { 096 return name_index; 097 } 098 099 100 /** 101 * @param name_index the name index in the constant pool of this Constant Class 102 */ 103 public void setNameIndex( final int name_index ) { 104 this.name_index = name_index; 105 } 106 107 108 /** @return String object 109 */ 110 @Override 111 public Object getConstantValue( final ConstantPool cp ) { 112 final Constant c = cp.getConstant(name_index, 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() + "(name_index = " + name_index + ")"; 130 } 131}