Path: blob/master/src/jdk.dynalink/share/classes/jdk/dynalink/beans/GuardedInvocationComponent.java
41161 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 Szegedi3435Redistribution and use in source and binary forms, with or without36modification, are permitted provided that the following conditions are37met:38* Redistributions of source code must retain the above copyright39notice, this list of conditions and the following disclaimer.40* Redistributions in binary form must reproduce the above copyright41notice, this list of conditions and the following disclaimer in the42documentation and/or other materials provided with the distribution.43* Neither the name of the copyright holder nor the names of44contributors may be used to endorse or promote products derived from45this software without specific prior written permission.4647THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS48IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED49TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A50PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER51BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR52CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF53SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR54BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,55WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR56OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF57ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.58*/5960package jdk.dynalink.beans;6162import java.lang.invoke.MethodHandle;63import jdk.dynalink.linker.GuardedInvocation;6465/**66* Represents one component for a GuardedInvocation of a potentially multi-namespace operation of an67* {@link AbstractJavaLinker}. In addition to holding a guarded invocation, it holds semantic information about its68* guard. All guards produced in the AbstractJavaLinker are either "Class.isInstance()" or "getClass() == clazz"69* expressions. This allows choosing the most restrictive guard as the guard for the composition of two components.70*/71class GuardedInvocationComponent {72enum ValidationType {73NONE, // No guard; the operation can be linked unconditionally (quite rare); least strict.74INSTANCE_OF, // "validatorClass.isInstance(obj)" guard75EXACT_CLASS, // "obj.getClass() == validatorClass" guard; most strict.76IS_ARRAY, // "obj.getClass().isArray()"77}7879private final GuardedInvocation guardedInvocation;80private final Validator validator;8182GuardedInvocationComponent(final MethodHandle invocation) {83this(invocation, null, ValidationType.NONE);84}8586GuardedInvocationComponent(final MethodHandle invocation, final MethodHandle guard, final ValidationType validationType) {87this(invocation, guard, null, validationType);88}8990GuardedInvocationComponent(final MethodHandle invocation, final MethodHandle guard, final Class<?> validatorClass,91final ValidationType validationType) {92this(invocation, guard, new Validator(validatorClass, validationType));93}9495GuardedInvocationComponent(final GuardedInvocation guardedInvocation, final Class<?> validatorClass,96final ValidationType validationType) {97this(guardedInvocation, new Validator(validatorClass, validationType));98}99100GuardedInvocationComponent replaceInvocation(final MethodHandle newInvocation) {101return replaceInvocation(newInvocation, guardedInvocation.getGuard());102}103104GuardedInvocationComponent replaceInvocation(final MethodHandle newInvocation, final MethodHandle newGuard) {105return new GuardedInvocationComponent(guardedInvocation.replaceMethods(newInvocation,106newGuard), validator);107}108109private GuardedInvocationComponent(final MethodHandle invocation, final MethodHandle guard, final Validator validator) {110this(new GuardedInvocation(invocation, guard), validator);111}112113private GuardedInvocationComponent(final GuardedInvocation guardedInvocation, final Validator validator) {114this.guardedInvocation = guardedInvocation;115this.validator = validator;116}117118GuardedInvocation getGuardedInvocation() {119return guardedInvocation;120}121122Class<?> getValidatorClass() {123return validator.validatorClass;124}125126ValidationType getValidationType() {127return validator.validationType;128}129130GuardedInvocationComponent compose(final MethodHandle compositeInvocation, final MethodHandle otherGuard,131final Class<?> otherValidatorClass, final ValidationType otherValidationType) {132final Validator compositeValidator = validator.compose(new Validator(otherValidatorClass, otherValidationType));133final MethodHandle compositeGuard = compositeValidator == validator ? guardedInvocation.getGuard() : otherGuard;134return new GuardedInvocationComponent(compositeInvocation, compositeGuard, compositeValidator);135}136137private static class Validator {138/*private*/ final Class<?> validatorClass;139/*private*/ final ValidationType validationType;140141Validator(final Class<?> validatorClass, final ValidationType validationType) {142this.validatorClass = validatorClass;143this.validationType = validationType;144}145146Validator compose(final Validator other) {147if(other.validationType == ValidationType.NONE) {148return this;149}150switch(validationType) {151case NONE:152return other;153case INSTANCE_OF:154switch(other.validationType) {155case INSTANCE_OF:156if(isAssignableFrom(other)) {157return other;158} else if(other.isAssignableFrom(this)) {159return this;160}161break;162case EXACT_CLASS:163if(isAssignableFrom(other)) {164return other;165}166break;167case IS_ARRAY:168if(validatorClass.isArray()) {169return this;170}171break;172default:173throw new AssertionError();174}175break;176case EXACT_CLASS:177switch(other.validationType) {178case INSTANCE_OF:179if(other.isAssignableFrom(this)) {180return this;181}182break;183case EXACT_CLASS:184if(validatorClass == other.validatorClass) {185return this;186}187break;188case IS_ARRAY:189if(validatorClass.isArray()) {190return this;191}192break;193default:194throw new AssertionError();195}196break;197case IS_ARRAY:198switch(other.validationType) {199case INSTANCE_OF:200case EXACT_CLASS:201if(other.validatorClass.isArray()) {202return other;203}204break;205case IS_ARRAY:206return this;207default:208throw new AssertionError();209}210break;211default:212throw new AssertionError();213}214throw new AssertionError("Incompatible composition " + this + " vs " + other);215}216217private boolean isAssignableFrom(final Validator other) {218return validatorClass.isAssignableFrom(other.validatorClass);219}220221@Override222public String toString() {223return "Validator[" + validationType + (validatorClass == null ? "" : (" " + validatorClass.getName())) + "]";224}225}226}227228229