KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > bcel > internal > classfile > FieldOrMethod


1 package com.sun.org.apache.bcel.internal.classfile;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache BCEL" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache BCEL", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56 import com.sun.org.apache.bcel.internal.Constants;
57 import java.io.*;
58
59 /**
60  * Abstract super class for fields and methods.
61  *
62  * @version $Id: FieldOrMethod.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  */

65 public abstract class FieldOrMethod extends AccessFlags implements Cloneable JavaDoc, Node {
66   protected int name_index; // Points to field name in constant pool
67
protected int signature_index; // Points to encoded signature
68
protected int attributes_count;// No. of attributes
69
protected Attribute[] attributes; // Collection of attributes
70
protected ConstantPool constant_pool;
71
72   FieldOrMethod() {}
73   
74   /**
75    * Initialize from another object. Note that both objects use the same
76    * references (shallow copy). Use clone() for a physical copy.
77    */

78   protected FieldOrMethod(FieldOrMethod c) {
79     this(c.getAccessFlags(), c.getNameIndex(), c.getSignatureIndex(),
80      c.getAttributes(), c.getConstantPool());
81   }
82
83   /**
84    * Construct object from file stream.
85    * @param file Input stream
86    * @throw IOException
87    * @throw ClassFormatError
88    */

89   protected FieldOrMethod(DataInputStream file, ConstantPool constant_pool)
90     throws IOException, ClassFormatError JavaDoc
91   {
92     this(file.readUnsignedShort(), file.readUnsignedShort(),
93      file.readUnsignedShort(), null, constant_pool);
94
95     attributes_count = file.readUnsignedShort();
96     attributes = new Attribute[attributes_count];
97     for(int i=0; i < attributes_count; i++)
98       attributes[i] = Attribute.readAttribute(file, constant_pool);
99   }
100
101   /**
102    * @param access_flags Access rights of method
103    * @param name_index Points to field name in constant pool
104    * @param signature_index Points to encoded signature
105    * @param attributes Collection of attributes
106    * @param constant_pool Array of constants
107    */

108   protected FieldOrMethod(int access_flags, int name_index, int signature_index,
109               Attribute[] attributes, ConstantPool constant_pool)
110   {
111     this.access_flags = access_flags;
112     this.name_index = name_index;
113     this.signature_index = signature_index;
114     this.constant_pool = constant_pool;
115
116     setAttributes(attributes);
117   }
118
119   /**
120    * Dump object to file stream on binary format.
121    *
122    * @param file Output file stream
123    * @throw IOException
124    */

125   public final void dump(DataOutputStream file) throws IOException
126   {
127     file.writeShort(access_flags);
128     file.writeShort(name_index);
129     file.writeShort(signature_index);
130     file.writeShort(attributes_count);
131
132     for(int i=0; i < attributes_count; i++)
133       attributes[i].dump(file);
134   }
135
136   /**
137    * @return Collection of object attributes.
138    */

139   public final Attribute[] getAttributes() { return attributes; }
140
141   /**
142    * @param attributes Collection of object attributes.
143    */

144   public final void setAttributes(Attribute[] attributes) {
145     this.attributes = attributes;
146     attributes_count = (attributes == null)? 0 : attributes.length;
147   }
148
149   /**
150    * @return Constant pool used by this object.
151    */

152   public final ConstantPool getConstantPool() { return constant_pool; }
153
154   /**
155    * @param constant_pool Constant pool to be used for this object.
156    */

157   public final void setConstantPool(ConstantPool constant_pool) {
158     this.constant_pool = constant_pool;
159   }
160
161   /**
162    * @return Index in constant pool of object's name.
163    */

164   public final int getNameIndex() { return name_index; }
165
166   /**
167    * @param name_index Index in constant pool of object's name.
168    */

169   public final void setNameIndex(int name_index) {
170     this.name_index = name_index;
171   }
172
173   /**
174    * @return Index in constant pool of field signature.
175    */

176   public final int getSignatureIndex() { return signature_index; }
177
178   /**
179    * @param signature_index Index in constant pool of field signature.
180    */

181   public final void setSignatureIndex(int signature_index) {
182     this.signature_index = signature_index;
183   }
184
185   /**
186    * @return Name of object, i.e., method name or field name
187    */

188   public final String JavaDoc getName() {
189     ConstantUtf8 c;
190     c = (ConstantUtf8)constant_pool.getConstant(name_index,
191                         Constants.CONSTANT_Utf8);
192     return c.getBytes();
193   }
194
195   /**
196    * @return String representation of object's type signature (java style)
197    */

198   public final String JavaDoc getSignature() {
199     ConstantUtf8 c;
200     c = (ConstantUtf8)constant_pool.getConstant(signature_index,
201                         Constants.CONSTANT_Utf8);
202     return c.getBytes();
203   }
204
205   /**
206    * @return deep copy of this field
207    */

208   protected FieldOrMethod copy_(ConstantPool constant_pool) {
209     FieldOrMethod c = null;
210
211     try {
212       c = (FieldOrMethod)clone();
213     } catch(CloneNotSupportedException JavaDoc e) {}
214
215     c.constant_pool = constant_pool;
216     c.attributes = new Attribute[attributes_count];
217
218     for(int i=0; i < attributes_count; i++)
219       c.attributes[i] = attributes[i].copy(constant_pool);
220
221     return c;
222   }
223 }
224
Popular Tags