nashorn/samples/UnderscoreNameLinkerExporter.java
author sundar
Wed, 02 Dec 2015 16:19:15 +0530
changeset 34455 cc9f05d3caf0
permissions -rw-r--r--
8144473: Nashorn code assumes NashornCallSiteDescriptor always Reviewed-by: hannesw, mhaupt
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.lang.invoke.MethodHandle;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    33
import java.lang.invoke.MethodHandles;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    34
import java.lang.invoke.MethodType;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    35
import java.util.ArrayList;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    36
import java.util.List;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    37
import java.util.regex.Pattern;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    38
import java.util.regex.Matcher;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    39
import jdk.dynalink.CallSiteDescriptor;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    40
import jdk.dynalink.CompositeOperation;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    41
import jdk.dynalink.NamedOperation;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    42
import jdk.dynalink.Operation;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    43
import jdk.dynalink.CompositeOperation;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    44
import jdk.dynalink.StandardOperation;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    45
import jdk.dynalink.linker.GuardingDynamicLinker;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    46
import jdk.dynalink.linker.GuardingDynamicLinkerExporter;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    47
import jdk.dynalink.linker.GuardedInvocation;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    48
import jdk.dynalink.linker.LinkRequest;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    49
import jdk.dynalink.linker.LinkerServices;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    50
import jdk.dynalink.linker.support.SimpleLinkRequest;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    51
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    52
/**
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    53
 * This is a dynalink pluggable linker (see http://openjdk.java.net/jeps/276).
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    54
 * This linker translater underscore_separated method names to CamelCase names
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    55
 * used in Java APIs.
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    56
 */
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    57
public final class UnderscoreNameLinkerExporter extends GuardingDynamicLinkerExporter {
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    58
    static {
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    59
        System.out.println("pluggable dynalink underscore name linker loaded");
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    60
    }
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    61
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    62
    private static final Pattern UNDERSCORE_NAME = Pattern.compile("_(.)");
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    63
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    64
    // translate underscore_separated name as a CamelCase name
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    65
    private static String translateToCamelCase(String name) {
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    66
        Matcher m = UNDERSCORE_NAME.matcher(name);
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    67
        StringBuilder buf = new StringBuilder();
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    68
        while (m.find()) {
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    69
            m.appendReplacement(buf, m.group(1).toUpperCase());
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    70
        }
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    71
        m.appendTail(buf);
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    72
        return buf.toString();
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    73
    }
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    74
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    75
    // locate the first standard operation from the call descriptor
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    76
    private static StandardOperation getFirstStandardOperation(final CallSiteDescriptor desc) {
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    77
        final Operation base = NamedOperation.getBaseOperation(desc.getOperation());
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    78
        if (base instanceof StandardOperation) {
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    79
            return (StandardOperation)base;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    80
        } else if (base instanceof CompositeOperation) {
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    81
            final CompositeOperation cop = (CompositeOperation)base;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    82
            for(int i = 0; i < cop.getOperationCount(); ++i) {
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    83
                final Operation op = cop.getOperation(i);
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    84
                if (op instanceof StandardOperation) {
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    85
                    return (StandardOperation)op;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    86
                }
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    87
            }
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    88
        }
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    89
        return null;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    90
    }
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    91
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    92
    @Override
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    93
    public List<GuardingDynamicLinker> get() {
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    94
        final ArrayList<GuardingDynamicLinker> linkers = new ArrayList<>();
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    95
        linkers.add(new GuardingDynamicLinker() {
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    96
            @Override
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    97
            public GuardedInvocation getGuardedInvocation(LinkRequest request,
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    98
                LinkerServices linkerServices) throws Exception {
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
    99
                final Object self = request.getReceiver();
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   100
                CallSiteDescriptor desc = request.getCallSiteDescriptor();
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   101
                Operation op = desc.getOperation();
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   102
                Object name = NamedOperation.getName(op);
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   103
                // is this a named GET_METHOD?
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   104
                boolean isGetMethod = getFirstStandardOperation(desc) == StandardOperation.GET_METHOD;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   105
                if (isGetMethod && name instanceof String) {
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   106
                    String str = (String)name;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   107
                    if (str.indexOf('_') == -1) {
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   108
                        return null;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   109
                    }
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   110
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   111
                    String nameStr = translateToCamelCase(str);
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   112
                    // create a new call descriptor to use translated name
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   113
                    CallSiteDescriptor newDesc = new CallSiteDescriptor(
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   114
                        desc.getLookup(),
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   115
                        new NamedOperation(NamedOperation.getBaseOperation(op), nameStr),
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   116
                        desc.getMethodType());
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   117
                    // create a new Link request to link the call site with translated name
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   118
                    LinkRequest newRequest = new SimpleLinkRequest(newDesc,
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   119
                        request.isCallSiteUnstable(), request.getArguments());
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   120
                    // return guarded invocation linking the translated request
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   121
                    return linkerServices.getGuardedInvocation(newRequest);
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   122
                }
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   123
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   124
                return null;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   125
            }
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   126
        });
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   127
        return linkers;
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   128
    }
cc9f05d3caf0 8144473: Nashorn code assumes NashornCallSiteDescriptor always
sundar
parents:
diff changeset
   129
}