view src/clojure/asm/Attribute.java @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
line wrap: on
line source
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 clojure.asm;
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{
40 /**
41 * The type of this attribute.
42 */
43 public final String type;
45 /**
46 * The raw value of this attribute, used only for unknown attributes.
47 */
48 byte[] value;
50 /**
51 * The next attribute in this attribute list. May be <tt>null</tt>.
52 */
53 Attribute next;
55 /**
56 * Constructs a new empty attribute.
57 *
58 * @param type the type of the attribute.
59 */
60 protected Attribute(final String type){
61 this.type = type;
62 }
64 /**
65 * Returns <tt>true</tt> if this type of attribute is unknown. The default
66 * implementation of this method always returns <tt>true</tt>.
67 *
68 * @return <tt>true</tt> if this type of attribute is unknown.
69 */
70 public boolean isUnknown(){
71 return true;
72 }
74 /**
75 * Returns <tt>true</tt> if this type of attribute is a code attribute.
76 *
77 * @return <tt>true</tt> if this type of attribute is a code attribute.
78 */
79 public boolean isCodeAttribute(){
80 return false;
81 }
83 /**
84 * Returns the labels corresponding to this attribute.
85 *
86 * @return the labels corresponding to this attribute, or <tt>null</tt> if
87 * this attribute is not a code attribute that contains labels.
88 */
89 protected Label[] getLabels(){
90 return null;
91 }
93 /**
94 * Reads a {@link #type type} attribute. This method must return a <i>new</i>
95 * {@link Attribute} object, of type {@link #type type}, corresponding to
96 * the <tt>len</tt> bytes starting at the given offset, in the given class
97 * reader.
98 *
99 * @param cr the class that contains the attribute to be read.
100 * @param off index of the first byte of the attribute's content in {@link
101 * ClassReader#b cr.b}. The 6 attribute header bytes, containing the
102 * type and the length of the attribute, are not taken into account
103 * here.
104 * @param len the length of the attribute's content.
105 * @param buf buffer to be used to call
106 * {@link ClassReader#readUTF8 readUTF8},
107 * {@link ClassReader#readClass(int,char[]) readClass} or
108 * {@link ClassReader#readConst readConst}.
109 * @param codeOff index of the first byte of code's attribute content in
110 * {@link ClassReader#b cr.b}, or -1 if the attribute to be read is
111 * not a code attribute. The 6 attribute header bytes, containing the
112 * type and the length of the attribute, are not taken into account
113 * here.
114 * @param labels the labels of the method's code, or <tt>null</tt> if the
115 * attribute to be read is not a code attribute.
116 * @return a <i>new</i> {@link Attribute} object corresponding to the given
117 * bytes.
118 */
119 protected Attribute read(
120 final ClassReader cr,
121 final int off,
122 final int len,
123 final char[] buf,
124 final int codeOff,
125 final Label[] labels){
126 Attribute attr = new Attribute(type);
127 attr.value = new byte[len];
128 System.arraycopy(cr.b, off, attr.value, 0, len);
129 return attr;
130 }
132 /**
133 * Returns the byte array form of this attribute.
134 *
135 * @param cw the class to which this attribute must be added. This parameter
136 * can be used to add to the constant pool of this class the items
137 * that corresponds to this attribute.
138 * @param code the bytecode of the method corresponding to this code
139 * attribute, or <tt>null</tt> if this attribute is not a code
140 * attributes.
141 * @param len the length of the bytecode of the method corresponding to this
142 * code attribute, or <tt>null</tt> if this attribute is not a code
143 * attribute.
144 * @param maxStack the maximum stack size of the method corresponding to
145 * this code attribute, or -1 if this attribute is not a code
146 * attribute.
147 * @param maxLocals the maximum number of local variables of the method
148 * corresponding to this code attribute, or -1 if this attribute is
149 * not a code attribute.
150 * @return the byte array form of this attribute.
151 */
152 protected ByteVector write(
153 final ClassWriter cw,
154 final byte[] code,
155 final int len,
156 final int maxStack,
157 final int maxLocals){
158 ByteVector v = new ByteVector();
159 v.data = value;
160 v.length = value.length;
161 return v;
162 }
164 /**
165 * Returns the length of the attribute list that begins with this attribute.
166 *
167 * @return the length of the attribute list that begins with this attribute.
168 */
169 final int getCount(){
170 int count = 0;
171 Attribute attr = this;
172 while(attr != null)
173 {
174 count += 1;
175 attr = attr.next;
176 }
177 return count;
178 }
180 /**
181 * Returns the size of all the attributes in this attribute list.
182 *
183 * @param cw the class writer to be used to convert the attributes into byte
184 * arrays, with the {@link #write write} method.
185 * @param code the bytecode of the method corresponding to these code
186 * attributes, or <tt>null</tt> if these attributes are not code
187 * attributes.
188 * @param len the length of the bytecode of the method corresponding to
189 * these code attributes, or <tt>null</tt> if these attributes are
190 * not code attributes.
191 * @param maxStack the maximum stack size of the method corresponding to
192 * these code attributes, or -1 if these attributes are not code
193 * attributes.
194 * @param maxLocals the maximum number of local variables of the method
195 * corresponding to these code attributes, or -1 if these attributes
196 * are not code attributes.
197 * @return the size of all the attributes in this attribute list. This size
198 * includes the size of the attribute headers.
199 */
200 final int getSize(
201 final ClassWriter cw,
202 final byte[] code,
203 final int len,
204 final int maxStack,
205 final int maxLocals){
206 Attribute attr = this;
207 int size = 0;
208 while(attr != null)
209 {
210 cw.newUTF8(attr.type);
211 size += attr.write(cw, code, len, maxStack, maxLocals).length + 6;
212 attr = attr.next;
213 }
214 return size;
215 }
217 /**
218 * Writes all the attributes of this attribute list in the given byte
219 * vector.
220 *
221 * @param cw the class writer to be used to convert the attributes into byte
222 * arrays, with the {@link #write write} method.
223 * @param code the bytecode of the method corresponding to these code
224 * attributes, or <tt>null</tt> if these attributes are not code
225 * attributes.
226 * @param len the length of the bytecode of the method corresponding to
227 * these code attributes, or <tt>null</tt> if these attributes are
228 * not code attributes.
229 * @param maxStack the maximum stack size of the method corresponding to
230 * these code attributes, or -1 if these attributes are not code
231 * attributes.
232 * @param maxLocals the maximum number of local variables of the method
233 * corresponding to these code attributes, or -1 if these attributes
234 * are not code attributes.
235 * @param out where the attributes must be written.
236 */
237 final void put(
238 final ClassWriter cw,
239 final byte[] code,
240 final int len,
241 final int maxStack,
242 final int maxLocals,
243 final ByteVector out){
244 Attribute attr = this;
245 while(attr != null)
246 {
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 attr = attr.next;
251 }
252 }
253 }