rlm@10
|
1 /***
|
rlm@10
|
2 * ASM: a very small and fast Java bytecode manipulation framework
|
rlm@10
|
3 * Copyright (c) 2000-2005 INRIA, France Telecom
|
rlm@10
|
4 * All rights reserved.
|
rlm@10
|
5 *
|
rlm@10
|
6 * Redistribution and use in source and binary forms, with or without
|
rlm@10
|
7 * modification, are permitted provided that the following conditions
|
rlm@10
|
8 * are met:
|
rlm@10
|
9 * 1. Redistributions of source code must retain the above copyright
|
rlm@10
|
10 * notice, this list of conditions and the following disclaimer.
|
rlm@10
|
11 * 2. Redistributions in binary form must reproduce the above copyright
|
rlm@10
|
12 * notice, this list of conditions and the following disclaimer in the
|
rlm@10
|
13 * documentation and/or other materials provided with the distribution.
|
rlm@10
|
14 * 3. Neither the name of the copyright holders nor the names of its
|
rlm@10
|
15 * contributors may be used to endorse or promote products derived from
|
rlm@10
|
16 * this software without specific prior written permission.
|
rlm@10
|
17 *
|
rlm@10
|
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
rlm@10
|
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
rlm@10
|
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
rlm@10
|
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
rlm@10
|
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
rlm@10
|
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
rlm@10
|
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
rlm@10
|
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
rlm@10
|
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
rlm@10
|
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
rlm@10
|
28 * THE POSSIBILITY OF SUCH DAMAGE.
|
rlm@10
|
29 */
|
rlm@10
|
30 package clojure.asm;
|
rlm@10
|
31
|
rlm@10
|
32 /**
|
rlm@10
|
33 * A non standard class, field, method or code attribute.
|
rlm@10
|
34 *
|
rlm@10
|
35 * @author Eric Bruneton
|
rlm@10
|
36 * @author Eugene Kuleshov
|
rlm@10
|
37 */
|
rlm@10
|
38 public class Attribute{
|
rlm@10
|
39
|
rlm@10
|
40 /**
|
rlm@10
|
41 * The type of this attribute.
|
rlm@10
|
42 */
|
rlm@10
|
43 public final String type;
|
rlm@10
|
44
|
rlm@10
|
45 /**
|
rlm@10
|
46 * The raw value of this attribute, used only for unknown attributes.
|
rlm@10
|
47 */
|
rlm@10
|
48 byte[] value;
|
rlm@10
|
49
|
rlm@10
|
50 /**
|
rlm@10
|
51 * The next attribute in this attribute list. May be <tt>null</tt>.
|
rlm@10
|
52 */
|
rlm@10
|
53 Attribute next;
|
rlm@10
|
54
|
rlm@10
|
55 /**
|
rlm@10
|
56 * Constructs a new empty attribute.
|
rlm@10
|
57 *
|
rlm@10
|
58 * @param type the type of the attribute.
|
rlm@10
|
59 */
|
rlm@10
|
60 protected Attribute(final String type){
|
rlm@10
|
61 this.type = type;
|
rlm@10
|
62 }
|
rlm@10
|
63
|
rlm@10
|
64 /**
|
rlm@10
|
65 * Returns <tt>true</tt> if this type of attribute is unknown. The default
|
rlm@10
|
66 * implementation of this method always returns <tt>true</tt>.
|
rlm@10
|
67 *
|
rlm@10
|
68 * @return <tt>true</tt> if this type of attribute is unknown.
|
rlm@10
|
69 */
|
rlm@10
|
70 public boolean isUnknown(){
|
rlm@10
|
71 return true;
|
rlm@10
|
72 }
|
rlm@10
|
73
|
rlm@10
|
74 /**
|
rlm@10
|
75 * Returns <tt>true</tt> if this type of attribute is a code attribute.
|
rlm@10
|
76 *
|
rlm@10
|
77 * @return <tt>true</tt> if this type of attribute is a code attribute.
|
rlm@10
|
78 */
|
rlm@10
|
79 public boolean isCodeAttribute(){
|
rlm@10
|
80 return false;
|
rlm@10
|
81 }
|
rlm@10
|
82
|
rlm@10
|
83 /**
|
rlm@10
|
84 * Returns the labels corresponding to this attribute.
|
rlm@10
|
85 *
|
rlm@10
|
86 * @return the labels corresponding to this attribute, or <tt>null</tt> if
|
rlm@10
|
87 * this attribute is not a code attribute that contains labels.
|
rlm@10
|
88 */
|
rlm@10
|
89 protected Label[] getLabels(){
|
rlm@10
|
90 return null;
|
rlm@10
|
91 }
|
rlm@10
|
92
|
rlm@10
|
93 /**
|
rlm@10
|
94 * Reads a {@link #type type} attribute. This method must return a <i>new</i>
|
rlm@10
|
95 * {@link Attribute} object, of type {@link #type type}, corresponding to
|
rlm@10
|
96 * the <tt>len</tt> bytes starting at the given offset, in the given class
|
rlm@10
|
97 * reader.
|
rlm@10
|
98 *
|
rlm@10
|
99 * @param cr the class that contains the attribute to be read.
|
rlm@10
|
100 * @param off index of the first byte of the attribute's content in {@link
|
rlm@10
|
101 * ClassReader#b cr.b}. The 6 attribute header bytes, containing the
|
rlm@10
|
102 * type and the length of the attribute, are not taken into account
|
rlm@10
|
103 * here.
|
rlm@10
|
104 * @param len the length of the attribute's content.
|
rlm@10
|
105 * @param buf buffer to be used to call
|
rlm@10
|
106 * {@link ClassReader#readUTF8 readUTF8},
|
rlm@10
|
107 * {@link ClassReader#readClass(int,char[]) readClass} or
|
rlm@10
|
108 * {@link ClassReader#readConst readConst}.
|
rlm@10
|
109 * @param codeOff index of the first byte of code's attribute content in
|
rlm@10
|
110 * {@link ClassReader#b cr.b}, or -1 if the attribute to be read is
|
rlm@10
|
111 * not a code attribute. The 6 attribute header bytes, containing the
|
rlm@10
|
112 * type and the length of the attribute, are not taken into account
|
rlm@10
|
113 * here.
|
rlm@10
|
114 * @param labels the labels of the method's code, or <tt>null</tt> if the
|
rlm@10
|
115 * attribute to be read is not a code attribute.
|
rlm@10
|
116 * @return a <i>new</i> {@link Attribute} object corresponding to the given
|
rlm@10
|
117 * bytes.
|
rlm@10
|
118 */
|
rlm@10
|
119 protected Attribute read(
|
rlm@10
|
120 final ClassReader cr,
|
rlm@10
|
121 final int off,
|
rlm@10
|
122 final int len,
|
rlm@10
|
123 final char[] buf,
|
rlm@10
|
124 final int codeOff,
|
rlm@10
|
125 final Label[] labels){
|
rlm@10
|
126 Attribute attr = new Attribute(type);
|
rlm@10
|
127 attr.value = new byte[len];
|
rlm@10
|
128 System.arraycopy(cr.b, off, attr.value, 0, len);
|
rlm@10
|
129 return attr;
|
rlm@10
|
130 }
|
rlm@10
|
131
|
rlm@10
|
132 /**
|
rlm@10
|
133 * Returns the byte array form of this attribute.
|
rlm@10
|
134 *
|
rlm@10
|
135 * @param cw the class to which this attribute must be added. This parameter
|
rlm@10
|
136 * can be used to add to the constant pool of this class the items
|
rlm@10
|
137 * that corresponds to this attribute.
|
rlm@10
|
138 * @param code the bytecode of the method corresponding to this code
|
rlm@10
|
139 * attribute, or <tt>null</tt> if this attribute is not a code
|
rlm@10
|
140 * attributes.
|
rlm@10
|
141 * @param len the length of the bytecode of the method corresponding to this
|
rlm@10
|
142 * code attribute, or <tt>null</tt> if this attribute is not a code
|
rlm@10
|
143 * attribute.
|
rlm@10
|
144 * @param maxStack the maximum stack size of the method corresponding to
|
rlm@10
|
145 * this code attribute, or -1 if this attribute is not a code
|
rlm@10
|
146 * attribute.
|
rlm@10
|
147 * @param maxLocals the maximum number of local variables of the method
|
rlm@10
|
148 * corresponding to this code attribute, or -1 if this attribute is
|
rlm@10
|
149 * not a code attribute.
|
rlm@10
|
150 * @return the byte array form of this attribute.
|
rlm@10
|
151 */
|
rlm@10
|
152 protected ByteVector write(
|
rlm@10
|
153 final ClassWriter cw,
|
rlm@10
|
154 final byte[] code,
|
rlm@10
|
155 final int len,
|
rlm@10
|
156 final int maxStack,
|
rlm@10
|
157 final int maxLocals){
|
rlm@10
|
158 ByteVector v = new ByteVector();
|
rlm@10
|
159 v.data = value;
|
rlm@10
|
160 v.length = value.length;
|
rlm@10
|
161 return v;
|
rlm@10
|
162 }
|
rlm@10
|
163
|
rlm@10
|
164 /**
|
rlm@10
|
165 * Returns the length of the attribute list that begins with this attribute.
|
rlm@10
|
166 *
|
rlm@10
|
167 * @return the length of the attribute list that begins with this attribute.
|
rlm@10
|
168 */
|
rlm@10
|
169 final int getCount(){
|
rlm@10
|
170 int count = 0;
|
rlm@10
|
171 Attribute attr = this;
|
rlm@10
|
172 while(attr != null)
|
rlm@10
|
173 {
|
rlm@10
|
174 count += 1;
|
rlm@10
|
175 attr = attr.next;
|
rlm@10
|
176 }
|
rlm@10
|
177 return count;
|
rlm@10
|
178 }
|
rlm@10
|
179
|
rlm@10
|
180 /**
|
rlm@10
|
181 * Returns the size of all the attributes in this attribute list.
|
rlm@10
|
182 *
|
rlm@10
|
183 * @param cw the class writer to be used to convert the attributes into byte
|
rlm@10
|
184 * arrays, with the {@link #write write} method.
|
rlm@10
|
185 * @param code the bytecode of the method corresponding to these code
|
rlm@10
|
186 * attributes, or <tt>null</tt> if these attributes are not code
|
rlm@10
|
187 * attributes.
|
rlm@10
|
188 * @param len the length of the bytecode of the method corresponding to
|
rlm@10
|
189 * these code attributes, or <tt>null</tt> if these attributes are
|
rlm@10
|
190 * not code attributes.
|
rlm@10
|
191 * @param maxStack the maximum stack size of the method corresponding to
|
rlm@10
|
192 * these code attributes, or -1 if these attributes are not code
|
rlm@10
|
193 * attributes.
|
rlm@10
|
194 * @param maxLocals the maximum number of local variables of the method
|
rlm@10
|
195 * corresponding to these code attributes, or -1 if these attributes
|
rlm@10
|
196 * are not code attributes.
|
rlm@10
|
197 * @return the size of all the attributes in this attribute list. This size
|
rlm@10
|
198 * includes the size of the attribute headers.
|
rlm@10
|
199 */
|
rlm@10
|
200 final int getSize(
|
rlm@10
|
201 final ClassWriter cw,
|
rlm@10
|
202 final byte[] code,
|
rlm@10
|
203 final int len,
|
rlm@10
|
204 final int maxStack,
|
rlm@10
|
205 final int maxLocals){
|
rlm@10
|
206 Attribute attr = this;
|
rlm@10
|
207 int size = 0;
|
rlm@10
|
208 while(attr != null)
|
rlm@10
|
209 {
|
rlm@10
|
210 cw.newUTF8(attr.type);
|
rlm@10
|
211 size += attr.write(cw, code, len, maxStack, maxLocals).length + 6;
|
rlm@10
|
212 attr = attr.next;
|
rlm@10
|
213 }
|
rlm@10
|
214 return size;
|
rlm@10
|
215 }
|
rlm@10
|
216
|
rlm@10
|
217 /**
|
rlm@10
|
218 * Writes all the attributes of this attribute list in the given byte
|
rlm@10
|
219 * vector.
|
rlm@10
|
220 *
|
rlm@10
|
221 * @param cw the class writer to be used to convert the attributes into byte
|
rlm@10
|
222 * arrays, with the {@link #write write} method.
|
rlm@10
|
223 * @param code the bytecode of the method corresponding to these code
|
rlm@10
|
224 * attributes, or <tt>null</tt> if these attributes are not code
|
rlm@10
|
225 * attributes.
|
rlm@10
|
226 * @param len the length of the bytecode of the method corresponding to
|
rlm@10
|
227 * these code attributes, or <tt>null</tt> if these attributes are
|
rlm@10
|
228 * not code attributes.
|
rlm@10
|
229 * @param maxStack the maximum stack size of the method corresponding to
|
rlm@10
|
230 * these code attributes, or -1 if these attributes are not code
|
rlm@10
|
231 * attributes.
|
rlm@10
|
232 * @param maxLocals the maximum number of local variables of the method
|
rlm@10
|
233 * corresponding to these code attributes, or -1 if these attributes
|
rlm@10
|
234 * are not code attributes.
|
rlm@10
|
235 * @param out where the attributes must be written.
|
rlm@10
|
236 */
|
rlm@10
|
237 final void put(
|
rlm@10
|
238 final ClassWriter cw,
|
rlm@10
|
239 final byte[] code,
|
rlm@10
|
240 final int len,
|
rlm@10
|
241 final int maxStack,
|
rlm@10
|
242 final int maxLocals,
|
rlm@10
|
243 final ByteVector out){
|
rlm@10
|
244 Attribute attr = this;
|
rlm@10
|
245 while(attr != null)
|
rlm@10
|
246 {
|
rlm@10
|
247 ByteVector b = attr.write(cw, code, len, maxStack, maxLocals);
|
rlm@10
|
248 out.putShort(cw.newUTF8(attr.type)).putInt(b.length);
|
rlm@10
|
249 out.putByteArray(b.data, 0, b.length);
|
rlm@10
|
250 attr = attr.next;
|
rlm@10
|
251 }
|
rlm@10
|
252 }
|
rlm@10
|
253 }
|