1 /*
2 * Copyright (c) 2022, 2024, 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, Util.Writable {
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, reader.readU2(startPos));
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 List<Attribute<?>> attributes() {
79 if (attributes == null) {
80 attributes = BoundAttribute.readAttributes(this, reader, attributesPos, reader.customAttributes());
81 }
82 return attributes;
83 }
84
85 @Override
86 public void writeTo(BufWriterImpl buf) {
87 if (buf.canWriteDirect(reader)) {
88 reader.copyBytesTo(buf, startPos, endPos - startPos);
89 }
90 else {
91 buf.writeU2U2U2(flags().flagsMask(),
92 buf.cpIndex(fieldName()),
93 buf.cpIndex(fieldType()));
94 Util.writeAttributes(buf, attributes());
95 }
96 }
|
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 }
|