1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 only, as
6  * published by the Free Software Foundation.  Oracle designates this
7  * particular file as subject to the "Classpath" exception as provided
8  * by Oracle in the LICENSE file that accompanied this code.
9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  */
24 
25 /*
26  * This file is available under and governed by the GNU General Public
27  * License version 2 only, as published by the Free Software Foundation.
28  * However, the following notice accompanied the original version of this
29  * file:
30  *
31  * ASM: a very small and fast Java bytecode manipulation framework
32  * Copyright (c) 2000-2011 INRIA, France Telecom
33  * All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  * 3. Neither the name of the copyright holders nor the names of its
44  *    contributors may be used to endorse or promote products derived from
45  *    this software without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
48  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
51  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
57  * THE POSSIBILITY OF SUCH DAMAGE.
58  */
59 package jdk.internal.org.objectweb.asm;
60 
61 /**
62  * A {@link FieldVisitor} that generates a corresponding 'field_info' structure, as defined in the
63  * Java Virtual Machine Specification (JVMS).
64  *
65  * @see <a href="https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-4.html#jvms-4.5">JVMS
66  *     4.5</a>
67  * @author Eric Bruneton
68  */
69 final class FieldWriter extends FieldVisitor {
70 
71     /** Where the constants used in this FieldWriter must be stored. */
72     private final SymbolTable symbolTable;
73 
74     // Note: fields are ordered as in the field_info structure, and those related to attributes are
75     // ordered as in Section 4.7 of the JVMS.
76 
77     /**
78       * The access_flags field of the field_info JVMS structure. This field can contain ASM specific
79       * access flags, such as {@link Opcodes#ACC_DEPRECATED}, which are removed when generating the
80       * ClassFile structure.
81       */
82     private final int accessFlags;
83 
84     /** The name_index field of the field_info JVMS structure. */
85     private final int nameIndex;
86 
87     /** The descriptor_index field of the field_info JVMS structure. */
88     private final int descriptorIndex;
89 
90     /**
91       * The signature_index field of the Signature attribute of this field_info, or 0 if there is no
92       * Signature attribute.
93       */
94     private int signatureIndex;
95 
96     /**
97       * The constantvalue_index field of the ConstantValue attribute of this field_info, or 0 if there
98       * is no ConstantValue attribute.
99       */
100     private int constantValueIndex;
101 
102     /**
103       * The last runtime visible annotation of this field. The previous ones can be accessed with the
104       * {@link AnnotationWriter#previousAnnotation} field. May be {@literal null}.
105       */
106     private AnnotationWriter lastRuntimeVisibleAnnotation;
107 
108     /**
109       * The last runtime invisible annotation of this field. The previous ones can be accessed with the
110       * {@link AnnotationWriter#previousAnnotation} field. May be {@literal null}.
111       */
112     private AnnotationWriter lastRuntimeInvisibleAnnotation;
113 
114     /**
115       * The last runtime visible type annotation of this field. The previous ones can be accessed with
116       * the {@link AnnotationWriter#previousAnnotation} field. May be {@literal null}.
117       */
118     private AnnotationWriter lastRuntimeVisibleTypeAnnotation;
119 
120     /**
121       * The last runtime invisible type annotation of this field. The previous ones can be accessed
122       * with the {@link AnnotationWriter#previousAnnotation} field. May be {@literal null}.
123       */
124     private AnnotationWriter lastRuntimeInvisibleTypeAnnotation;
125 
126     /**
127       * The first non standard attribute of this field. The next ones can be accessed with the {@link
128       * Attribute#nextAttribute} field. May be {@literal null}.
129       *
130       * <p><b>WARNING</b>: this list stores the attributes in the <i>reverse</i> order of their visit.
131       * firstAttribute is actually the last attribute visited in {@link #visitAttribute}. The {@link
132       * #putFieldInfo} method writes the attributes in the order defined by this list, i.e. in the
133       * reverse order specified by the user.
134       */
135     private Attribute firstAttribute;
136 
137     // -----------------------------------------------------------------------------------------------
138     // Constructor
139     // -----------------------------------------------------------------------------------------------
140 
141     /**
142       * Constructs a new {@link FieldWriter}.
143       *
144       * @param symbolTable where the constants used in this FieldWriter must be stored.
145       * @param access the field's access flags (see {@link Opcodes}).
146       * @param name the field's name.
147       * @param descriptor the field's descriptor (see {@link Type}).
148       * @param signature the field's signature. May be {@literal null}.
149       * @param constantValue the field's constant value. May be {@literal null}.
150       */
FieldWriter( final SymbolTable symbolTable, final int access, final String name, final String descriptor, final String signature, final Object constantValue)151     FieldWriter(
152             final SymbolTable symbolTable,
153             final int access,
154             final String name,
155             final String descriptor,
156             final String signature,
157             final Object constantValue) {
158         super(/* latest api = */ Opcodes.ASM8);
159         this.symbolTable = symbolTable;
160         this.accessFlags = access;
161         this.nameIndex = symbolTable.addConstantUtf8(name);
162         this.descriptorIndex = symbolTable.addConstantUtf8(descriptor);
163         if (signature != null) {
164             this.signatureIndex = symbolTable.addConstantUtf8(signature);
165         }
166         if (constantValue != null) {
167             this.constantValueIndex = symbolTable.addConstant(constantValue).index;
168         }
169     }
170 
171     // -----------------------------------------------------------------------------------------------
172     // Implementation of the FieldVisitor abstract class
173     // -----------------------------------------------------------------------------------------------
174 
175     @Override
visitAnnotation(final String descriptor, final boolean visible)176     public AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible) {
177         if (visible) {
178             return lastRuntimeVisibleAnnotation =
179                     AnnotationWriter.create(symbolTable, descriptor, lastRuntimeVisibleAnnotation);
180         } else {
181             return lastRuntimeInvisibleAnnotation =
182                     AnnotationWriter.create(symbolTable, descriptor, lastRuntimeInvisibleAnnotation);
183         }
184     }
185 
186     @Override
visitTypeAnnotation( final int typeRef, final TypePath typePath, final String descriptor, final boolean visible)187     public AnnotationVisitor visitTypeAnnotation(
188             final int typeRef, final TypePath typePath, final String descriptor, final boolean visible) {
189         if (visible) {
190             return lastRuntimeVisibleTypeAnnotation =
191                     AnnotationWriter.create(
192                             symbolTable, typeRef, typePath, descriptor, lastRuntimeVisibleTypeAnnotation);
193         } else {
194             return lastRuntimeInvisibleTypeAnnotation =
195                     AnnotationWriter.create(
196                             symbolTable, typeRef, typePath, descriptor, lastRuntimeInvisibleTypeAnnotation);
197         }
198     }
199 
200     @Override
visitAttribute(final Attribute attribute)201     public void visitAttribute(final Attribute attribute) {
202         // Store the attributes in the <i>reverse</i> order of their visit by this method.
203         attribute.nextAttribute = firstAttribute;
204         firstAttribute = attribute;
205     }
206 
207     @Override
visitEnd()208     public void visitEnd() {
209         // Nothing to do.
210     }
211 
212     // -----------------------------------------------------------------------------------------------
213     // Utility methods
214     // -----------------------------------------------------------------------------------------------
215 
216     /**
217       * Returns the size of the field_info JVMS structure generated by this FieldWriter. Also adds the
218       * names of the attributes of this field in the constant pool.
219       *
220       * @return the size in bytes of the field_info JVMS structure.
221       */
computeFieldInfoSize()222     int computeFieldInfoSize() {
223         // The access_flags, name_index, descriptor_index and attributes_count fields use 8 bytes.
224         int size = 8;
225         // For ease of reference, we use here the same attribute order as in Section 4.7 of the JVMS.
226         if (constantValueIndex != 0) {
227             // ConstantValue attributes always use 8 bytes.
228             symbolTable.addConstantUtf8(Constants.CONSTANT_VALUE);
229             size += 8;
230         }
231         size += Attribute.computeAttributesSize(symbolTable, accessFlags, signatureIndex);
232         size +=
233                 AnnotationWriter.computeAnnotationsSize(
234                         lastRuntimeVisibleAnnotation,
235                         lastRuntimeInvisibleAnnotation,
236                         lastRuntimeVisibleTypeAnnotation,
237                         lastRuntimeInvisibleTypeAnnotation);
238         if (firstAttribute != null) {
239             size += firstAttribute.computeAttributesSize(symbolTable);
240         }
241         return size;
242     }
243 
244     /**
245       * Puts the content of the field_info JVMS structure generated by this FieldWriter into the given
246       * ByteVector.
247       *
248       * @param output where the field_info structure must be put.
249       */
putFieldInfo(final ByteVector output)250     void putFieldInfo(final ByteVector output) {
251         boolean useSyntheticAttribute = symbolTable.getMajorVersion() < Opcodes.V1_5;
252         // Put the access_flags, name_index and descriptor_index fields.
253         int mask = useSyntheticAttribute ? Opcodes.ACC_SYNTHETIC : 0;
254         output.putShort(accessFlags & ~mask).putShort(nameIndex).putShort(descriptorIndex);
255         // Compute and put the attributes_count field.
256         // For ease of reference, we use here the same attribute order as in Section 4.7 of the JVMS.
257         int attributesCount = 0;
258         if (constantValueIndex != 0) {
259             ++attributesCount;
260         }
261         if ((accessFlags & Opcodes.ACC_SYNTHETIC) != 0 && useSyntheticAttribute) {
262             ++attributesCount;
263         }
264         if (signatureIndex != 0) {
265             ++attributesCount;
266         }
267         if ((accessFlags & Opcodes.ACC_DEPRECATED) != 0) {
268             ++attributesCount;
269         }
270         if (lastRuntimeVisibleAnnotation != null) {
271             ++attributesCount;
272         }
273         if (lastRuntimeInvisibleAnnotation != null) {
274             ++attributesCount;
275         }
276         if (lastRuntimeVisibleTypeAnnotation != null) {
277             ++attributesCount;
278         }
279         if (lastRuntimeInvisibleTypeAnnotation != null) {
280             ++attributesCount;
281         }
282         if (firstAttribute != null) {
283             attributesCount += firstAttribute.getAttributeCount();
284         }
285         output.putShort(attributesCount);
286         // Put the field_info attributes.
287         // For ease of reference, we use here the same attribute order as in Section 4.7 of the JVMS.
288         if (constantValueIndex != 0) {
289             output
290                     .putShort(symbolTable.addConstantUtf8(Constants.CONSTANT_VALUE))
291                     .putInt(2)
292                     .putShort(constantValueIndex);
293         }
294         Attribute.putAttributes(symbolTable, accessFlags, signatureIndex, output);
295         AnnotationWriter.putAnnotations(
296                 symbolTable,
297                 lastRuntimeVisibleAnnotation,
298                 lastRuntimeInvisibleAnnotation,
299                 lastRuntimeVisibleTypeAnnotation,
300                 lastRuntimeInvisibleTypeAnnotation,
301                 output);
302         if (firstAttribute != null) {
303             firstAttribute.putAttributes(symbolTable, output);
304         }
305     }
306 
307     /**
308       * Collects the attributes of this field into the given set of attribute prototypes.
309       *
310       * @param attributePrototypes a set of attribute prototypes.
311       */
312     final void collectAttributePrototypes(final Attribute.Set attributePrototypes) {
313         attributePrototypes.addAttributes(firstAttribute);
314     }
315 }
316