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.ConstantPoolBuilder;
 28 import java.lang.classfile.constantpool.Utf8Entry;
 29 import java.util.Arrays;
 30 
 31 import static java.lang.classfile.ClassFile.ACC_STATIC;
 32 import static java.lang.classfile.ClassFile.ACC_STRICT;
 33 
 34 /**
 35  * An interface to obtain field properties for direct class builders.
 36  * Required to filter strict instance fields for stack map generation.
 37  * Public for benchmark access.
 38  */
 39 public sealed interface WritableField extends Util.Writable
 40         permits FieldImpl, DirectFieldBuilder {
 41     Utf8Entry fieldName();
 42     Utf8Entry fieldType();
 43     int fieldFlags();
 44 
 45     static WritableField.UnsetField[] filterStrictInstanceFields(ConstantPoolBuilder cpb, WritableField[] array, int count) {
 46         // assume there's no toctou for trusted incoming array
 47         int size = 0;
 48         for (int i = 0; i < count; i++) {
 49             var field = array[i];
 50             if ((field.fieldFlags() & (ACC_STATIC | ACC_STRICT)) == ACC_STRICT) {
 51                 size++;
 52             }
 53         }
 54         if (size == 0)
 55             return UnsetField.EMPTY_ARRAY;
 56         UnsetField[] ret = new UnsetField[size];
 57         int j = 0;
 58         for (int i = 0; i < count; i++) {
 59             var field = array[i];
 60             if ((field.fieldFlags() & (ACC_STATIC | ACC_STRICT)) == ACC_STRICT) {
 61                 ret[j++] = new UnsetField(AbstractPoolEntry.maybeClone(cpb, field.fieldName()),
 62                         AbstractPoolEntry.maybeClone(cpb, field.fieldType()));
 63             }
 64         }
 65         assert j == size : "toctou: " + j + " != " + size;
 66         Arrays.sort(ret);
 67         return ret;
 68     }
 69 
 70     // The captured information of unset fields, pool entries localized to class writing context
 71     // avoid creating NAT until we need to write the fields to stack maps
 72     record UnsetField(Utf8Entry name, Utf8Entry type) implements Comparable<UnsetField> {
 73         public UnsetField {
 74             assert Util.checkConstantPoolsCompatible(name.constantPool(), type.constantPool());
 75         }
 76         public static final UnsetField[] EMPTY_ARRAY = new UnsetField[0];
 77 
 78         public static UnsetField[] copyArray(UnsetField[] incoming, int resultLen) {
 79             assert resultLen <= incoming.length : resultLen + " > " + incoming.length;
 80             return resultLen == 0 ? EMPTY_ARRAY : Arrays.copyOf(incoming, resultLen, UnsetField[].class);
 81         }
 82 
 83         public static boolean matches(UnsetField[] one, int sizeOne, UnsetField[] two, int sizeTwo) {
 84             if (sizeOne != sizeTwo)
 85                 return false;
 86             for (int i = 0; i < sizeOne; i++) {
 87                 if (!one[i].equals(two[i])) {
 88                     return false;
 89                 }
 90             }
 91             return true;
 92         }
 93 
 94         // Warning: inconsistent with equals (which uses UTF8 object equality)
 95         @Override
 96         public int compareTo(UnsetField o) {
 97             assert Util.checkConstantPoolsCompatible(name.constantPool(), o.name.constantPool());
 98             var ret = Integer.compare(name.index(), o.name.index());
 99             if (ret != 0)
100                 return ret;
101             return Integer.compare(type.index(), o.type.index());
102         }
103     }
104 }