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. * Abstract super class for fields and methods.
  59. *
  60. * @version $Id: FieldOrMethod.java,v 1.1.1.1 2001/10/29 20:00:01 jvanzyl Exp $
  61. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  62. */
  63. public abstract class FieldOrMethod extends AccessFlags implements Cloneable, Node {
  64. protected int name_index; // Points to field name in constant pool
  65. protected int signature_index; // Points to encoded signature
  66. protected int attributes_count;// No. of attributes
  67. protected Attribute[] attributes; // Collection of attributes
  68. protected ConstantPool constant_pool;
  69. FieldOrMethod() {}
  70. /**
  71. * Initialize from another object. Note that both objects use the same
  72. * references (shallow copy). Use clone() for a physical copy.
  73. */
  74. protected FieldOrMethod(FieldOrMethod c) {
  75. this(c.getAccessFlags(), c.getNameIndex(), c.getSignatureIndex(),
  76. c.getAttributes(), c.getConstantPool());
  77. }
  78. /**
  79. * Construct object from file stream.
  80. * @param file Input stream
  81. * @throw IOException
  82. * @throw ClassFormatError
  83. */
  84. protected FieldOrMethod(DataInputStream file, ConstantPool constant_pool)
  85. throws IOException, ClassFormatError
  86. {
  87. this(file.readUnsignedShort(), file.readUnsignedShort(),
  88. file.readUnsignedShort(), null, constant_pool);
  89. attributes_count = file.readUnsignedShort();
  90. attributes = new Attribute[attributes_count];
  91. for(int i=0; i < attributes_count; i++)
  92. attributes[i] = Attribute.readAttribute(file, constant_pool);
  93. }
  94. /**
  95. * @param access_flags Access rights of method
  96. * @param name_index Points to field name in constant pool
  97. * @param signature_index Points to encoded signature
  98. * @param attributes Collection of attributes
  99. * @param constant_pool Array of constants
  100. */
  101. protected FieldOrMethod(int access_flags, int name_index, int signature_index,
  102. Attribute[] attributes, ConstantPool constant_pool)
  103. {
  104. this.access_flags = access_flags;
  105. this.name_index = name_index;
  106. this.signature_index = signature_index;
  107. this.constant_pool = constant_pool;
  108. setAttributes(attributes);
  109. }
  110. /**
  111. * Dump object to file stream on binary format.
  112. *
  113. * @param file Output file stream
  114. * @throw IOException
  115. */
  116. public final void dump(DataOutputStream file) throws IOException
  117. {
  118. file.writeShort(access_flags);
  119. file.writeShort(name_index);
  120. file.writeShort(signature_index);
  121. file.writeShort(attributes_count);
  122. for(int i=0; i < attributes_count; i++)
  123. attributes[i].dump(file);
  124. }
  125. /**
  126. * @return Collection of object attributes.
  127. */
  128. public final Attribute[] getAttributes() { return attributes; }
  129. /**
  130. * @param attributes Collection of object attributes.
  131. */
  132. public final void setAttributes(Attribute[] attributes) {
  133. this.attributes = attributes;
  134. attributes_count = (attributes == null)? 0 : attributes.length;
  135. }
  136. /**
  137. * @return Constant pool used by this object.
  138. */
  139. public final ConstantPool getConstantPool() { return constant_pool; }
  140. /**
  141. * @param constant_pool Constant pool to be used for this object.
  142. */
  143. public final void setConstantPool(ConstantPool constant_pool) {
  144. this.constant_pool = constant_pool;
  145. }
  146. /**
  147. * @return Index in constant pool of object's name.
  148. */
  149. public final int getNameIndex() { return name_index; }
  150. /**
  151. * @param name_index Index in constant pool of object's name.
  152. */
  153. public final void setNameIndex(int name_index) {
  154. this.name_index = name_index;
  155. }
  156. /**
  157. * @return Index in constant pool of field signature.
  158. */
  159. public final int getSignatureIndex() { return signature_index; }
  160. /**
  161. * @param signature_index Index in constant pool of field signature.
  162. */
  163. public final void setSignatureIndex(int signature_index) {
  164. this.signature_index = signature_index;
  165. }
  166. /**
  167. * @return Name of object, i.e., method name or field name
  168. */
  169. public final String getName() {
  170. ConstantUtf8 c;
  171. c = (ConstantUtf8)constant_pool.getConstant(name_index,
  172. Constants.CONSTANT_Utf8);
  173. return c.getBytes();
  174. }
  175. /**
  176. * @return String representation of object's type signature (java style)
  177. */
  178. public final String getSignature() {
  179. ConstantUtf8 c;
  180. c = (ConstantUtf8)constant_pool.getConstant(signature_index,
  181. Constants.CONSTANT_Utf8);
  182. return c.getBytes();
  183. }
  184. /**
  185. * @return deep copy of this field
  186. */
  187. protected FieldOrMethod copy_(ConstantPool constant_pool) {
  188. FieldOrMethod c = null;
  189. try {
  190. c = (FieldOrMethod)clone();
  191. } catch(CloneNotSupportedException e) {}
  192. c.constant_pool = constant_pool;
  193. c.attributes = new Attribute[attributes_count];
  194. for(int i=0; i < attributes_count; i++)
  195. c.attributes[i] = attributes[i].copy(constant_pool);
  196. return c;
  197. }
  198. }