view src/clojure/asm/FieldWriter.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 * An {@link FieldVisitor} that generates Java fields in bytecode form.
34 *
35 * @author Eric Bruneton
36 */
37 final class FieldWriter implements FieldVisitor{
39 /**
40 * Next field writer (see {@link ClassWriter#firstField firstField}).
41 */
42 FieldWriter next;
44 /**
45 * The class writer to which this field must be added.
46 */
47 private ClassWriter cw;
49 /**
50 * Access flags of this field.
51 */
52 private int access;
54 /**
55 * The index of the constant pool item that contains the name of this
56 * method.
57 */
58 private int name;
60 /**
61 * The index of the constant pool item that contains the descriptor of this
62 * field.
63 */
64 private int desc;
66 /**
67 * The index of the constant pool item that contains the signature of this
68 * field.
69 */
70 private int signature;
72 /**
73 * The index of the constant pool item that contains the constant value of
74 * this field.
75 */
76 private int value;
78 /**
79 * The runtime visible annotations of this field. May be <tt>null</tt>.
80 */
81 private AnnotationWriter anns;
83 /**
84 * The runtime invisible annotations of this field. May be <tt>null</tt>.
85 */
86 private AnnotationWriter ianns;
88 /**
89 * The non standard attributes of this field. May be <tt>null</tt>.
90 */
91 private Attribute attrs;
93 // ------------------------------------------------------------------------
94 // Constructor
95 // ------------------------------------------------------------------------
97 /**
98 * Constructs a new {@link FieldWriter}.
99 *
100 * @param cw the class writer to which this field must be added.
101 * @param access the field's access flags (see {@link Opcodes}).
102 * @param name the field's name.
103 * @param desc the field's descriptor (see {@link Type}).
104 * @param signature the field's signature. May be <tt>null</tt>.
105 * @param value the field's constant value. May be <tt>null</tt>.
106 */
107 protected FieldWriter(
108 final ClassWriter cw,
109 final int access,
110 final String name,
111 final String desc,
112 final String signature,
113 final Object value){
114 if(cw.firstField == null)
115 {
116 cw.firstField = this;
117 }
118 else
119 {
120 cw.lastField.next = this;
121 }
122 cw.lastField = this;
123 this.cw = cw;
124 this.access = access;
125 this.name = cw.newUTF8(name);
126 this.desc = cw.newUTF8(desc);
127 if(signature != null)
128 {
129 this.signature = cw.newUTF8(signature);
130 }
131 if(value != null)
132 {
133 this.value = cw.newConstItem(value).index;
134 }
135 }
137 // ------------------------------------------------------------------------
138 // Implementation of the FieldVisitor interface
139 // ------------------------------------------------------------------------
141 public AnnotationVisitor visitAnnotation(
142 final String desc,
143 final boolean visible){
144 ByteVector bv = new ByteVector();
145 // write type, and reserve space for values count
146 bv.putShort(cw.newUTF8(desc)).putShort(0);
147 AnnotationWriter aw = new AnnotationWriter(cw, true, bv, bv, 2);
148 if(visible)
149 {
150 aw.next = anns;
151 anns = aw;
152 }
153 else
154 {
155 aw.next = ianns;
156 ianns = aw;
157 }
158 return aw;
159 }
161 public void visitAttribute(final Attribute attr){
162 attr.next = attrs;
163 attrs = attr;
164 }
166 public void visitEnd(){
167 }
169 // ------------------------------------------------------------------------
170 // Utility methods
171 // ------------------------------------------------------------------------
173 /**
174 * Returns the size of this field.
175 *
176 * @return the size of this field.
177 */
178 int getSize(){
179 int size = 8;
180 if(value != 0)
181 {
182 cw.newUTF8("ConstantValue");
183 size += 8;
184 }
185 if((access & Opcodes.ACC_SYNTHETIC) != 0
186 && (cw.version & 0xffff) < Opcodes.V1_5)
187 {
188 cw.newUTF8("Synthetic");
189 size += 6;
190 }
191 if((access & Opcodes.ACC_DEPRECATED) != 0)
192 {
193 cw.newUTF8("Deprecated");
194 size += 6;
195 }
196 if(signature != 0)
197 {
198 cw.newUTF8("Signature");
199 size += 8;
200 }
201 if(anns != null)
202 {
203 cw.newUTF8("RuntimeVisibleAnnotations");
204 size += 8 + anns.getSize();
205 }
206 if(ianns != null)
207 {
208 cw.newUTF8("RuntimeInvisibleAnnotations");
209 size += 8 + ianns.getSize();
210 }
211 if(attrs != null)
212 {
213 size += attrs.getSize(cw, null, 0, -1, -1);
214 }
215 return size;
216 }
218 /**
219 * Puts the content of this field into the given byte vector.
220 *
221 * @param out where the content of this field must be put.
222 */
223 void put(final ByteVector out){
224 out.putShort(access).putShort(name).putShort(desc);
225 int attributeCount = 0;
226 if(value != 0)
227 {
228 ++attributeCount;
229 }
230 if((access & Opcodes.ACC_SYNTHETIC) != 0
231 && (cw.version & 0xffff) < Opcodes.V1_5)
232 {
233 ++attributeCount;
234 }
235 if((access & Opcodes.ACC_DEPRECATED) != 0)
236 {
237 ++attributeCount;
238 }
239 if(signature != 0)
240 {
241 ++attributeCount;
242 }
243 if(anns != null)
244 {
245 ++attributeCount;
246 }
247 if(ianns != null)
248 {
249 ++attributeCount;
250 }
251 if(attrs != null)
252 {
253 attributeCount += attrs.getCount();
254 }
255 out.putShort(attributeCount);
256 if(value != 0)
257 {
258 out.putShort(cw.newUTF8("ConstantValue"));
259 out.putInt(2).putShort(value);
260 }
261 if((access & Opcodes.ACC_SYNTHETIC) != 0
262 && (cw.version & 0xffff) < Opcodes.V1_5)
263 {
264 out.putShort(cw.newUTF8("Synthetic")).putInt(0);
265 }
266 if((access & Opcodes.ACC_DEPRECATED) != 0)
267 {
268 out.putShort(cw.newUTF8("Deprecated")).putInt(0);
269 }
270 if(signature != 0)
271 {
272 out.putShort(cw.newUTF8("Signature"));
273 out.putInt(2).putShort(signature);
274 }
275 if(anns != null)
276 {
277 out.putShort(cw.newUTF8("RuntimeVisibleAnnotations"));
278 anns.put(out);
279 }
280 if(ianns != null)
281 {
282 out.putShort(cw.newUTF8("RuntimeInvisibleAnnotations"));
283 ianns.put(out);
284 }
285 if(attrs != null)
286 {
287 attrs.put(cw, null, 0, -1, -1, out);
288 }
289 }
290 }