< prev index next >

src/java.base/share/classes/java/lang/String.java

Print this page
rev 52786 : 8210031: implementation for JVM Constants API


  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 
  26 package java.lang;
  27 
  28 import java.io.ObjectStreamField;
  29 import java.io.UnsupportedEncodingException;
  30 import java.lang.annotation.Native;



  31 import java.nio.charset.Charset;
  32 import java.util.ArrayList;
  33 import java.util.Arrays;
  34 import java.util.Comparator;
  35 import java.util.Formatter;
  36 import java.util.Locale;
  37 import java.util.Objects;

  38 import java.util.Spliterator;
  39 import java.util.StringJoiner;
  40 import java.util.function.Function;
  41 import java.util.regex.Matcher;
  42 import java.util.regex.Pattern;
  43 import java.util.regex.PatternSyntaxException;
  44 import java.util.stream.Collectors;
  45 import java.util.stream.IntStream;
  46 import java.util.stream.Stream;
  47 import java.util.stream.StreamSupport;
  48 import jdk.internal.HotSpotIntrinsicCandidate;
  49 import jdk.internal.vm.annotation.Stable;
  50 
  51 import static java.util.function.Predicate.not;
  52 
  53 /**
  54  * The {@code String} class represents character strings. All
  55  * string literals in Java programs, such as {@code "abc"}, are
  56  * implemented as instances of this class.
  57  * <p>


 110  * the discretion of a Java compiler, as long as the compiler ultimately conforms
 111  * to <i>The Java&trade; Language Specification</i>. For example, the {@code javac} compiler
 112  * may implement the operator with {@code StringBuffer}, {@code StringBuilder},
 113  * or {@code java.lang.invoke.StringConcatFactory} depending on the JDK version. The
 114  * implementation of string conversion is typically through the method {@code toString},
 115  * defined by {@code Object} and inherited by all classes in Java.
 116  *
 117  * @author  Lee Boynton
 118  * @author  Arthur van Hoff
 119  * @author  Martin Buchholz
 120  * @author  Ulf Zibis
 121  * @see     java.lang.Object#toString()
 122  * @see     java.lang.StringBuffer
 123  * @see     java.lang.StringBuilder
 124  * @see     java.nio.charset.Charset
 125  * @since   1.0
 126  * @jls     15.18.1 String Concatenation Operator +
 127  */
 128 
 129 public final class String
 130     implements java.io.Serializable, Comparable<String>, CharSequence {

 131 
 132     /**
 133      * The value is used for character storage.
 134      *
 135      * @implNote This field is trusted by the VM, and is a subject to
 136      * constant folding if String instance is constant. Overwriting this
 137      * field after construction will cause problems.
 138      *
 139      * Additionally, it is marked with {@link Stable} to trust the contents
 140      * of the array. No other facility in JDK provides this functionality (yet).
 141      * {@link Stable} is safe here, because value is never null.
 142      */
 143     @Stable
 144     private final byte[] value;
 145 
 146     /**
 147      * The identifier of the encoding used to encode the bytes in
 148      * {@code value}. The supported values in this implementation are
 149      *
 150      * LATIN1


3521      *
3522      * @param   codePoint a {@code codePoint}.
3523      * @return  a string of length {@code 1} or {@code 2} containing
3524      *          as its single character the argument {@code codePoint}.
3525      * @throws IllegalArgumentException if the specified
3526      *          {@code codePoint} is not a {@linkplain Character#isValidCodePoint
3527      *          valid Unicode code point}.
3528      */
3529     static String valueOfCodePoint(int codePoint) {
3530         if (COMPACT_STRINGS && StringLatin1.canEncode(codePoint)) {
3531             return new String(StringLatin1.toBytes((char)codePoint), LATIN1);
3532         } else if (Character.isBmpCodePoint(codePoint)) {
3533             return new String(StringUTF16.toBytes((char)codePoint), UTF16);
3534         } else if (Character.isSupplementaryCodePoint(codePoint)) {
3535             return new String(StringUTF16.toBytesSupplementary(codePoint), UTF16);
3536         }
3537 
3538         throw new IllegalArgumentException(
3539             format("Not a valid Unicode code point: 0x%X", codePoint));
3540     }


























3541 }


  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 
  26 package java.lang;
  27 
  28 import java.io.ObjectStreamField;
  29 import java.io.UnsupportedEncodingException;
  30 import java.lang.annotation.Native;
  31 import java.lang.invoke.MethodHandles;
  32 import java.lang.constant.Constable;
  33 import java.lang.constant.ConstantDesc;
  34 import java.nio.charset.Charset;
  35 import java.util.ArrayList;
  36 import java.util.Arrays;
  37 import java.util.Comparator;
  38 import java.util.Formatter;
  39 import java.util.Locale;
  40 import java.util.Objects;
  41 import java.util.Optional;
  42 import java.util.Spliterator;
  43 import java.util.StringJoiner;
  44 import java.util.function.Function;
  45 import java.util.regex.Matcher;
  46 import java.util.regex.Pattern;
  47 import java.util.regex.PatternSyntaxException;
  48 import java.util.stream.Collectors;
  49 import java.util.stream.IntStream;
  50 import java.util.stream.Stream;
  51 import java.util.stream.StreamSupport;
  52 import jdk.internal.HotSpotIntrinsicCandidate;
  53 import jdk.internal.vm.annotation.Stable;
  54 
  55 import static java.util.function.Predicate.not;
  56 
  57 /**
  58  * The {@code String} class represents character strings. All
  59  * string literals in Java programs, such as {@code "abc"}, are
  60  * implemented as instances of this class.
  61  * <p>


 114  * the discretion of a Java compiler, as long as the compiler ultimately conforms
 115  * to <i>The Java&trade; Language Specification</i>. For example, the {@code javac} compiler
 116  * may implement the operator with {@code StringBuffer}, {@code StringBuilder},
 117  * or {@code java.lang.invoke.StringConcatFactory} depending on the JDK version. The
 118  * implementation of string conversion is typically through the method {@code toString},
 119  * defined by {@code Object} and inherited by all classes in Java.
 120  *
 121  * @author  Lee Boynton
 122  * @author  Arthur van Hoff
 123  * @author  Martin Buchholz
 124  * @author  Ulf Zibis
 125  * @see     java.lang.Object#toString()
 126  * @see     java.lang.StringBuffer
 127  * @see     java.lang.StringBuilder
 128  * @see     java.nio.charset.Charset
 129  * @since   1.0
 130  * @jls     15.18.1 String Concatenation Operator +
 131  */
 132 
 133 public final class String
 134     implements java.io.Serializable, Comparable<String>, CharSequence,
 135                ConstantDesc, Constable {
 136 
 137     /**
 138      * The value is used for character storage.
 139      *
 140      * @implNote This field is trusted by the VM, and is a subject to
 141      * constant folding if String instance is constant. Overwriting this
 142      * field after construction will cause problems.
 143      *
 144      * Additionally, it is marked with {@link Stable} to trust the contents
 145      * of the array. No other facility in JDK provides this functionality (yet).
 146      * {@link Stable} is safe here, because value is never null.
 147      */
 148     @Stable
 149     private final byte[] value;
 150 
 151     /**
 152      * The identifier of the encoding used to encode the bytes in
 153      * {@code value}. The supported values in this implementation are
 154      *
 155      * LATIN1


3526      *
3527      * @param   codePoint a {@code codePoint}.
3528      * @return  a string of length {@code 1} or {@code 2} containing
3529      *          as its single character the argument {@code codePoint}.
3530      * @throws IllegalArgumentException if the specified
3531      *          {@code codePoint} is not a {@linkplain Character#isValidCodePoint
3532      *          valid Unicode code point}.
3533      */
3534     static String valueOfCodePoint(int codePoint) {
3535         if (COMPACT_STRINGS && StringLatin1.canEncode(codePoint)) {
3536             return new String(StringLatin1.toBytes((char)codePoint), LATIN1);
3537         } else if (Character.isBmpCodePoint(codePoint)) {
3538             return new String(StringUTF16.toBytes((char)codePoint), UTF16);
3539         } else if (Character.isSupplementaryCodePoint(codePoint)) {
3540             return new String(StringUTF16.toBytesSupplementary(codePoint), UTF16);
3541         }
3542 
3543         throw new IllegalArgumentException(
3544             format("Not a valid Unicode code point: 0x%X", codePoint));
3545     }
3546 
3547     /**
3548      * Returns a nominal descriptor for this instance, which is the instance
3549      * itself.
3550      *
3551      * @return an {@link Optional} describing the {@linkplain String} instance
3552      * @since 12
3553      */
3554     @Override
3555     public Optional<String> describeConstable() {
3556         return Optional.of(this);
3557     }
3558 
3559     /**
3560      * Resolve this instance as a {@link ConstantDesc}, the result of which is
3561      * the instance itself.
3562      *
3563      * @param lookup ignored
3564      * @return the {@linkplain String} instance
3565      * @since 12
3566      */
3567     @Override
3568     public String resolveConstantDesc(MethodHandles.Lookup lookup) {
3569         return this;
3570     }
3571 
3572 }
< prev index next >