I am reading BufferedInputStream's source code in Java.I noticed there's a private Field:BUF_OFFSET.enter image description here
private static final long BUF_OFFSET
= U.objectFieldOffset(BufferedInputStream.class, "buf");
I tried to check it out,then I found the method:objectFieldOffset1.It is a native method.So I download the JDK source code(jdk-17 35) from github.https://github.com/openjdk/jdk/tree/jdk-17+35 Finally I found the jlong find_field_offset method.
I can basically understand the code except the JavaFieldStreamenter image description here
static jlong find_field_offset(jclass clazz, jstring name, TRAPS) {
assert(clazz != NULL, "clazz must not be NULL");
assert(name != NULL, "name must not be NULL");
ResourceMark rm(THREAD);
char *utf_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name));
InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz)));
jint offset = -1;
for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
Symbol *name = fs.name();
if (name->equals(utf_name)) {
offset = fs.offset();
break;
}
}
if (offset < 0) {
THROW_0(vmSymbols::java_lang_InternalError());
}
return field_offset_from_byte_offset(offset);
}
I tried to search in google,but nothing found.I tried to find it in Java offical document,nothing either. Looks like JavaFieldStream is a iterator?
CodePudding user response:
Looks like JavaFieldStream is a iterator?
Correct.
It is declared in "jdk17u/src/hotspot/share/oops/fieldStreams.hpp"
The comments in the file say (for FieldStreamBase):
// The is the base class for iteration over the fields array
// describing the declared fields in the class. Several subclasses
// are provided depending on the kind of iteration required. The
// JavaFieldStream is for iterating over regular Java fields and it
// generally the preferred iterator. InternalFieldStream only
// iterates over fields that have been injected by the JVM.
// AllFieldStream exposes all fields and should only be used in rare
// cases.
Hint: find jdk17u -type f | xargs grep JavaFieldStream | less