< prev index next >

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

Print this page

  1 /*
  2  * Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2024, Alibaba Group Holding Limited. All Rights Reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.  Oracle designates this
  9  * particular file as subject to the "Classpath" exception as provided
 10  * by Oracle in the LICENSE file that accompanied this code.
 11  *
 12  * This code is distributed in the hope that it will be useful, but WITHOUT
 13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 15  * version 2 for more details (a copy is included in the LICENSE file that
 16  * accompanied this code).
 17  *
 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 package jdk.internal.classfile.impl;
 27 
 28 import java.lang.classfile.BufWriter;

 29 import java.lang.classfile.constantpool.ClassEntry;
 30 import java.lang.classfile.constantpool.ConstantPool;
 31 import java.lang.classfile.constantpool.ConstantPoolBuilder;
 32 import java.lang.classfile.constantpool.PoolEntry;
 33 import java.util.Arrays;

 34 
 35 import jdk.internal.access.JavaLangAccess;
 36 import jdk.internal.access.SharedSecrets;
 37 import jdk.internal.vm.annotation.ForceInline;
 38 


 39 import static java.lang.classfile.constantpool.PoolEntry.TAG_UTF8;
 40 import static jdk.internal.util.ModifiedUtf.putChar;
 41 import static jdk.internal.util.ModifiedUtf.utfLen;
 42 
 43 public final class BufWriterImpl implements BufWriter {
 44     private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
 45 
 46     private final ConstantPoolBuilder constantPool;
 47     private final ClassFileImpl context;
 48     private LabelContext labelContext;



 49     private final ClassEntry thisClass;
 50     private final int majorVersion;
 51     byte[] elems;
 52     int offset = 0;
 53 
 54     public BufWriterImpl(ConstantPoolBuilder constantPool, ClassFileImpl context) {
 55         this(constantPool, context, 64, null, 0);
 56     }
 57 
 58     public BufWriterImpl(ConstantPoolBuilder constantPool, ClassFileImpl context, int initialSize) {
 59         this(constantPool, context, initialSize, null, 0);
 60     }
 61 
 62     public BufWriterImpl(ConstantPoolBuilder constantPool, ClassFileImpl context, int initialSize, ClassEntry thisClass, int majorVersion) {
 63         this.constantPool = constantPool;
 64         this.context = context;
 65         elems = new byte[initialSize];
 66         this.thisClass = thisClass;
 67         this.majorVersion = majorVersion;
 68     }
 69 



























 70     @Override
 71     public ConstantPoolBuilder constantPool() {
 72         return constantPool;
 73     }
 74 
 75     public LabelContext labelContext() {
 76         return labelContext;
 77     }
 78 
 79     public void setLabelContext(LabelContext labelContext) {
 80         this.labelContext = labelContext;
 81     }










 82     @Override
 83     public boolean canWriteDirect(ConstantPool other) {
 84         return constantPool.canWriteDirect(other);
 85     }
 86 
 87     public ClassEntry thisClass() {
 88         return thisClass;
 89     }
 90 
 91     public int getMajorVersion() {
 92         return majorVersion;
 93     }
 94 
 95     public ClassFileImpl context() {
 96         return context;
 97     }
 98 
 99     @Override
100     public void writeU1(int x) {
101         reserveSpace(1);

  1 /*
  2  * Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2024, Alibaba Group Holding Limited. All Rights Reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.  Oracle designates this
  9  * particular file as subject to the "Classpath" exception as provided
 10  * by Oracle in the LICENSE file that accompanied this code.
 11  *
 12  * This code is distributed in the hope that it will be useful, but WITHOUT
 13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 15  * version 2 for more details (a copy is included in the LICENSE file that
 16  * accompanied this code).
 17  *
 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 package jdk.internal.classfile.impl;
 27 
 28 import java.lang.classfile.BufWriter;
 29 import java.lang.classfile.ClassModel;
 30 import java.lang.classfile.constantpool.ClassEntry;
 31 import java.lang.classfile.constantpool.ConstantPool;
 32 import java.lang.classfile.constantpool.ConstantPoolBuilder;
 33 import java.lang.classfile.constantpool.PoolEntry;
 34 import java.util.Arrays;
 35 import java.util.HashSet;
 36 
 37 import jdk.internal.access.JavaLangAccess;
 38 import jdk.internal.access.SharedSecrets;
 39 import jdk.internal.vm.annotation.ForceInline;
 40 
 41 import static java.lang.classfile.ClassFile.ACC_STATIC;
 42 import static java.lang.classfile.ClassFile.ACC_STRICT;
 43 import static java.lang.classfile.constantpool.PoolEntry.TAG_UTF8;
 44 import static jdk.internal.util.ModifiedUtf.putChar;
 45 import static jdk.internal.util.ModifiedUtf.utfLen;
 46 
 47 public final class BufWriterImpl implements BufWriter {
 48     private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
 49 
 50     private final ConstantPoolBuilder constantPool;
 51     private final ClassFileImpl context;
 52     private LabelContext labelContext;
 53     private WritableField.UnsetField[] strictInstanceFields; // do not modify array contents
 54     private ClassModel lastStrictCheckClass; // buf writer has short life, so do not need weak here
 55     private boolean lastStrictCheckResult;
 56     private final ClassEntry thisClass;
 57     private final int majorVersion;
 58     byte[] elems;
 59     int offset = 0;
 60 
 61     public BufWriterImpl(ConstantPoolBuilder constantPool, ClassFileImpl context) {
 62         this(constantPool, context, 64, null, 0);
 63     }
 64 
 65     public BufWriterImpl(ConstantPoolBuilder constantPool, ClassFileImpl context, int initialSize) {
 66         this(constantPool, context, initialSize, null, 0);
 67     }
 68 
 69     public BufWriterImpl(ConstantPoolBuilder constantPool, ClassFileImpl context, int initialSize, ClassEntry thisClass, int majorVersion) {
 70         this.constantPool = constantPool;
 71         this.context = context;
 72         elems = new byte[initialSize];
 73         this.thisClass = thisClass;
 74         this.majorVersion = majorVersion;
 75     }
 76 
 77     public boolean strictFieldsMatch(ClassModel cm) {
 78         // We have a cache because this check will be called multiple times
 79         // if a MethodModel is sent wholesale
 80         if (lastStrictCheckClass == cm) {
 81             return lastStrictCheckResult;
 82         }
 83 
 84         var result = doStrictFieldsMatchCheck(cm);
 85         lastStrictCheckClass = cm;
 86         lastStrictCheckResult = result;
 87         return result;
 88     }
 89 
 90     private boolean doStrictFieldsMatchCheck(ClassModel cm) {
 91         // TODO only check for preview class files?
 92         // UTF8 Entry can be used as equality objects
 93         var checks = new HashSet<>(Arrays.asList(getStrictInstanceFields()));
 94         for (var f : cm.fields()) {
 95             if ((f.flags().flagsMask() & (ACC_STATIC | ACC_STRICT)) == ACC_STRICT) {
 96                 if (!checks.remove(new WritableField.UnsetField(f.fieldName(), f.fieldType()))) {
 97                     return false; // Field mismatch!
 98                 }
 99             }
100         }
101         return checks.isEmpty();
102     }
103 
104     @Override
105     public ConstantPoolBuilder constantPool() {
106         return constantPool;
107     }
108 
109     public LabelContext labelContext() {
110         return labelContext;
111     }
112 
113     public void setLabelContext(LabelContext labelContext) {
114         this.labelContext = labelContext;
115     }
116 
117     public WritableField.UnsetField[] getStrictInstanceFields() {
118         assert strictInstanceFields != null : "should access only after setter call in DirectClassBuilder";
119         return strictInstanceFields;
120     }
121 
122     public void setStrictInstanceFields(WritableField.UnsetField[] strictInstanceFields) {
123         this.strictInstanceFields = strictInstanceFields;
124     }
125 
126     @Override
127     public boolean canWriteDirect(ConstantPool other) {
128         return constantPool.canWriteDirect(other);
129     }
130 
131     public ClassEntry thisClass() {
132         return thisClass;
133     }
134 
135     public int getMajorVersion() {
136         return majorVersion;
137     }
138 
139     public ClassFileImpl context() {
140         return context;
141     }
142 
143     @Override
144     public void writeU1(int x) {
145         reserveSpace(1);
< prev index next >