1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  *  Unless required by applicable law or agreed to in writing, software
  16  *  distributed under the License is distributed on an "AS IS" BASIS,
  17  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  *  See the License for the specific language governing permissions and
  19  *  limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.bcel.internal.classfile;
  23 
  24 import java.io.DataInput;
  25 import java.io.DataOutputStream;
  26 import java.io.IOException;
  27 
  28 import com.sun.org.apache.bcel.internal.Const;
  29 
  30 /**
  31  * This class is derived from <em>Attribute</em> and records the nest host of the nest
  32  * to which the current class or interface claims to belong.
  33  * There may be at most one NestHost attribute in a ClassFile structure.
  34  *
  35  * @see     Attribute
  36  */
  37 public final class NestHost extends Attribute {
  38 
  39     private int host_class_index;
  40 
  41 
  42     /**
  43      * Initializes from another object. Note that both objects use the same
  44      * references (shallow copy). Use copy() for a physical copy.
  45      */
  46     public NestHost(final NestHost c) {
  47         this(c.getNameIndex(), c.getLength(), c.getHostClassIndex(), c.getConstantPool());
  48     }
  49 
  50 
  51     /**
  52      * @param name_index Index in constant pool
  53      * @param length Content length in bytes
  54      * @param host_class_index Host class index
  55      * @param constant_pool Array of constants
  56      */
  57     public NestHost(final int name_index, final int length, final int host_class_index,
  58             final ConstantPool constant_pool) {
  59         super(Const.ATTR_NEST_MEMBERS, name_index, length, constant_pool);
  60         this.host_class_index = host_class_index;
  61     }
  62 
  63 
  64     /**
  65      * Constructs object from input stream.
  66      * @param name_index Index in constant pool
  67      * @param length Content length in bytes
  68      * @param input Input stream
  69      * @param constant_pool Array of constants
  70      * @throws IOException
  71      */
  72     NestHost(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool) throws IOException {
  73         this(name_index, length, 0, constant_pool);
  74         host_class_index = input.readUnsignedShort();
  75     }
  76 
  77 
  78     /**
  79      * Called by objects that are traversing the nodes of the tree implicitely
  80      * defined by the contents of a Java class. I.e., the hierarchy of methods,
  81      * fields, attributes, etc. spawns a tree of objects.
  82      *
  83      * @param v Visitor object
  84      */
  85     @Override
  86     public void accept( final Visitor v ) {
  87         v.visitNestHost(this);
  88     }
  89 
  90 
  91     /**
  92      * Dumps NestHost attribute to file stream in binary format.
  93      *
  94      * @param file Output file stream
  95      * @throws IOException if an I/O error occurs.
  96      */
  97     @Override
  98     public void dump( final DataOutputStream file ) throws IOException {
  99         super.dump(file);
 100         file.writeShort(host_class_index);
 101     }
 102 
 103 
 104     /**
 105      * @return index into constant pool of host class name.
 106      */
 107     public int getHostClassIndex() {
 108         return host_class_index;
 109     }
 110 
 111 
 112     /**
 113      * @param host_class_index the host class index
 114      */
 115     public void setHostClassIndex( final int host_class_index ) {
 116         this.host_class_index = host_class_index;
 117     }
 118 
 119 
 120     /**
 121      * @return String representation
 122      */
 123     @Override
 124     public String toString() {
 125         final StringBuilder buf = new StringBuilder();
 126         buf.append("NestHost: ");
 127         final String class_name = super.getConstantPool().getConstantString(host_class_index, Const.CONSTANT_Class);
 128         buf.append(Utility.compactClassName(class_name, false));
 129         return buf.toString();
 130     }
 131 
 132 
 133     /**
 134      * @return deep copy of this attribute
 135      */
 136     @Override
 137     public Attribute copy( final ConstantPool _constant_pool ) {
 138         final NestHost c = (NestHost) clone();
 139         c.setConstantPool(_constant_pool);
 140         return c;
 141     }
 142 }