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 * @version $Id: ConstantClass.java 1749603 2016-06-21 20:50:19Z ggregory $ 031 * @see Constant 032 */ 033public final class ConstantClass extends Constant implements ConstantObject { 034 035 private int name_index; // Identical to ConstantString except for the name 036 037 038 /** 039 * Initialize from another object. 040 */ 041 public ConstantClass(final ConstantClass c) { 042 this(c.getNameIndex()); 043 } 044 045 046 /** 047 * Initialize instance from file data. 048 * 049 * @param file Input stream 050 * @throws IOException 051 */ 052 ConstantClass(final DataInput file) throws IOException { 053 this(file.readUnsignedShort()); 054 } 055 056 057 /** 058 * @param name_index Name index in constant pool. Should refer to a 059 * ConstantUtf8. 060 */ 061 public ConstantClass(final int name_index) { 062 super(Const.CONSTANT_Class); 063 this.name_index = name_index; 064 } 065 066 067 /** 068 * Called by objects that are traversing the nodes of the tree implicitely 069 * defined by the contents of a Java class. I.e., the hierarchy of methods, 070 * fields, attributes, etc. spawns a tree of objects. 071 * 072 * @param v Visitor object 073 */ 074 @Override 075 public void accept( final Visitor v ) { 076 v.visitConstantClass(this); 077 } 078 079 080 /** 081 * Dump constant class to file stream in binary format. 082 * 083 * @param file Output file stream 084 * @throws IOException 085 */ 086 @Override 087 public final void dump( final DataOutputStream file ) throws IOException { 088 file.writeByte(super.getTag()); 089 file.writeShort(name_index); 090 } 091 092 093 /** 094 * @return Name index in constant pool of class name. 095 */ 096 public final int getNameIndex() { 097 return name_index; 098 } 099 100 101 /** 102 * @param name_index the name index in the constant pool of this Constant Class 103 */ 104 public final void setNameIndex( final int name_index ) { 105 this.name_index = name_index; 106 } 107 108 109 /** @return String object 110 */ 111 @Override 112 public Object getConstantValue( final ConstantPool cp ) { 113 final Constant c = cp.getConstant(name_index, Const.CONSTANT_Utf8); 114 return ((ConstantUtf8) c).getBytes(); 115 } 116 117 118 /** @return dereferenced string 119 */ 120 public String getBytes( final ConstantPool cp ) { 121 return (String) getConstantValue(cp); 122 } 123 124 125 /** 126 * @return String representation. 127 */ 128 @Override 129 public final String toString() { 130 return super.toString() + "(name_index = " + name_index + ")"; 131 } 132}