< prev index next >

src/java.base/share/classes/jdk/internal/classfile/impl/DirectClassBuilder.java

Print this page

 18  * You should have received a copy of the GNU General Public License version
 19  * 2 along with this work; if not, write to the Free Software Foundation,
 20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 21  *
 22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 23  * or visit www.oracle.com if you need additional information or have any
 24  * questions.
 25  */
 26 
 27 package jdk.internal.classfile.impl;
 28 
 29 import java.lang.classfile.*;
 30 import java.lang.classfile.constantpool.ClassEntry;
 31 import java.lang.classfile.constantpool.Utf8Entry;
 32 import java.lang.constant.ConstantDescs;
 33 import java.util.Arrays;
 34 import java.util.Collections;
 35 import java.util.List;
 36 import java.util.function.Consumer;
 37 

 38 import static java.util.Objects.requireNonNull;
 39 
 40 public final class DirectClassBuilder
 41         extends AbstractDirectBuilder<ClassModel>
 42         implements ClassBuilder {
 43 
 44     /** The value of default class access flags */
 45     static final int DEFAULT_CLASS_FLAGS = ClassFile.ACC_PUBLIC | ClassFile.ACC_SUPER;
 46     static final Util.Writable[] EMPTY_WRITABLE_ARRAY = {};

 47     static final ClassEntry[] EMPTY_CLASS_ENTRY_ARRAY = {};
 48     final ClassEntry thisClassEntry;
 49     private Util.Writable[] fields = EMPTY_WRITABLE_ARRAY;
 50     private Util.Writable[] methods = EMPTY_WRITABLE_ARRAY;
 51     private int fieldsCount = 0;
 52     private int methodsCount = 0;
 53     private ClassEntry superclassEntry;
 54     private List<ClassEntry> interfaceEntries;
 55     private int majorVersion;
 56     private int minorVersion;
 57     private int flags;
 58     private int sizeHint;
 59 
 60     public DirectClassBuilder(SplitConstantPool constantPool,
 61                               ClassFileImpl context,
 62                               ClassEntry thisClass) {
 63         super(constantPool, context);
 64         this.thisClassEntry = AbstractPoolEntry.maybeClone(constantPool, thisClass);
 65         this.flags = DEFAULT_CLASS_FLAGS;
 66         this.superclassEntry = null;
 67         this.interfaceEntries = Collections.emptyList();
 68         this.majorVersion = ClassFile.latestMajorVersion();
 69         this.minorVersion = ClassFile.latestMinorVersion();

112     public ClassBuilder withMethod(Utf8Entry name,
113                                    Utf8Entry descriptor,
114                                    int flags,
115                                    Consumer<? super MethodBuilder> handler) {
116         return withMethod(new DirectMethodBuilder(constantPool, context, name, descriptor, flags, null)
117                                   .run(handler));
118     }
119 
120     @Override
121     public ClassBuilder transformMethod(MethodModel method, MethodTransform transform) {
122         DirectMethodBuilder builder = new DirectMethodBuilder(constantPool, context, method.methodName(),
123                                                               method.methodType(),
124                                                               method.flags().flagsMask(),
125                                                               method);
126         builder.transform(method, transform);
127         return withMethod(builder);
128     }
129 
130     // internal / for use by elements
131 
132     ClassBuilder withField(Util.Writable field) {
133         if (fieldsCount >= fields.length) {
134             int newCapacity = fieldsCount + 8;
135             this.fields = Arrays.copyOf(fields, newCapacity);
136         }
137         fields[fieldsCount++] = field;
138         return this;
139     }
140 
141     ClassBuilder withMethod(Util.Writable method) {
142         if (methodsCount >= methods.length) {
143             int newCapacity = methodsCount + 8;
144             this.methods = Arrays.copyOf(methods, newCapacity);
145         }
146         methods[methodsCount++] = method;
147         return this;
148     }
149 
150     void setSuperclass(ClassEntry superclassEntry) {
151         this.superclassEntry = superclassEntry;
152     }

177         // BSM writers until everything else is written.
178 
179         // Do this early because it might trigger CP activity
180         var constantPool = this.constantPool;
181         ClassEntry superclass = superclassEntry;
182         if (superclass != null)
183             superclass = AbstractPoolEntry.maybeClone(constantPool, superclass);
184         else if ((flags & ClassFile.ACC_MODULE) == 0 && !"java/lang/Object".equals(thisClassEntry.asInternalName()))
185             superclass = constantPool.classEntry(ConstantDescs.CD_Object);
186         int interfaceEntriesSize = interfaceEntries.size();
187         ClassEntry[] ies = interfaceEntriesSize == 0 ? EMPTY_CLASS_ENTRY_ARRAY : buildInterfaceEnties(interfaceEntriesSize);
188 
189         // We maintain two writers, and then we join them at the end
190         int size = sizeHint == 0 ? 256 : sizeHint;
191         BufWriterImpl head = new BufWriterImpl(constantPool, context, size);
192         BufWriterImpl tail = new BufWriterImpl(constantPool, context, size, thisClassEntry, majorVersion);
193 
194         // The tail consists of fields and methods, and attributes
195         // This should trigger all the CP/BSM mutation
196         Util.writeList(tail, fields, fieldsCount);







197         Util.writeList(tail, methods, methodsCount);
198         int attributesOffset = tail.size();
199         attributes.writeTo(tail);
200 
201         // Now we have to append the BSM, if there is one
202         if (constantPool.writeBootstrapMethods(tail)) {
203             // Update attributes count
204             tail.patchU2(attributesOffset, attributes.size() + 1);
205         }
206 
207         // Now we can make the head
208         head.writeInt(ClassFile.MAGIC_NUMBER);
209         head.writeU2U2(minorVersion, majorVersion);
210         constantPool.writeTo(head);
211         head.writeU2U2U2(flags, head.cpIndex(thisClassEntry), head.cpIndexOrZero(superclass));
212         head.writeU2(interfaceEntriesSize);
213         for (int i = 0; i < interfaceEntriesSize; i++) {
214             head.writeIndex(ies[i]);
215         }
216 

 18  * You should have received a copy of the GNU General Public License version
 19  * 2 along with this work; if not, write to the Free Software Foundation,
 20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 21  *
 22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 23  * or visit www.oracle.com if you need additional information or have any
 24  * questions.
 25  */
 26 
 27 package jdk.internal.classfile.impl;
 28 
 29 import java.lang.classfile.*;
 30 import java.lang.classfile.constantpool.ClassEntry;
 31 import java.lang.classfile.constantpool.Utf8Entry;
 32 import java.lang.constant.ConstantDescs;
 33 import java.util.Arrays;
 34 import java.util.Collections;
 35 import java.util.List;
 36 import java.util.function.Consumer;
 37 
 38 import static java.lang.classfile.ClassFile.PREVIEW_MINOR_VERSION;
 39 import static java.util.Objects.requireNonNull;
 40 
 41 public final class DirectClassBuilder
 42         extends AbstractDirectBuilder<ClassModel>
 43         implements ClassBuilder {
 44 
 45     /** The value of default class access flags */
 46     static final int DEFAULT_CLASS_FLAGS = ClassFile.ACC_PUBLIC | ClassFile.ACC_SUPER;
 47     static final Util.Writable[] EMPTY_WRITABLE_ARRAY = {};
 48     static final WritableField[] EMPTY_WRITABLE_FIELD_ARRAY = {};
 49     static final ClassEntry[] EMPTY_CLASS_ENTRY_ARRAY = {};
 50     final ClassEntry thisClassEntry;
 51     private WritableField[] fields = EMPTY_WRITABLE_FIELD_ARRAY;
 52     private Util.Writable[] methods = EMPTY_WRITABLE_ARRAY;
 53     private int fieldsCount = 0;
 54     private int methodsCount = 0;
 55     private ClassEntry superclassEntry;
 56     private List<ClassEntry> interfaceEntries;
 57     private int majorVersion;
 58     private int minorVersion;
 59     private int flags;
 60     private int sizeHint;
 61 
 62     public DirectClassBuilder(SplitConstantPool constantPool,
 63                               ClassFileImpl context,
 64                               ClassEntry thisClass) {
 65         super(constantPool, context);
 66         this.thisClassEntry = AbstractPoolEntry.maybeClone(constantPool, thisClass);
 67         this.flags = DEFAULT_CLASS_FLAGS;
 68         this.superclassEntry = null;
 69         this.interfaceEntries = Collections.emptyList();
 70         this.majorVersion = ClassFile.latestMajorVersion();
 71         this.minorVersion = ClassFile.latestMinorVersion();

114     public ClassBuilder withMethod(Utf8Entry name,
115                                    Utf8Entry descriptor,
116                                    int flags,
117                                    Consumer<? super MethodBuilder> handler) {
118         return withMethod(new DirectMethodBuilder(constantPool, context, name, descriptor, flags, null)
119                                   .run(handler));
120     }
121 
122     @Override
123     public ClassBuilder transformMethod(MethodModel method, MethodTransform transform) {
124         DirectMethodBuilder builder = new DirectMethodBuilder(constantPool, context, method.methodName(),
125                                                               method.methodType(),
126                                                               method.flags().flagsMask(),
127                                                               method);
128         builder.transform(method, transform);
129         return withMethod(builder);
130     }
131 
132     // internal / for use by elements
133 
134     ClassBuilder withField(WritableField field) {
135         if (fieldsCount >= fields.length) {
136             int newCapacity = fieldsCount + 8;
137             this.fields = Arrays.copyOf(fields, newCapacity);
138         }
139         fields[fieldsCount++] = field;
140         return this;
141     }
142 
143     ClassBuilder withMethod(Util.Writable method) {
144         if (methodsCount >= methods.length) {
145             int newCapacity = methodsCount + 8;
146             this.methods = Arrays.copyOf(methods, newCapacity);
147         }
148         methods[methodsCount++] = method;
149         return this;
150     }
151 
152     void setSuperclass(ClassEntry superclassEntry) {
153         this.superclassEntry = superclassEntry;
154     }

179         // BSM writers until everything else is written.
180 
181         // Do this early because it might trigger CP activity
182         var constantPool = this.constantPool;
183         ClassEntry superclass = superclassEntry;
184         if (superclass != null)
185             superclass = AbstractPoolEntry.maybeClone(constantPool, superclass);
186         else if ((flags & ClassFile.ACC_MODULE) == 0 && !"java/lang/Object".equals(thisClassEntry.asInternalName()))
187             superclass = constantPool.classEntry(ConstantDescs.CD_Object);
188         int interfaceEntriesSize = interfaceEntries.size();
189         ClassEntry[] ies = interfaceEntriesSize == 0 ? EMPTY_CLASS_ENTRY_ARRAY : buildInterfaceEnties(interfaceEntriesSize);
190 
191         // We maintain two writers, and then we join them at the end
192         int size = sizeHint == 0 ? 256 : sizeHint;
193         BufWriterImpl head = new BufWriterImpl(constantPool, context, size);
194         BufWriterImpl tail = new BufWriterImpl(constantPool, context, size, thisClassEntry, majorVersion);
195 
196         // The tail consists of fields and methods, and attributes
197         // This should trigger all the CP/BSM mutation
198         Util.writeList(tail, fields, fieldsCount);
199         WritableField.UnsetField[] strictInstanceFields;
200         if (minorVersion == PREVIEW_MINOR_VERSION && majorVersion >= Util.VALUE_OBJECTS_MAJOR) {
201             strictInstanceFields = WritableField.filterStrictInstanceFields(constantPool, fields, fieldsCount);
202         } else {
203             strictInstanceFields = WritableField.UnsetField.EMPTY_ARRAY;
204         }
205         tail.setStrictInstanceFields(strictInstanceFields);
206         Util.writeList(tail, methods, methodsCount);
207         int attributesOffset = tail.size();
208         attributes.writeTo(tail);
209 
210         // Now we have to append the BSM, if there is one
211         if (constantPool.writeBootstrapMethods(tail)) {
212             // Update attributes count
213             tail.patchU2(attributesOffset, attributes.size() + 1);
214         }
215 
216         // Now we can make the head
217         head.writeInt(ClassFile.MAGIC_NUMBER);
218         head.writeU2U2(minorVersion, majorVersion);
219         constantPool.writeTo(head);
220         head.writeU2U2U2(flags, head.cpIndex(thisClassEntry), head.cpIndexOrZero(superclass));
221         head.writeU2(interfaceEntriesSize);
222         for (int i = 0; i < interfaceEntriesSize; i++) {
223             head.writeIndex(ies[i]);
224         }
225 
< prev index next >