author | sundar |
Thu, 07 Nov 2013 17:26:46 +0530 | |
changeset 21691 | 8e284e9a6144 |
parent 19097 | f544a2ea40ef |
child 22669 | 75563515567f |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 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.internal.runtime.linker; |
|
27 |
||
28 |
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; |
|
29 |
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; |
|
16277 | 30 |
import static jdk.nashorn.internal.lookup.Lookup.MH; |
16147 | 31 |
|
32 |
import java.lang.invoke.MethodHandle; |
|
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
33 |
import java.lang.invoke.MethodType; |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
34 |
import java.lang.reflect.Method; |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
35 |
import java.lang.reflect.Modifier; |
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
36 |
import java.util.Map; |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
37 |
import java.util.HashMap; |
16234
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16188
diff
changeset
|
38 |
import jdk.internal.dynalink.CallSiteDescriptor; |
16246
f60e04200be3
8008387: Improve code coverage tests for JSObjectLinker and NashornBottomLinker
sundar
parents:
16234
diff
changeset
|
39 |
import jdk.internal.dynalink.beans.BeansLinker; |
16234
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16188
diff
changeset
|
40 |
import jdk.internal.dynalink.linker.GuardedInvocation; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16188
diff
changeset
|
41 |
import jdk.internal.dynalink.linker.GuardingDynamicLinker; |
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
42 |
import jdk.internal.dynalink.linker.GuardingTypeConverterFactory; |
16234
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16188
diff
changeset
|
43 |
import jdk.internal.dynalink.linker.LinkRequest; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16188
diff
changeset
|
44 |
import jdk.internal.dynalink.linker.LinkerServices; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16188
diff
changeset
|
45 |
import jdk.internal.dynalink.support.Guards; |
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
46 |
import jdk.nashorn.internal.runtime.Context; |
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
47 |
import jdk.nashorn.internal.runtime.JSType; |
16147 | 48 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
49 |
||
50 |
/** |
|
51 |
* Nashorn bottom linker; used as a last-resort catch-all linker for all linking requests that fall through all other |
|
52 |
* linkers (see how {@link Bootstrap} class configures the dynamic linker in its static initializer). It will throw |
|
53 |
* appropriate ECMAScript errors for attempts to invoke operations on {@code null}, link no-op property getters and |
|
54 |
* setters for Java objects that couldn't be linked by any other linker, and throw appropriate ECMAScript errors for |
|
55 |
* attempts to invoke arbitrary Java objects as functions or constructors. |
|
56 |
*/ |
|
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
57 |
final class NashornBottomLinker implements GuardingDynamicLinker, GuardingTypeConverterFactory { |
16147 | 58 |
|
59 |
@Override |
|
60 |
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) |
|
61 |
throws Exception { |
|
62 |
final Object self = linkRequest.getReceiver(); |
|
63 |
||
64 |
if (self == null) { |
|
65 |
return linkNull(linkRequest); |
|
66 |
} |
|
67 |
||
68 |
// None of the objects that can be linked by NashornLinker should ever reach here. Basically, anything below |
|
69 |
// this point is a generic Java bean. Therefore, reaching here with a ScriptObject is a Nashorn bug. |
|
70 |
assert isExpectedObject(self) : "Couldn't link " + linkRequest.getCallSiteDescriptor() + " for " + self.getClass().getName(); |
|
71 |
||
72 |
return linkBean(linkRequest, linkerServices); |
|
73 |
} |
|
74 |
||
75 |
private static final MethodHandle EMPTY_PROP_GETTER = |
|
76 |
MH.dropArguments(MH.constant(Object.class, UNDEFINED), 0, Object.class); |
|
77 |
private static final MethodHandle EMPTY_ELEM_GETTER = |
|
78 |
MH.dropArguments(EMPTY_PROP_GETTER, 0, Object.class); |
|
79 |
private static final MethodHandle EMPTY_PROP_SETTER = |
|
80 |
MH.asType(EMPTY_ELEM_GETTER, EMPTY_ELEM_GETTER.type().changeReturnType(void.class)); |
|
81 |
private static final MethodHandle EMPTY_ELEM_SETTER = |
|
82 |
MH.dropArguments(EMPTY_PROP_SETTER, 0, Object.class); |
|
83 |
||
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
84 |
private static GuardedInvocation linkBean(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception { |
16147 | 85 |
final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor)linkRequest.getCallSiteDescriptor(); |
86 |
final Object self = linkRequest.getReceiver(); |
|
87 |
final String operator = desc.getFirstOperator(); |
|
88 |
switch (operator) { |
|
89 |
case "new": |
|
16246
f60e04200be3
8008387: Improve code coverage tests for JSObjectLinker and NashornBottomLinker
sundar
parents:
16234
diff
changeset
|
90 |
if(BeansLinker.isDynamicMethod(self)) { |
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16246
diff
changeset
|
91 |
throw typeError("method.not.constructor", ScriptRuntime.safeToString(self)); |
16147 | 92 |
} |
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16246
diff
changeset
|
93 |
throw typeError("not.a.function", ScriptRuntime.safeToString(self)); |
16147 | 94 |
case "call": |
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
95 |
// Support dyn:call on any object that supports some @FunctionalInterface |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
96 |
// annotated interface. This way Java method, constructor references or |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
97 |
// implementations of java.util.function.* interfaces can be called as though |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
98 |
// those are script functions. |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
99 |
final Method m = getFunctionalInterfaceMethod(self.getClass()); |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
100 |
if (m != null) { |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
101 |
final MethodType callType = desc.getMethodType(); |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
102 |
// 'callee' and 'thiz' passed from script + actual arguments |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
103 |
if (callType.parameterCount() != m.getParameterCount() + 2) { |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
104 |
throw typeError("no.method.matches.args", ScriptRuntime.safeToString(self)); |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
105 |
} |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
106 |
return new GuardedInvocation( |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
107 |
// drop 'thiz' passed from the script. |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
108 |
MH.dropArguments(desc.getLookup().unreflect(m), 1, callType.parameterType(1)), |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
109 |
Guards.getInstanceOfGuard(m.getDeclaringClass())).asType(callType); |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
110 |
} |
16246
f60e04200be3
8008387: Improve code coverage tests for JSObjectLinker and NashornBottomLinker
sundar
parents:
16234
diff
changeset
|
111 |
if(BeansLinker.isDynamicMethod(self)) { |
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16246
diff
changeset
|
112 |
throw typeError("no.method.matches.args", ScriptRuntime.safeToString(self)); |
16147 | 113 |
} |
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16246
diff
changeset
|
114 |
throw typeError("not.a.function", ScriptRuntime.safeToString(self)); |
16147 | 115 |
case "callMethod": |
116 |
case "getMethod": |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16246
diff
changeset
|
117 |
throw typeError("no.such.function", getArgument(linkRequest), ScriptRuntime.safeToString(self)); |
16147 | 118 |
case "getProp": |
119 |
case "getElem": |
|
120 |
if (desc.getOperand() != null) { |
|
121 |
return getInvocation(EMPTY_PROP_GETTER, self, linkerServices, desc); |
|
122 |
} |
|
123 |
return getInvocation(EMPTY_ELEM_GETTER, self, linkerServices, desc); |
|
124 |
case "setProp": |
|
125 |
case "setElem": |
|
126 |
if (desc.getOperand() != null) { |
|
127 |
return getInvocation(EMPTY_PROP_SETTER, self, linkerServices, desc); |
|
128 |
} |
|
129 |
return getInvocation(EMPTY_ELEM_SETTER, self, linkerServices, desc); |
|
130 |
default: |
|
131 |
break; |
|
132 |
} |
|
133 |
throw new AssertionError("unknown call type " + desc); |
|
134 |
} |
|
135 |
||
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
136 |
@Override |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
137 |
public GuardedInvocation convertToType(final Class<?> sourceType, final Class<?> targetType) throws Exception { |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
138 |
final GuardedInvocation gi = convertToTypeNoCast(sourceType, targetType); |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
139 |
return gi == null ? null : gi.asType(MH.type(targetType, sourceType)); |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
140 |
} |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
141 |
|
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
142 |
/** |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
143 |
* Main part of the implementation of {@link GuardingTypeConverterFactory#convertToType(Class, Class)} that doesn't |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
144 |
* care about adapting the method signature; that's done by the invoking method. Returns conversion from Object to String/number/boolean (JS primitive types). |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
145 |
* @param sourceType the source type |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
146 |
* @param targetType the target type |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
147 |
* @return a guarded invocation that converts from the source type to the target type. |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
148 |
* @throws Exception if something goes wrong |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
149 |
*/ |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
150 |
private static GuardedInvocation convertToTypeNoCast(final Class<?> sourceType, final Class<?> targetType) throws Exception { |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
151 |
final MethodHandle mh = CONVERTERS.get(targetType); |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
152 |
if (mh != null) { |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
153 |
return new GuardedInvocation(mh, null); |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
154 |
} |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
155 |
|
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
156 |
return null; |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
157 |
} |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
158 |
|
16147 | 159 |
private static GuardedInvocation getInvocation(final MethodHandle handle, final Object self, final LinkerServices linkerServices, final CallSiteDescriptor desc) { |
160 |
return Bootstrap.asType(new GuardedInvocation(handle, Guards.getClassGuard(self.getClass())), linkerServices, desc); |
|
161 |
} |
|
162 |
||
163 |
// Used solely in an assertion to figure out if the object we get here is something we in fact expect. Objects |
|
164 |
// linked by NashornLinker should never reach here. |
|
165 |
private static boolean isExpectedObject(final Object obj) { |
|
166 |
return !(NashornLinker.canLinkTypeStatic(obj.getClass())); |
|
167 |
} |
|
168 |
||
169 |
private static GuardedInvocation linkNull(final LinkRequest linkRequest) { |
|
170 |
final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor)linkRequest.getCallSiteDescriptor(); |
|
171 |
final String operator = desc.getFirstOperator(); |
|
172 |
switch (operator) { |
|
173 |
case "new": |
|
174 |
case "call": |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16246
diff
changeset
|
175 |
throw typeError("not.a.function", "null"); |
16147 | 176 |
case "callMethod": |
177 |
case "getMethod": |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16246
diff
changeset
|
178 |
throw typeError("no.such.function", getArgument(linkRequest), "null"); |
16147 | 179 |
case "getProp": |
180 |
case "getElem": |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16246
diff
changeset
|
181 |
throw typeError("cant.get.property", getArgument(linkRequest), "null"); |
16147 | 182 |
case "setProp": |
183 |
case "setElem": |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16246
diff
changeset
|
184 |
throw typeError("cant.set.property", getArgument(linkRequest), "null"); |
16147 | 185 |
default: |
186 |
break; |
|
187 |
} |
|
188 |
throw new AssertionError("unknown call type " + desc); |
|
189 |
} |
|
190 |
||
21691
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
191 |
private static final Map<Class<?>, MethodHandle> CONVERTERS = new HashMap<>(); |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
192 |
static { |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
193 |
CONVERTERS.put(boolean.class, JSType.TO_BOOLEAN.methodHandle()); |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
194 |
CONVERTERS.put(double.class, JSType.TO_NUMBER.methodHandle()); |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
195 |
CONVERTERS.put(int.class, JSType.TO_INTEGER.methodHandle()); |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
196 |
CONVERTERS.put(long.class, JSType.TO_LONG.methodHandle()); |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
197 |
CONVERTERS.put(String.class, JSType.TO_STRING.methodHandle()); |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
198 |
} |
8e284e9a6144
8027828: ClassCastException when converting return value of a Java method to boolean
sundar
parents:
19097
diff
changeset
|
199 |
|
16147 | 200 |
private static String getArgument(final LinkRequest linkRequest) { |
201 |
final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor(); |
|
202 |
if (desc.getNameTokenCount() > 2) { |
|
203 |
return desc.getNameToken(2); |
|
204 |
} |
|
205 |
return ScriptRuntime.safeToString(linkRequest.getArguments()[1]); |
|
206 |
} |
|
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
207 |
|
19097
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
208 |
// cache of @FunctionalInterface method of implementor classes |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
209 |
private static final ClassValue<Method> FUNCTIONAL_IFACE_METHOD = new ClassValue<Method>() { |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
210 |
@Override |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
211 |
protected Method computeValue(final Class<?> type) { |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
212 |
return findFunctionalInterfaceMethod(type); |
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
213 |
} |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
214 |
|
19097
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
215 |
private Method findFunctionalInterfaceMethod(final Class<?> clazz) { |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
216 |
if (clazz == null) { |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
217 |
return null; |
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
218 |
} |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
219 |
|
19097
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
220 |
for (Class<?> iface : clazz.getInterfaces()) { |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
221 |
// check accessiblity up-front |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
222 |
if (! Context.isAccessibleClass(iface)) { |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
223 |
continue; |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
224 |
} |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
225 |
|
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
226 |
// check for @FunctionalInterface |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
227 |
if (iface.isAnnotationPresent(FunctionalInterface.class)) { |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
228 |
// return the first abstract method |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
229 |
for (final Method m : iface.getMethods()) { |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
230 |
if (Modifier.isAbstract(m.getModifiers())) { |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
231 |
return m; |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
232 |
} |
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
233 |
} |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
234 |
} |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
235 |
} |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
236 |
|
19097
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
237 |
// did not find here, try super class |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
238 |
return findFunctionalInterfaceMethod(clazz.getSuperclass()); |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
239 |
} |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
240 |
}; |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
241 |
|
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
242 |
// Returns @FunctionalInterface annotated interface's single abstract |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
243 |
// method. If not found, returns null. |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
244 |
static Method getFunctionalInterfaceMethod(final Class<?> clazz) { |
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
19088
diff
changeset
|
245 |
return FUNCTIONAL_IFACE_METHOD.get(clazz); |
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
16277
diff
changeset
|
246 |
} |
16147 | 247 |
} |