author | phh |
Sat, 30 Nov 2019 14:33:05 -0800 | |
changeset 59330 | 5b96c12f909d |
parent 50878 | fb7800b66c92 |
permissions | -rw-r--r-- |
16522 | 1 |
/* |
2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.nashorn.api.scripting; |
|
27 |
||
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
28 |
import java.lang.invoke.MethodHandle; |
34447
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
32527
diff
changeset
|
29 |
import jdk.dynalink.beans.StaticClass; |
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
32527
diff
changeset
|
30 |
import jdk.dynalink.linker.LinkerServices; |
21688
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
31 |
import jdk.nashorn.internal.runtime.Context; |
20946 | 32 |
import jdk.nashorn.internal.runtime.ScriptFunction; |
21688
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
33 |
import jdk.nashorn.internal.runtime.ScriptObject; |
16522 | 34 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
21691
diff
changeset
|
35 |
import jdk.nashorn.internal.runtime.linker.Bootstrap; |
16522 | 36 |
|
37 |
/** |
|
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
38 |
* Utilities that are to be called from script code. |
28317
0aeeae75e696
8068431: @since and @jdk.Exported are missing in jdk.nashorn.api.scripting classes and package-info.java files
sundar
parents:
27102
diff
changeset
|
39 |
* |
50878
fb7800b66c92
8204492: Add deprecation annotation to Nashorn APIs and warning to nashorn, jjs
sundar
parents:
47216
diff
changeset
|
40 |
* @deprecated Nashorn JavaScript script engine and APIs, and the jjs tool |
fb7800b66c92
8204492: Add deprecation annotation to Nashorn APIs and warning to nashorn, jjs
sundar
parents:
47216
diff
changeset
|
41 |
* are deprecated with the intent to remove them in a future release. |
fb7800b66c92
8204492: Add deprecation annotation to Nashorn APIs and warning to nashorn, jjs
sundar
parents:
47216
diff
changeset
|
42 |
* |
28317
0aeeae75e696
8068431: @since and @jdk.Exported are missing in jdk.nashorn.api.scripting classes and package-info.java files
sundar
parents:
27102
diff
changeset
|
43 |
* @since 1.8u40 |
16522 | 44 |
*/ |
50878
fb7800b66c92
8204492: Add deprecation annotation to Nashorn APIs and warning to nashorn, jjs
sundar
parents:
47216
diff
changeset
|
45 |
@Deprecated(since="11", forRemoval=true) |
16522 | 46 |
public final class ScriptUtils { |
47 |
private ScriptUtils() {} |
|
48 |
||
49 |
/** |
|
50 |
* Returns AST as JSON compatible string. This is used to |
|
51 |
* implement "parse" function in resources/parse.js script. |
|
52 |
* |
|
53 |
* @param code code to be parsed |
|
54 |
* @param name name of the code source (used for location) |
|
55 |
* @param includeLoc tells whether to include location information for nodes or not |
|
56 |
* @return JSON string representation of AST of the supplied code |
|
57 |
*/ |
|
58 |
public static String parse(final String code, final String name, final boolean includeLoc) { |
|
59 |
return ScriptRuntime.parse(code, name, includeLoc); |
|
60 |
} |
|
16527
5ba350ebcdbb
8010145: removed workaround "init.js" in nashorn repo
sundar
parents:
16522
diff
changeset
|
61 |
|
5ba350ebcdbb
8010145: removed workaround "init.js" in nashorn repo
sundar
parents:
16522
diff
changeset
|
62 |
/** |
5ba350ebcdbb
8010145: removed workaround "init.js" in nashorn repo
sundar
parents:
16522
diff
changeset
|
63 |
* Method which converts javascript types to java types for the |
5ba350ebcdbb
8010145: removed workaround "init.js" in nashorn repo
sundar
parents:
16522
diff
changeset
|
64 |
* String.format method (jrunscript function sprintf). |
5ba350ebcdbb
8010145: removed workaround "init.js" in nashorn repo
sundar
parents:
16522
diff
changeset
|
65 |
* |
5ba350ebcdbb
8010145: removed workaround "init.js" in nashorn repo
sundar
parents:
16522
diff
changeset
|
66 |
* @param format a format string |
5ba350ebcdbb
8010145: removed workaround "init.js" in nashorn repo
sundar
parents:
16522
diff
changeset
|
67 |
* @param args arguments referenced by the format specifiers in format |
5ba350ebcdbb
8010145: removed workaround "init.js" in nashorn repo
sundar
parents:
16522
diff
changeset
|
68 |
* @return a formatted string |
5ba350ebcdbb
8010145: removed workaround "init.js" in nashorn repo
sundar
parents:
16522
diff
changeset
|
69 |
*/ |
5ba350ebcdbb
8010145: removed workaround "init.js" in nashorn repo
sundar
parents:
16522
diff
changeset
|
70 |
public static String format(final String format, final Object[] args) { |
5ba350ebcdbb
8010145: removed workaround "init.js" in nashorn repo
sundar
parents:
16522
diff
changeset
|
71 |
return Formatter.format(format, args); |
5ba350ebcdbb
8010145: removed workaround "init.js" in nashorn repo
sundar
parents:
16522
diff
changeset
|
72 |
} |
20946 | 73 |
|
74 |
/** |
|
75 |
* Create a wrapper function that calls {@code func} synchronized on {@code sync} or, if that is undefined, |
|
76 |
* {@code self}. Used to implement "sync" function in resources/mozilla_compat.js. |
|
77 |
* |
|
25420
18a7c7d3c1ca
8049086: Minor API convenience functions on "Java" object
sundar
parents:
24778
diff
changeset
|
78 |
* @param func the function to wrap |
20946 | 79 |
* @param sync the object to synchronize on |
80 |
* @return a synchronizing wrapper function |
|
36141
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
81 |
* @throws IllegalArgumentException if func does not represent a script function |
20946 | 82 |
*/ |
36141
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
83 |
public static Object makeSynchronizedFunction(final Object func, final Object sync) { |
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
84 |
final Object unwrapped = unwrap(func); |
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
85 |
if (unwrapped instanceof ScriptFunction) { |
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
86 |
return ((ScriptFunction)unwrapped).createSynchronized(unwrap(sync)); |
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
87 |
} |
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
88 |
|
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
89 |
throw new IllegalArgumentException(); |
20946 | 90 |
} |
91 |
||
21688
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
92 |
/** |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
93 |
* Make a script object mirror on given object if needed. |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
94 |
* |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
95 |
* @param obj object to be wrapped |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
96 |
* @return wrapped object |
36141
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
97 |
* @throws IllegalArgumentException if obj cannot be wrapped |
21688
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
98 |
*/ |
36141
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
99 |
public static ScriptObjectMirror wrap(final Object obj) { |
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
100 |
if (obj instanceof ScriptObjectMirror) { |
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
101 |
return (ScriptObjectMirror)obj; |
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
102 |
} |
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
103 |
|
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
104 |
if (obj instanceof ScriptObject) { |
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
105 |
final ScriptObject sobj = (ScriptObject)obj; |
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
106 |
return (ScriptObjectMirror) ScriptObjectMirror.wrap(sobj, Context.getGlobal()); |
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
107 |
} |
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
108 |
|
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
109 |
throw new IllegalArgumentException(); |
21688
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
110 |
} |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
111 |
|
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
112 |
/** |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
113 |
* Unwrap a script object mirror if needed. |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
114 |
* |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
115 |
* @param obj object to be unwrapped |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
116 |
* @return unwrapped object |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
117 |
*/ |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
118 |
public static Object unwrap(final Object obj) { |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
119 |
if (obj instanceof ScriptObjectMirror) { |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
120 |
return ScriptObjectMirror.unwrap(obj, Context.getGlobal()); |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
121 |
} |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
122 |
|
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
123 |
return obj; |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
124 |
} |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
125 |
|
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
126 |
/** |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
127 |
* Wrap an array of object to script object mirrors if needed. |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
128 |
* |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
129 |
* @param args array to be unwrapped |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
130 |
* @return wrapped array |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
131 |
*/ |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
132 |
public static Object[] wrapArray(final Object[] args) { |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
133 |
if (args == null || args.length == 0) { |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
134 |
return args; |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
135 |
} |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
136 |
|
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
137 |
return ScriptObjectMirror.wrapArray(args, Context.getGlobal()); |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
138 |
} |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
139 |
|
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
140 |
/** |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
141 |
* Unwrap an array of script object mirrors if needed. |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
142 |
* |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
143 |
* @param args array to be unwrapped |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
144 |
* @return unwrapped array |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
145 |
*/ |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
146 |
public static Object[] unwrapArray(final Object[] args) { |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
147 |
if (args == null || args.length == 0) { |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
148 |
return args; |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
149 |
} |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
150 |
|
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
151 |
return ScriptObjectMirror.unwrapArray(args, Context.getGlobal()); |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20946
diff
changeset
|
152 |
} |
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
153 |
|
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
154 |
/** |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
155 |
* Convert the given object to the given type. |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
156 |
* |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
157 |
* @param obj object to be converted |
36141
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
158 |
* @param type destination type to convert to. type is either a Class |
696d662bcdb7
8148379: jdk.nashorn.api.scripting spec. adjustments, clarifications
sundar
parents:
34903
diff
changeset
|
159 |
* or nashorn representation of a Java type returned by Java.type() call in script. |
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
160 |
* @return converted object |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
161 |
*/ |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
162 |
public static Object convert(final Object obj, final Object type) { |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
163 |
if (obj == null) { |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
164 |
return null; |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
165 |
} |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
166 |
|
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
167 |
final Class<?> clazz; |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
168 |
if (type instanceof Class) { |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
169 |
clazz = (Class<?>)type; |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
170 |
} else if (type instanceof StaticClass) { |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
171 |
clazz = ((StaticClass)type).getRepresentedClass(); |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
172 |
} else { |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
173 |
throw new IllegalArgumentException("type expected"); |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
174 |
} |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
175 |
|
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
176 |
final LinkerServices linker = Bootstrap.getLinkerServices(); |
27099
eceb216332cb
8050977: Java8 Javascript Nashorn exception: no current Global instance for nashorn
sundar
parents:
25865
diff
changeset
|
177 |
final Object objToConvert = unwrap(obj); |
36686
a351eacd4c42
8150218: Autoconversion SAM adapters sometimes don't get privileges
attila
parents:
36141
diff
changeset
|
178 |
final MethodHandle converter = linker.getTypeConverter(objToConvert.getClass(), clazz); |
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
179 |
if (converter == null) { |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
180 |
// no supported conversion! |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
181 |
throw new UnsupportedOperationException("conversion not supported"); |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
182 |
} |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
183 |
|
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
184 |
try { |
27099
eceb216332cb
8050977: Java8 Javascript Nashorn exception: no current Global instance for nashorn
sundar
parents:
25865
diff
changeset
|
185 |
return converter.invoke(objToConvert); |
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
186 |
} catch (final RuntimeException | Error e) { |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
187 |
throw e; |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
188 |
} catch (final Throwable t) { |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
189 |
throw new RuntimeException(t); |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
190 |
} |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
21688
diff
changeset
|
191 |
} |
16522 | 192 |
} |