From ef5eb42e40bfd979df5b4acd6a77e71134f71a15 Mon Sep 17 00:00:00 2001 From: Aleksey Shipilev Date: Tue, 23 Feb 2016 17:55:28 +0300 Subject: [PATCH] 8150180: String.value contents should be trusted Reviewed-by: vlivanov, redestad, jrose, twisti --- .../share/classes/java/lang/String.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/jdk/src/java.base/share/classes/java/lang/String.java b/jdk/src/java.base/share/classes/java/lang/String.java index 86ec043d5b1..85d22cbc1ec 100644 --- a/jdk/src/java.base/share/classes/java/lang/String.java +++ b/jdk/src/java.base/share/classes/java/lang/String.java @@ -42,6 +42,7 @@ import java.util.regex.PatternSyntaxException; import java.util.stream.IntStream; import java.util.stream.StreamSupport; import jdk.internal.HotSpotIntrinsicCandidate; +import jdk.internal.vm.annotation.Stable; /** * The {@code String} class represents character strings. All @@ -119,7 +120,18 @@ import jdk.internal.HotSpotIntrinsicCandidate; public final class String implements java.io.Serializable, Comparable, CharSequence { - /** The value is used for character storage. */ + /** + * The value is used for character storage. + * + * @implNote This field is trusted by the VM, and is a subject to + * constant folding if String instance is constant. Overwriting this + * field after construction will cause problems. + * + * Additionally, it is marked with {@link Stable} to trust the contents + * of the array. No other facility in JDK provides this functionality (yet). + * {@link Stable} is safe here, because value is never null. + */ + @Stable private final byte[] value; /** @@ -129,6 +141,9 @@ public final class String * LATIN1 * UTF16 * + * @implNote This field is trusted by the VM, and is a subject to + * constant folding if String instance is constant. Overwriting this + * field after construction will cause problems. */ private final byte coder;