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.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 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)) == ACC_STRICT) {
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() {
|