NestHost.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */

  17. package org.apache.bcel.classfile;

  18. import java.io.DataInput;
  19. import java.io.DataOutputStream;
  20. import java.io.IOException;

  21. import org.apache.bcel.Const;
  22. import org.apache.bcel.util.Args;

  23. /**
  24.  * This class is derived from <em>Attribute</em> and records the nest host of the nest to which the current class or
  25.  * interface claims to belong. There may be at most one NestHost attribute in a ClassFile structure.
  26.  *
  27.  * @see Attribute
  28.  */
  29. public final class NestHost extends Attribute {

  30.     private int hostClassIndex;

  31.     /**
  32.      * Constructs object from input stream.
  33.      *
  34.      * @param nameIndex Index in constant pool
  35.      * @param length Content length in bytes
  36.      * @param input Input stream
  37.      * @param constantPool Array of constants
  38.      * @throws IOException if an I/O error occurs.
  39.      */
  40.     NestHost(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
  41.         this(nameIndex, length, 0, constantPool);
  42.         hostClassIndex = input.readUnsignedShort();
  43.     }

  44.     /**
  45.      * @param nameIndex Index in constant pool
  46.      * @param length Content length in bytes
  47.      * @param hostClassIndex Host class index
  48.      * @param constantPool Array of constants
  49.      */
  50.     public NestHost(final int nameIndex, final int length, final int hostClassIndex, final ConstantPool constantPool) {
  51.         super(Const.ATTR_NEST_MEMBERS, nameIndex, length, constantPool);
  52.         this.hostClassIndex = Args.requireU2(hostClassIndex, "hostClassIndex");
  53.     }

  54.     /**
  55.      * Initializes from another object. Note that both objects use the same references (shallow copy). Use copy() for a
  56.      * physical copy.
  57.      *
  58.      * @param c Source to copy.
  59.      */
  60.     public NestHost(final NestHost c) {
  61.         this(c.getNameIndex(), c.getLength(), c.getHostClassIndex(), c.getConstantPool());
  62.     }

  63.     /**
  64.      * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
  65.      * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
  66.      *
  67.      * @param v Visitor object
  68.      */
  69.     @Override
  70.     public void accept(final Visitor v) {
  71.         v.visitNestHost(this);
  72.     }

  73.     /**
  74.      * @return deep copy of this attribute
  75.      */
  76.     @Override
  77.     public Attribute copy(final ConstantPool constantPool) {
  78.         final NestHost c = (NestHost) clone();
  79.         c.setConstantPool(constantPool);
  80.         return c;
  81.     }

  82.     /**
  83.      * Dumps NestHost attribute to file stream in binary format.
  84.      *
  85.      * @param file Output file stream
  86.      * @throws IOException if an I/O error occurs.
  87.      */
  88.     @Override
  89.     public void dump(final DataOutputStream file) throws IOException {
  90.         super.dump(file);
  91.         file.writeShort(hostClassIndex);
  92.     }

  93.     /**
  94.      * @return index into constant pool of host class name.
  95.      */
  96.     public int getHostClassIndex() {
  97.         return hostClassIndex;
  98.     }

  99.     /**
  100.      * @param hostClassIndex the host class index
  101.      */
  102.     public void setHostClassIndex(final int hostClassIndex) {
  103.         this.hostClassIndex = hostClassIndex;
  104.     }

  105.     /**
  106.      * @return String representation
  107.      */
  108.     @Override
  109.     public String toString() {
  110.         final StringBuilder buf = new StringBuilder();
  111.         buf.append("NestHost: ");
  112.         final String className = super.getConstantPool().getConstantString(hostClassIndex, Const.CONSTANT_Class);
  113.         buf.append(Utility.compactClassName(className, false));
  114.         return buf.toString();
  115.     }
  116. }