nashorn/samples/dynalink/UnderscoreNameLinkerExporter.java
author attila
Tue, 01 Nov 2016 15:31:44 +0100
changeset 41842 50202a344d28
parent 41422 97eda72f53b6
permissions -rw-r--r--
8168005: Introduce namespaces for GET, SET Dynalink operations Reviewed-by: hannesw, sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34455
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
     1
/*
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
     3
 *
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
     6
 * are met:
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
     7
 *
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    10
 *
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    14
 *
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    18
 *
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    30
 */
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    31
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    32
import java.util.ArrayList;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    33
import java.util.List;
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 34549
diff changeset
    34
import java.util.regex.Matcher;
34455
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    35
import java.util.regex.Pattern;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    36
import jdk.dynalink.CallSiteDescriptor;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    37
import jdk.dynalink.NamedOperation;
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 41422
diff changeset
    38
import jdk.dynalink.NamespaceOperation;
34455
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    39
import jdk.dynalink.Operation;
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 41422
diff changeset
    40
import jdk.dynalink.StandardNamespace;
34455
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    41
import jdk.dynalink.StandardOperation;
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 34549
diff changeset
    42
import jdk.dynalink.linker.GuardedInvocation;
34455
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    43
import jdk.dynalink.linker.GuardingDynamicLinker;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    44
import jdk.dynalink.linker.GuardingDynamicLinkerExporter;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    45
import jdk.dynalink.linker.LinkRequest;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    46
import jdk.dynalink.linker.LinkerServices;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    47
import jdk.dynalink.linker.support.SimpleLinkRequest;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    48
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    49
/**
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    50
 * This is a dynalink pluggable linker (see http://openjdk.java.net/jeps/276).
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    51
 * This linker translater underscore_separated method names to CamelCase names
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    52
 * used in Java APIs.
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    53
 */
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    54
public final class UnderscoreNameLinkerExporter extends GuardingDynamicLinkerExporter {
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    55
    static {
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    56
        System.out.println("pluggable dynalink underscore name linker loaded");
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    57
    }
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    58
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    59
    private static final Pattern UNDERSCORE_NAME = Pattern.compile("_(.)");
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    60
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    61
    // translate underscore_separated name as a CamelCase name
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 34549
diff changeset
    62
    private static String translateToCamelCase(final String name) {
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 34549
diff changeset
    63
        final Matcher m = UNDERSCORE_NAME.matcher(name);
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 34549
diff changeset
    64
        final StringBuilder buf = new StringBuilder();
34455
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    65
        while (m.find()) {
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    66
            m.appendReplacement(buf, m.group(1).toUpperCase());
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    67
        }
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    68
        m.appendTail(buf);
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    69
        return buf.toString();
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    70
    }
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    71
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    72
    @Override
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    73
    public List<GuardingDynamicLinker> get() {
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    74
        final ArrayList<GuardingDynamicLinker> linkers = new ArrayList<>();
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    75
        linkers.add(new GuardingDynamicLinker() {
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    76
            @Override
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 34549
diff changeset
    77
            public GuardedInvocation getGuardedInvocation(final LinkRequest request,
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 34549
diff changeset
    78
                final LinkerServices linkerServices) throws Exception {
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 34549
diff changeset
    79
                final CallSiteDescriptor desc = request.getCallSiteDescriptor();
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 34549
diff changeset
    80
                final Operation op = desc.getOperation();
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 34549
diff changeset
    81
                final Object name = NamedOperation.getName(op);
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 41422
diff changeset
    82
                final Operation namespaceOp = NamedOperation.getBaseOperation(op);
34455
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    83
                // is this a named GET_METHOD?
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 41422
diff changeset
    84
                final boolean isGetMethod =
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 41422
diff changeset
    85
                        NamespaceOperation.getBaseOperation(namespaceOp) == StandardOperation.GET
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 41422
diff changeset
    86
                        && StandardNamespace.findFirst(namespaceOp) == StandardNamespace.METHOD;
34455
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    87
                if (isGetMethod && name instanceof String) {
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 34549
diff changeset
    88
                    final String str = (String)name;
34455
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    89
                    if (str.indexOf('_') == -1) {
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    90
                        return null;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    91
                    }
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    92
41422
97eda72f53b6 8167117: insert missing final keywords
attila
parents: 34549
diff changeset
    93
                    final String nameStr = translateToCamelCase(str);
34455
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    94
                    // create a new call descriptor to use translated name
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 41422
diff changeset
    95
                    final CallSiteDescriptor newDesc = desc.changeOperation(((NamedOperation)op).changeName(nameStr));
34455
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    96
                    // create a new Link request to link the call site with translated name
41842
50202a344d28 8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents: 41422
diff changeset
    97
                    final LinkRequest newRequest = request.replaceArguments(newDesc, request.getArguments());
34455
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    98
                    // return guarded invocation linking the translated request
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    99
                    return linkerServices.getGuardedInvocation(newRequest);
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   100
                }
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   101
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   102
                return null;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   103
            }
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   104
        });
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   105
        return linkers;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   106
    }
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   107
}