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