Path: blob/aarch64-shenandoah-jdk8u272-b10/nashorn/src/jdk/internal/dynalink/beans/GuardedInvocationComponent.java
48580 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.MethodHandle;86import jdk.internal.dynalink.linker.GuardedInvocation;8788/**89* Represents one component for a GuardedInvocation of a potentially composite operation of an90* {@link AbstractJavaLinker}. In addition to holding a guarded invocation, it holds semantic information about its91* guard. All guards produced in the AbstractJavaLinker are either "Class.isInstance()" or "getClass() == clazz"92* expressions. This allows choosing the most restrictive guard as the guard for the composition of two components.93* @author Attila Szegedi94* @version $Id: $95*/96class GuardedInvocationComponent {97enum ValidationType {98NONE, // No guard; the operation can be linked unconditionally (quite rare); least strict.99INSTANCE_OF, // "validatorClass.isInstance(obj)" guard100EXACT_CLASS, // "obj.getClass() == validatorClass" guard; most strict.101IS_ARRAY, // "obj.getClass().isArray()"102}103104private final GuardedInvocation guardedInvocation;105private final Validator validator;106107GuardedInvocationComponent(final MethodHandle invocation) {108this(invocation, null, ValidationType.NONE);109}110111GuardedInvocationComponent(final MethodHandle invocation, final MethodHandle guard, final ValidationType validationType) {112this(invocation, guard, null, validationType);113}114115GuardedInvocationComponent(final MethodHandle invocation, final MethodHandle guard, final Class<?> validatorClass,116final ValidationType validationType) {117this(invocation, guard, new Validator(validatorClass, validationType));118}119120GuardedInvocationComponent(final GuardedInvocation guardedInvocation, final Class<?> validatorClass,121final ValidationType validationType) {122this(guardedInvocation, new Validator(validatorClass, validationType));123}124125GuardedInvocationComponent replaceInvocation(final MethodHandle newInvocation) {126return replaceInvocation(newInvocation, guardedInvocation.getGuard());127}128129GuardedInvocationComponent replaceInvocation(final MethodHandle newInvocation, final MethodHandle newGuard) {130return new GuardedInvocationComponent(guardedInvocation.replaceMethods(newInvocation,131newGuard), validator);132}133134private GuardedInvocationComponent(final MethodHandle invocation, final MethodHandle guard, final Validator validator) {135this(new GuardedInvocation(invocation, guard), validator);136}137138private GuardedInvocationComponent(final GuardedInvocation guardedInvocation, final Validator validator) {139this.guardedInvocation = guardedInvocation;140this.validator = validator;141}142143GuardedInvocation getGuardedInvocation() {144return guardedInvocation;145}146147Class<?> getValidatorClass() {148return validator.validatorClass;149}150151ValidationType getValidationType() {152return validator.validationType;153}154155GuardedInvocationComponent compose(final MethodHandle compositeInvocation, final MethodHandle otherGuard,156final Class<?> otherValidatorClass, final ValidationType otherValidationType) {157final Validator compositeValidator = validator.compose(new Validator(otherValidatorClass, otherValidationType));158final MethodHandle compositeGuard = compositeValidator == validator ? guardedInvocation.getGuard() : otherGuard;159return new GuardedInvocationComponent(compositeInvocation, compositeGuard, compositeValidator);160}161162private static class Validator {163/*private*/ final Class<?> validatorClass;164/*private*/ final ValidationType validationType;165166Validator(final Class<?> validatorClass, final ValidationType validationType) {167this.validatorClass = validatorClass;168this.validationType = validationType;169}170171Validator compose(final Validator other) {172if(other.validationType == ValidationType.NONE) {173return this;174}175switch(validationType) {176case NONE:177return other;178case INSTANCE_OF:179switch(other.validationType) {180case INSTANCE_OF:181if(isAssignableFrom(other)) {182return other;183} else if(other.isAssignableFrom(this)) {184return this;185}186break;187case EXACT_CLASS:188if(isAssignableFrom(other)) {189return other;190}191break;192case IS_ARRAY:193if(validatorClass.isArray()) {194return this;195}196break;197default:198throw new AssertionError();199}200break;201case EXACT_CLASS:202switch(other.validationType) {203case INSTANCE_OF:204if(other.isAssignableFrom(this)) {205return this;206}207break;208case EXACT_CLASS:209if(validatorClass == other.validatorClass) {210return this;211}212break;213case IS_ARRAY:214if(validatorClass.isArray()) {215return this;216}217break;218default:219throw new AssertionError();220}221break;222case IS_ARRAY:223switch(other.validationType) {224case INSTANCE_OF:225case EXACT_CLASS:226if(other.validatorClass.isArray()) {227return other;228}229break;230case IS_ARRAY:231return this;232default:233throw new AssertionError();234}235break;236default:237throw new AssertionError();238}239throw new AssertionError("Incompatible composition " + this + " vs " + other);240}241242private boolean isAssignableFrom(final Validator other) {243return validatorClass.isAssignableFrom(other.validatorClass);244}245246@Override247public String toString() {248return "Validator[" + validationType + (validatorClass == null ? "" : (" " + validatorClass.getName())) + "]";249}250}251}252253254