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 package jdk.internal.classfile.impl; 26 27 import java.lang.classfile.AccessFlags; 28 import java.lang.classfile.Attribute; 29 import java.lang.classfile.ClassModel; 30 import java.lang.classfile.ClassReader; 31 import java.lang.classfile.FieldElement; 32 import java.lang.classfile.FieldModel; 33 import java.lang.classfile.constantpool.Utf8Entry; 34 import java.lang.reflect.AccessFlag; 35 import java.util.List; 36 import java.util.Optional; 37 import java.util.function.Consumer; 38 39 public final class FieldImpl 40 extends AbstractElement 41 implements FieldModel, WritableField { 42 43 private final ClassReader reader; 44 private final int startPos, endPos, attributesPos; 45 private List<Attribute<?>> attributes; 46 47 public FieldImpl(ClassReader reader, int startPos, int endPos, int attributesPos) { 48 this.reader = reader; 49 this.startPos = startPos; 50 this.endPos = endPos; 51 this.attributesPos = attributesPos; 52 } 53 54 @Override 55 public AccessFlags flags() { 56 return new AccessFlagsImpl(AccessFlag.Location.FIELD, fieldFlags()); 57 } 58 59 @Override 60 public Optional<ClassModel> parent() { 61 if (reader instanceof ClassReaderImpl cri) 62 return Optional.of(cri.getContainedClass()); 63 else 64 return Optional.empty(); 65 } 66 67 @Override 68 public Utf8Entry fieldName() { 69 return reader.readEntry(startPos + 2, Utf8Entry.class); 70 } 71 72 @Override 73 public Utf8Entry fieldType() { 74 return reader.readEntry(startPos + 4, Utf8Entry.class); 75 } 76 77 @Override 78 public int fieldFlags() { 79 return reader.readU2(startPos); 80 } 81 82 @Override 83 public List<Attribute<?>> attributes() { 84 if (attributes == null) { 85 attributes = BoundAttribute.readAttributes(this, reader, attributesPos, reader.customAttributes()); 86 } 87 return attributes; 88 } 89 90 @Override 91 public void writeTo(BufWriterImpl buf) { 92 if (buf.canWriteDirect(reader)) { 93 reader.copyBytesTo(buf, startPos, endPos - startPos); 94 } 95 else { 96 buf.writeU2U2U2(flags().flagsMask(), 97 buf.cpIndex(fieldName()), 98 buf.cpIndex(fieldType())); 99 Util.writeAttributes(buf, attributes()); 100 } 101 } 102 103 // FieldModel 104 105 @Override 106 public void writeTo(DirectClassBuilder builder) { 107 if (builder.canWriteDirect(reader)) { 108 builder.withField(this); 109 } 110 else { 111 builder.withField(fieldName(), fieldType(), Util.writingAll(this)); 112 } 113 } 114 115 @Override 116 public void forEach(Consumer<? super FieldElement> consumer) { 117 consumer.accept(flags()); 118 for (Attribute<?> attr : attributes()) { 119 if (attr instanceof FieldElement e) 120 consumer.accept(e); 121 } 122 } 123 124 @Override 125 public String toString() { 126 return String.format("FieldModel[fieldName=%s, fieldType=%s, flags=%d]", 127 fieldName().stringValue(), fieldType().stringValue(), flags().flagsMask()); 128 } 129 }