NashornBeansLinker.java revision 1645:15d52fdd9168
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())) {
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    // Returns @FunctionalInterface annotated interface's single abstract
201    // method name. If not found, returns null.
202    static String getFunctionalInterfaceMethodName(final Class<?> clazz) {
203        return FUNCTIONAL_IFACE_METHOD_NAME.get(clazz);
204    }
205
206    static MethodHandleTransformer createHiddenObjectFilter() {
207        return new DefaultInternalObjectFilter(EXPORT_ARGUMENT, MIRROR_ALWAYS ? IMPORT_RESULT : null);
208    }
209
210    private static class NashornBeansLinkerServices implements LinkerServices {
211        private final LinkerServices linkerServices;
212
213        NashornBeansLinkerServices(final LinkerServices linkerServices) {
214            this.linkerServices = linkerServices;
215        }
216
217        @Override
218        public MethodHandle asType(final MethodHandle handle, final MethodType fromType) {
219            return linkerServices.asType(handle, fromType);
220        }
221
222        @Override
223        public MethodHandle getTypeConverter(final Class<?> sourceType, final Class<?> targetType) {
224            return linkerServices.getTypeConverter(sourceType, targetType);
225        }
226
227        @Override
228        public boolean canConvert(final Class<?> from, final Class<?> to) {
229            return linkerServices.canConvert(from, to);
230        }
231
232        @Override
233        public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest) throws Exception {
234            return linkerServices.getGuardedInvocation(linkRequest);
235        }
236
237        @Override
238        public Comparison compareConversion(final Class<?> sourceType, final Class<?> targetType1, final Class<?> targetType2) {
239            if (sourceType == ConsString.class) {
240                if (String.class == targetType1 || CharSequence.class == targetType1) {
241                    return Comparison.TYPE_1_BETTER;
242                }
243
244                if (String.class == targetType2 || CharSequence.class == targetType2) {
245                    return Comparison.TYPE_2_BETTER;
246                }
247            }
248            return linkerServices.compareConversion(sourceType, targetType1, targetType2);
249        }
250
251        @Override
252        public MethodHandle filterInternalObjects(final MethodHandle target) {
253            return linkerServices.filterInternalObjects(target);
254        }
255
256        @Override
257        public <T> T getWithLookup(final Supplier<T> operation, final SecureLookupSupplier lookupSupplier) {
258            return linkerServices.getWithLookup(operation, lookupSupplier);
259        }
260    }
261}
262