NashornStaticClassLinker.java revision 1643:133ea8746b37
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 java.lang.invoke.MethodHandles; 29import java.lang.reflect.Modifier; 30import jdk.internal.module.Modules; 31import jdk.dynalink.CallSiteDescriptor; 32import jdk.dynalink.NamedOperation; 33import jdk.dynalink.StandardOperation; 34import jdk.dynalink.beans.BeansLinker; 35import jdk.dynalink.beans.StaticClass; 36import jdk.dynalink.linker.GuardedInvocation; 37import jdk.dynalink.linker.GuardingDynamicLinker; 38import jdk.dynalink.linker.LinkRequest; 39import jdk.dynalink.linker.LinkerServices; 40import jdk.dynalink.linker.TypeBasedGuardingDynamicLinker; 41import jdk.dynalink.linker.support.Guards; 42import jdk.nashorn.internal.runtime.Context; 43import jdk.nashorn.internal.runtime.ECMAErrors; 44 45/** 46 * Internal linker for {@link StaticClass} objects, only ever used by Nashorn engine and not exposed to other engines. 47 * It is used for extending the "new" operator on StaticClass in order to be able to instantiate interfaces and abstract 48 * classes by passing a ScriptObject or ScriptFunction as their implementation, e.g.: 49 * <pre> 50 * var r = new Runnable() { run: function() { print("Hello World" } } 51 * </pre> 52 * or for SAM types, even just passing a function: 53 * <pre> 54 * var r = new Runnable(function() { print("Hello World" }) 55 * </pre> 56 */ 57final class NashornStaticClassLinker implements TypeBasedGuardingDynamicLinker { 58 private final GuardingDynamicLinker staticClassLinker; 59 60 NashornStaticClassLinker(final BeansLinker beansLinker) { 61 this.staticClassLinker = beansLinker.getLinkerForClass(StaticClass.class); 62 } 63 64 @Override 65 public boolean canLinkType(final Class<?> type) { 66 return type == StaticClass.class; 67 } 68 69 @Override 70 public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices) throws Exception { 71 final Object self = request.getReceiver(); 72 if (self.getClass() != StaticClass.class) { 73 return null; 74 } 75 final Class<?> receiverClass = ((StaticClass) self).getRepresentedClass(); 76 77 Bootstrap.checkReflectionAccess(receiverClass, true); 78 final CallSiteDescriptor desc = request.getCallSiteDescriptor(); 79 // We intercept "new" on StaticClass instances to provide additional capabilities 80 if (NamedOperation.getBaseOperation(desc.getOperation()) == StandardOperation.NEW) { 81 if (! Modifier.isPublic(receiverClass.getModifiers())) { 82 throw ECMAErrors.typeError("new.on.nonpublic.javatype", receiverClass.getName()); 83 } 84 85 // make sure new is on accessible Class 86 Context.checkPackageAccess(receiverClass); 87 88 // Is the class abstract? (This includes interfaces.) 89 if (NashornLinker.isAbstractClass(receiverClass)) { 90 // Change this link request into a link request on the adapter class. 91 final Object[] args = request.getArguments(); 92 final MethodHandles.Lookup lookup = 93 NashornCallSiteDescriptor.getLookupInternal(request.getCallSiteDescriptor()); 94 95 args[0] = JavaAdapterFactory.getAdapterClassFor(new Class<?>[] { receiverClass }, null, lookup); 96 Modules.addReads(lookup.lookupClass().getModule(), ((StaticClass)args[0]).getRepresentedClass().getModule()); 97 final LinkRequest adapterRequest = request.replaceArguments(request.getCallSiteDescriptor(), args); 98 final GuardedInvocation gi = checkNullConstructor(delegate(linkerServices, adapterRequest), receiverClass); 99 // Finally, modify the guard to test for the original abstract class. 100 return gi.replaceMethods(gi.getInvocation(), Guards.getIdentityGuard(self)); 101 } 102 // If the class was not abstract, just delegate linking to the standard StaticClass linker. Make an 103 // additional check to ensure we have a constructor. We could just fall through to the next "return" 104 // statement, except we also insert a call to checkNullConstructor() which throws an ECMAScript TypeError 105 // with a more intuitive message when no suitable constructor is found. 106 return checkNullConstructor(delegate(linkerServices, request), receiverClass); 107 } 108 // In case this was not a "new" operation, just delegate to the the standard StaticClass linker. 109 return delegate(linkerServices, request); 110 } 111 112 private GuardedInvocation delegate(final LinkerServices linkerServices, final LinkRequest request) throws Exception { 113 return NashornBeansLinker.getGuardedInvocation(staticClassLinker, request, linkerServices); 114 } 115 116 private static GuardedInvocation checkNullConstructor(final GuardedInvocation ctorInvocation, final Class<?> receiverClass) { 117 if(ctorInvocation == null) { 118 throw ECMAErrors.typeError("no.constructor.matches.args", receiverClass.getName()); 119 } 120 return ctorInvocation; 121 } 122} 123