SimpleDynamicMethod.java revision 1551:f3b883bec2d0
14Srgrimes/*
24Srgrimes * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
34Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44Srgrimes *
54Srgrimes * This code is free software; you can redistribute it and/or modify it
64Srgrimes * under the terms of the GNU General Public License version 2 only, as
74Srgrimes * published by the Free Software Foundation.  Oracle designates this
84Srgrimes * particular file as subject to the "Classpath" exception as provided
94Srgrimes * by Oracle in the LICENSE file that accompanied this code.
104Srgrimes *
114Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
124Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
134Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
144Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
154Srgrimes * accompanied this code).
164Srgrimes *
174Srgrimes * You should have received a copy of the GNU General Public License version
184Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
194Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
204Srgrimes *
214Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
224Srgrimes * or visit www.oracle.com if you need additional information or have any
234Srgrimes * questions.
244Srgrimes */
254Srgrimes
264Srgrimes/*
274Srgrimes * This file is available under and governed by the GNU General Public
284Srgrimes * License version 2 only, as published by the Free Software Foundation.
294Srgrimes * However, the following notice accompanied the original version of this
304Srgrimes * file, and Oracle licenses the original version of this file under the BSD
314Srgrimes * license:
32619Srgrimes */
3350477Speter/*
344Srgrimes   Copyright 2009-2013 Attila Szegedi
354Srgrimes
3616363Sasami   Licensed under both the Apache License, Version 2.0 (the "Apache License")
3716363Sasami   and the BSD License (the "BSD License"), with licensee being free to
3816363Sasami   choose either of the two at their discretion.
3916363Sasami
40720Swollman   You may not use this file except in compliance with either the Apache
414474Sbde   License or the BSD License.
42720Swollman
434682Sphk   If you choose to use this file in compliance with the Apache License, the
449223Sbde   following notice applies to you:
454Srgrimes
464Srgrimes       You may obtain a copy of the Apache License at
474Srgrimes
484Srgrimes           http://www.apache.org/licenses/LICENSE-2.0
494Srgrimes
504Srgrimes       Unless required by applicable law or agreed to in writing, software
514Srgrimes       distributed under the License is distributed on an "AS IS" BASIS,
529223Sbde       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
534Srgrimes       implied. See the License for the specific language governing
544Srgrimes       permissions and limitations under the License.
554Srgrimes
569223Sbde   If you choose to use this file in compliance with the BSD License, the
579223Sbde   following notice applies to you:
589223Sbde
599223Sbde       Redistribution and use in source and binary forms, with or without
609223Sbde       modification, are permitted provided that the following conditions are
619223Sbde       met:
629223Sbde       * Redistributions of source code must retain the above copyright
639223Sbde         notice, this list of conditions and the following disclaimer.
649223Sbde       * Redistributions in binary form must reproduce the above copyright
659223Sbde         notice, this list of conditions and the following disclaimer in the
669223Sbde         documentation and/or other materials provided with the distribution.
679223Sbde       * Neither the name of the copyright holder nor the names of
689223Sbde         contributors may be used to endorse or promote products derived from
694Srgrimes         this software without specific prior written permission.
704Srgrimes
719223Sbde       THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
729223Sbde       IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
73131643Simp       TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
744Srgrimes       PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
759223Sbde       BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
7612366Sbde       CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
774Srgrimes       SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
784Srgrimes       BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
794Srgrimes       WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
809223Sbde       OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
814Srgrimes       ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
824Srgrimes*/
834Srgrimes
844Srgrimespackage jdk.dynalink.beans;
8542332Syokota
8642332Syokotaimport java.lang.invoke.MethodHandle;
8712366Sbdeimport java.lang.invoke.MethodType;
884Srgrimesimport jdk.dynalink.CallSiteDescriptor;
894Srgrimes
9012366Sbde/**
914Srgrimes * A dynamic method bound to exactly one Java method or constructor that is not caller sensitive. Since its target is
924Srgrimes * not caller sensitive, this class pre-caches its method handle and always returns it from the call to
934Srgrimes * {@link #getTarget(CallSiteDescriptor)}. Can be used in general to represents dynamic methods bound to a single method handle,
944Srgrimes * even if that handle is not mapped to a Java method, i.e. as a wrapper around field getters/setters, array element
954Srgrimes * getters/setters, etc.
969223Sbde */
979223Sbdeclass SimpleDynamicMethod extends SingleDynamicMethod {
984Srgrimes    private final MethodHandle target;
9912366Sbde    private final boolean constructor;
1004Srgrimes
1014Srgrimes    /**
1024Srgrimes     * Creates a new simple dynamic method, with a name constructed from the class name, method name, and handle
1034Srgrimes     * signature.
1044Srgrimes     *
1054Srgrimes     * @param target the target method handle
1064Srgrimes     * @param clazz the class declaring the method
1074Srgrimes     * @param name the simple name of the method
10812366Sbde     */
1094Srgrimes    SimpleDynamicMethod(final MethodHandle target, final Class<?> clazz, final String name) {
1104474Sbde        this(target, clazz, name, false);
111    }
112
113    /**
114     * Creates a new simple dynamic method, with a name constructed from the class name, method name, and handle
115     * signature.
116     *
117     * @param target the target method handle
118     * @param clazz the class declaring the method
119     * @param name the simple name of the method
120     * @param constructor does this represent a constructor?
121     */
122    SimpleDynamicMethod(final MethodHandle target, final Class<?> clazz, final String name, final boolean constructor) {
123        super(getName(target, clazz, name, constructor));
124        this.target = target;
125        this.constructor = constructor;
126    }
127
128    private static String getName(final MethodHandle target, final Class<?> clazz, final String name, final boolean constructor) {
129        return getMethodNameWithSignature(target.type(), constructor ? name : getClassAndMethodName(clazz, name), !constructor);
130    }
131
132    @Override
133    boolean isVarArgs() {
134        return target.isVarargsCollector();
135    }
136
137    @Override
138    MethodType getMethodType() {
139        return target.type();
140    }
141
142    @Override
143    MethodHandle getTarget(final CallSiteDescriptor desc) {
144        return target;
145    }
146
147    @Override
148    boolean isConstructor() {
149        return constructor;
150    }
151}
152