KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > Attribute


1 /***
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000-2005 INRIA, France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package org.objectweb.asm;
31
32 /**
33  * A non standard class, field, method or code attribute.
34  *
35  * @author Eric Bruneton
36  * @author Eugene Kuleshov
37  */

38 public class Attribute {
39
40     /**
41      * The type of this attribute.
42      */

43     public final String JavaDoc type;
44
45     /**
46      * The next attribute in this attribute list. May be <tt>null</tt>.
47      */

48     Attribute next;
49
50     /**
51      * Constructs a new empty attribute.
52      *
53      * @param type the type of the attribute.
54      */

55     protected Attribute(final String JavaDoc type) {
56         this.type = type;
57     }
58
59     /**
60      * Returns <tt>true</tt> if this type of attribute is unknown. The default
61      * implementation of this method always returns <tt>true</tt>.
62      *
63      * @return <tt>true</tt> if this type of attribute is unknown.
64      */

65     public boolean isUnknown() {
66         return true;
67     }
68
69     /**
70      * Returns <tt>true</tt> if this type of attribute is a code attribute.
71      *
72      * @return <tt>true</tt> if this type of attribute is a code attribute.
73      */

74     public boolean isCodeAttribute() {
75         return false;
76     }
77
78     /**
79      * Returns the labels corresponding to this attribute.
80      *
81      * @return the labels corresponding to this attribute, or <tt>null</tt> if
82      * this attribute is not a code attribute that contains labels.
83      */

84     protected Label[] getLabels() {
85         return null;
86     }
87
88     /**
89      * Reads a {@link #type type} attribute. This method must return a <i>new</i>
90      * {@link Attribute} object, of type {@link #type type}, corresponding to
91      * the <tt>len</tt> bytes starting at the given offset, in the given class
92      * reader.
93      *
94      * @param cr the class that contains the attribute to be read.
95      * @param off index of the first byte of the attribute's content in {@link
96      * ClassReader#b cr.b}. The 6 attribute header bytes, containing the
97      * type and the length of the attribute, are not taken into account
98      * here.
99      * @param len the length of the attribute's content.
100      * @param buf buffer to be used to call
101      * {@link ClassReader#readUTF8 readUTF8},
102      * {@link ClassReader#readClass(int,char[]) readClass} or
103      * {@link ClassReader#readConst readConst}.
104      * @param codeOff index of the first byte of code's attribute content in
105      * {@link ClassReader#b cr.b}, or -1 if the attribute to be read is
106      * not a code attribute. The 6 attribute header bytes, containing the
107      * type and the length of the attribute, are not taken into account
108      * here.
109      * @param labels the labels of the method's code, or <tt>null</tt> if the
110      * attribute to be read is not a code attribute.
111      * @return a <i>new</i> {@link Attribute} object corresponding to the given
112      * bytes.
113      */

114     protected Attribute read(
115         ClassReader cr,
116         int off,
117         int len,
118         char[] buf,
119         int codeOff,
120         Label[] labels)
121     {
122         return new Attribute(type);
123     }
124
125     /**
126      * Returns the byte array form of this attribute.
127      *
128      * @param cw the class to which this attribute must be added. This parameter
129      * can be used to add to the constant pool of this class the items
130      * that corresponds to this attribute.
131      * @param code the bytecode of the method corresponding to this code
132      * attribute, or <tt>null</tt> if this attribute is not a code
133      * attributes.
134      * @param len the length of the bytecode of the method corresponding to this
135      * code attribute, or <tt>null</tt> if this attribute is not a code
136      * attribute.
137      * @param maxStack the maximum stack size of the method corresponding to
138      * this code attribute, or -1 if this attribute is not a code
139      * attribute.
140      * @param maxLocals the maximum number of local variables of the method
141      * corresponding to this code attribute, or -1 if this attribute is
142      * not a code attribute.
143      * @return the byte array form of this attribute.
144      */

145     protected ByteVector write(
146         ClassWriter cw,
147         byte[] code,
148         int len,
149         int maxStack,
150         int maxLocals)
151     {
152         return new ByteVector();
153     }
154
155     /**
156      * Returns the length of the attribute list that begins with this attribute.
157      *
158      * @return the length of the attribute list that begins with this attribute.
159      */

160     final int getCount() {
161         int count = 0;
162         Attribute attr = this;
163         while (attr != null) {
164             if (!attr.isUnknown()) {
165                 count += 1;
166             }
167             attr = attr.next;
168         }
169         return count;
170     }
171
172     /**
173      * Returns the size of all the attributes in this attribute list.
174      *
175      * @param cw the class writer to be used to convert the attributes into byte
176      * arrays, with the {@link #write write} method.
177      * @param code the bytecode of the method corresponding to these code
178      * attributes, or <tt>null</tt> if these attributes are not code
179      * attributes.
180      * @param len the length of the bytecode of the method corresponding to
181      * these code attributes, or <tt>null</tt> if these attributes are
182      * not code attributes.
183      * @param maxStack the maximum stack size of the method corresponding to
184      * these code attributes, or -1 if these attributes are not code
185      * attributes.
186      * @param maxLocals the maximum number of local variables of the method
187      * corresponding to these code attributes, or -1 if these attributes
188      * are not code attributes.
189      * @return the size of all the attributes in this attribute list. This size
190      * includes the size of the attribute headers.
191      */

192     final int getSize(
193         final ClassWriter cw,
194         final byte[] code,
195         final int len,
196         final int maxStack,
197         final int maxLocals)
198     {
199         Attribute attr = this;
200         int size = 0;
201         while (attr != null) {
202             if (!attr.isUnknown()) {
203                 cw.newUTF8(attr.type);
204                 size += attr.write(cw, code, len, maxStack, maxLocals).length + 6;
205             }
206             attr = attr.next;
207         }
208         return size;
209     }
210
211     /**
212      * Writes all the attributes of this attribute list in the given byte
213      * vector.
214      *
215      * @param cw the class writer to be used to convert the attributes into byte
216      * arrays, with the {@link #write write} method.
217      * @param code the bytecode of the method corresponding to these code
218      * attributes, or <tt>null</tt> if these attributes are not code
219      * attributes.
220      * @param len the length of the bytecode of the method corresponding to
221      * these code attributes, or <tt>null</tt> if these attributes are
222      * not code attributes.
223      * @param maxStack the maximum stack size of the method corresponding to
224      * these code attributes, or -1 if these attributes are not code
225      * attributes.
226      * @param maxLocals the maximum number of local variables of the method
227      * corresponding to these code attributes, or -1 if these attributes
228      * are not code attributes.
229      * @param out where the attributes must be written.
230      */

231     final void put(
232         final ClassWriter cw,
233         final byte[] code,
234         final int len,
235         final int maxStack,
236         final int maxLocals,
237         final ByteVector out)
238     {
239         Attribute attr = this;
240         while (attr != null) {
241             if (attr.isUnknown()) {
242                 if (cw.checkAttributes) {
243                     throw new IllegalArgumentException JavaDoc("Unknown attribute type: "
244                             + attr.type);
245                 }
246             } else {
247                 ByteVector b = attr.write(cw, code, len, maxStack, maxLocals);
248                 out.putShort(cw.newUTF8(attr.type)).putInt(b.length);
249                 out.putByteArray(b.data, 0, b.length);
250             }
251             attr = attr.next;
252         }
253     }
254 }
255
Popular Tags