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