mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 15:24:43 +02:00
8247444: Trust final fields in records
Co-authored-by: Christoph Dreis <christoph.dreis@freenet.de> Reviewed-by: jrose, dholmes, forax, coleenp, vlivanov
This commit is contained in:
parent
983e012c9f
commit
f2b191a6e9
26 changed files with 227 additions and 49 deletions
|
@ -177,10 +177,16 @@ public class AccessibleObject implements AnnotatedElement {
|
|||
* to the caller's module. </p>
|
||||
*
|
||||
* <p> This method cannot be used to enable {@linkplain Field#set <em>write</em>}
|
||||
* access to a final field declared in a {@linkplain Class#isHidden() hidden class},
|
||||
* since such fields are not modifiable. The {@code accessible} flag when
|
||||
* {@code true} suppresses Java language access control checks to only
|
||||
* enable {@linkplain Field#get <em>read</em>} access to such fields.
|
||||
* access to a <em>non-modifiable</em> final field. The following fields
|
||||
* are non-modifiable:
|
||||
* <ul>
|
||||
* <li>static final fields declared in any class or interface</li>
|
||||
* <li>final fields declared in a {@linkplain Class#isHidden() hidden class}</li>
|
||||
* <li>final fields declared in a {@linkplain Class#isRecord() record}</li>
|
||||
* </ul>
|
||||
* <p> The {@code accessible} flag when {@code true} suppresses Java language access
|
||||
* control checks to only enable {@linkplain Field#get <em>read</em>} access to
|
||||
* these non-modifiable final fields.
|
||||
*
|
||||
* <p> If there is a security manager, its
|
||||
* {@code checkPermission} method is first called with a
|
||||
|
|
|
@ -72,6 +72,7 @@ class Field extends AccessibleObject implements Member {
|
|||
private String name;
|
||||
private Class<?> type;
|
||||
private int modifiers;
|
||||
private boolean trustedFinal;
|
||||
// Generics and annotations support
|
||||
private transient String signature;
|
||||
// generic info repository; lazily initialized
|
||||
|
@ -119,6 +120,7 @@ class Field extends AccessibleObject implements Member {
|
|||
String name,
|
||||
Class<?> type,
|
||||
int modifiers,
|
||||
boolean trustedFinal,
|
||||
int slot,
|
||||
String signature,
|
||||
byte[] annotations)
|
||||
|
@ -127,6 +129,7 @@ class Field extends AccessibleObject implements Member {
|
|||
this.name = name;
|
||||
this.type = type;
|
||||
this.modifiers = modifiers;
|
||||
this.trustedFinal = trustedFinal;
|
||||
this.slot = slot;
|
||||
this.signature = signature;
|
||||
this.annotations = annotations;
|
||||
|
@ -148,7 +151,7 @@ class Field extends AccessibleObject implements Member {
|
|||
if (this.root != null)
|
||||
throw new IllegalArgumentException("Can not copy a non-root Field");
|
||||
|
||||
Field res = new Field(clazz, name, type, modifiers, slot, signature, annotations);
|
||||
Field res = new Field(clazz, name, type, modifiers, trustedFinal, slot, signature, annotations);
|
||||
res.root = this;
|
||||
// Might as well eagerly propagate this if already present
|
||||
res.fieldAccessor = fieldAccessor;
|
||||
|
@ -728,7 +731,9 @@ class Field extends AccessibleObject implements Member {
|
|||
* this {@code Field} object;</li>
|
||||
* <li>the field is non-static; and</li>
|
||||
* <li>the field's declaring class is not a {@linkplain Class#isHidden()
|
||||
* hidden class}.</li>
|
||||
* hidden class}; and</li>
|
||||
* <li>the field's declaring class is not a {@linkplain Class#isRecord()
|
||||
* record class}.</li>
|
||||
* </ul>
|
||||
* If any of the above checks is not met, this method throws an
|
||||
* {@code IllegalAccessException}.
|
||||
|
@ -1145,10 +1150,14 @@ class Field extends AccessibleObject implements Member {
|
|||
}
|
||||
|
||||
@Override
|
||||
Field getRoot() {
|
||||
/* package-private */ Field getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
/* package-private */ boolean isTrustedFinal() {
|
||||
return trustedFinal;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2001, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -118,6 +118,10 @@ class ReflectAccess implements jdk.internal.access.JavaLangReflectAccess {
|
|||
return (T) obj.getRoot();
|
||||
}
|
||||
|
||||
public boolean isTrustedFinalField(Field f) {
|
||||
return f.isTrustedFinal();
|
||||
}
|
||||
|
||||
public <T> T newInstance(Constructor<T> ctor, Object[] args, Class<?> caller)
|
||||
throws IllegalAccessException, InstantiationException, InvocationTargetException
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue