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