< prev index next >

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

Print this page

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



























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










 85     public boolean labelsMatch(LabelContext lc) {
 86         return labelsMatch
 87                 && labelContext instanceof DirectCodeBuilder dcb
 88                 && dcb.original == lc;
 89     }
 90 
 91     @Override
 92     public boolean canWriteDirect(ConstantPool other) {
 93         return constantPool.canWriteDirect(other);
 94     }
 95 
 96     public ClassEntry thisClass() {
 97         return thisClass;
 98     }
 99 
100     public int getMajorVersion() {
101         return majorVersion;
102     }
103 
104     public ClassFileImpl context() {

  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.*;
 42 import static java.lang.classfile.constantpool.PoolEntry.TAG_UTF8;
 43 import static jdk.internal.util.ModifiedUtf.putChar;
 44 import static jdk.internal.util.ModifiedUtf.utfLen;
 45 
 46 public final class BufWriterImpl implements BufWriter {
 47     private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
 48 
 49     private final ConstantPoolBuilder constantPool;
 50     private final ClassFileImpl context;
 51     private LabelContext labelContext;
 52     private WritableField.UnsetField[] strictInstanceFields; // do not modify array contents
 53     private ClassModel lastStrictCheckClass; // buf writer has short life, so do not need weak here
 54     private boolean lastStrictCheckResult;
 55     private boolean labelsMatch;
 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_INIT)) == ACC_STRICT_INIT) {
 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, boolean labelsMatch) {
114         this.labelContext = labelContext;
115         this.labelsMatch = labelsMatch;
116     }
117 
118     public WritableField.UnsetField[] getStrictInstanceFields() {
119         assert strictInstanceFields != null : "should access only after setter call in DirectClassBuilder";
120         return strictInstanceFields;
121     }
122 
123     public void setStrictInstanceFields(WritableField.UnsetField[] strictInstanceFields) {
124         this.strictInstanceFields = strictInstanceFields;
125     }
126 
127 
128     public boolean labelsMatch(LabelContext lc) {
129         return labelsMatch
130                 && labelContext instanceof DirectCodeBuilder dcb
131                 && dcb.original == lc;
132     }
133 
134     @Override
135     public boolean canWriteDirect(ConstantPool other) {
136         return constantPool.canWriteDirect(other);
137     }
138 
139     public ClassEntry thisClass() {
140         return thisClass;
141     }
142 
143     public int getMajorVersion() {
144         return majorVersion;
145     }
146 
147     public ClassFileImpl context() {
< prev index next >