001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * https://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.bcel.classfile; 020 021import java.io.DataInput; 022import java.io.DataOutputStream; 023import java.io.IOException; 024 025import org.apache.bcel.Const; 026 027/** 028 * This class is derived from the abstract {@link Constant} 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 nameIndex; // Identical to ConstantString except for the name 035 036 /** 037 * Initialize from another object. 038 * 039 * @param c Source to copy. 040 */ 041 public ConstantClass(final ConstantClass c) { 042 this(c.getNameIndex()); 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 * Constructs a ConstantClass. 057 * 058 * @param nameIndex Name index in constant pool. Should refer to a ConstantUtf8. 059 */ 060 public ConstantClass(final int nameIndex) { 061 super(Const.CONSTANT_Class); 062 this.nameIndex = nameIndex; 063 } 064 065 /** 066 * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. 067 * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects. 068 * 069 * @param v Visitor object. 070 */ 071 @Override 072 public void accept(final Visitor v) { 073 v.visitConstantClass(this); 074 } 075 076 /** 077 * Dumps constant class to file stream in binary format. 078 * 079 * @param file Output file stream. 080 * @throws IOException if an I/O error occurs writing to the DataOutputStream. 081 */ 082 @Override 083 public void dump(final DataOutputStream file) throws IOException { 084 file.writeByte(super.getTag()); 085 file.writeShort(nameIndex); 086 } 087 088 /** 089 * Gets the dereferenced string. 090 * 091 * @param cp the constant pool. 092 * @return dereferenced string. 093 */ 094 public String getBytes(final ConstantPool cp) { 095 return (String) getConstantValue(cp); 096 } 097 098 /** 099 * Gets the String object. 100 * 101 * @param cp the constant pool. 102 * @return String object. 103 */ 104 @Override 105 public Object getConstantValue(final ConstantPool cp) { 106 return cp.getConstantUtf8(nameIndex).getBytes(); 107 } 108 109 /** 110 * Gets the name index in constant pool of class name. 111 * 112 * @return Name index in constant pool of class name. 113 */ 114 public int getNameIndex() { 115 return nameIndex; 116 } 117 118 /** 119 * Sets the name index in the constant pool. 120 * 121 * @param nameIndex the name index in the constant pool of this Constant Class. 122 */ 123 public void setNameIndex(final int nameIndex) { 124 this.nameIndex = nameIndex; 125 } 126 127 /** 128 * @return String representation. 129 */ 130 @Override 131 public String toString() { 132 return super.toString() + "(nameIndex = " + nameIndex + ")"; 133 } 134}