KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > libraries > asm > Attribute


1 /***
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000,2002,2003 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
31 package oracle.toplink.libraries.asm;
32
33 /**
34  * A non standard class, field, method or code attribute.
35  *
36  * @author Eric Bruneton, Eugene Kuleshov
37  */

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

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

50
51   public Attribute next;
52
53   /**
54    * Constructs a new empty attribute.
55    *
56    * @param type the type of the attribute.
57    */

58
59   protected Attribute (final String JavaDoc type) {
60     this.type = type;
61   }
62
63   /**
64    * Returns <tt>true</tt> if this type of attribute is unknown.
65    *
66    * @return <tt>true</tt> if the class of this object is equal to
67    * {@link Attribute}.
68    */

69
70   public boolean isUnknown () {
71     return getClass().getName().equals("oracle.toplink.libraries.asm.Attribute");
72   }
73
74   /**
75    * Returns the labels corresponding to this attribute.
76    *
77    * @return the labels corresponding to this attribute, or <tt>null</tt> if
78    * this attribute is not a code attribute that contains labels.
79    */

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

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

133
134   protected ByteVector write (
135     ClassWriter cw, byte[] code, int len, int maxStack, int maxLocals)
136   {
137     return new ByteVector();
138   }
139
140   /**
141    * Returns the length of the attribute list that begins with this attribute.
142    *
143    * @return the length of the attribute list that begins with this attribute.
144    */

145
146   final int getCount () {
147     int count = 0;
148     Attribute attr = this;
149     while (attr != null) {
150       if (!attr.isUnknown()) {
151         count += 1;
152       }
153       attr = attr.next;
154     }
155     return count;
156   }
157
158   /**
159    * Returns the size of all the attributes in this attribute list.
160    *
161    * @param cw the class writer to be used to convert the attributes into byte
162    * arrays, with the {@link #write write} method.
163    * @param code the bytecode of the method corresponding to these code
164    * attributes, or <tt>null</tt> if these attributes are not code
165    * attributes.
166    * @param len the length of the bytecode of the method corresponding to these
167    * code attributes, or <tt>null</tt> if these attributes are not code
168    * attributes.
169    * @param maxStack the maximum stack size of the method corresponding to these
170    * code attributes, or -1 if these attributes are not code attributes.
171    * @param maxLocals the maximum number of local variables of the method
172    * corresponding to these code attributes, or -1 if these attributes are
173    * not code attributes.
174    * @return the size of all the attributes in this attribute list. This size
175    * includes the size of the attribute headers.
176    */

177
178   final int getSize (
179     final ClassWriter cw,
180     final byte[] code,
181     final int len,
182     final int maxStack,
183     final int maxLocals)
184   {
185     Attribute attr = this;
186     int size = 0;
187     while (attr != null) {
188       if (!attr.isUnknown()) {
189         cw.newUTF8(attr.type);
190         size += attr.write(cw, code, len, maxStack, maxLocals).length + 6;
191       }
192       attr = attr.next;
193     }
194     return size;
195   }
196
197   /**
198    * Writes all the attributes of this attribute list in the given byte vector.
199    *
200    * @param cw the class writer to be used to convert the attributes into byte
201    * arrays, with the {@link #write write} method.
202    * @param code the bytecode of the method corresponding to these code
203    * attributes, or <tt>null</tt> if these attributes are not code
204    * attributes.
205    * @param len the length of the bytecode of the method corresponding to these
206    * code attributes, or <tt>null</tt> if these attributes are not code
207    * attributes.
208    * @param maxStack the maximum stack size of the method corresponding to these
209    * code attributes, or -1 if these attributes are not code attributes.
210    * @param maxLocals the maximum number of local variables of the method
211    * corresponding to these code attributes, or -1 if these attributes are
212    * not code attributes.
213    * @param out where the attributes must be written.
214    */

215
216   final void put (
217     final ClassWriter cw,
218     final byte[] code,
219     final int len,
220     final int maxStack,
221     final int maxLocals,
222     final ByteVector out)
223   {
224     if (next != null) {
225       next.put(cw, code, len, maxStack, maxLocals, out);
226     }
227     if (isUnknown()) {
228       if (cw.checkAttributes) {
229         throw new IllegalArgumentException JavaDoc("Unknown attribute type " + type);
230       }
231     } else {
232       ByteVector b = write(cw, code, len, maxStack, maxLocals);
233       out.putShort(cw.newUTF8(type)).putInt(b.length);
234       out.putByteArray(b.data, 0, b.length);
235     }
236   }
237 }
238
Popular Tags