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 */ 019 020package org.apache.bcel.classfile; 021 022import java.io.DataInput; 023import java.io.DataOutputStream; 024import java.io.IOException; 025 026import org.apache.bcel.Const; 027import org.apache.bcel.util.Args; 028 029/** 030 * This class is derived from <em>Attribute</em> and records the nest host of the nest to which the current class or 031 * interface claims to belong. There may be at most one NestHost attribute in a ClassFile structure. 032 * 033 * @see Attribute 034 */ 035public final class NestHost extends Attribute { 036 037 private int hostClassIndex; 038 039 /** 040 * Constructs object from input stream. 041 * 042 * @param nameIndex Index in constant pool. 043 * @param length Content length in bytes. 044 * @param input Input stream. 045 * @param constantPool Array of constants. 046 * @throws IOException Thrown if an I/O error occurs. 047 */ 048 NestHost(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException { 049 this(nameIndex, length, 0, constantPool); 050 hostClassIndex = input.readUnsignedShort(); 051 } 052 053 /** 054 * Constructs a new NestHost. 055 * 056 * @param nameIndex Index in constant pool. 057 * @param length Content length in bytes. 058 * @param hostClassIndex Host class index. 059 * @param constantPool Array of constants. 060 */ 061 public NestHost(final int nameIndex, final int length, final int hostClassIndex, final ConstantPool constantPool) { 062 super(Const.ATTR_NEST_HOST, nameIndex, length, constantPool); 063 this.hostClassIndex = Args.requireU2(hostClassIndex, "hostClassIndex"); 064 } 065 066 /** 067 * Initializes from another object. Note that both objects use the same references (shallow copy). Use copy() for a 068 * physical copy. 069 * 070 * @param c Source to copy. 071 */ 072 public NestHost(final NestHost c) { 073 this(c.getNameIndex(), c.getLength(), c.getHostClassIndex(), c.getConstantPool()); 074 } 075 076 /** 077 * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. 078 * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects. 079 * 080 * @param v Visitor object. 081 */ 082 @Override 083 public void accept(final Visitor v) { 084 v.visitNestHost(this); 085 } 086 087 /** 088 * @return deep copy of this attribute. 089 */ 090 @Override 091 public Attribute copy(final ConstantPool constantPool) { 092 final NestHost c = (NestHost) clone(); 093 c.setConstantPool(constantPool); 094 return c; 095 } 096 097 /** 098 * Dumps NestHost attribute to file stream in binary format. 099 * 100 * @param file Output file stream. 101 * @throws IOException Thrown if an I/O error occurs. 102 */ 103 @Override 104 public void dump(final DataOutputStream file) throws IOException { 105 super.dump(file); 106 file.writeShort(hostClassIndex); 107 } 108 109 /** 110 * Gets the host class index. 111 * 112 * @return index into constant pool of host class name. 113 */ 114 public int getHostClassIndex() { 115 return hostClassIndex; 116 } 117 118 /** 119 * Sets the host class index. 120 * 121 * @param hostClassIndex The host class index. 122 */ 123 public void setHostClassIndex(final int hostClassIndex) { 124 this.hostClassIndex = hostClassIndex; 125 } 126 127 /** 128 * @return String representation. 129 */ 130 @Override 131 public String toString() { 132 final StringBuilder buf = new StringBuilder(); 133 buf.append("NestHost: "); 134 final String className = super.getConstantPool().getConstantString(hostClassIndex, Const.CONSTANT_Class); 135 buf.append(Utility.compactClassName(className, false)); 136 return buf.toString(); 137 } 138}