Path: blob/jdk8u272-b10-aarch32-20201026/nashorn/src/jdk/internal/dynalink/beans/BeansLinker.java
48797 views
/*1* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file, and Oracle licenses the original version of this file under the BSD30* license:31*/32/*33Copyright 2009-2013 Attila Szegedi3435Licensed under both the Apache License, Version 2.0 (the "Apache License")36and the BSD License (the "BSD License"), with licensee being free to37choose either of the two at their discretion.3839You may not use this file except in compliance with either the Apache40License or the BSD License.4142If you choose to use this file in compliance with the Apache License, the43following notice applies to you:4445You may obtain a copy of the Apache License at4647http://www.apache.org/licenses/LICENSE-2.04849Unless required by applicable law or agreed to in writing, software50distributed under the License is distributed on an "AS IS" BASIS,51WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or52implied. See the License for the specific language governing53permissions and limitations under the License.5455If you choose to use this file in compliance with the BSD License, the56following notice applies to you:5758Redistribution and use in source and binary forms, with or without59modification, are permitted provided that the following conditions are60met:61* Redistributions of source code must retain the above copyright62notice, this list of conditions and the following disclaimer.63* Redistributions in binary form must reproduce the above copyright64notice, this list of conditions and the following disclaimer in the65documentation and/or other materials provided with the distribution.66* Neither the name of the copyright holder nor the names of67contributors may be used to endorse or promote products derived from68this software without specific prior written permission.6970THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS71IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED72TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A73PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER74BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR75CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF76SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR77BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,78WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR79OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF80ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.81*/8283package jdk.internal.dynalink.beans;8485import java.lang.invoke.MethodHandles;86import java.util.Collection;87import java.util.Collections;88import jdk.internal.dynalink.CallSiteDescriptor;89import jdk.internal.dynalink.DynamicLinkerFactory;90import jdk.internal.dynalink.linker.GuardedInvocation;91import jdk.internal.dynalink.linker.GuardingDynamicLinker;92import jdk.internal.dynalink.linker.LinkRequest;93import jdk.internal.dynalink.linker.LinkerServices;94import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker;9596/**97* A linker for POJOs. Normally used as the ultimate fallback linker by the {@link DynamicLinkerFactory} so it is given98* the chance to link calls to all objects that no other language runtime recognizes. Specifically, this linker will:99* <ul>100* <li>expose all public methods of form {@code setXxx()}, {@code getXxx()}, and {@code isXxx()} as property setters and101* getters for {@code dyn:setProp} and {@code dyn:getProp} operations;</li>102* <li>expose all public methods for invocation through {@code dyn:callMethod} operation;</li>103* <li>expose all public methods for retrieval for {@code dyn:getMethod} operation; the methods thus retrieved can then104* be invoked using {@code dyn:call};</li>105* <li>expose all public fields as properties, unless there are getters or setters for the properties of the same name;</li>106* <li>expose {@code dyn:getLength}, {@code dyn:getElem} and {@code dyn:setElem} on native Java arrays, as well as107* {@link java.util.List} and {@link java.util.Map} objects; ({@code dyn:getLength} works on any108* {@link java.util.Collection});</li>109* <li>expose a virtual property named {@code length} on Java arrays;</li>110* <li>expose {@code dyn:new} on instances of {@link StaticClass} as calls to constructors, including those static class111* objects that represent Java arrays (their constructors take a single {@code int} parameter representing the length of112* the array to create);</li>113* <li>expose static methods, fields, and properties of classes in a similar manner to how instance method, fields, and114* properties are exposed, on {@link StaticClass} objects.</li>115* <li>expose a virtual property named {@code static} on instances of {@link java.lang.Class} to access their116* {@link StaticClass}.</li>117* </ul>118* <p><strong>Overloaded method resolution</strong> is performed automatically for property setters, methods, and119* constructors. Additionally, manual overloaded method selection is supported by having a call site specify a name for120* a method that contains an explicit signature, i.e. {@code dyn:getMethod:parseInt(String,int)}. You can use121* non-qualified class names in such signatures regardless of those classes' packages, they will match any class with122* the same non-qualified name. You only have to use a fully qualified class name in case non-qualified class names123* would cause selection ambiguity (that is extremely rare).</p>124* <p><strong>Variable argument invocation</strong> is handled for both methods and constructors.</p>125* <p>Currently, only public fields and methods are supported. Any Lookup objects passed in the126* {@link LinkRequest}s are ignored and {@link MethodHandles#publicLookup()} is used instead.</p>127*128* @author Attila Szegedi129*/130public class BeansLinker implements GuardingDynamicLinker {131private static final ClassValue<TypeBasedGuardingDynamicLinker> linkers = new ClassValue<TypeBasedGuardingDynamicLinker>() {132@Override133protected TypeBasedGuardingDynamicLinker computeValue(final Class<?> clazz) {134// If ClassValue.put() were public, we could just pre-populate with these known mappings...135return136clazz == Class.class ? new ClassLinker() :137clazz == StaticClass.class ? new StaticClassLinker() :138DynamicMethod.class.isAssignableFrom(clazz) ? new DynamicMethodLinker() :139new BeanLinker(clazz);140}141};142143/**144* Creates a new POJO linker.145*/146public BeansLinker() {147}148149/**150* Returns a bean linker for a particular single class. Useful when you need to override or extend the behavior of151* linking for some classes in your language runtime's linker, but still want to delegate to the default behavior in152* some cases.153* @param clazz the class154* @return a bean linker for that class155*/156public static TypeBasedGuardingDynamicLinker getLinkerForClass(final Class<?> clazz) {157return linkers.get(clazz);158}159160/**161* Returns true if the object is a Dynalink Java dynamic method.162*163* @param obj the object we want to test for being a dynamic method164* @return true if it is a dynamic method, false otherwise.165*/166public static boolean isDynamicMethod(final Object obj) {167return obj instanceof DynamicMethod;168}169170/**171* Returns true if the object is a Dynalink Java constructor.172*173* @param obj the object we want to test for being a constructor174* @return true if it is a constructor, false otherwise.175*/176public static boolean isDynamicConstructor(final Object obj) {177return obj instanceof DynamicMethod && ((DynamicMethod)obj).isConstructor();178}179180/**181* Return the dynamic method of constructor of the given class and the given signature.182* @param clazz the class183* @param signature full signature of the constructor184* @return DynamicMethod for the constructor185*/186public static Object getConstructorMethod(final Class<?> clazz, final String signature) {187return StaticClassLinker.getConstructorMethod(clazz, signature);188}189190/**191* Returns a collection of names of all readable instance properties of a class.192* @param clazz the class193* @return a collection of names of all readable instance properties of a class.194*/195public static Collection<String> getReadableInstancePropertyNames(final Class<?> clazz) {196final TypeBasedGuardingDynamicLinker linker = getLinkerForClass(clazz);197if(linker instanceof BeanLinker) {198return ((BeanLinker)linker).getReadablePropertyNames();199}200return Collections.emptySet();201}202203/**204* Returns a collection of names of all writable instance properties of a class.205* @param clazz the class206* @return a collection of names of all writable instance properties of a class.207*/208public static Collection<String> getWritableInstancePropertyNames(final Class<?> clazz) {209final TypeBasedGuardingDynamicLinker linker = getLinkerForClass(clazz);210if(linker instanceof BeanLinker) {211return ((BeanLinker)linker).getWritablePropertyNames();212}213return Collections.emptySet();214}215216/**217* Returns a collection of names of all instance methods of a class.218* @param clazz the class219* @return a collection of names of all instance methods of a class.220*/221public static Collection<String> getInstanceMethodNames(final Class<?> clazz) {222final TypeBasedGuardingDynamicLinker linker = getLinkerForClass(clazz);223if(linker instanceof BeanLinker) {224return ((BeanLinker)linker).getMethodNames();225}226return Collections.emptySet();227}228229/**230* Returns a collection of names of all readable static properties of a class.231* @param clazz the class232* @return a collection of names of all readable static properties of a class.233*/234public static Collection<String> getReadableStaticPropertyNames(final Class<?> clazz) {235return StaticClassLinker.getReadableStaticPropertyNames(clazz);236}237238/**239* Returns a collection of names of all writable static properties of a class.240* @param clazz the class241* @return a collection of names of all writable static properties of a class.242*/243public static Collection<String> getWritableStaticPropertyNames(final Class<?> clazz) {244return StaticClassLinker.getWritableStaticPropertyNames(clazz);245}246247/**248* Returns a collection of names of all static methods of a class.249* @param clazz the class250* @return a collection of names of all static methods of a class.251*/252public static Collection<String> getStaticMethodNames(final Class<?> clazz) {253return StaticClassLinker.getStaticMethodNames(clazz);254}255256@Override257public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices)258throws Exception {259final CallSiteDescriptor callSiteDescriptor = request.getCallSiteDescriptor();260final int l = callSiteDescriptor.getNameTokenCount();261// All names conforming to the dynalang MOP should have at least two tokens, the first one being "dyn"262if(l < 2 || "dyn" != callSiteDescriptor.getNameToken(CallSiteDescriptor.SCHEME)) {263return null;264}265266final Object receiver = request.getReceiver();267if(receiver == null) {268// Can't operate on null269return null;270}271return getLinkerForClass(receiver.getClass()).getGuardedInvocation(request, linkerServices);272}273}274275276