1 /*
  2  * Copyright (c) 2022, 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 
 26 package jdk.internal.classfile.impl;
 27 
 28 import java.lang.classfile.CustomAttribute;
 29 import java.lang.classfile.FieldBuilder;
 30 import java.lang.classfile.FieldElement;
 31 import java.lang.classfile.FieldModel;
 32 import java.lang.classfile.constantpool.Utf8Entry;
 33 import java.util.function.Consumer;
 34 
 35 import static java.util.Objects.requireNonNull;
 36 
 37 public final class DirectFieldBuilder
 38         extends AbstractDirectBuilder<FieldModel>
 39         implements TerminalFieldBuilder, WritableField {
 40     private final Utf8Entry name;
 41     private final Utf8Entry desc;
 42     private int flags;
 43 
 44     public DirectFieldBuilder(SplitConstantPool constantPool,
 45                               ClassFileImpl context,
 46                               Utf8Entry name,
 47                               Utf8Entry type,
 48                               int flags,
 49                               FieldModel original) {
 50         super(constantPool, context);
 51         setOriginal(original);
 52         this.name = requireNonNull(name);
 53         this.desc = requireNonNull(type);
 54         this.flags = flags;
 55     }
 56 
 57     @Override
 58     public FieldBuilder with(FieldElement element) {
 59         if (element instanceof AbstractElement ae) {
 60             ae.writeTo(this);
 61         } else {
 62             writeAttribute((CustomAttribute<?>) requireNonNull(element));
 63         }
 64         return this;
 65     }
 66 
 67     public DirectFieldBuilder run(Consumer<? super FieldBuilder> handler) {
 68         handler.accept(this);
 69         return this;
 70     }
 71 
 72     @Override
 73     public FieldBuilder withFlags(int flags) {
 74         setFlags(flags);
 75         return this;
 76     }
 77 
 78     void setFlags(int flags) {
 79         this.flags = flags;
 80     }
 81 
 82     @Override
 83     public void writeTo(BufWriterImpl buf) {
 84         buf.writeU2U2U2(flags, buf.cpIndex(name), buf.cpIndex(desc));
 85         attributes.writeTo(buf);
 86     }
 87 
 88     // These values must only be accessed after the field is definitely configured
 89     @Override
 90     public Utf8Entry fieldName() {
 91         return name;
 92     }
 93 
 94     @Override
 95     public Utf8Entry fieldType() {
 96         return desc;
 97     }
 98 
 99     @Override
100     public int fieldFlags() {
101         return flags;
102     }
103 }