KickJava   Java API By Example, From Geeks To Geeks.

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


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
57 import com.sun.org.apache.bcel.internal.Constants;
58 import java.io.*;
59
60 /**
61  * Abstract super class for <em>Attribute</em> objects. Currently the
62  * <em>ConstantValue</em>, <em>SourceFile</em>, <em>Code</em>,
63  * <em>Exceptiontable</em>, <em>LineNumberTable</em>,
64  * <em>LocalVariableTable</em>, <em>InnerClasses</em> and
65  * <em>Synthetic</em> attributes are supported. The
66  * <em>Unknown</em> attribute stands for non-standard-attributes.
67  *
68  * @version $Id: Attribute.java,v 1.1.1.1 2001/10/29 19:59:57 jvanzyl Exp $
69  * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
70  * @see ConstantValue
71  * @see SourceFile
72  * @see Code
73  * @see Unknown
74  * @see ExceptionTable
75  * @see LineNumberTable
76  * @see LocalVariableTable
77  * @see InnerClasses
78  * @see Synthetic
79  * @see Deprecated
80 */

81 public abstract class Attribute implements Cloneable JavaDoc, Node {
82   protected int name_index; // Points to attribute name in constant pool
83
protected int length; // Content length of attribute field
84
protected byte tag; // Tag to distiguish subclasses
85
protected ConstantPool constant_pool;
86
87   Attribute(byte tag, int name_index, int length, ConstantPool constant_pool) {
88     this.tag = tag;
89     this.name_index = name_index;
90     this.length = length;
91     this.constant_pool = constant_pool;
92   }
93
94   /**
95    * Called by objects that are traversing the nodes of the tree implicitely
96    * defined by the contents of a Java class. I.e., the hierarchy of methods,
97    * fields, attributes, etc. spawns a tree of objects.
98    *
99    * @param v Visitor object
100    */

101   public abstract void accept(Visitor v);
102
103   /**
104    * Dump attribute to file stream in binary format.
105    *
106    * @param file Output file stream
107    * @throw IOException
108    */

109   public void dump(DataOutputStream file) throws IOException
110   {
111     file.writeShort(name_index);
112     file.writeInt(length);
113   }
114
115   /* Class method reads one attribute from the input data stream.
116    * This method must not be accessible from the outside. It is
117    * called by the Field and Method constructor methods.
118    *
119    * @see Field
120    * @see Method
121    * @param file Input stream
122    * @param constant_pool Array of constants
123    * @return Attribute
124    * @throw IOException
125    * @throw ClassFormatError
126    * @throw InternalError
127    */

128   static final Attribute readAttribute(DataInputStream file,
129                        ConstantPool constant_pool)
130     throws IOException, ClassFormatError JavaDoc, InternalError JavaDoc
131   {
132     ConstantUtf8 c;
133     String JavaDoc name;
134     int name_index;
135     int length;
136     byte tag = Constants.ATTR_UNKNOWN; // Unknown attribute
137

138     // Get class name from constant pool via `name_index' indirection
139
name_index = (int)(file.readUnsignedShort());
140     c = (ConstantUtf8)constant_pool.getConstant(name_index,
141                              Constants.CONSTANT_Utf8);
142     name = c.getBytes();
143
144     // Length of data in bytes
145
length = file.readInt();
146
147     // Compare strings to find known attribute
148
for(byte i=0; i < Constants.KNOWN_ATTRIBUTES; i++) {
149       if(name.equals(Constants.ATTRIBUTE_NAMES[i])) {
150     tag = i; // found!
151
break;
152       }
153     }
154
155     // Call proper constructor, depending on `tag'
156
switch(tag) {
157     case Constants.ATTR_UNKNOWN:
158       return new Unknown(name_index, length, file, constant_pool);
159
160     case Constants.ATTR_CONSTANT_VALUE:
161       return new ConstantValue(name_index, length, file, constant_pool);
162
163     case Constants.ATTR_SOURCE_FILE:
164       return new SourceFile(name_index, length, file, constant_pool);
165       
166     case Constants.ATTR_CODE:
167       return new Code(name_index, length, file, constant_pool);
168       
169     case Constants.ATTR_EXCEPTIONS:
170       return new ExceptionTable(name_index, length, file, constant_pool);
171       
172     case Constants.ATTR_LINE_NUMBER_TABLE:
173       return new LineNumberTable(name_index, length, file, constant_pool);
174       
175     case Constants.ATTR_LOCAL_VARIABLE_TABLE:
176       return new LocalVariableTable(name_index, length, file, constant_pool);
177
178     case Constants.ATTR_INNER_CLASSES:
179       return new InnerClasses(name_index, length, file, constant_pool);
180
181     case Constants.ATTR_SYNTHETIC:
182       return new Synthetic(name_index, length, file, constant_pool);
183
184     case Constants.ATTR_DEPRECATED:
185       return new Deprecated JavaDoc(name_index, length, file, constant_pool);
186
187     case Constants.ATTR_PMG:
188       return new PMGClass(name_index, length, file, constant_pool);
189
190     case Constants.ATTR_SIGNATURE:
191       return new Signature(name_index, length, file, constant_pool);
192
193     case Constants.ATTR_STACK_MAP:
194       return new StackMap(name_index, length, file, constant_pool);
195
196     default: // Never reached
197
throw new InternalError JavaDoc("Ooops! default case reached.");
198     }
199   }
200
201   /**
202    * @return Length of attribute field in bytes.
203    */

204   public final int getLength() { return length; }
205
206   /**
207    * @param Attribute length in bytes.
208    */

209   public final void setLength(int length) {
210     this.length = length;
211   }
212
213   /**
214    * @param name_index of attribute.
215    */

216   public final void setNameIndex(int name_index) {
217     this.name_index = name_index;
218   }
219
220   /**
221    * @return Name index in constant pool of attribute name.
222    */

223   public final int getNameIndex() { return name_index; }
224
225   /**
226    * @return Tag of attribute, i.e., its type. Value may not be altered, thus
227    * there is no setTag() method.
228    */

229   public final byte getTag() { return tag; }
230
231   /**
232    * @return Constant pool used by this object.
233    * @see ConstantPool
234    */

235   public final ConstantPool getConstantPool() { return constant_pool; }
236
237   /**
238    * @param constant_pool Constant pool to be used for this object.
239    * @see ConstantPool
240    */

241   public final void setConstantPool(ConstantPool constant_pool) {
242     this.constant_pool = constant_pool;
243   }
244   
245   /**
246    * Use copy() if you want to have a deep copy(), i.e., with all references
247    * copied correctly.
248    *
249    * @return shallow copy of this attribute
250    */

251   public Object JavaDoc clone() {
252     Object JavaDoc o = null;
253
254     try {
255       o = super.clone();
256     } catch(CloneNotSupportedException JavaDoc e) {
257       e.printStackTrace(); // Never occurs
258
}
259
260     return o;
261   }
262
263   /**
264    * @return deep copy of this attribute
265    */

266   public abstract Attribute copy(ConstantPool constant_pool);
267
268   /**
269    * @return attribute name.
270    */

271   public String JavaDoc toString() {
272     return Constants.ATTRIBUTE_NAMES[tag];
273   }
274 }
275
Popular Tags