1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 package com.sun.org.apache.bcel.internal.classfile;
   6 
   7 /* ====================================================================
   8  * The Apache Software License, Version 1.1
   9  *
  10  * Copyright (c) 2001 The Apache Software Foundation.  All rights
  11  * reserved.
  12  *
  13  * Redistribution and use in source and binary forms, with or without
  14  * modification, are permitted provided that the following conditions
  15  * are met:
  16  *
  17  * 1. Redistributions of source code must retain the above copyright
  18  *    notice, this list of conditions and the following disclaimer.
  19  *
  20  * 2. Redistributions in binary form must reproduce the above copyright
  21  *    notice, this list of conditions and the following disclaimer in
  22  *    the documentation and/or other materials provided with the
  23  *    distribution.
  24  *
  25  * 3. The end-user documentation included with the redistribution,
  26  *    if any, must include the following acknowledgment:
  27  *       "This product includes software developed by the
  28  *        Apache Software Foundation (http://www.apache.org/)."
  29  *    Alternately, this acknowledgment may appear in the software itself,
  30  *    if and wherever such third-party acknowledgments normally appear.
  31  *
  32  * 4. The names "Apache" and "Apache Software Foundation" and
  33  *    "Apache BCEL" must not be used to endorse or promote products
  34  *    derived from this software without prior written permission. For
  35  *    written permission, please contact apache@apache.org.
  36  *
  37  * 5. Products derived from this software may not be called "Apache",
  38  *    "Apache BCEL", nor may "Apache" appear in their name, without
  39  *    prior written permission of the Apache Software Foundation.
  40  *
  41  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  44  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  47  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  48  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  49  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  50  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  51  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  52  * SUCH DAMAGE.
  53  * ====================================================================
  54  *
  55  * This software consists of voluntary contributions made by many
  56  * individuals on behalf of the Apache Software Foundation.  For more
  57  * information on the Apache Software Foundation, please see
  58  * <http://www.apache.org/>.
  59  */
  60 
  61 import  com.sun.org.apache.bcel.internal.Constants;
  62 import  java.io.*;
  63 
  64 /**
  65  * This class represents a inner class attribute, i.e., the class
  66  * indices of the inner and outer classes, the name and the attributes
  67  * of the inner class.
  68  *
  69  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  70  * @see InnerClasses
  71  */
  72 public final class InnerClass implements Cloneable, Node {
  73   private int inner_class_index;
  74   private int outer_class_index;
  75   private int inner_name_index;
  76   private int inner_access_flags;
  77 
  78   /**
  79    * Initialize from another object.
  80    */
  81   public InnerClass(InnerClass c) {
  82     this(c.getInnerClassIndex(), c.getOuterClassIndex(), c.getInnerNameIndex(),
  83          c.getInnerAccessFlags());
  84   }
  85 
  86   /**
  87    * Construct object from file stream.
  88    * @param file Input stream
  89    * @throws IOException
  90    */
  91   InnerClass(DataInputStream file) throws IOException
  92   {
  93     this(file.readUnsignedShort(), file.readUnsignedShort(),
  94          file.readUnsignedShort(), file.readUnsignedShort());
  95   }
  96 
  97   /**
  98    * @param inner_class_index Class index in constant pool of inner class
  99    * @param outer_class_index Class index in constant pool of outer class
 100    * @param inner_name_index  Name index in constant pool of inner class
 101    * @param inner_access_flags Access flags of inner class
 102    */
 103   public InnerClass(int inner_class_index, int outer_class_index,
 104                     int inner_name_index, int inner_access_flags)
 105   {
 106     this.inner_class_index  = inner_class_index;
 107     this.outer_class_index  = outer_class_index;
 108     this.inner_name_index   = inner_name_index;
 109     this.inner_access_flags = inner_access_flags;
 110   }
 111 
 112   /**
 113    * Called by objects that are traversing the nodes of the tree implicitely
 114    * defined by the contents of a Java class. I.e., the hierarchy of methods,
 115    * fields, attributes, etc. spawns a tree of objects.
 116    *
 117    * @param v Visitor object
 118    */
 119   public void accept(Visitor v) {
 120     v.visitInnerClass(this);
 121   }
 122   /**
 123    * Dump inner class attribute to file stream in binary format.
 124    *
 125    * @param file Output file stream
 126    * @throws IOException
 127    */
 128   public final void dump(DataOutputStream file) throws IOException
 129   {
 130     file.writeShort(inner_class_index);
 131     file.writeShort(outer_class_index);
 132     file.writeShort(inner_name_index);
 133     file.writeShort(inner_access_flags);
 134   }
 135   /**
 136    * @return access flags of inner class.
 137    */
 138   public final int getInnerAccessFlags() { return inner_access_flags; }
 139   /**
 140    * @return class index of inner class.
 141    */
 142   public final int getInnerClassIndex() { return inner_class_index; }
 143   /**
 144    * @return name index of inner class.
 145    */
 146   public final int getInnerNameIndex() { return inner_name_index; }
 147   /**
 148    * @return class index of outer class.
 149    */
 150   public final int getOuterClassIndex() { return outer_class_index; }
 151   /**
 152    * @param inner_access_flags.
 153    */
 154   public final void setInnerAccessFlags(int inner_access_flags) {
 155     this.inner_access_flags = inner_access_flags;
 156   }
 157   /**
 158    * @param inner_class_index.
 159    */
 160   public final void setInnerClassIndex(int inner_class_index) {
 161     this.inner_class_index = inner_class_index;
 162   }
 163   /**
 164    * @param inner_name_index.
 165    */
 166   public final void setInnerNameIndex(int inner_name_index) {
 167     this.inner_name_index = inner_name_index;
 168   }
 169   /**
 170    * @param outer_class_index.
 171    */
 172   public final void setOuterClassIndex(int outer_class_index) {
 173     this.outer_class_index = outer_class_index;
 174   }
 175   /**
 176    * @return String representation.
 177    */
 178   public final String toString() {
 179     return "InnerClass(" + inner_class_index + ", " + outer_class_index +
 180       ", " + inner_name_index + ", " + inner_access_flags + ")";
 181   }
 182 
 183   /**
 184    * @return Resolved string representation
 185    */
 186   public final String toString(ConstantPool constant_pool) {
 187     String inner_class_name, outer_class_name, inner_name, access;
 188 
 189     inner_class_name = constant_pool.getConstantString(inner_class_index,
 190                                                        Constants.CONSTANT_Class);
 191     inner_class_name = Utility.compactClassName(inner_class_name);
 192 
 193     if (outer_class_index != 0) {
 194       outer_class_name = constant_pool.getConstantString(outer_class_index,
 195                                                          Constants.CONSTANT_Class);
 196       outer_class_name = Utility.compactClassName(outer_class_name);
 197     }
 198     else
 199       outer_class_name = "<not a member>";
 200 
 201     if(inner_name_index != 0)
 202       inner_name = ((ConstantUtf8)constant_pool.
 203                     getConstant(inner_name_index, Constants.CONSTANT_Utf8)).getBytes();
 204     else
 205       inner_name = "<anonymous>";
 206 
 207     access = Utility.accessToString(inner_access_flags, true);
 208     access = access.equals("")? "" : (access + " ");
 209 
 210     return "InnerClass:" + access + inner_class_name +
 211       "(\"" + outer_class_name + "\", \"" + inner_name + "\")";
 212   }
 213 
 214   /**
 215    * @return deep copy of this object
 216    */
 217   public InnerClass copy() {
 218     try {
 219       return (InnerClass)clone();
 220     } catch(CloneNotSupportedException e) {}
 221 
 222     return null;
 223   }
 224 }