1/*
2 * Copyright (c) 2015, 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.dynalink.beans;
27
28import java.lang.invoke.MethodHandle;
29import jdk.dynalink.DynamicLinkerFactory;
30import jdk.dynalink.NamedOperation;
31import jdk.dynalink.NoSuchDynamicMethodException;
32import jdk.dynalink.StandardOperation;
33import jdk.dynalink.linker.LinkRequest;
34import jdk.dynalink.linker.LinkerServices;
35
36/**
37 * A factory for creating method handles for linking missing member behavior
38 * in {@link BeansLinker}. BeansLinker links these method handles into guarded
39 * invocations for link requests specifying {@code GET_*} and {@code SET_*}
40 * {@link StandardOperation}s when it is either certain or possible that the
41 * requested member (property, method, or element) is missing. They will be
42 * linked both for {@link NamedOperation named} and unnamed operations. The
43 * implementer must ensure that the parameter types of the returned method
44 * handle match the parameter types of the call site described in the link
45 * request. The return types can differ, though, to allow
46 * {@link DynamicLinkerFactory#setPrelinkTransformer(jdk.dynalink.linker.GuardedInvocationTransformer)}
47 * late return type transformations}. It is allowed to return {@code null} for a
48 * method handle if the default behavior is sufficient.
49 * <h2>Default missing member behavior</h2>
50 * When a {@link BeansLinker} is configured without a missing member handler
51 * factory, or the factory returns {@code null} for a particular handler
52 * creation invocation, the default behavior is used. The default behavior is to
53 * return {@code null} from
54 * {@link BeansLinker#getGuardedInvocation(LinkRequest, LinkerServices)} when it
55 * can be determined at link time that the linked operation will never address
56 * an existing member. This lets the {@code DynamicLinker} attempt the next
57 * linker if there is one, or ultimately fail the link request with
58 * {@link NoSuchDynamicMethodException}. For other cases (typically all unnamed
59 * member operations as well as most named operations on collection elements)
60 * {@code BeansLinker} will produce a conditional linkage that will return
61 * {@code null} when invoked at runtime with a name that does not match any
62 * member for getters and silently ignore the passed values for setters.
63 * <h2>Implementing exception-throwing behavior</h2>
64 * Note that if the language-specific behavior for an operation on a missing
65 * member is to throw an exception then the factory should produce a method
66 * handle that throws the exception when invoked, and must not throw an
67 * exception itself, as the linkage for the missing member is often conditional.
68 *
69 * @see BeansLinker#BeansLinker(MissingMemberHandlerFactory)
70 */
71@FunctionalInterface
72public interface MissingMemberHandlerFactory {
73    /**
74     * Returns a method handle suitable for implementing missing member behavior
75     * for a particular link request. See the class description for details.
76     * @param linkRequest the current link request
77     * @param linkerServices the current link services
78     * @return a method handle that can be invoked if the property, element, or
79     * method being addressed by an operation is missing. The return value can
80     * be null.
81     * @throws Exception if the operation fails for any reason. Please observe
82     * the class documentation notes for implementing exception-throwing
83     * missing member behavior.
84     */
85    public MethodHandle createMissingMemberHandler(LinkRequest linkRequest, LinkerServices linkerServices) throws Exception;
86}
87