src/jdk/internal/dynalink/support/NameCodec.java

changeset 90
5a820fb11814
parent 0
b1a7da25b547
child 962
ac62e33a99b0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/jdk/internal/dynalink/support/NameCodec.java	Thu Feb 14 13:22:26 2013 +0100
     1.3 @@ -0,0 +1,218 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +/*
    1.30 + * This file is available under and governed by the GNU General Public
    1.31 + * License version 2 only, as published by the Free Software Foundation.
    1.32 + * However, the following notice accompanied the original version of this
    1.33 + * file, and Oracle licenses the original version of this file under the BSD
    1.34 + * license:
    1.35 + */
    1.36 +/*
    1.37 +   Copyright 2009-2013 Attila Szegedi
    1.38 +
    1.39 +   Licensed under both the Apache License, Version 2.0 (the "Apache License")
    1.40 +   and the BSD License (the "BSD License"), with licensee being free to
    1.41 +   choose either of the two at their discretion.
    1.42 +
    1.43 +   You may not use this file except in compliance with either the Apache
    1.44 +   License or the BSD License.
    1.45 +
    1.46 +   If you choose to use this file in compliance with the Apache License, the
    1.47 +   following notice applies to you:
    1.48 +
    1.49 +       You may obtain a copy of the Apache License at
    1.50 +
    1.51 +           http://www.apache.org/licenses/LICENSE-2.0
    1.52 +
    1.53 +       Unless required by applicable law or agreed to in writing, software
    1.54 +       distributed under the License is distributed on an "AS IS" BASIS,
    1.55 +       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    1.56 +       implied. See the License for the specific language governing
    1.57 +       permissions and limitations under the License.
    1.58 +
    1.59 +   If you choose to use this file in compliance with the BSD License, the
    1.60 +   following notice applies to you:
    1.61 +
    1.62 +       Redistribution and use in source and binary forms, with or without
    1.63 +       modification, are permitted provided that the following conditions are
    1.64 +       met:
    1.65 +       * Redistributions of source code must retain the above copyright
    1.66 +         notice, this list of conditions and the following disclaimer.
    1.67 +       * Redistributions in binary form must reproduce the above copyright
    1.68 +         notice, this list of conditions and the following disclaimer in the
    1.69 +         documentation and/or other materials provided with the distribution.
    1.70 +       * Neither the name of the copyright holder nor the names of
    1.71 +         contributors may be used to endorse or promote products derived from
    1.72 +         this software without specific prior written permission.
    1.73 +
    1.74 +       THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
    1.75 +       IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
    1.76 +       TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
    1.77 +       PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
    1.78 +       BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.79 +       CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.80 +       SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
    1.81 +       BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    1.82 +       WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
    1.83 +       OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
    1.84 +       ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.85 +*/
    1.86 +
    1.87 +package jdk.internal.dynalink.support;
    1.88 +
    1.89 +import jdk.internal.dynalink.CallSiteDescriptor;
    1.90 +
    1.91 +/**
    1.92 + * Implements the name mangling and demangling as specified by John Rose's
    1.93 + * <a href="https://blogs.oracle.com/jrose/entry/symbolic_freedom_in_the_vm" target="_blank">"Symbolic Freedom in the
    1.94 + * VM"</a> article. It is recommended that implementers of languages on the JVM uniformly adopt this for symbolic
    1.95 + * interoperability between languages. Normally, you would mangle the names as you're generating bytecode, and then
    1.96 + * demangle them when you're creating {@link CallSiteDescriptor} objects. Note that you are expected to mangle
    1.97 + * individual tokens, and not the whole name at the call site, i.e. the colon character normally separating the tokens
    1.98 + * is never mangled. I.e. you wouldn't mangle {@code dyn:getProp:color} into {@code dyn\!getProp\!color}, but you would
    1.99 + * mangle {@code dyn:getProp:color$} into {@code dyn:getProp:\=color\%} (only mangling the individual token containing
   1.100 + * the symbol {@code color$}). {@link CallSiteDescriptorFactory#tokenizeName(String)} (and by implication, all call site
   1.101 + * descriptors it creates) will automatically perform demangling on the passed names. If you use this factory, or you
   1.102 + * have your own way of creating call site descriptors, but you still delegate to this method of the default factory
   1.103 + * (it is recommended that you do), then you have demangling handled for you already, and only need to ensure that you
   1.104 + * mangle the names when you're emitting them in the bytecode.
   1.105 + *
   1.106 + * @author Attila Szegedi
   1.107 + */
   1.108 +public class NameCodec {
   1.109 +    private static final char ESCAPE_CHAR = '\\';
   1.110 +    private static final char EMPTY_ESCAPE = '=';
   1.111 +    private static final String EMPTY_NAME = new String(new char[] { ESCAPE_CHAR, EMPTY_ESCAPE });
   1.112 +    private static final char EMPTY_CHAR = 0xFEFF;
   1.113 +
   1.114 +    private static final int MIN_ENCODING = '$';
   1.115 +    private static final int MAX_ENCODING = ']';
   1.116 +    private static final char[] ENCODING = new char[MAX_ENCODING - MIN_ENCODING + 1];
   1.117 +    private static final int MIN_DECODING = '!';
   1.118 +    private static final int MAX_DECODING = '}';
   1.119 +    private static final char[] DECODING = new char[MAX_DECODING - MIN_DECODING + 1];
   1.120 +
   1.121 +    static {
   1.122 +        addEncoding('/', '|');
   1.123 +        addEncoding('.', ',');
   1.124 +        addEncoding(';', '?');
   1.125 +        addEncoding('$', '%');
   1.126 +        addEncoding('<', '^');
   1.127 +        addEncoding('>', '_');
   1.128 +        addEncoding('[', '{');
   1.129 +        addEncoding(']', '}');
   1.130 +        addEncoding(':', '!');
   1.131 +        addEncoding('\\', '-');
   1.132 +        DECODING[EMPTY_ESCAPE - MIN_DECODING] = EMPTY_CHAR;
   1.133 +    }
   1.134 +
   1.135 +    private NameCodec() {
   1.136 +    }
   1.137 +
   1.138 +    /**
   1.139 +     * Encodes ("mangles") an unencoded symbolic name.
   1.140 +     * @param name the symbolic name to mangle
   1.141 +     * @return the mangled form of the symbolic name.
   1.142 +     */
   1.143 +    public static String encode(String name) {
   1.144 +        final int l = name.length();
   1.145 +        if(l == 0) {
   1.146 +            return EMPTY_NAME;
   1.147 +        }
   1.148 +        StringBuilder b = null;
   1.149 +        int lastEscape = -1;
   1.150 +        for(int i = 0; i < l; ++i) {
   1.151 +            final int encodeIndex = name.charAt(i) - MIN_ENCODING;
   1.152 +            if(encodeIndex >= 0 && encodeIndex < ENCODING.length) {
   1.153 +                final char e = ENCODING[encodeIndex];
   1.154 +                if(e != 0) {
   1.155 +                    if(b == null) {
   1.156 +                        b = new StringBuilder(name.length() + 3);
   1.157 +                        if(name.charAt(0) != ESCAPE_CHAR && i > 0) {
   1.158 +                            b.append(EMPTY_NAME);
   1.159 +                        }
   1.160 +                        b.append(name, 0, i);
   1.161 +                    } else {
   1.162 +                        b.append(name, lastEscape + 1, i);
   1.163 +                    }
   1.164 +                    b.append(ESCAPE_CHAR).append(e);
   1.165 +                    lastEscape = i;
   1.166 +                }
   1.167 +            }
   1.168 +        }
   1.169 +        if(b == null) {
   1.170 +            return name.toString();
   1.171 +        }
   1.172 +        assert lastEscape != -1;
   1.173 +        b.append(name, lastEscape + 1, l);
   1.174 +        return b.toString();
   1.175 +    }
   1.176 +
   1.177 +    /**
   1.178 +     * Decodes ("demangles") an encoded symbolic name.
   1.179 +     * @param name the symbolic name to demangle
   1.180 +     * @return the demangled form of the symbolic name.
   1.181 +     */
   1.182 +    public static String decode(String name) {
   1.183 +        if(name.charAt(0) != ESCAPE_CHAR) {
   1.184 +            return name;
   1.185 +        }
   1.186 +        final int l = name.length();
   1.187 +        if(l == 2 && name.charAt(1) == EMPTY_CHAR) {
   1.188 +            return "";
   1.189 +        }
   1.190 +        StringBuilder b = new StringBuilder(name.length());
   1.191 +        int lastEscape = -2;
   1.192 +        int lastBackslash = -1;
   1.193 +        for(;;) {
   1.194 +            int nextBackslash = name.indexOf(ESCAPE_CHAR, lastBackslash + 1);
   1.195 +            if(nextBackslash == -1 || nextBackslash == l - 1) {
   1.196 +                break;
   1.197 +            }
   1.198 +            final int decodeIndex = name.charAt(nextBackslash + 1) - MIN_DECODING;
   1.199 +            if(decodeIndex >= 0 && decodeIndex < DECODING.length) {
   1.200 +                final char d = DECODING[decodeIndex];
   1.201 +                if(d == EMPTY_CHAR) {
   1.202 +                    // "\=" is only valid at the beginning of a mangled string
   1.203 +                    if(nextBackslash == 0) {
   1.204 +                        lastEscape = 0;
   1.205 +                    }
   1.206 +                } else if(d != 0) {
   1.207 +                    b.append(name, lastEscape + 2, nextBackslash).append(d);
   1.208 +                    lastEscape = nextBackslash;
   1.209 +                }
   1.210 +            }
   1.211 +            lastBackslash = nextBackslash;
   1.212 +        }
   1.213 +        b.append(name, lastEscape + 2, l);
   1.214 +        return b.toString();
   1.215 +    }
   1.216 +
   1.217 +    private static void addEncoding(char from, char to) {
   1.218 +        ENCODING[from - MIN_ENCODING] = to;
   1.219 +        DECODING[to - MIN_DECODING] = from;
   1.220 +    }
   1.221 +}

mercurial