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

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 2605
afbc625eaca7
parent 1490
d85f981c8cf8
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset 7c756d901f9a

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 /*
aoqi@0 27 * This file is available under and governed by the GNU General Public
aoqi@0 28 * License version 2 only, as published by the Free Software Foundation.
aoqi@0 29 * However, the following notice accompanied the original version of this
aoqi@0 30 * file, and Oracle licenses the original version of this file under the BSD
aoqi@0 31 * license:
aoqi@0 32 */
aoqi@0 33 /*
aoqi@0 34 Copyright 2009-2013 Attila Szegedi
aoqi@0 35
aoqi@0 36 Licensed under both the Apache License, Version 2.0 (the "Apache License")
aoqi@0 37 and the BSD License (the "BSD License"), with licensee being free to
aoqi@0 38 choose either of the two at their discretion.
aoqi@0 39
aoqi@0 40 You may not use this file except in compliance with either the Apache
aoqi@0 41 License or the BSD License.
aoqi@0 42
aoqi@0 43 If you choose to use this file in compliance with the Apache License, the
aoqi@0 44 following notice applies to you:
aoqi@0 45
aoqi@0 46 You may obtain a copy of the Apache License at
aoqi@0 47
aoqi@0 48 http://www.apache.org/licenses/LICENSE-2.0
aoqi@0 49
aoqi@0 50 Unless required by applicable law or agreed to in writing, software
aoqi@0 51 distributed under the License is distributed on an "AS IS" BASIS,
aoqi@0 52 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
aoqi@0 53 implied. See the License for the specific language governing
aoqi@0 54 permissions and limitations under the License.
aoqi@0 55
aoqi@0 56 If you choose to use this file in compliance with the BSD License, the
aoqi@0 57 following notice applies to you:
aoqi@0 58
aoqi@0 59 Redistribution and use in source and binary forms, with or without
aoqi@0 60 modification, are permitted provided that the following conditions are
aoqi@0 61 met:
aoqi@0 62 * Redistributions of source code must retain the above copyright
aoqi@0 63 notice, this list of conditions and the following disclaimer.
aoqi@0 64 * Redistributions in binary form must reproduce the above copyright
aoqi@0 65 notice, this list of conditions and the following disclaimer in the
aoqi@0 66 documentation and/or other materials provided with the distribution.
aoqi@0 67 * Neither the name of the copyright holder nor the names of
aoqi@0 68 contributors may be used to endorse or promote products derived from
aoqi@0 69 this software without specific prior written permission.
aoqi@0 70
aoqi@0 71 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
aoqi@0 72 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
aoqi@0 73 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
aoqi@0 74 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
aoqi@0 75 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
aoqi@0 76 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
aoqi@0 77 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
aoqi@0 78 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
aoqi@0 79 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
aoqi@0 80 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
aoqi@0 81 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
aoqi@0 82 */
aoqi@0 83
aoqi@0 84 package jdk.internal.dynalink.beans;
aoqi@0 85
aoqi@0 86 import java.lang.invoke.MethodHandle;
aoqi@0 87 import java.lang.invoke.MethodHandles;
aoqi@0 88 import java.lang.invoke.MethodType;
aoqi@0 89 import java.lang.reflect.Array;
aoqi@0 90 import java.util.Collection;
aoqi@0 91 import java.util.List;
aoqi@0 92 import java.util.Map;
aoqi@0 93 import jdk.internal.dynalink.CallSiteDescriptor;
aoqi@0 94 import jdk.internal.dynalink.beans.GuardedInvocationComponent.ValidationType;
aoqi@0 95 import jdk.internal.dynalink.linker.GuardedInvocation;
aoqi@0 96 import jdk.internal.dynalink.linker.LinkerServices;
aoqi@0 97 import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker;
aoqi@0 98 import jdk.internal.dynalink.support.Guards;
aoqi@0 99 import jdk.internal.dynalink.support.Lookup;
aoqi@0 100 import jdk.internal.dynalink.support.TypeUtilities;
aoqi@0 101
aoqi@0 102 /**
aoqi@0 103 * A class that provides linking capabilities for a single POJO class. Normally not used directly, but managed by
aoqi@0 104 * {@link BeansLinker}.
aoqi@0 105 *
aoqi@0 106 * @author Attila Szegedi
aoqi@0 107 */
aoqi@0 108 class BeanLinker extends AbstractJavaLinker implements TypeBasedGuardingDynamicLinker {
attila@962 109 BeanLinker(final Class<?> clazz) {
aoqi@0 110 super(clazz, Guards.getClassGuard(clazz), Guards.getInstanceOfGuard(clazz));
aoqi@0 111 if(clazz.isArray()) {
aoqi@0 112 // Some languages won't have a notion of manipulating collections. Exposing "length" on arrays as an
aoqi@0 113 // explicit property is beneficial for them.
aoqi@0 114 // REVISIT: is it maybe a code smell that "dyn:getLength" is not needed?
aoqi@0 115 setPropertyGetter("length", GET_ARRAY_LENGTH, ValidationType.IS_ARRAY);
aoqi@0 116 } else if(List.class.isAssignableFrom(clazz)) {
aoqi@0 117 setPropertyGetter("length", GET_COLLECTION_LENGTH, ValidationType.INSTANCE_OF);
aoqi@0 118 }
aoqi@0 119 }
aoqi@0 120
aoqi@0 121 @Override
attila@962 122 public boolean canLinkType(final Class<?> type) {
aoqi@0 123 return type == clazz;
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 @Override
aoqi@0 127 FacetIntrospector createFacetIntrospector() {
aoqi@0 128 return new BeanIntrospector(clazz);
aoqi@0 129 }
aoqi@0 130
aoqi@0 131 @Override
attila@962 132 protected GuardedInvocationComponent getGuardedInvocationComponent(final CallSiteDescriptor callSiteDescriptor,
attila@962 133 final LinkerServices linkerServices, final List<String> operations) throws Exception {
aoqi@0 134 final GuardedInvocationComponent superGic = super.getGuardedInvocationComponent(callSiteDescriptor,
aoqi@0 135 linkerServices, operations);
aoqi@0 136 if(superGic != null) {
aoqi@0 137 return superGic;
aoqi@0 138 }
aoqi@0 139 if(operations.isEmpty()) {
aoqi@0 140 return null;
aoqi@0 141 }
aoqi@0 142 final String op = operations.get(0);
aoqi@0 143 // dyn:getElem(this, id)
aoqi@0 144 // id is typically either an int (for arrays and lists) or an object (for maps). linkerServices can provide
aoqi@0 145 // conversion from call site argument type though.
aoqi@0 146 if("getElem".equals(op)) {
aoqi@0 147 return getElementGetter(callSiteDescriptor, linkerServices, pop(operations));
aoqi@0 148 }
aoqi@0 149 if("setElem".equals(op)) {
aoqi@0 150 return getElementSetter(callSiteDescriptor, linkerServices, pop(operations));
aoqi@0 151 }
aoqi@0 152 // dyn:getLength(this) (works on Java arrays, collections, and maps)
aoqi@0 153 if("getLength".equals(op)) {
aoqi@0 154 return getLengthGetter(callSiteDescriptor);
aoqi@0 155 }
aoqi@0 156 return null;
aoqi@0 157 }
aoqi@0 158
aoqi@0 159 private static MethodHandle GET_LIST_ELEMENT = Lookup.PUBLIC.findVirtual(List.class, "get",
aoqi@0 160 MethodType.methodType(Object.class, int.class));
aoqi@0 161
aoqi@0 162 private static MethodHandle GET_MAP_ELEMENT = Lookup.PUBLIC.findVirtual(Map.class, "get",
aoqi@0 163 MethodType.methodType(Object.class, Object.class));
aoqi@0 164
aoqi@0 165 private static MethodHandle LIST_GUARD = Guards.getInstanceOfGuard(List.class);
aoqi@0 166 private static MethodHandle MAP_GUARD = Guards.getInstanceOfGuard(Map.class);
aoqi@0 167
attila@1239 168 private enum CollectionType {
attila@1239 169 ARRAY, LIST, MAP
attila@1239 170 };
attila@1239 171
aoqi@0 172 private GuardedInvocationComponent getElementGetter(final CallSiteDescriptor callSiteDescriptor,
attila@962 173 final LinkerServices linkerServices, final List<String> operations) throws Exception {
aoqi@0 174 final MethodType callSiteType = callSiteDescriptor.getMethodType();
aoqi@0 175 final Class<?> declaredType = callSiteType.parameterType(0);
aoqi@0 176 final GuardedInvocationComponent nextComponent = getGuardedInvocationComponent(callSiteDescriptor,
aoqi@0 177 linkerServices, operations);
aoqi@0 178
aoqi@0 179 // If declared type of receiver at the call site is already an array, a list or map, bind without guard. Thing
aoqi@0 180 // is, it'd be quite stupid of a call site creator to go though invokedynamic when it knows in advance they're
aoqi@0 181 // dealing with an array, or a list or map, but hey...
aoqi@0 182 // Note that for arrays and lists, using LinkerServices.asType() will ensure that any language specific linkers
aoqi@0 183 // in use will get a chance to perform any (if there's any) implicit conversion to integer for the indices.
aoqi@0 184 final GuardedInvocationComponent gic;
attila@1239 185 final CollectionType collectionType;
aoqi@0 186 if(declaredType.isArray()) {
attila@1239 187 gic = createInternalFilteredGuardedInvocationComponent(MethodHandles.arrayElementGetter(declaredType), linkerServices);
attila@1239 188 collectionType = CollectionType.ARRAY;
aoqi@0 189 } else if(List.class.isAssignableFrom(declaredType)) {
attila@1239 190 gic = createInternalFilteredGuardedInvocationComponent(GET_LIST_ELEMENT, linkerServices);
attila@1239 191 collectionType = CollectionType.LIST;
aoqi@0 192 } else if(Map.class.isAssignableFrom(declaredType)) {
attila@1239 193 gic = createInternalFilteredGuardedInvocationComponent(GET_MAP_ELEMENT, linkerServices);
attila@1239 194 collectionType = CollectionType.MAP;
aoqi@0 195 } else if(clazz.isArray()) {
attila@1239 196 gic = getClassGuardedInvocationComponent(linkerServices.filterInternalObjects(MethodHandles.arrayElementGetter(clazz)), callSiteType);
attila@1239 197 collectionType = CollectionType.ARRAY;
aoqi@0 198 } else if(List.class.isAssignableFrom(clazz)) {
attila@1239 199 gic = createInternalFilteredGuardedInvocationComponent(GET_LIST_ELEMENT, Guards.asType(LIST_GUARD, callSiteType), List.class, ValidationType.INSTANCE_OF,
attila@1239 200 linkerServices);
attila@1239 201 collectionType = CollectionType.LIST;
aoqi@0 202 } else if(Map.class.isAssignableFrom(clazz)) {
attila@1239 203 gic = createInternalFilteredGuardedInvocationComponent(GET_MAP_ELEMENT, Guards.asType(MAP_GUARD, callSiteType), Map.class, ValidationType.INSTANCE_OF,
attila@1239 204 linkerServices);
attila@1239 205 collectionType = CollectionType.MAP;
aoqi@0 206 } else {
aoqi@0 207 // Can't retrieve elements for objects that are neither arrays, nor list, nor maps.
aoqi@0 208 return nextComponent;
aoqi@0 209 }
aoqi@0 210
aoqi@0 211 // We can have "dyn:getElem:foo", especially in composites, i.e. "dyn:getElem|getProp|getMethod:foo"
aoqi@0 212 final String fixedKey = getFixedKey(callSiteDescriptor);
aoqi@0 213 // Convert the key to a number if we're working with a list or array
aoqi@0 214 final Object typedFixedKey;
attila@1239 215 if(collectionType != CollectionType.MAP && fixedKey != null) {
aoqi@0 216 typedFixedKey = convertKeyToInteger(fixedKey, linkerServices);
aoqi@0 217 if(typedFixedKey == null) {
aoqi@0 218 // key is not numeric, it can never succeed
aoqi@0 219 return nextComponent;
aoqi@0 220 }
aoqi@0 221 } else {
aoqi@0 222 typedFixedKey = fixedKey;
aoqi@0 223 }
aoqi@0 224
aoqi@0 225 final GuardedInvocation gi = gic.getGuardedInvocation();
aoqi@0 226 final Binder binder = new Binder(linkerServices, callSiteType, typedFixedKey);
aoqi@0 227 final MethodHandle invocation = gi.getInvocation();
aoqi@0 228
aoqi@0 229 if(nextComponent == null) {
aoqi@0 230 return gic.replaceInvocation(binder.bind(invocation));
aoqi@0 231 }
aoqi@0 232
aoqi@0 233 final MethodHandle checkGuard;
attila@1239 234 switch(collectionType) {
attila@1239 235 case LIST:
aoqi@0 236 checkGuard = convertArgToInt(RANGE_CHECK_LIST, linkerServices, callSiteDescriptor);
attila@1239 237 break;
attila@1239 238 case MAP:
aoqi@0 239 // TODO: A more complex solution could be devised for maps, one where we do a get() first, and fold it
aoqi@0 240 // into a GWT that tests if it returned null, and if it did, do another GWT with containsKey()
aoqi@0 241 // that returns constant null (on true), or falls back to next component (on false)
attila@1239 242 checkGuard = linkerServices.filterInternalObjects(CONTAINS_MAP);
attila@1239 243 break;
attila@1239 244 case ARRAY:
aoqi@0 245 checkGuard = convertArgToInt(RANGE_CHECK_ARRAY, linkerServices, callSiteDescriptor);
attila@1239 246 break;
attila@1239 247 default:
attila@1239 248 throw new AssertionError();
aoqi@0 249 }
attila@963 250 final MethodPair matchedInvocations = matchReturnTypes(binder.bind(invocation),
attila@963 251 nextComponent.getGuardedInvocation().getInvocation());
attila@963 252 return nextComponent.compose(matchedInvocations.guardWithTest(binder.bindTest(checkGuard)), gi.getGuard(),
aoqi@0 253 gic.getValidatorClass(), gic.getValidationType());
aoqi@0 254 }
aoqi@0 255
attila@1239 256 private static GuardedInvocationComponent createInternalFilteredGuardedInvocationComponent(
attila@1239 257 final MethodHandle invocation, final LinkerServices linkerServices) {
attila@1239 258 return new GuardedInvocationComponent(linkerServices.filterInternalObjects(invocation));
attila@1239 259 }
attila@1239 260
attila@1239 261 private static GuardedInvocationComponent createInternalFilteredGuardedInvocationComponent(
attila@1239 262 final MethodHandle invocation, final MethodHandle guard, final Class<?> validatorClass,
attila@1239 263 final ValidationType validationType, final LinkerServices linkerServices) {
attila@1239 264 return new GuardedInvocationComponent(linkerServices.filterInternalObjects(invocation), guard,
attila@1239 265 validatorClass, validationType);
attila@1239 266 }
attila@1239 267
aoqi@0 268 private static String getFixedKey(final CallSiteDescriptor callSiteDescriptor) {
aoqi@0 269 return callSiteDescriptor.getNameTokenCount() == 2 ? null : callSiteDescriptor.getNameToken(
aoqi@0 270 CallSiteDescriptor.NAME_OPERAND);
aoqi@0 271 }
aoqi@0 272
attila@962 273 private static Object convertKeyToInteger(final String fixedKey, final LinkerServices linkerServices) throws Exception {
aoqi@0 274 try {
aoqi@0 275 if(linkerServices.canConvert(String.class, Number.class)) {
aoqi@0 276 try {
aoqi@0 277 final Object val = linkerServices.getTypeConverter(String.class, Number.class).invoke(fixedKey);
aoqi@0 278 if(!(val instanceof Number)) {
aoqi@0 279 return null; // not a number
aoqi@0 280 }
aoqi@0 281 final Number n = (Number)val;
aoqi@0 282 if(n instanceof Integer) {
aoqi@0 283 return n;
aoqi@0 284 }
aoqi@0 285 final int intIndex = n.intValue();
aoqi@0 286 final double doubleValue = n.doubleValue();
aoqi@0 287 if(intIndex != doubleValue && !Double.isInfinite(doubleValue)) { // let infinites trigger IOOBE
aoqi@0 288 return null; // not an exact integer
aoqi@0 289 }
aoqi@0 290 return Integer.valueOf(intIndex);
aoqi@0 291 } catch(Exception|Error e) {
aoqi@0 292 throw e;
attila@962 293 } catch(final Throwable t) {
aoqi@0 294 throw new RuntimeException(t);
aoqi@0 295 }
aoqi@0 296 }
aoqi@0 297 return Integer.valueOf(fixedKey);
attila@962 298 } catch(final NumberFormatException e) {
aoqi@0 299 // key is not a number
aoqi@0 300 return null;
aoqi@0 301 }
aoqi@0 302 }
aoqi@0 303
attila@962 304 private static MethodHandle convertArgToInt(final MethodHandle mh, final LinkerServices ls, final CallSiteDescriptor desc) {
aoqi@0 305 final Class<?> sourceType = desc.getMethodType().parameterType(1);
aoqi@0 306 if(TypeUtilities.isMethodInvocationConvertible(sourceType, Number.class)) {
aoqi@0 307 return mh;
aoqi@0 308 } else if(ls.canConvert(sourceType, Number.class)) {
aoqi@0 309 final MethodHandle converter = ls.getTypeConverter(sourceType, Number.class);
aoqi@0 310 return MethodHandles.filterArguments(mh, 1, converter.asType(converter.type().changeReturnType(
aoqi@0 311 mh.type().parameterType(1))));
aoqi@0 312 }
aoqi@0 313 return mh;
aoqi@0 314 }
aoqi@0 315
aoqi@0 316 /**
aoqi@0 317 * Contains methods to adapt an item getter/setter method handle to the requested type, optionally binding it to a
aoqi@0 318 * fixed key first.
aoqi@0 319 * @author Attila Szegedi
aoqi@0 320 * @version $Id: $
aoqi@0 321 */
aoqi@0 322 private static class Binder {
aoqi@0 323 private final LinkerServices linkerServices;
aoqi@0 324 private final MethodType methodType;
aoqi@0 325 private final Object fixedKey;
aoqi@0 326
attila@962 327 Binder(final LinkerServices linkerServices, final MethodType methodType, final Object fixedKey) {
aoqi@0 328 this.linkerServices = linkerServices;
aoqi@0 329 this.methodType = fixedKey == null ? methodType : methodType.insertParameterTypes(1, fixedKey.getClass());
aoqi@0 330 this.fixedKey = fixedKey;
aoqi@0 331 }
aoqi@0 332
attila@962 333 /*private*/ MethodHandle bind(final MethodHandle handle) {
attila@963 334 return bindToFixedKey(linkerServices.asTypeLosslessReturn(handle, methodType));
aoqi@0 335 }
aoqi@0 336
attila@962 337 /*private*/ MethodHandle bindTest(final MethodHandle handle) {
aoqi@0 338 return bindToFixedKey(Guards.asType(handle, methodType));
aoqi@0 339 }
aoqi@0 340
attila@962 341 private MethodHandle bindToFixedKey(final MethodHandle handle) {
aoqi@0 342 return fixedKey == null ? handle : MethodHandles.insertArguments(handle, 1, fixedKey);
aoqi@0 343 }
aoqi@0 344 }
aoqi@0 345
aoqi@0 346 private static MethodHandle RANGE_CHECK_ARRAY = findRangeCheck(Object.class);
aoqi@0 347 private static MethodHandle RANGE_CHECK_LIST = findRangeCheck(List.class);
aoqi@0 348 private static MethodHandle CONTAINS_MAP = Lookup.PUBLIC.findVirtual(Map.class, "containsKey",
aoqi@0 349 MethodType.methodType(boolean.class, Object.class));
aoqi@0 350
attila@962 351 private static MethodHandle findRangeCheck(final Class<?> collectionType) {
aoqi@0 352 return Lookup.findOwnStatic(MethodHandles.lookup(), "rangeCheck", boolean.class, collectionType, Object.class);
aoqi@0 353 }
aoqi@0 354
aoqi@0 355 @SuppressWarnings("unused")
attila@962 356 private static final boolean rangeCheck(final Object array, final Object index) {
aoqi@0 357 if(!(index instanceof Number)) {
aoqi@0 358 return false;
aoqi@0 359 }
aoqi@0 360 final Number n = (Number)index;
aoqi@0 361 final int intIndex = n.intValue();
aoqi@0 362 final double doubleValue = n.doubleValue();
aoqi@0 363 if(intIndex != doubleValue && !Double.isInfinite(doubleValue)) { // let infinite trigger IOOBE
aoqi@0 364 return false;
aoqi@0 365 }
aoqi@0 366 if(0 <= intIndex && intIndex < Array.getLength(array)) {
aoqi@0 367 return true;
aoqi@0 368 }
aoqi@0 369 throw new ArrayIndexOutOfBoundsException("Array index out of range: " + n);
aoqi@0 370 }
aoqi@0 371
aoqi@0 372 @SuppressWarnings("unused")
attila@962 373 private static final boolean rangeCheck(final List<?> list, final Object index) {
aoqi@0 374 if(!(index instanceof Number)) {
aoqi@0 375 return false;
aoqi@0 376 }
aoqi@0 377 final Number n = (Number)index;
aoqi@0 378 final int intIndex = n.intValue();
aoqi@0 379 final double doubleValue = n.doubleValue();
aoqi@0 380 if(intIndex != doubleValue && !Double.isInfinite(doubleValue)) { // let infinite trigger IOOBE
aoqi@0 381 return false;
aoqi@0 382 }
aoqi@0 383 if(0 <= intIndex && intIndex < list.size()) {
aoqi@0 384 return true;
aoqi@0 385 }
aoqi@0 386 throw new IndexOutOfBoundsException("Index: " + n + ", Size: " + list.size());
aoqi@0 387 }
aoqi@0 388
aoqi@0 389 private static MethodHandle SET_LIST_ELEMENT = Lookup.PUBLIC.findVirtual(List.class, "set",
aoqi@0 390 MethodType.methodType(Object.class, int.class, Object.class));
aoqi@0 391
aoqi@0 392 private static MethodHandle PUT_MAP_ELEMENT = Lookup.PUBLIC.findVirtual(Map.class, "put",
aoqi@0 393 MethodType.methodType(Object.class, Object.class, Object.class));
aoqi@0 394
attila@962 395 private GuardedInvocationComponent getElementSetter(final CallSiteDescriptor callSiteDescriptor,
attila@962 396 final LinkerServices linkerServices, final List<String> operations) throws Exception {
aoqi@0 397 final MethodType callSiteType = callSiteDescriptor.getMethodType();
aoqi@0 398 final Class<?> declaredType = callSiteType.parameterType(0);
aoqi@0 399
aoqi@0 400 final GuardedInvocationComponent gic;
aoqi@0 401 // If declared type of receiver at the call site is already an array, a list or map, bind without guard. Thing
aoqi@0 402 // is, it'd be quite stupid of a call site creator to go though invokedynamic when it knows in advance they're
aoqi@0 403 // dealing with an array, or a list or map, but hey...
aoqi@0 404 // Note that for arrays and lists, using LinkerServices.asType() will ensure that any language specific linkers
aoqi@0 405 // in use will get a chance to perform any (if there's any) implicit conversion to integer for the indices.
attila@1239 406 final CollectionType collectionType;
aoqi@0 407 if(declaredType.isArray()) {
attila@1239 408 gic = createInternalFilteredGuardedInvocationComponent(MethodHandles.arrayElementSetter(declaredType), linkerServices);
attila@1239 409 collectionType = CollectionType.ARRAY;
aoqi@0 410 } else if(List.class.isAssignableFrom(declaredType)) {
attila@1239 411 gic = createInternalFilteredGuardedInvocationComponent(SET_LIST_ELEMENT, linkerServices);
attila@1239 412 collectionType = CollectionType.LIST;
aoqi@0 413 } else if(Map.class.isAssignableFrom(declaredType)) {
attila@1239 414 gic = createInternalFilteredGuardedInvocationComponent(PUT_MAP_ELEMENT, linkerServices);
attila@1239 415 collectionType = CollectionType.MAP;
aoqi@0 416 } else if(clazz.isArray()) {
attila@1239 417 gic = getClassGuardedInvocationComponent(linkerServices.filterInternalObjects(
attila@1239 418 MethodHandles.arrayElementSetter(clazz)), callSiteType);
attila@1239 419 collectionType = CollectionType.ARRAY;
aoqi@0 420 } else if(List.class.isAssignableFrom(clazz)) {
attila@1239 421 gic = createInternalFilteredGuardedInvocationComponent(SET_LIST_ELEMENT, Guards.asType(LIST_GUARD, callSiteType), List.class, ValidationType.INSTANCE_OF,
attila@1239 422 linkerServices);
attila@1239 423 collectionType = CollectionType.LIST;
aoqi@0 424 } else if(Map.class.isAssignableFrom(clazz)) {
attila@1239 425 gic = createInternalFilteredGuardedInvocationComponent(PUT_MAP_ELEMENT, Guards.asType(MAP_GUARD, callSiteType),
attila@1239 426 Map.class, ValidationType.INSTANCE_OF, linkerServices);
attila@1239 427 collectionType = CollectionType.MAP;
aoqi@0 428 } else {
aoqi@0 429 // Can't set elements for objects that are neither arrays, nor list, nor maps.
aoqi@0 430 gic = null;
attila@1239 431 collectionType = null;
aoqi@0 432 }
aoqi@0 433
aoqi@0 434 // In contrast to, say, getElementGetter, we only compute the nextComponent if the target object is not a map,
aoqi@0 435 // as maps will always succeed in setting the element and will never need to fall back to the next component
aoqi@0 436 // operation.
attila@1239 437 final GuardedInvocationComponent nextComponent = collectionType == CollectionType.MAP ? null : getGuardedInvocationComponent(
aoqi@0 438 callSiteDescriptor, linkerServices, operations);
aoqi@0 439 if(gic == null) {
aoqi@0 440 return nextComponent;
aoqi@0 441 }
aoqi@0 442
aoqi@0 443 // We can have "dyn:setElem:foo", especially in composites, i.e. "dyn:setElem|setProp:foo"
aoqi@0 444 final String fixedKey = getFixedKey(callSiteDescriptor);
aoqi@0 445 // Convert the key to a number if we're working with a list or array
aoqi@0 446 final Object typedFixedKey;
attila@1239 447 if(collectionType != CollectionType.MAP && fixedKey != null) {
aoqi@0 448 typedFixedKey = convertKeyToInteger(fixedKey, linkerServices);
aoqi@0 449 if(typedFixedKey == null) {
aoqi@0 450 // key is not numeric, it can never succeed
aoqi@0 451 return nextComponent;
aoqi@0 452 }
aoqi@0 453 } else {
aoqi@0 454 typedFixedKey = fixedKey;
aoqi@0 455 }
aoqi@0 456
aoqi@0 457 final GuardedInvocation gi = gic.getGuardedInvocation();
aoqi@0 458 final Binder binder = new Binder(linkerServices, callSiteType, typedFixedKey);
aoqi@0 459 final MethodHandle invocation = gi.getInvocation();
aoqi@0 460
aoqi@0 461 if(nextComponent == null) {
aoqi@0 462 return gic.replaceInvocation(binder.bind(invocation));
aoqi@0 463 }
aoqi@0 464
attila@1239 465 assert collectionType == CollectionType.LIST || collectionType == CollectionType.ARRAY;
attila@1239 466 final MethodHandle checkGuard = convertArgToInt(collectionType == CollectionType.LIST ? RANGE_CHECK_LIST :
aoqi@0 467 RANGE_CHECK_ARRAY, linkerServices, callSiteDescriptor);
attila@963 468 final MethodPair matchedInvocations = matchReturnTypes(binder.bind(invocation),
attila@963 469 nextComponent.getGuardedInvocation().getInvocation());
attila@963 470 return nextComponent.compose(matchedInvocations.guardWithTest(binder.bindTest(checkGuard)), gi.getGuard(),
aoqi@0 471 gic.getValidatorClass(), gic.getValidationType());
aoqi@0 472 }
aoqi@0 473
aoqi@0 474 private static MethodHandle GET_ARRAY_LENGTH = Lookup.PUBLIC.findStatic(Array.class, "getLength",
aoqi@0 475 MethodType.methodType(int.class, Object.class));
aoqi@0 476
aoqi@0 477 private static MethodHandle GET_COLLECTION_LENGTH = Lookup.PUBLIC.findVirtual(Collection.class, "size",
aoqi@0 478 MethodType.methodType(int.class));
aoqi@0 479
aoqi@0 480 private static MethodHandle GET_MAP_LENGTH = Lookup.PUBLIC.findVirtual(Map.class, "size",
aoqi@0 481 MethodType.methodType(int.class));
aoqi@0 482
aoqi@0 483 private static MethodHandle COLLECTION_GUARD = Guards.getInstanceOfGuard(Collection.class);
aoqi@0 484
attila@962 485 private GuardedInvocationComponent getLengthGetter(final CallSiteDescriptor callSiteDescriptor) {
aoqi@0 486 assertParameterCount(callSiteDescriptor, 1);
aoqi@0 487 final MethodType callSiteType = callSiteDescriptor.getMethodType();
aoqi@0 488 final Class<?> declaredType = callSiteType.parameterType(0);
aoqi@0 489 // If declared type of receiver at the call site is already an array, collection, or map, bind without guard.
aoqi@0 490 // Thing is, it'd be quite stupid of a call site creator to go though invokedynamic when it knows in advance
aoqi@0 491 // they're dealing with an array, collection, or map, but hey...
aoqi@0 492 if(declaredType.isArray()) {
aoqi@0 493 return new GuardedInvocationComponent(GET_ARRAY_LENGTH.asType(callSiteType));
aoqi@0 494 } else if(Collection.class.isAssignableFrom(declaredType)) {
aoqi@0 495 return new GuardedInvocationComponent(GET_COLLECTION_LENGTH.asType(callSiteType));
aoqi@0 496 } else if(Map.class.isAssignableFrom(declaredType)) {
aoqi@0 497 return new GuardedInvocationComponent(GET_MAP_LENGTH.asType(callSiteType));
aoqi@0 498 }
aoqi@0 499
aoqi@0 500 // Otherwise, create a binding based on the actual type of the argument with an appropriate guard.
aoqi@0 501 if(clazz.isArray()) {
aoqi@0 502 return new GuardedInvocationComponent(GET_ARRAY_LENGTH.asType(callSiteType), Guards.isArray(0,
aoqi@0 503 callSiteType), ValidationType.IS_ARRAY);
aoqi@0 504 } if(Collection.class.isAssignableFrom(clazz)) {
aoqi@0 505 return new GuardedInvocationComponent(GET_COLLECTION_LENGTH.asType(callSiteType), Guards.asType(
aoqi@0 506 COLLECTION_GUARD, callSiteType), Collection.class, ValidationType.INSTANCE_OF);
aoqi@0 507 } if(Map.class.isAssignableFrom(clazz)) {
aoqi@0 508 return new GuardedInvocationComponent(GET_MAP_LENGTH.asType(callSiteType), Guards.asType(MAP_GUARD,
aoqi@0 509 callSiteType), Map.class, ValidationType.INSTANCE_OF);
aoqi@0 510 }
aoqi@0 511 // Can't retrieve length for objects that are neither arrays, nor collections, nor maps.
aoqi@0 512 return null;
aoqi@0 513 }
aoqi@0 514
attila@962 515 private static void assertParameterCount(final CallSiteDescriptor descriptor, final int paramCount) {
aoqi@0 516 if(descriptor.getMethodType().parameterCount() != paramCount) {
aoqi@0 517 throw new BootstrapMethodError(descriptor.getName() + " must have exactly " + paramCount + " parameters.");
aoqi@0 518 }
aoqi@0 519 }
aoqi@0 520 }

mercurial