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.lang.runtime.ExactConversionsSupport;
34 import java.util.Arrays;
35
36 import jdk.internal.access.JavaLangAccess;
37 import jdk.internal.access.SharedSecrets;
38 import jdk.internal.vm.annotation.ForceInline;
39
40 import static java.lang.classfile.constantpool.PoolEntry.TAG_UTF8;
41 import static jdk.internal.util.ModifiedUtf.putChar;
42 import static jdk.internal.util.ModifiedUtf.utfLen;
43
44 public final class BufWriterImpl implements BufWriter {
45 private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
46
47 private final ConstantPoolBuilder constantPool;
48 private final ClassFileImpl context;
49 private LabelContext labelContext;
50 private boolean labelsMatch;
51 private final ClassEntry thisClass;
52 private final int majorVersion;
53 byte[] elems;
54 int offset = 0;
55
56 public BufWriterImpl(ConstantPoolBuilder constantPool, ClassFileImpl context) {
57 this(constantPool, context, 64, null, 0);
58 }
59
60 public BufWriterImpl(ConstantPoolBuilder constantPool, ClassFileImpl context, int initialSize) {
61 this(constantPool, context, initialSize, null, 0);
62 }
63
64 public BufWriterImpl(ConstantPoolBuilder constantPool, ClassFileImpl context, int initialSize, ClassEntry thisClass, int majorVersion) {
65 this.constantPool = constantPool;
66 this.context = context;
67 elems = new byte[initialSize];
68 this.thisClass = thisClass;
69 this.majorVersion = majorVersion;
70 }
71
72 @Override
73 public ConstantPoolBuilder constantPool() {
74 return constantPool;
75 }
76
77 public LabelContext labelContext() {
78 return labelContext;
79 }
80
81 public void setLabelContext(LabelContext labelContext, boolean labelsMatch) {
82 this.labelContext = labelContext;
83 this.labelsMatch = labelsMatch;
84 }
85
86 public boolean labelsMatch(LabelContext lc) {
87 return labelsMatch
88 && labelContext instanceof DirectCodeBuilder dcb
89 && dcb.original == lc;
90 }
91
92 @Override
93 public boolean canWriteDirect(ConstantPool other) {
94 return constantPool.canWriteDirect(other);
95 }
96
97 public ClassEntry thisClass() {
98 return thisClass;
99 }
100
101 public int getMajorVersion() {
102 return majorVersion;
103 }
104
105 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.lang.runtime.ExactConversionsSupport;
35 import java.util.Arrays;
36 import java.util.HashSet;
37
38 import jdk.internal.access.JavaLangAccess;
39 import jdk.internal.access.SharedSecrets;
40 import jdk.internal.vm.annotation.ForceInline;
41
42 import static java.lang.classfile.ClassFile.*;
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 boolean labelsMatch;
57 private final ClassEntry thisClass;
58 private final int majorVersion;
59 byte[] elems;
60 int offset = 0;
61
62 public BufWriterImpl(ConstantPoolBuilder constantPool, ClassFileImpl context) {
63 this(constantPool, context, 64, null, 0);
64 }
65
66 public BufWriterImpl(ConstantPoolBuilder constantPool, ClassFileImpl context, int initialSize) {
67 this(constantPool, context, initialSize, null, 0);
68 }
69
70 public BufWriterImpl(ConstantPoolBuilder constantPool, ClassFileImpl context, int initialSize, ClassEntry thisClass, int majorVersion) {
71 this.constantPool = constantPool;
72 this.context = context;
73 elems = new byte[initialSize];
74 this.thisClass = thisClass;
75 this.majorVersion = majorVersion;
76 }
77
78 public boolean strictFieldsMatch(ClassModel cm) {
79 // We have a cache because this check will be called multiple times
80 // if a MethodModel is sent wholesale
81 if (lastStrictCheckClass == cm) {
82 return lastStrictCheckResult;
83 }
84
85 var result = doStrictFieldsMatchCheck(cm);
86 lastStrictCheckClass = cm;
87 lastStrictCheckResult = result;
88 return result;
89 }
90
91 private boolean doStrictFieldsMatchCheck(ClassModel cm) {
92 // TODO only check for preview class files?
93 // UTF8 Entry can be used as equality objects
94 var checks = new HashSet<>(Arrays.asList(getStrictInstanceFields()));
95 for (var f : cm.fields()) {
96 if ((f.flags().flagsMask() & (ACC_STATIC | ACC_STRICT_INIT)) == ACC_STRICT_INIT) {
97 if (!checks.remove(new WritableField.UnsetField(f.fieldName(), f.fieldType()))) {
98 return false; // Field mismatch!
99 }
100 }
101 }
102 return checks.isEmpty();
103 }
104
105 @Override
106 public ConstantPoolBuilder constantPool() {
107 return constantPool;
108 }
109
110 public LabelContext labelContext() {
111 return labelContext;
112 }
113
114 public void setLabelContext(LabelContext labelContext, boolean labelsMatch) {
115 this.labelContext = labelContext;
116 this.labelsMatch = labelsMatch;
117 }
118
119 public WritableField.UnsetField[] getStrictInstanceFields() {
120 assert strictInstanceFields != null : "should access only after setter call in DirectClassBuilder";
121 return strictInstanceFields;
122 }
123
124 public void setStrictInstanceFields(WritableField.UnsetField[] strictInstanceFields) {
125 this.strictInstanceFields = strictInstanceFields;
126 }
127
128
129 public boolean labelsMatch(LabelContext lc) {
130 return labelsMatch
131 && labelContext instanceof DirectCodeBuilder dcb
132 && dcb.original == lc;
133 }
134
135 @Override
136 public boolean canWriteDirect(ConstantPool other) {
137 return constantPool.canWriteDirect(other);
138 }
139
140 public ClassEntry thisClass() {
141 return thisClass;
142 }
143
144 public int getMajorVersion() {
145 return majorVersion;
146 }
147
148 public ClassFileImpl context() {
|