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