You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InnerClass.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package org.aspectj.apache.bcel.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 (https://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. * <https://www.apache.org/>.
  54. */
  55. import java.io.DataInputStream;
  56. import java.io.DataOutputStream;
  57. import java.io.IOException;
  58. import org.aspectj.apache.bcel.Constants;
  59. /**
  60. * This class represents a inner class attribute, i.e., the class indices of the inner and outer classes, the name and the
  61. * attributes of the inner class.
  62. *
  63. * @version $Id: InnerClass.java,v 1.4 2009/09/10 15:35:05 aclement Exp $
  64. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  65. * @see InnerClasses
  66. */
  67. public final class InnerClass implements Cloneable, Node {
  68. private int inner_class_index;
  69. private int outer_class_index;
  70. private int inner_name_index;
  71. private int inner_access_flags;
  72. /**
  73. * Initialize from another object.
  74. */
  75. public InnerClass(InnerClass c) {
  76. this(c.getInnerClassIndex(), c.getOuterClassIndex(), c.getInnerNameIndex(), c.getInnerAccessFlags());
  77. }
  78. /**
  79. * Construct object from file stream.
  80. *
  81. * @param file Input stream
  82. * @throws IOException
  83. */
  84. InnerClass(DataInputStream file) throws IOException {
  85. this(file.readUnsignedShort(), file.readUnsignedShort(), file.readUnsignedShort(), file.readUnsignedShort());
  86. }
  87. /**
  88. * @param inner_class_index Class index in constant pool of inner class
  89. * @param outer_class_index Class index in constant pool of outer class
  90. * @param inner_name_index Name index in constant pool of inner class
  91. * @param inner_access_flags Access flags of inner class
  92. */
  93. public InnerClass(int inner_class_index, int outer_class_index, int inner_name_index, int inner_access_flags) {
  94. this.inner_class_index = inner_class_index;
  95. this.outer_class_index = outer_class_index;
  96. this.inner_name_index = inner_name_index;
  97. this.inner_access_flags = inner_access_flags;
  98. }
  99. /**
  100. * Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class. I.e., the
  101. * hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
  102. *
  103. * @param v Visitor object
  104. */
  105. public void accept(ClassVisitor v) {
  106. v.visitInnerClass(this);
  107. }
  108. /**
  109. * Dump inner class attribute to file stream in binary format.
  110. *
  111. * @param file Output file stream
  112. * @throws IOException
  113. */
  114. public final void dump(DataOutputStream file) throws IOException {
  115. file.writeShort(inner_class_index);
  116. file.writeShort(outer_class_index);
  117. file.writeShort(inner_name_index);
  118. file.writeShort(inner_access_flags);
  119. }
  120. /**
  121. * @return access flags of inner class.
  122. */
  123. public final int getInnerAccessFlags() {
  124. return inner_access_flags;
  125. }
  126. /**
  127. * @return class index of inner class.
  128. */
  129. public final int getInnerClassIndex() {
  130. return inner_class_index;
  131. }
  132. /**
  133. * @return name index of inner class.
  134. */
  135. public final int getInnerNameIndex() {
  136. return inner_name_index;
  137. }
  138. /**
  139. * @return class index of outer class.
  140. */
  141. public final int getOuterClassIndex() {
  142. return outer_class_index;
  143. }
  144. /**
  145. * @param inner_access_flags.
  146. */
  147. public final void setInnerAccessFlags(int inner_access_flags) {
  148. this.inner_access_flags = inner_access_flags;
  149. }
  150. /**
  151. * @param inner_class_index.
  152. */
  153. public final void setInnerClassIndex(int inner_class_index) {
  154. this.inner_class_index = inner_class_index;
  155. }
  156. /**
  157. * @param inner_name_index.
  158. */
  159. public final void setInnerNameIndex(int inner_name_index) {
  160. this.inner_name_index = inner_name_index;
  161. }
  162. /**
  163. * @param outer_class_index.
  164. */
  165. public final void setOuterClassIndex(int outer_class_index) {
  166. this.outer_class_index = outer_class_index;
  167. }
  168. /**
  169. * @return String representation.
  170. */
  171. @Override
  172. public final String toString() {
  173. return "InnerClass(" + inner_class_index + ", " + outer_class_index + ", " + inner_name_index + ", " + inner_access_flags
  174. + ")";
  175. }
  176. /**
  177. * @return Resolved string representation
  178. */
  179. public final String toString(ConstantPool constant_pool) {
  180. String inner_class_name, outer_class_name, inner_name, access;
  181. inner_class_name = constant_pool.getConstantString(inner_class_index, Constants.CONSTANT_Class);
  182. inner_class_name = Utility.compactClassName(inner_class_name);
  183. if (outer_class_index != 0) {
  184. outer_class_name = constant_pool.getConstantString(outer_class_index, Constants.CONSTANT_Class);
  185. outer_class_name = Utility.compactClassName(outer_class_name);
  186. } else
  187. outer_class_name = "<not a member>";
  188. if (inner_name_index != 0)
  189. inner_name = ((ConstantUtf8) constant_pool.getConstant(inner_name_index, Constants.CONSTANT_Utf8)).getValue();
  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 + "(\"" + outer_class_name + "\", \"" + inner_name + "\")";
  195. }
  196. /**
  197. * @return deep copy of this object
  198. */
  199. public InnerClass copy() {
  200. try {
  201. return (InnerClass) clone();
  202. } catch (CloneNotSupportedException e) {
  203. }
  204. return null;
  205. }
  206. }