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