src/jdk/internal/dynalink/beans/BeansLinker.java

changeset 90
5a820fb11814
child 101
f8221ce53c2e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/jdk/internal/dynalink/beans/BeansLinker.java	Thu Feb 14 13:22:26 2013 +0100
     1.3 @@ -0,0 +1,180 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +/*
    1.30 + * This file is available under and governed by the GNU General Public
    1.31 + * License version 2 only, as published by the Free Software Foundation.
    1.32 + * However, the following notice accompanied the original version of this
    1.33 + * file, and Oracle licenses the original version of this file under the BSD
    1.34 + * license:
    1.35 + */
    1.36 +/*
    1.37 +   Copyright 2009-2013 Attila Szegedi
    1.38 +
    1.39 +   Licensed under both the Apache License, Version 2.0 (the "Apache License")
    1.40 +   and the BSD License (the "BSD License"), with licensee being free to
    1.41 +   choose either of the two at their discretion.
    1.42 +
    1.43 +   You may not use this file except in compliance with either the Apache
    1.44 +   License or the BSD License.
    1.45 +
    1.46 +   If you choose to use this file in compliance with the Apache License, the
    1.47 +   following notice applies to you:
    1.48 +
    1.49 +       You may obtain a copy of the Apache License at
    1.50 +
    1.51 +           http://www.apache.org/licenses/LICENSE-2.0
    1.52 +
    1.53 +       Unless required by applicable law or agreed to in writing, software
    1.54 +       distributed under the License is distributed on an "AS IS" BASIS,
    1.55 +       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    1.56 +       implied. See the License for the specific language governing
    1.57 +       permissions and limitations under the License.
    1.58 +
    1.59 +   If you choose to use this file in compliance with the BSD License, the
    1.60 +   following notice applies to you:
    1.61 +
    1.62 +       Redistribution and use in source and binary forms, with or without
    1.63 +       modification, are permitted provided that the following conditions are
    1.64 +       met:
    1.65 +       * Redistributions of source code must retain the above copyright
    1.66 +         notice, this list of conditions and the following disclaimer.
    1.67 +       * Redistributions in binary form must reproduce the above copyright
    1.68 +         notice, this list of conditions and the following disclaimer in the
    1.69 +         documentation and/or other materials provided with the distribution.
    1.70 +       * Neither the name of the copyright holder nor the names of
    1.71 +         contributors may be used to endorse or promote products derived from
    1.72 +         this software without specific prior written permission.
    1.73 +
    1.74 +       THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
    1.75 +       IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
    1.76 +       TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
    1.77 +       PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
    1.78 +       BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.79 +       CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.80 +       SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
    1.81 +       BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    1.82 +       WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
    1.83 +       OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
    1.84 +       ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.85 +*/
    1.86 +
    1.87 +package jdk.internal.dynalink.beans;
    1.88 +
    1.89 +import java.beans.BeanInfo;
    1.90 +import java.lang.invoke.MethodHandles;
    1.91 +import jdk.internal.dynalink.CallSiteDescriptor;
    1.92 +import jdk.internal.dynalink.DynamicLinkerFactory;
    1.93 +import jdk.internal.dynalink.linker.GuardedInvocation;
    1.94 +import jdk.internal.dynalink.linker.GuardingDynamicLinker;
    1.95 +import jdk.internal.dynalink.linker.LinkRequest;
    1.96 +import jdk.internal.dynalink.linker.LinkerServices;
    1.97 +import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker;
    1.98 +
    1.99 +
   1.100 +/**
   1.101 + * A linker for POJOs. Normally used as the ultimate fallback linker by the {@link DynamicLinkerFactory} so it is given
   1.102 + * the chance to link calls to all objects that no other language runtime recognizes. Specifically, this linker will:
   1.103 + * <ul>
   1.104 + * <li>expose all public methods of form {@code setXxx()}, {@code getXxx()}, and {@code isXxx()} as property setters and
   1.105 + * getters for {@code dyn:setProp} and {@code dyn:getProp} operations;</li>
   1.106 + * <li>expose all property getters and setters declared by the class' {@link BeanInfo};</li>
   1.107 + * <li>expose all public methods and methods declared by the class' {@link BeanInfo} for invocation through
   1.108 + * {@code dyn:callMethod} operation;</li>
   1.109 + * <li>expose all public methods and methods declared by the class' {@link BeanInfo} for retrieval for
   1.110 + * {@code dyn:getMethod} operation; the methods thus retrieved can then be invoked using {@code dyn:call};</li>
   1.111 + * <li>expose all public fields as properties, unless there are getters or setters for the properties of the same name;</li>
   1.112 + * <li>expose {@code dyn:getLength}, {@code dyn:getElem} and {@code dyn:setElem} on native Java arrays, as well as
   1.113 + * {@link java.util.List} and {@link java.util.Map} objects; ({@code dyn:getLength} works on any
   1.114 + * {@link java.util.Collection});</li>
   1.115 + * <li>expose a virtual property named {@code length} on Java arrays;</li>
   1.116 + * <li>expose {@code dyn:new} on instances of {@link StaticClass} as calls to constructors, including those static class
   1.117 + * objects that represent Java arrays (their constructors take a single {@code int} parameter representing the length of
   1.118 + * the array to create);</li>
   1.119 + * <li>expose static methods, fields, and properties of classes in a similar manner to how instance method, fields, and
   1.120 + * properties are exposed, on {@link StaticClass} objects.</li>
   1.121 + * <li>expose a virtual property named {@code static} on instances of {@link java.lang.Class} to access their
   1.122 + * {@link StaticClass}.</li>
   1.123 + * </ul>
   1.124 + * <p><strong>Overloaded method resolution</strong> is performed automatically for property setters, methods, and
   1.125 + * constructors. Additionally, manual overloaded method selection is supported by having a call site specify a name for
   1.126 + * a method that contains an explicit signature, i.e. {@code dyn:getMethod:parseInt(String,int)}. You can use
   1.127 + * non-qualified class names in such signatures regardless of those classes' packages, they will match any class with
   1.128 + * the same non-qualified name. You only have to use a fully qualified class name in case non-qualified class names
   1.129 + * would cause selection ambiguity (that is extremely rare).</p>
   1.130 + * <p><strong>Variable argument invocation</strong> is handled for both methods and constructors.</p>
   1.131 + * <p>Currently, only public fields and methods are supported. Any Lookup objects passed in the
   1.132 + * {@link LinkRequest}s are ignored and {@link MethodHandles#publicLookup()} is used instead.</p>
   1.133 + *
   1.134 + * @author Attila Szegedi
   1.135 + */
   1.136 +public class BeansLinker implements GuardingDynamicLinker {
   1.137 +    private static final ClassValue<TypeBasedGuardingDynamicLinker> linkers = new ClassValue<TypeBasedGuardingDynamicLinker>() {
   1.138 +        @Override
   1.139 +        protected TypeBasedGuardingDynamicLinker computeValue(Class<?> clazz) {
   1.140 +            // If ClassValue.put() were public, we could just pre-populate with these known mappings...
   1.141 +            return
   1.142 +                clazz == Class.class ? new ClassLinker() :
   1.143 +                clazz == StaticClass.class ? new StaticClassLinker() :
   1.144 +                DynamicMethod.class.isAssignableFrom(clazz) ? new DynamicMethodLinker() :
   1.145 +                new BeanLinker(clazz);
   1.146 +        }
   1.147 +    };
   1.148 +
   1.149 +    /**
   1.150 +     * Creates a new POJO linker.
   1.151 +     */
   1.152 +    public BeansLinker() {
   1.153 +    }
   1.154 +
   1.155 +    /**
   1.156 +     * Returns a bean linker for a particular single class. Useful when you need to override or extend the behavior of
   1.157 +     * linking for some classes in your language runtime's linker, but still want to delegate to the default behavior in
   1.158 +     * some cases.
   1.159 +     * @param clazz the class
   1.160 +     * @return a bean linker for that class
   1.161 +     */
   1.162 +    public static TypeBasedGuardingDynamicLinker getLinkerForClass(Class<?> clazz) {
   1.163 +        return linkers.get(clazz);
   1.164 +    }
   1.165 +
   1.166 +    @Override
   1.167 +    public GuardedInvocation getGuardedInvocation(LinkRequest request, final LinkerServices linkerServices)
   1.168 +            throws Exception {
   1.169 +        final CallSiteDescriptor callSiteDescriptor = request.getCallSiteDescriptor();
   1.170 +        final int l = callSiteDescriptor.getNameTokenCount();
   1.171 +        // All names conforming to the dynalang MOP should have at least two tokens, the first one being "dyn"
   1.172 +        if(l < 2 || "dyn" != callSiteDescriptor.getNameToken(CallSiteDescriptor.SCHEME)) {
   1.173 +            return null;
   1.174 +        }
   1.175 +
   1.176 +        final Object receiver = request.getReceiver();
   1.177 +        if(receiver == null) {
   1.178 +            // Can't operate on null
   1.179 +            return null;
   1.180 +        }
   1.181 +        return getLinkerForClass(receiver.getClass()).getGuardedInvocation(request, linkerServices);
   1.182 +    }
   1.183 +}
   1.184 \ No newline at end of file

mercurial