NashornBeansLinker.java revision 1705:59d31c4e3f77
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
26package jdk.nashorn.internal.runtime.linker;
27
28import static jdk.nashorn.internal.lookup.Lookup.MH;
29
30import java.lang.invoke.MethodHandle;
31import java.lang.invoke.MethodHandles;
32import java.lang.invoke.MethodType;
33import java.lang.reflect.Method;
34import java.lang.reflect.Modifier;
35import java.util.function.Supplier;
36import jdk.dynalink.CallSiteDescriptor;
37import jdk.dynalink.NamedOperation;
38import jdk.dynalink.SecureLookupSupplier;
39import jdk.dynalink.StandardOperation;
40import jdk.dynalink.beans.BeansLinker;
41import jdk.dynalink.linker.ConversionComparator.Comparison;
42import jdk.dynalink.linker.GuardedInvocation;
43import jdk.dynalink.linker.GuardingDynamicLinker;
44import jdk.dynalink.linker.LinkRequest;
45import jdk.dynalink.linker.LinkerServices;
46import jdk.dynalink.linker.MethodHandleTransformer;
47import jdk.dynalink.linker.support.DefaultInternalObjectFilter;
48import jdk.dynalink.linker.support.Lookup;
49import jdk.nashorn.api.scripting.ScriptUtils;
50import jdk.nashorn.internal.runtime.ConsString;
51import jdk.nashorn.internal.runtime.Context;
52import jdk.nashorn.internal.runtime.ScriptObject;
53import jdk.nashorn.internal.runtime.options.Options;
54
55/**
56 * This linker delegates to a {@code BeansLinker} but passes it a special linker services object that has a modified
57 * {@code compareConversion} method that favors conversion of {@link ConsString} to either {@link String} or
58 * {@link CharSequence}. It also provides a {@link #createHiddenObjectFilter()} method for use with bootstrap that will
59 * ensure that we never pass internal engine objects that should not be externally observable (currently ConsString and
60 * ScriptObject) to Java APIs, but rather that we flatten it into a String. We can't just add this functionality as
61 * custom converters via {@code GuaardingTypeConverterFactory}, since they are not consulted when
62 * the target method handle parameter signature is {@code Object}. This linker also makes sure that primitive
63 * {@link String} operations can be invoked on a {@link ConsString}, and allows invocation of objects implementing
64 * the {@link FunctionalInterface} attribute.
65 */
66public class NashornBeansLinker implements GuardingDynamicLinker {
67    // System property to control whether to wrap ScriptObject->ScriptObjectMirror for
68    // Object type arguments of Java method calls, field set and array set.
69    private static final boolean MIRROR_ALWAYS = Options.getBooleanProperty("nashorn.mirror.always", true);
70
71    private static final MethodHandle EXPORT_ARGUMENT;
72    private static final MethodHandle IMPORT_RESULT;
73    private static final MethodHandle FILTER_CONSSTRING;
74
75    static {
76        final Lookup lookup  = new Lookup(MethodHandles.lookup());
77        EXPORT_ARGUMENT      = lookup.findOwnStatic("exportArgument", Object.class, Object.class);
78        IMPORT_RESULT        = lookup.findOwnStatic("importResult", Object.class, Object.class);
79        FILTER_CONSSTRING    = lookup.findOwnStatic("consStringFilter", Object.class, Object.class);
80    }
81
82    // cache of @FunctionalInterface method of implementor classes
83    private static final ClassValue<String> FUNCTIONAL_IFACE_METHOD_NAME = new ClassValue<String>() {
84        @Override
85        protected String computeValue(final Class<?> type) {
86            return findFunctionalInterfaceMethodName(type);
87        }
88    };
89
90    private final BeansLinker beansLinker;
91
92    NashornBeansLinker(final BeansLinker beansLinker) {
93        this.beansLinker = beansLinker;
94    }
95
96    @Override
97    public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
98        final Object self = linkRequest.getReceiver();
99        final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
100        if (self instanceof ConsString) {
101            // In order to treat ConsString like a java.lang.String we need a link request with a string receiver.
102            final Object[] arguments = linkRequest.getArguments();
103            arguments[0] = "";
104            final LinkRequest forgedLinkRequest = linkRequest.replaceArguments(desc, arguments);
105            final GuardedInvocation invocation = getGuardedInvocation(beansLinker, forgedLinkRequest, linkerServices);
106            // If an invocation is found we add a filter that makes it work for both Strings and ConsStrings.
107            return invocation == null ? null : invocation.filterArguments(0, FILTER_CONSSTRING);
108        }
109
110        if (self != null && NamedOperation.getBaseOperation(desc.getOperation()) == StandardOperation.CALL) {
111            // Support CALL on any object that supports some @FunctionalInterface
112            // annotated interface. This way Java method, constructor references or
113            // implementations of java.util.function.* interfaces can be called as though
114            // those are script functions.
115            final String name = getFunctionalInterfaceMethodName(self.getClass());
116            if (name != null) {
117                final MethodType callType = desc.getMethodType();
118                // drop callee (Undefined ScriptFunction) and change the request to be CALL_METHOD:<name>
119                final CallSiteDescriptor newDesc = new CallSiteDescriptor(
120                        NashornCallSiteDescriptor.getLookupInternal(desc),
121                        new NamedOperation(StandardOperation.CALL_METHOD, name),
122                        desc.getMethodType().dropParameterTypes(1, 2));
123                final GuardedInvocation gi = getGuardedInvocation(beansLinker,
124                        linkRequest.replaceArguments(newDesc, linkRequest.getArguments()),
125                        new NashornBeansLinkerServices(linkerServices));
126
127                // drop 'thiz' passed from the script.
128                return gi.replaceMethods(
129                    MH.dropArguments(linkerServices.filterInternalObjects(gi.getInvocation()), 1, callType.parameterType(1)),
130                    gi.getGuard());
131            }
132        }
133        return getGuardedInvocation(beansLinker, linkRequest, linkerServices);
134    }
135
136    /**
137     * Delegates to the specified linker but injects its linker services wrapper so that it will apply all special
138     * conversions that this class does.
139     * @param delegateLinker the linker to which the actual work is delegated to.
140     * @param linkRequest the delegated link request
141     * @param linkerServices the original link services that will be augmented with special conversions
142     * @return the guarded invocation from the delegate, possibly augmented with special conversions
143     * @throws Exception if the delegate throws an exception
144     */
145    public static GuardedInvocation getGuardedInvocation(final GuardingDynamicLinker delegateLinker, final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
146        return delegateLinker.getGuardedInvocation(linkRequest, new NashornBeansLinkerServices(linkerServices));
147    }
148
149    @SuppressWarnings("unused")
150    private static Object exportArgument(final Object arg) {
151        return exportArgument(arg, MIRROR_ALWAYS);
152    }
153
154    static Object exportArgument(final Object arg, final boolean mirrorAlways) {
155        if (arg instanceof ConsString) {
156            return arg.toString();
157        } else if (mirrorAlways && arg instanceof ScriptObject) {
158            return ScriptUtils.wrap(arg);
159        } else {
160            return arg;
161        }
162    }
163
164    @SuppressWarnings("unused")
165    private static Object importResult(final Object arg) {
166        return ScriptUtils.unwrap(arg);
167    }
168
169    @SuppressWarnings("unused")
170    private static Object consStringFilter(final Object arg) {
171        return arg instanceof ConsString ? arg.toString() : arg;
172    }
173
174    private static String findFunctionalInterfaceMethodName(final Class<?> clazz) {
175        if (clazz == null) {
176            return null;
177        }
178
179        for (final Class<?> iface : clazz.getInterfaces()) {
180            // check accessibility up-front
181            if (! Context.isAccessibleClass(iface)) {
182                continue;
183            }
184
185            // check for @FunctionalInterface
186            if (iface.isAnnotationPresent(FunctionalInterface.class)) {
187                // return the first abstract method
188                for (final Method m : iface.getMethods()) {
189                    if (Modifier.isAbstract(m.getModifiers()) && !isOverridableObjectMethod(m)) {
190                        return m.getName();
191                    }
192                }
193            }
194        }
195
196        // did not find here, try super class
197        return findFunctionalInterfaceMethodName(clazz.getSuperclass());
198    }
199
200    // is this an overridable java.lang.Object method?
201    private static boolean isOverridableObjectMethod(final Method m) {
202        switch (m.getName()) {
203            case "equals":
204                if (m.getReturnType() == boolean.class) {
205                    final Class<?>[] params = m.getParameterTypes();
206                    return params.length == 1 && params[0] == Object.class;
207                }
208                return false;
209            case "hashCode":
210                return m.getReturnType() == int.class && m.getParameterCount() == 0;
211            case "toString":
212                return m.getReturnType() == String.class && m.getParameterCount() == 0;
213        }
214        return false;
215    }
216
217    // Returns @FunctionalInterface annotated interface's single abstract
218    // method name. If not found, returns null.
219    static String getFunctionalInterfaceMethodName(final Class<?> clazz) {
220        return FUNCTIONAL_IFACE_METHOD_NAME.get(clazz);
221    }
222
223    static MethodHandleTransformer createHiddenObjectFilter() {
224        return new DefaultInternalObjectFilter(EXPORT_ARGUMENT, MIRROR_ALWAYS ? IMPORT_RESULT : null);
225    }
226
227    private static class NashornBeansLinkerServices implements LinkerServices {
228        private final LinkerServices linkerServices;
229
230        NashornBeansLinkerServices(final LinkerServices linkerServices) {
231            this.linkerServices = linkerServices;
232        }
233
234        @Override
235        public MethodHandle asType(final MethodHandle handle, final MethodType fromType) {
236            return linkerServices.asType(handle, fromType);
237        }
238
239        @Override
240        public MethodHandle getTypeConverter(final Class<?> sourceType, final Class<?> targetType) {
241            return linkerServices.getTypeConverter(sourceType, targetType);
242        }
243
244        @Override
245        public boolean canConvert(final Class<?> from, final Class<?> to) {
246            return linkerServices.canConvert(from, to);
247        }
248
249        @Override
250        public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest) throws Exception {
251            return linkerServices.getGuardedInvocation(linkRequest);
252        }
253
254        @Override
255        public Comparison compareConversion(final Class<?> sourceType, final Class<?> targetType1, final Class<?> targetType2) {
256            if (sourceType == ConsString.class) {
257                if (String.class == targetType1 || CharSequence.class == targetType1) {
258                    return Comparison.TYPE_1_BETTER;
259                }
260
261                if (String.class == targetType2 || CharSequence.class == targetType2) {
262                    return Comparison.TYPE_2_BETTER;
263                }
264            }
265            return linkerServices.compareConversion(sourceType, targetType1, targetType2);
266        }
267
268        @Override
269        public MethodHandle filterInternalObjects(final MethodHandle target) {
270            return linkerServices.filterInternalObjects(target);
271        }
272
273        @Override
274        public <T> T getWithLookup(final Supplier<T> operation, final SecureLookupSupplier lookupSupplier) {
275            return linkerServices.getWithLookup(operation, lookupSupplier);
276        }
277    }
278}
279