mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 14:54:52 +02:00
8257596: Clarify trusted final fields for record classes
Reviewed-by: hseigel, chegar, psandoz
This commit is contained in:
parent
b1afed7501
commit
2001da3dd4
4 changed files with 44 additions and 29 deletions
|
@ -739,6 +739,12 @@ void InstanceKlass::deallocate_contents(ClassLoaderData* loader_data) {
|
|||
}
|
||||
}
|
||||
|
||||
bool InstanceKlass::is_record() const {
|
||||
return _record_components != NULL &&
|
||||
is_final() &&
|
||||
java_super() == SystemDictionary::Record_klass();
|
||||
}
|
||||
|
||||
bool InstanceKlass::is_sealed() const {
|
||||
return _permitted_subclasses != NULL &&
|
||||
_permitted_subclasses != Universe::the_empty_short_array();
|
||||
|
|
|
@ -473,7 +473,7 @@ class InstanceKlass: public Klass {
|
|||
void set_record_components(Array<RecordComponent*>* record_components) {
|
||||
_record_components = record_components;
|
||||
}
|
||||
bool is_record() const { return _record_components != NULL; }
|
||||
bool is_record() const;
|
||||
|
||||
// permitted subclasses
|
||||
Array<u2>* permitted_subclasses() const { return _permitted_subclasses; }
|
||||
|
|
|
@ -1851,6 +1851,9 @@ JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredFields(JNIEnv *env, jclass ofClass,
|
|||
}
|
||||
JVM_END
|
||||
|
||||
|
||||
// A class is a record if and only if it is final and a direct subclass of
|
||||
// java.lang.Record and has a Record attribute; otherwise, it is not a record.
|
||||
JVM_ENTRY(jboolean, JVM_IsRecord(JNIEnv *env, jclass cls))
|
||||
{
|
||||
JVMWrapper("JVM_IsRecord");
|
||||
|
@ -1864,6 +1867,11 @@ JVM_ENTRY(jboolean, JVM_IsRecord(JNIEnv *env, jclass cls))
|
|||
}
|
||||
JVM_END
|
||||
|
||||
// Returns an array containing the components of the Record attribute,
|
||||
// or NULL if the attribute is not present.
|
||||
//
|
||||
// Note that this function returns the components of the Record attribute
|
||||
// even if the class is not a record.
|
||||
JVM_ENTRY(jobjectArray, JVM_GetRecordComponents(JNIEnv* env, jclass ofClass))
|
||||
{
|
||||
JVMWrapper("JVM_GetRecordComponents");
|
||||
|
@ -1871,10 +1879,8 @@ JVM_ENTRY(jobjectArray, JVM_GetRecordComponents(JNIEnv* env, jclass ofClass))
|
|||
assert(c->is_instance_klass(), "must be");
|
||||
InstanceKlass* ik = InstanceKlass::cast(c);
|
||||
|
||||
if (ik->is_record()) {
|
||||
Array<RecordComponent*>* components = ik->record_components();
|
||||
assert(components != NULL, "components should not be NULL");
|
||||
{
|
||||
if (components != NULL) {
|
||||
JvmtiVMObjectAllocEventCollector oam;
|
||||
constantPoolHandle cp(THREAD, ik->constants());
|
||||
int length = components->length();
|
||||
|
@ -1891,11 +1897,8 @@ JVM_ENTRY(jobjectArray, JVM_GetRecordComponents(JNIEnv* env, jclass ofClass))
|
|||
}
|
||||
return (jobjectArray)JNIHandles::make_local(THREAD, components_h());
|
||||
}
|
||||
}
|
||||
|
||||
// Return empty array if ofClass is not a record.
|
||||
objArrayOop result = oopFactory::new_objArray(SystemDictionary::RecordComponent_klass(), 0, CHECK_NULL);
|
||||
return (jobjectArray)JNIHandles::make_local(THREAD, result);
|
||||
return NULL;
|
||||
}
|
||||
JVM_END
|
||||
|
||||
|
|
|
@ -2383,11 +2383,7 @@ public final class Class<T> implements java.io.Serializable,
|
|||
if (!isRecord()) {
|
||||
return null;
|
||||
}
|
||||
RecordComponent[] recordComponents = getRecordComponents0();
|
||||
if (recordComponents == null) {
|
||||
return new RecordComponent[0];
|
||||
}
|
||||
return recordComponents;
|
||||
return getRecordComponents0();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -3578,6 +3574,14 @@ public final class Class<T> implements java.io.Serializable,
|
|||
private native Method[] getDeclaredMethods0(boolean publicOnly);
|
||||
private native Constructor<T>[] getDeclaredConstructors0(boolean publicOnly);
|
||||
private native Class<?>[] getDeclaredClasses0();
|
||||
|
||||
/*
|
||||
* Returns an array containing the components of the Record attribute,
|
||||
* or null if the attribute is not present.
|
||||
*
|
||||
* Note that this method returns non-null array on a class with
|
||||
* the Record attribute even if this class is not a record.
|
||||
*/
|
||||
private native RecordComponent[] getRecordComponents0();
|
||||
private native boolean isRecord0();
|
||||
|
||||
|
@ -3706,6 +3710,8 @@ public final class Class<T> implements java.io.Serializable,
|
|||
* @since 16
|
||||
*/
|
||||
public boolean isRecord() {
|
||||
// this superclass and final modifier check is not strictly necessary
|
||||
// they are intrinsified and serve as a fast-path check
|
||||
return getSuperclass() == java.lang.Record.class &&
|
||||
(this.getModifiers() & Modifier.FINAL) != 0 &&
|
||||
isRecord0();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue