src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/NameCodec.java
author hannesw
Wed, 07 Mar 2018 18:36:21 +0100
changeset 49145 2854589fd853
parent 47216 71c04702a3d5
child 54547 53aecb049e0a
permissions -rw-r--r--
8199236: Nashorn uses deprecated HTML tags in Javadoc Reviewed-by: jlaskey, sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     1
/*
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     4
 *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    10
 *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    15
 * accompanied this code).
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    16
 *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    20
 *
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    23
 * questions.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    24
 */
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    25
33533
43400f0f2b47 8141144: Move NameCodec to jdk.nashorn.internal space
attila
parents: 33343
diff changeset
    26
package jdk.nashorn.internal.runtime.linker;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    27
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    28
/**
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    29
 * <p>
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
    30
 * Implements the name mangling and demangling as specified by John Rose's
33333
0bad500ce4e0 8139590: Improve Dynalink JavaDoc
attila
parents: 33330
diff changeset
    31
 * <a href="https://blogs.oracle.com/jrose/entry/symbolic_freedom_in_the_vm"
33343
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33339
diff changeset
    32
 * target="_blank">"Symbolic Freedom in the VM"</a> article. Normally, you would
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33339
diff changeset
    33
 * mangle the names in the call sites as you're generating bytecode, and then
23abd10384a5 8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents: 33339
diff changeset
    34
 * demangle them when you receive them in bootstrap methods.
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    35
 * </p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    36
 * <p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    37
 * This code is derived from sun.invoke.util.BytecodeName. Apart from subsetting that
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    38
 * class, we don't want to create dependency between non-exported package from java.base
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    39
 * to nashorn module.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    40
 * </p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    41
 *
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    42
 * <h3>Comment from BytecodeName class reproduced here:</h3>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    43
 *
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    44
 * Includes universal mangling rules for the JVM.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    45
 *
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    46
 * <h3>Avoiding Dangerous Characters </h3>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    47
 *
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    48
 * <p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    49
 * The JVM defines a very small set of characters which are illegal
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    50
 * in name spellings.  We will slightly extend and regularize this set
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    51
 * into a group of <cite>dangerous characters</cite>.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    52
 * These characters will then be replaced, in mangled names, by escape sequences.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    53
 * In addition, accidental escape sequences must be further escaped.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    54
 * Finally, a special prefix will be applied if and only if
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    55
 * the mangling would otherwise fail to begin with the escape character.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    56
 * This happens to cover the corner case of the null string,
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    57
 * and also clearly marks symbols which need demangling.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    58
 * </p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    59
 * <p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    60
 * Dangerous characters are the union of all characters forbidden
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    61
 * or otherwise restricted by the JVM specification,
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    62
 * plus their mates, if they are brackets
49145
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
    63
 * (<code><b>[</b></code> and <code><b>]</b></code>,
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
    64
 * <code><b>&lt;</b></code> and <code><b>&gt;</b></code>),
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
    65
 * plus, arbitrarily, the colon character <code><b>:</b></code>.
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    66
 * There is no distinction between type, method, and field names.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    67
 * This makes it easier to convert between mangled names of different
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    68
 * types, since they do not need to be decoded (demangled).
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    69
 * </p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    70
 * <p>
49145
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
    71
 * The escape character is backslash <code><b>\</b></code>
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    72
 * (also known as reverse solidus).
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    73
 * This character is, until now, unheard of in bytecode names,
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    74
 * but traditional in the proposed role.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    75
 *
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    76
 * </p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    77
 * <h3> Replacement Characters </h3>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    78
 *
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    79
 *
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    80
 * <p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    81
 * Every escape sequence is two characters
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    82
 * (in fact, two UTF8 bytes) beginning with
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    83
 * the escape character and followed by a
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    84
 * <cite>replacement character</cite>.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    85
 * (Since the replacement character is never a backslash,
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    86
 * iterated manglings do not double in size.)
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    87
 * </p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    88
 * <p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    89
 * Each dangerous character has some rough visual similarity
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    90
 * to its corresponding replacement character.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    91
 * This makes mangled symbols easier to recognize by sight.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    92
 * </p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    93
 * <p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
    94
 * The dangerous characters are
49145
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
    95
 * <code><b>/</b></code> (forward slash, used to delimit package components),
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
    96
 * <code><b>.</b></code> (dot, also a package delimiter),
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
    97
 * <code><b>;</b></code> (semicolon, used in signatures),
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
    98
 * <code><b>$</b></code> (dollar, used in inner classes and synthetic members),
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
    99
 * <code><b>&lt;</b></code> (left angle),
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   100
 * <code><b>&gt;</b></code> (right angle),
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   101
 * <code><b>[</b></code> (left square bracket, used in array types),
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   102
 * <code><b>]</b></code> (right square bracket, reserved in this scheme for language use),
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   103
 * and <code><b>:</b></code> (colon, reserved in this scheme for language use).
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   104
 * Their replacements are, respectively,
49145
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   105
 * <code><b>|</b></code> (vertical bar),
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   106
 * <code><b>,</b></code> (comma),
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   107
 * <code><b>?</b></code> (question mark),
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   108
 * <code><b>%</b></code> (percent),
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   109
 * <code><b>^</b></code> (caret),
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   110
 * <code><b>_</b></code> (underscore), and
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   111
 * <code><b>{</b></code> (left curly bracket),
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   112
 * <code><b>}</b></code> (right curly bracket),
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   113
 * <code><b>!</b></code> (exclamation mark).
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   114
 * In addition, the replacement character for the escape character itself is
49145
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   115
 * <code><b>-</b></code> (hyphen),
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   116
 * and the replacement character for the null prefix is
49145
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   117
 * <code><b>=</b></code> (equal sign).
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   118
 * </p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   119
 * <p>
49145
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   120
 * An escape character <code><b>\</b></code>
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   121
 * followed by any of these replacement characters
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   122
 * is an escape sequence, and there are no other escape sequences.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   123
 * An equal sign is only part of an escape sequence
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   124
 * if it is the second character in the whole string, following a backslash.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   125
 * Two consecutive backslashes do <em>not</em> form an escape sequence.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   126
 * </p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   127
 * <p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   128
 * Each escape sequence replaces a so-called <cite>original character</cite>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   129
 * which is either one of the dangerous characters or the escape character.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   130
 * A null prefix replaces an initial null string, not a character.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   131
 * </p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   132
 * <p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   133
 * All this implies that escape sequences cannot overlap and may be
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   134
 * determined all at once for a whole string.  Note that a spelling
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   135
 * string can contain <cite>accidental escapes</cite>, apparent escape
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   136
 * sequences which must not be interpreted as manglings.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   137
 * These are disabled by replacing their leading backslash with an
49145
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   138
 * escape sequence (<code><b>\-</b></code>).  To mangle a string, three logical steps
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   139
 * are required, though they may be carried out in one pass:
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   140
 * </p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   141
 * <ol>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   142
 *   <li>In each accidental escape, replace the backslash with an escape sequence
49145
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   143
 * (<code><b>\-</b></code>).</li>
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   144
 *   <li>Replace each dangerous character with an escape sequence
49145
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   145
 * (<code><b>\|</b></code> for <code><b>/</b></code>, etc.).</li>
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   146
 *   <li>If the first two steps introduced any change, <em>and</em>
49145
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   147
 * if the string does not already begin with a backslash, prepend a null prefix (<code><b>\=</b></code>).</li>
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   148
 * </ol>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   149
 *
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   150
 * To demangle a mangled string that begins with an escape,
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   151
 * remove any null prefix, and then replace (in parallel)
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   152
 * each escape sequence by its original character.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   153
 * <p>Spelling strings which contain accidental
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   154
 * escapes <em>must</em> have them replaced, even if those
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   155
 * strings do not contain dangerous characters.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   156
 * This restriction means that mangling a string always
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   157
 * requires a scan of the string for escapes.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   158
 * But then, a scan would be required anyway,
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   159
 * to check for dangerous characters.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   160
 *
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   161
 * </p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   162
 * <h3> Nice Properties </h3>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   163
 *
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   164
 * <p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   165
 * If a bytecode name does not contain any escape sequence,
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   166
 * demangling is a no-op:  The string demangles to itself.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   167
 * Such a string is called <cite>self-mangling</cite>.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   168
 * Almost all strings are self-mangling.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   169
 * In practice, to demangle almost any name &ldquo;found in nature&rdquo;,
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   170
 * simply verify that it does not begin with a backslash.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   171
 * </p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   172
 * <p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   173
 * Mangling is a one-to-one function, while demangling
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   174
 * is a many-to-one function.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   175
 * A mangled string is defined as <cite>validly mangled</cite> if
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   176
 * it is in fact the unique mangling of its spelling string.
49145
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   177
 * Three examples of invalidly mangled strings are <code><b>\=foo</b></code>,
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   178
 * <code><b>\-bar</b></code>, and <code><b>baz\!</b></code>, which demangle to <code><b>foo</b></code>, <code><b>\bar</b></code>, and
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   179
 * <code><b>baz\!</b></code>, but then remangle to <code><b>foo</b></code>, <code><b>\bar</b></code>, and <code><b>\=baz\-!</b></code>.
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   180
 * If a language back-end or runtime is using mangled names,
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   181
 * it should never present an invalidly mangled bytecode
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   182
 * name to the JVM.  If the runtime encounters one,
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   183
 * it should also report an error, since such an occurrence
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   184
 * probably indicates a bug in name encoding which
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   185
 * will lead to errors in linkage.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   186
 * However, this note does not propose that the JVM verifier
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   187
 * detect invalidly mangled names.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   188
 * </p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   189
 * <p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   190
 * As a result of these rules, it is a simple matter to
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   191
 * compute validly mangled substrings and concatenations
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   192
 * of validly mangled strings, and (with a little care)
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   193
 * these correspond to corresponding operations on their
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   194
 * spelling strings.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   195
 * </p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   196
 * <ul>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   197
 *   <li>Any prefix of a validly mangled string is also validly mangled,
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   198
 * although a null prefix may need to be removed.</li>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   199
 *   <li>Any suffix of a validly mangled string is also validly mangled,
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   200
 * although a null prefix may need to be added.</li>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   201
 *   <li>Two validly mangled strings, when concatenated,
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   202
 * are also validly mangled, although any null prefix
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   203
 * must be removed from the second string,
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   204
 * and a trailing backslash on the first string may need escaping,
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   205
 * if it would participate in an accidental escape when followed
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   206
 * by the first character of the second string.</li>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   207
 * </ul>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   208
 * <p>If languages that include non-Java symbol spellings use this
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   209
 * mangling convention, they will enjoy the following advantages:
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   210
 * </p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   211
 * <ul>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   212
 *   <li>They can interoperate via symbols they share in common.</li>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   213
 *   <li>Low-level tools, such as backtrace printers, will have readable displays.</li>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   214
 *   <li>Future JVM and language extensions can safely use the dangerous characters
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   215
 * for structuring symbols, but will never interfere with valid spellings.</li>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   216
 *   <li>Runtimes and compilers can use standard libraries for mangling and demangling.</li>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   217
 *   <li>Occasional transliterations and name composition will be simple and regular,
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   218
 * for classes, methods, and fields.</li>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   219
 *   <li>Bytecode names will continue to be compact.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   220
 * When mangled, spellings will at most double in length, either in
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   221
 * UTF8 or UTF16 format, and most will not change at all.</li>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   222
 * </ul>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   223
 *
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   224
 *
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   225
 * <h3> Suggestions for Human Readable Presentations </h3>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   226
 *
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   227
 *
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   228
 * <p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   229
 * For human readable displays of symbols,
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   230
 * it will be better to present a string-like quoted
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   231
 * representation of the spelling, because JVM users
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   232
 * are generally familiar with such tokens.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   233
 * We suggest using single or double quotes before and after
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   234
 * mangled symbols which are not valid Java identifiers,
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   235
 * with quotes, backslashes, and non-printing characters
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   236
 * escaped as if for literals in the Java language.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   237
 * </p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   238
 * <p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   239
 * For example, an HTML-like spelling
49145
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   240
 * <code><b>&lt;pre&gt;</b></code> mangles to
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   241
 * <code><b>\^pre\_</b></code> and could
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   242
 * display more cleanly as
49145
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   243
 * <code><b>'&lt;pre&gt;'</b></code>,
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   244
 * with the quotes included.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   245
 * Such string-like conventions are <em>not</em> suitable
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   246
 * for mangled bytecode names, in part because
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   247
 * dangerous characters must be eliminated, rather
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   248
 * than just quoted.  Otherwise internally structured
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   249
 * strings like package prefixes and method signatures
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   250
 * could not be reliably parsed.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   251
 * </p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   252
 * <p>
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   253
 * In such human-readable displays, invalidly mangled
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   254
 * names should <em>not</em> be demangled and quoted,
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   255
 * for this would be misleading.  Likewise, JVM symbols
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   256
 * which contain dangerous characters (like dots in field
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   257
 * names or brackets in method names) should not be
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   258
 * simply quoted.  The bytecode names
49145
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   259
 * <code><b>\=phase\,1</b></code> and
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   260
 * <code><b>phase.1</b></code> are distinct,
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   261
 * and in demangled displays they should be presented as
49145
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   262
 * <code><b>'phase.1'</b></code> and something like
2854589fd853 8199236: Nashorn uses deprecated HTML tags in Javadoc
hannesw
parents: 47216
diff changeset
   263
 * <code><b>'phase'.1</b></code>, respectively.
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   264
 * </p>
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   265
 */
33339
334cd3ebfa5e 8139888: Improve Dynalink JavaDoc some more
attila
parents: 33333
diff changeset
   266
public final class NameCodec {
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   267
    private NameCodec() {
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   268
    }
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   269
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   270
    private static final char ESCAPE_C = '\\';
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   271
    // empty escape sequence to avoid a null name or illegal prefix
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   272
    private static final char NULL_ESCAPE_C = '=';
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   273
    private static final String NULL_ESCAPE = ESCAPE_C+""+NULL_ESCAPE_C;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   274
33533
43400f0f2b47 8141144: Move NameCodec to jdk.nashorn.internal space
attila
parents: 33343
diff changeset
   275
    /**
43400f0f2b47 8141144: Move NameCodec to jdk.nashorn.internal space
attila
parents: 33343
diff changeset
   276
     * Canonical encoding for the empty name.
43400f0f2b47 8141144: Move NameCodec to jdk.nashorn.internal space
attila
parents: 33343
diff changeset
   277
     */
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   278
    public static final String EMPTY_NAME =  new String(new char[] { ESCAPE_C, NULL_ESCAPE_C });
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   279
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   280
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   281
     * Encodes ("mangles") an unencoded symbolic name.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   282
     * @param name the symbolic name to mangle
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   283
     * @return the mangled form of the symbolic name.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   284
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 16234
diff changeset
   285
    public static String encode(final String name) {
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 33534
diff changeset
   286
        final String bn = mangle(name);
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   287
        assert((Object)bn == name || looksMangled(bn)) : bn;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   288
        assert(name.equals(decode(bn))) : name;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   289
        return bn;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   290
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   291
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   292
    /**
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   293
     * Decodes ("demangles") an encoded symbolic name.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   294
     * @param name the symbolic name to demangle
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   295
     * @return the demangled form of the symbolic name.
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   296
     */
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 16234
diff changeset
   297
    public static String decode(final String name) {
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   298
        String sn = name;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   299
        if (!sn.isEmpty() && looksMangled(name)) {
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   300
            sn = demangle(name);
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   301
            assert(name.equals(mangle(sn))) : name+" => "+sn+" => "+mangle(sn);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   302
        }
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   303
        return sn;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   304
    }
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   305
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 33534
diff changeset
   306
    private static boolean looksMangled(final String s) {
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   307
        return s.charAt(0) == ESCAPE_C;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   308
    }
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   309
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 33534
diff changeset
   310
    private static String mangle(final String s) {
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   311
        if (s.length() == 0)
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   312
            return NULL_ESCAPE;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   313
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   314
        // build this lazily, when we first need an escape:
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   315
        StringBuilder sb = null;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   316
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   317
        for (int i = 0, slen = s.length(); i < slen; i++) {
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 33534
diff changeset
   318
            final char c = s.charAt(i);
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   319
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   320
            boolean needEscape = false;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   321
            if (c == ESCAPE_C) {
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   322
                if (i+1 < slen) {
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 33534
diff changeset
   323
                    final char c1 = s.charAt(i+1);
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   324
                    if ((i == 0 && c1 == NULL_ESCAPE_C)
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   325
                        || c1 != originalOfReplacement(c1)) {
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   326
                        // an accidental escape
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   327
                        needEscape = true;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   328
                    }
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   329
                }
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   330
            } else {
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   331
                needEscape = isDangerous(c);
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   332
            }
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   333
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   334
            if (!needEscape) {
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   335
                if (sb != null)  sb.append(c);
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   336
                continue;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   337
            }
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   338
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   339
            // build sb if this is the first escape
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   340
            if (sb == null) {
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   341
                sb = new StringBuilder(s.length()+10);
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   342
                // mangled names must begin with a backslash:
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   343
                if (s.charAt(0) != ESCAPE_C && i > 0)
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   344
                    sb.append(NULL_ESCAPE);
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   345
                // append the string so far, which is unremarkable:
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   346
                sb.append(s, 0, i);
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   347
            }
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   348
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   349
            // rewrite \ to \-, / to \|, etc.
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   350
            sb.append(ESCAPE_C);
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   351
            sb.append(replacementOf(c));
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   352
        }
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   353
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   354
        if (sb != null)   return sb.toString();
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   355
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   356
        return s;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   357
    }
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   358
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 33534
diff changeset
   359
    private static String demangle(final String s) {
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   360
        // build this lazily, when we first meet an escape:
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   361
        StringBuilder sb = null;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   362
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   363
        int stringStart = 0;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   364
        if (s.startsWith(NULL_ESCAPE))
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   365
            stringStart = 2;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   366
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   367
        for (int i = stringStart, slen = s.length(); i < slen; i++) {
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   368
            char c = s.charAt(i);
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   369
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   370
            if (c == ESCAPE_C && i+1 < slen) {
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   371
                // might be an escape sequence
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 33534
diff changeset
   372
                final char rc = s.charAt(i+1);
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 33534
diff changeset
   373
                final char oc = originalOfReplacement(rc);
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   374
                if (oc != rc) {
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   375
                    // build sb if this is the first escape
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   376
                    if (sb == null) {
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   377
                        sb = new StringBuilder(s.length());
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   378
                        // append the string so far, which is unremarkable:
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   379
                        sb.append(s, stringStart, i);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   380
                    }
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   381
                    ++i;  // skip both characters
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   382
                    c = oc;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   383
                }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   384
            }
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   385
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   386
            if (sb != null)
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   387
                sb.append(c);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   388
        }
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   389
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   390
        if (sb != null)   return sb.toString();
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   391
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   392
        return s.substring(stringStart);
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   393
    }
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   394
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   395
    private static final String DANGEROUS_CHARS   = "\\/.;:$[]<>"; // \\ must be first
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   396
    private static final String REPLACEMENT_CHARS =  "-|,?!%{}^_";
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   397
    private static final int DANGEROUS_CHAR_FIRST_INDEX = 1; // index after \\
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   398
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   399
    private static final long[] SPECIAL_BITMAP = new long[2];  // 128 bits
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   400
    static {
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 33534
diff changeset
   401
        final String SPECIAL = DANGEROUS_CHARS + REPLACEMENT_CHARS;
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 33534
diff changeset
   402
        for (final char c : SPECIAL.toCharArray()) {
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   403
            SPECIAL_BITMAP[c >>> 6] |= 1L << c;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   404
        }
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   405
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   406
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 33534
diff changeset
   407
    private static boolean isSpecial(final char c) {
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   408
        if ((c >>> 6) < SPECIAL_BITMAP.length)
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   409
            return ((SPECIAL_BITMAP[c >>> 6] >> c) & 1) != 0;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   410
        else
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   411
            return false;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   412
    }
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   413
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 33534
diff changeset
   414
    private static char replacementOf(final char c) {
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   415
        if (!isSpecial(c))  return c;
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 33534
diff changeset
   416
        final int i = DANGEROUS_CHARS.indexOf(c);
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   417
        if (i < 0)  return c;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   418
        return REPLACEMENT_CHARS.charAt(i);
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   419
    }
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   420
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 33534
diff changeset
   421
    private static char originalOfReplacement(final char c) {
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   422
        if (!isSpecial(c))  return c;
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 33534
diff changeset
   423
        final int i = REPLACEMENT_CHARS.indexOf(c);
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   424
        if (i < 0)  return c;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   425
        return DANGEROUS_CHARS.charAt(i);
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   426
    }
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   427
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 33534
diff changeset
   428
    private static boolean isDangerous(final char c) {
33534
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   429
        if (!isSpecial(c))  return false;
28065d9f5521 8141285: NameCode should pass tests from BytecodeNameTest.java
sundar
parents: 33533
diff changeset
   430
        return (DANGEROUS_CHARS.indexOf(c) >= DANGEROUS_CHAR_FIRST_INDEX);
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   431
    }
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
diff changeset
   432
}