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