1 /* 2 * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package jdk.internal.classfile.impl; 26 27 import java.lang.classfile.constantpool.ConstantPool; 28 import java.lang.classfile.constantpool.ConstantPoolBuilder; 29 import java.lang.classfile.constantpool.Utf8Entry; 30 import java.util.Arrays; 31 32 import static java.lang.classfile.ClassFile.ACC_STATIC; 33 import static java.lang.classfile.ClassFile.ACC_STRICT; 34 35 /** 36 * An interface to obtain field properties for direct class builders. 37 * Required to filter strict instance fields for stack map generation. 38 * Public for benchmark access. 39 */ 40 public sealed interface WritableField extends Util.Writable 41 permits FieldImpl, DirectFieldBuilder { 42 Utf8Entry fieldName(); 43 Utf8Entry fieldType(); 44 int fieldFlags(); 45 46 static WritableField.UnsetField[] filterStrictInstanceFields(ConstantPoolBuilder cpb, WritableField[] array, int count) { 47 // assume there's no toctou for trusted incoming array 48 int size = 0; 49 for (int i = 0; i < count; i++) { 50 var field = array[i]; 51 if ((field.fieldFlags() & (ACC_STATIC | ACC_STRICT)) == ACC_STRICT) { 52 size++; 53 } 54 } 55 if (size == 0) 56 return UnsetField.EMPTY_ARRAY; 57 UnsetField[] ret = new UnsetField[size]; 58 int j = 0; 59 for (int i = 0; i < count; i++) { 60 var field = array[i]; 61 if ((field.fieldFlags() & (ACC_STATIC | ACC_STRICT)) == ACC_STRICT) { 62 ret[j++] = new UnsetField(AbstractPoolEntry.maybeClone(cpb, field.fieldName()), 63 AbstractPoolEntry.maybeClone(cpb, field.fieldType())); 64 } 65 } 66 assert j == size : "toctou: " + j + " != " + size; 67 Arrays.sort(ret); 68 return ret; 69 } 70 71 // The captured information of unset fields, pool entries localized to class writing context 72 // avoid creating NAT until we need to write the fields to stack maps 73 record UnsetField(Utf8Entry name, Utf8Entry type) implements Comparable<UnsetField> { 74 public UnsetField { 75 assert Util.checkConstantPoolsCompatible(name.constantPool(), type.constantPool()); 76 } 77 public static final UnsetField[] EMPTY_ARRAY = new UnsetField[0]; 78 79 public static UnsetField[] copyArray(UnsetField[] incoming, int resultLen) { 80 assert resultLen <= incoming.length : resultLen + " > " + incoming.length; 81 return resultLen == 0 ? EMPTY_ARRAY : Arrays.copyOf(incoming, resultLen, UnsetField[].class); 82 } 83 84 public static boolean mismatches(UnsetField[] one, int sizeOne, UnsetField[] two, int sizeTwo) { 85 if (sizeOne != sizeTwo) 86 return true; 87 for (int i = 0; i < sizeOne; i++) { 88 if (!one[i].equals(two[i])) { 89 return true; 90 } 91 } 92 return false; 93 } 94 95 // Warning: inconsistent with equals (which uses UTF8 object equality) 96 @Override 97 public int compareTo(UnsetField o) { 98 assert Util.checkConstantPoolsCompatible(name.constantPool(), o.name.constantPool()); 99 var ret = Integer.compare(name.index(), o.name.index()); 100 if (ret != 0) 101 return ret; 102 return Integer.compare(type.index(), o.type.index()); 103 } 104 } 105 }