130 // ------------------------------------------------------------------------
131
132 /**
133 * Constructs a new {@link FieldWriter}.
134 *
135 * @param cw
136 * the class writer to which this field must be added.
137 * @param access
138 * the field's access flags (see {@link Opcodes}).
139 * @param name
140 * the field's name.
141 * @param desc
142 * the field's descriptor (see {@link Type}).
143 * @param signature
144 * the field's signature. May be <tt>null</tt>.
145 * @param value
146 * the field's constant value. May be <tt>null</tt>.
147 */
148 FieldWriter(final ClassWriter cw, final int access, final String name,
149 final String desc, final String signature, final Object value) {
150 super(Opcodes.ASM5);
151 if (cw.firstField == null) {
152 cw.firstField = this;
153 } else {
154 cw.lastField.fv = this;
155 }
156 cw.lastField = this;
157 this.cw = cw;
158 this.access = access;
159 this.name = cw.newUTF8(name);
160 this.desc = cw.newUTF8(desc);
161 if (ClassReader.SIGNATURES && signature != null) {
162 this.signature = cw.newUTF8(signature);
163 }
164 if (value != null) {
165 this.value = cw.newConstItem(value).index;
166 }
167 }
168
169 // ------------------------------------------------------------------------
170 // Implementation of the FieldVisitor abstract class
171 // ------------------------------------------------------------------------
172
173 @Override
174 public AnnotationVisitor visitAnnotation(final String desc,
175 final boolean visible) {
176 if (!ClassReader.ANNOTATIONS) {
177 return null;
178 }
179 ByteVector bv = new ByteVector();
180 // write type, and reserve space for values count
181 bv.putShort(cw.newUTF8(desc)).putShort(0);
182 AnnotationWriter aw = new AnnotationWriter(cw, true, bv, bv, 2);
183 if (visible) {
184 aw.next = anns;
185 anns = aw;
186 } else {
187 aw.next = ianns;
188 ianns = aw;
189 }
190 return aw;
191 }
192
193 @Override
194 public AnnotationVisitor visitTypeAnnotation(final int typeRef,
195 final TypePath typePath, final String desc, final boolean visible) {
196 if (!ClassReader.ANNOTATIONS) {
197 return null;
198 }
199 ByteVector bv = new ByteVector();
200 // write target_type and target_info
201 AnnotationWriter.putTarget(typeRef, typePath, bv);
202 // write type, and reserve space for values count
203 bv.putShort(cw.newUTF8(desc)).putShort(0);
204 AnnotationWriter aw = new AnnotationWriter(cw, true, bv, bv,
205 bv.length - 2);
206 if (visible) {
207 aw.next = tanns;
208 tanns = aw;
209 } else {
210 aw.next = itanns;
211 itanns = aw;
212 }
213 return aw;
214 }
215
216 @Override
217 public void visitAttribute(final Attribute attr) {
218 attr.next = attrs;
232 *
233 * @return the size of this field.
234 */
235 int getSize() {
236 int size = 8;
237 if (value != 0) {
238 cw.newUTF8("ConstantValue");
239 size += 8;
240 }
241 if ((access & Opcodes.ACC_SYNTHETIC) != 0) {
242 if ((cw.version & 0xFFFF) < Opcodes.V1_5
243 || (access & ClassWriter.ACC_SYNTHETIC_ATTRIBUTE) != 0) {
244 cw.newUTF8("Synthetic");
245 size += 6;
246 }
247 }
248 if ((access & Opcodes.ACC_DEPRECATED) != 0) {
249 cw.newUTF8("Deprecated");
250 size += 6;
251 }
252 if (ClassReader.SIGNATURES && signature != 0) {
253 cw.newUTF8("Signature");
254 size += 8;
255 }
256 if (ClassReader.ANNOTATIONS && anns != null) {
257 cw.newUTF8("RuntimeVisibleAnnotations");
258 size += 8 + anns.getSize();
259 }
260 if (ClassReader.ANNOTATIONS && ianns != null) {
261 cw.newUTF8("RuntimeInvisibleAnnotations");
262 size += 8 + ianns.getSize();
263 }
264 if (ClassReader.ANNOTATIONS && tanns != null) {
265 cw.newUTF8("RuntimeVisibleTypeAnnotations");
266 size += 8 + tanns.getSize();
267 }
268 if (ClassReader.ANNOTATIONS && itanns != null) {
269 cw.newUTF8("RuntimeInvisibleTypeAnnotations");
270 size += 8 + itanns.getSize();
271 }
272 if (attrs != null) {
273 size += attrs.getSize(cw, null, 0, -1, -1);
274 }
275 return size;
276 }
277
278 /**
279 * Puts the content of this field into the given byte vector.
280 *
281 * @param out
282 * where the content of this field must be put.
283 */
284 void put(final ByteVector out) {
285 final int FACTOR = ClassWriter.TO_ACC_SYNTHETIC;
286 int mask = Opcodes.ACC_DEPRECATED | ClassWriter.ACC_SYNTHETIC_ATTRIBUTE
287 | ((access & ClassWriter.ACC_SYNTHETIC_ATTRIBUTE) / FACTOR);
288 out.putShort(access & ~mask).putShort(name).putShort(desc);
289 int attributeCount = 0;
290 if (value != 0) {
291 ++attributeCount;
292 }
293 if ((access & Opcodes.ACC_SYNTHETIC) != 0) {
294 if ((cw.version & 0xFFFF) < Opcodes.V1_5
295 || (access & ClassWriter.ACC_SYNTHETIC_ATTRIBUTE) != 0) {
296 ++attributeCount;
297 }
298 }
299 if ((access & Opcodes.ACC_DEPRECATED) != 0) {
300 ++attributeCount;
301 }
302 if (ClassReader.SIGNATURES && signature != 0) {
303 ++attributeCount;
304 }
305 if (ClassReader.ANNOTATIONS && anns != null) {
306 ++attributeCount;
307 }
308 if (ClassReader.ANNOTATIONS && ianns != null) {
309 ++attributeCount;
310 }
311 if (ClassReader.ANNOTATIONS && tanns != null) {
312 ++attributeCount;
313 }
314 if (ClassReader.ANNOTATIONS && itanns != null) {
315 ++attributeCount;
316 }
317 if (attrs != null) {
318 attributeCount += attrs.getCount();
319 }
320 out.putShort(attributeCount);
321 if (value != 0) {
322 out.putShort(cw.newUTF8("ConstantValue"));
323 out.putInt(2).putShort(value);
324 }
325 if ((access & Opcodes.ACC_SYNTHETIC) != 0) {
326 if ((cw.version & 0xFFFF) < Opcodes.V1_5
327 || (access & ClassWriter.ACC_SYNTHETIC_ATTRIBUTE) != 0) {
328 out.putShort(cw.newUTF8("Synthetic")).putInt(0);
329 }
330 }
331 if ((access & Opcodes.ACC_DEPRECATED) != 0) {
332 out.putShort(cw.newUTF8("Deprecated")).putInt(0);
333 }
334 if (ClassReader.SIGNATURES && signature != 0) {
335 out.putShort(cw.newUTF8("Signature"));
336 out.putInt(2).putShort(signature);
337 }
338 if (ClassReader.ANNOTATIONS && anns != null) {
339 out.putShort(cw.newUTF8("RuntimeVisibleAnnotations"));
340 anns.put(out);
341 }
342 if (ClassReader.ANNOTATIONS && ianns != null) {
343 out.putShort(cw.newUTF8("RuntimeInvisibleAnnotations"));
344 ianns.put(out);
345 }
346 if (ClassReader.ANNOTATIONS && tanns != null) {
347 out.putShort(cw.newUTF8("RuntimeVisibleTypeAnnotations"));
348 tanns.put(out);
349 }
350 if (ClassReader.ANNOTATIONS && itanns != null) {
351 out.putShort(cw.newUTF8("RuntimeInvisibleTypeAnnotations"));
352 itanns.put(out);
353 }
354 if (attrs != null) {
355 attrs.put(cw, null, 0, -1, -1, out);
356 }
357 }
358 }
|
130 // ------------------------------------------------------------------------
131
132 /**
133 * Constructs a new {@link FieldWriter}.
134 *
135 * @param cw
136 * the class writer to which this field must be added.
137 * @param access
138 * the field's access flags (see {@link Opcodes}).
139 * @param name
140 * the field's name.
141 * @param desc
142 * the field's descriptor (see {@link Type}).
143 * @param signature
144 * the field's signature. May be <tt>null</tt>.
145 * @param value
146 * the field's constant value. May be <tt>null</tt>.
147 */
148 FieldWriter(final ClassWriter cw, final int access, final String name,
149 final String desc, final String signature, final Object value) {
150 super(Opcodes.ASM6);
151 if (cw.firstField == null) {
152 cw.firstField = this;
153 } else {
154 cw.lastField.fv = this;
155 }
156 cw.lastField = this;
157 this.cw = cw;
158 this.access = access;
159 this.name = cw.newUTF8(name);
160 this.desc = cw.newUTF8(desc);
161 if (signature != null) {
162 this.signature = cw.newUTF8(signature);
163 }
164 if (value != null) {
165 this.value = cw.newConstItem(value).index;
166 }
167 }
168
169 // ------------------------------------------------------------------------
170 // Implementation of the FieldVisitor abstract class
171 // ------------------------------------------------------------------------
172
173 @Override
174 public AnnotationVisitor visitAnnotation(final String desc,
175 final boolean visible) {
176 ByteVector bv = new ByteVector();
177 // write type, and reserve space for values count
178 bv.putShort(cw.newUTF8(desc)).putShort(0);
179 AnnotationWriter aw = new AnnotationWriter(cw, true, bv, bv, 2);
180 if (visible) {
181 aw.next = anns;
182 anns = aw;
183 } else {
184 aw.next = ianns;
185 ianns = aw;
186 }
187 return aw;
188 }
189
190 @Override
191 public AnnotationVisitor visitTypeAnnotation(final int typeRef,
192 final TypePath typePath, final String desc, final boolean visible) {
193 ByteVector bv = new ByteVector();
194 // write target_type and target_info
195 AnnotationWriter.putTarget(typeRef, typePath, bv);
196 // write type, and reserve space for values count
197 bv.putShort(cw.newUTF8(desc)).putShort(0);
198 AnnotationWriter aw = new AnnotationWriter(cw, true, bv, bv,
199 bv.length - 2);
200 if (visible) {
201 aw.next = tanns;
202 tanns = aw;
203 } else {
204 aw.next = itanns;
205 itanns = aw;
206 }
207 return aw;
208 }
209
210 @Override
211 public void visitAttribute(final Attribute attr) {
212 attr.next = attrs;
226 *
227 * @return the size of this field.
228 */
229 int getSize() {
230 int size = 8;
231 if (value != 0) {
232 cw.newUTF8("ConstantValue");
233 size += 8;
234 }
235 if ((access & Opcodes.ACC_SYNTHETIC) != 0) {
236 if ((cw.version & 0xFFFF) < Opcodes.V1_5
237 || (access & ClassWriter.ACC_SYNTHETIC_ATTRIBUTE) != 0) {
238 cw.newUTF8("Synthetic");
239 size += 6;
240 }
241 }
242 if ((access & Opcodes.ACC_DEPRECATED) != 0) {
243 cw.newUTF8("Deprecated");
244 size += 6;
245 }
246 if (signature != 0) {
247 cw.newUTF8("Signature");
248 size += 8;
249 }
250 if (anns != null) {
251 cw.newUTF8("RuntimeVisibleAnnotations");
252 size += 8 + anns.getSize();
253 }
254 if (ianns != null) {
255 cw.newUTF8("RuntimeInvisibleAnnotations");
256 size += 8 + ianns.getSize();
257 }
258 if (tanns != null) {
259 cw.newUTF8("RuntimeVisibleTypeAnnotations");
260 size += 8 + tanns.getSize();
261 }
262 if (itanns != null) {
263 cw.newUTF8("RuntimeInvisibleTypeAnnotations");
264 size += 8 + itanns.getSize();
265 }
266 if (attrs != null) {
267 size += attrs.getSize(cw, null, 0, -1, -1);
268 }
269 return size;
270 }
271
272 /**
273 * Puts the content of this field into the given byte vector.
274 *
275 * @param out
276 * where the content of this field must be put.
277 */
278 void put(final ByteVector out) {
279 final int FACTOR = ClassWriter.TO_ACC_SYNTHETIC;
280 int mask = Opcodes.ACC_DEPRECATED | ClassWriter.ACC_SYNTHETIC_ATTRIBUTE
281 | ((access & ClassWriter.ACC_SYNTHETIC_ATTRIBUTE) / FACTOR);
282 out.putShort(access & ~mask).putShort(name).putShort(desc);
283 int attributeCount = 0;
284 if (value != 0) {
285 ++attributeCount;
286 }
287 if ((access & Opcodes.ACC_SYNTHETIC) != 0) {
288 if ((cw.version & 0xFFFF) < Opcodes.V1_5
289 || (access & ClassWriter.ACC_SYNTHETIC_ATTRIBUTE) != 0) {
290 ++attributeCount;
291 }
292 }
293 if ((access & Opcodes.ACC_DEPRECATED) != 0) {
294 ++attributeCount;
295 }
296 if (signature != 0) {
297 ++attributeCount;
298 }
299 if (anns != null) {
300 ++attributeCount;
301 }
302 if (ianns != null) {
303 ++attributeCount;
304 }
305 if (tanns != null) {
306 ++attributeCount;
307 }
308 if (itanns != null) {
309 ++attributeCount;
310 }
311 if (attrs != null) {
312 attributeCount += attrs.getCount();
313 }
314 out.putShort(attributeCount);
315 if (value != 0) {
316 out.putShort(cw.newUTF8("ConstantValue"));
317 out.putInt(2).putShort(value);
318 }
319 if ((access & Opcodes.ACC_SYNTHETIC) != 0) {
320 if ((cw.version & 0xFFFF) < Opcodes.V1_5
321 || (access & ClassWriter.ACC_SYNTHETIC_ATTRIBUTE) != 0) {
322 out.putShort(cw.newUTF8("Synthetic")).putInt(0);
323 }
324 }
325 if ((access & Opcodes.ACC_DEPRECATED) != 0) {
326 out.putShort(cw.newUTF8("Deprecated")).putInt(0);
327 }
328 if (signature != 0) {
329 out.putShort(cw.newUTF8("Signature"));
330 out.putInt(2).putShort(signature);
331 }
332 if (anns != null) {
333 out.putShort(cw.newUTF8("RuntimeVisibleAnnotations"));
334 anns.put(out);
335 }
336 if (ianns != null) {
337 out.putShort(cw.newUTF8("RuntimeInvisibleAnnotations"));
338 ianns.put(out);
339 }
340 if (tanns != null) {
341 out.putShort(cw.newUTF8("RuntimeVisibleTypeAnnotations"));
342 tanns.put(out);
343 }
344 if (itanns != null) {
345 out.putShort(cw.newUTF8("RuntimeInvisibleTypeAnnotations"));
346 itanns.put(out);
347 }
348 if (attrs != null) {
349 attrs.put(cw, null, 0, -1, -1, out);
350 }
351 }
352 }
|