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

Thu, 12 Oct 2017 19:52:15 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:52:15 +0800
changeset 1205
4112748288bb
parent 963
e2497b11a021
parent 952
6d5471a497fb
child 1490
d85f981c8cf8
permissions
-rw-r--r--

merge

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
aoqi@0 168 private GuardedInvocationComponent getElementGetter(final CallSiteDescriptor callSiteDescriptor,
attila@962 169 final LinkerServices linkerServices, final List<String> operations) throws Exception {
aoqi@0 170 final MethodType callSiteType = callSiteDescriptor.getMethodType();
aoqi@0 171 final Class<?> declaredType = callSiteType.parameterType(0);
aoqi@0 172 final GuardedInvocationComponent nextComponent = getGuardedInvocationComponent(callSiteDescriptor,
aoqi@0 173 linkerServices, operations);
aoqi@0 174
aoqi@0 175 // If declared type of receiver at the call site is already an array, a list or map, bind without guard. Thing
aoqi@0 176 // is, it'd be quite stupid of a call site creator to go though invokedynamic when it knows in advance they're
aoqi@0 177 // dealing with an array, or a list or map, but hey...
aoqi@0 178 // Note that for arrays and lists, using LinkerServices.asType() will ensure that any language specific linkers
aoqi@0 179 // in use will get a chance to perform any (if there's any) implicit conversion to integer for the indices.
aoqi@0 180 final GuardedInvocationComponent gic;
aoqi@0 181 final boolean isMap;
aoqi@0 182 if(declaredType.isArray()) {
aoqi@0 183 gic = new GuardedInvocationComponent(MethodHandles.arrayElementGetter(declaredType));
aoqi@0 184 isMap = false;
aoqi@0 185 } else if(List.class.isAssignableFrom(declaredType)) {
aoqi@0 186 gic = new GuardedInvocationComponent(GET_LIST_ELEMENT);
aoqi@0 187 isMap = false;
aoqi@0 188 } else if(Map.class.isAssignableFrom(declaredType)) {
aoqi@0 189 gic = new GuardedInvocationComponent(GET_MAP_ELEMENT);
aoqi@0 190 isMap = true;
aoqi@0 191 } else if(clazz.isArray()) {
aoqi@0 192 gic = getClassGuardedInvocationComponent(MethodHandles.arrayElementGetter(clazz), callSiteType);
aoqi@0 193 isMap = false;
aoqi@0 194 } else if(List.class.isAssignableFrom(clazz)) {
aoqi@0 195 gic = new GuardedInvocationComponent(GET_LIST_ELEMENT, Guards.asType(LIST_GUARD, callSiteType), List.class,
aoqi@0 196 ValidationType.INSTANCE_OF);
aoqi@0 197 isMap = false;
aoqi@0 198 } else if(Map.class.isAssignableFrom(clazz)) {
aoqi@0 199 gic = new GuardedInvocationComponent(GET_MAP_ELEMENT, Guards.asType(MAP_GUARD, callSiteType), Map.class,
aoqi@0 200 ValidationType.INSTANCE_OF);
aoqi@0 201 isMap = true;
aoqi@0 202 } else {
aoqi@0 203 // Can't retrieve elements for objects that are neither arrays, nor list, nor maps.
aoqi@0 204 return nextComponent;
aoqi@0 205 }
aoqi@0 206
aoqi@0 207 // We can have "dyn:getElem:foo", especially in composites, i.e. "dyn:getElem|getProp|getMethod:foo"
aoqi@0 208 final String fixedKey = getFixedKey(callSiteDescriptor);
aoqi@0 209 // Convert the key to a number if we're working with a list or array
aoqi@0 210 final Object typedFixedKey;
aoqi@0 211 if(!isMap && fixedKey != null) {
aoqi@0 212 typedFixedKey = convertKeyToInteger(fixedKey, linkerServices);
aoqi@0 213 if(typedFixedKey == null) {
aoqi@0 214 // key is not numeric, it can never succeed
aoqi@0 215 return nextComponent;
aoqi@0 216 }
aoqi@0 217 } else {
aoqi@0 218 typedFixedKey = fixedKey;
aoqi@0 219 }
aoqi@0 220
aoqi@0 221 final GuardedInvocation gi = gic.getGuardedInvocation();
aoqi@0 222 final Binder binder = new Binder(linkerServices, callSiteType, typedFixedKey);
aoqi@0 223 final MethodHandle invocation = gi.getInvocation();
aoqi@0 224
aoqi@0 225 if(nextComponent == null) {
aoqi@0 226 return gic.replaceInvocation(binder.bind(invocation));
aoqi@0 227 }
aoqi@0 228
aoqi@0 229 final MethodHandle checkGuard;
aoqi@0 230 if(invocation == GET_LIST_ELEMENT) {
aoqi@0 231 checkGuard = convertArgToInt(RANGE_CHECK_LIST, linkerServices, callSiteDescriptor);
aoqi@0 232 } else if(invocation == GET_MAP_ELEMENT) {
aoqi@0 233 // TODO: A more complex solution could be devised for maps, one where we do a get() first, and fold it
aoqi@0 234 // into a GWT that tests if it returned null, and if it did, do another GWT with containsKey()
aoqi@0 235 // that returns constant null (on true), or falls back to next component (on false)
aoqi@0 236 checkGuard = CONTAINS_MAP;
aoqi@0 237 } else {
aoqi@0 238 checkGuard = convertArgToInt(RANGE_CHECK_ARRAY, linkerServices, callSiteDescriptor);
aoqi@0 239 }
attila@963 240 final MethodPair matchedInvocations = matchReturnTypes(binder.bind(invocation),
attila@963 241 nextComponent.getGuardedInvocation().getInvocation());
attila@963 242 return nextComponent.compose(matchedInvocations.guardWithTest(binder.bindTest(checkGuard)), gi.getGuard(),
aoqi@0 243 gic.getValidatorClass(), gic.getValidationType());
aoqi@0 244 }
aoqi@0 245
aoqi@0 246 private static String getFixedKey(final CallSiteDescriptor callSiteDescriptor) {
aoqi@0 247 return callSiteDescriptor.getNameTokenCount() == 2 ? null : callSiteDescriptor.getNameToken(
aoqi@0 248 CallSiteDescriptor.NAME_OPERAND);
aoqi@0 249 }
aoqi@0 250
attila@962 251 private static Object convertKeyToInteger(final String fixedKey, final LinkerServices linkerServices) throws Exception {
aoqi@0 252 try {
aoqi@0 253 if(linkerServices.canConvert(String.class, Number.class)) {
aoqi@0 254 try {
aoqi@0 255 final Object val = linkerServices.getTypeConverter(String.class, Number.class).invoke(fixedKey);
aoqi@0 256 if(!(val instanceof Number)) {
aoqi@0 257 return null; // not a number
aoqi@0 258 }
aoqi@0 259 final Number n = (Number)val;
aoqi@0 260 if(n instanceof Integer) {
aoqi@0 261 return n;
aoqi@0 262 }
aoqi@0 263 final int intIndex = n.intValue();
aoqi@0 264 final double doubleValue = n.doubleValue();
aoqi@0 265 if(intIndex != doubleValue && !Double.isInfinite(doubleValue)) { // let infinites trigger IOOBE
aoqi@0 266 return null; // not an exact integer
aoqi@0 267 }
aoqi@0 268 return Integer.valueOf(intIndex);
aoqi@0 269 } catch(Exception|Error e) {
aoqi@0 270 throw e;
attila@962 271 } catch(final Throwable t) {
aoqi@0 272 throw new RuntimeException(t);
aoqi@0 273 }
aoqi@0 274 }
aoqi@0 275 return Integer.valueOf(fixedKey);
attila@962 276 } catch(final NumberFormatException e) {
aoqi@0 277 // key is not a number
aoqi@0 278 return null;
aoqi@0 279 }
aoqi@0 280 }
aoqi@0 281
attila@962 282 private static MethodHandle convertArgToInt(final MethodHandle mh, final LinkerServices ls, final CallSiteDescriptor desc) {
aoqi@0 283 final Class<?> sourceType = desc.getMethodType().parameterType(1);
aoqi@0 284 if(TypeUtilities.isMethodInvocationConvertible(sourceType, Number.class)) {
aoqi@0 285 return mh;
aoqi@0 286 } else if(ls.canConvert(sourceType, Number.class)) {
aoqi@0 287 final MethodHandle converter = ls.getTypeConverter(sourceType, Number.class);
aoqi@0 288 return MethodHandles.filterArguments(mh, 1, converter.asType(converter.type().changeReturnType(
aoqi@0 289 mh.type().parameterType(1))));
aoqi@0 290 }
aoqi@0 291 return mh;
aoqi@0 292 }
aoqi@0 293
aoqi@0 294 /**
aoqi@0 295 * Contains methods to adapt an item getter/setter method handle to the requested type, optionally binding it to a
aoqi@0 296 * fixed key first.
aoqi@0 297 * @author Attila Szegedi
aoqi@0 298 * @version $Id: $
aoqi@0 299 */
aoqi@0 300 private static class Binder {
aoqi@0 301 private final LinkerServices linkerServices;
aoqi@0 302 private final MethodType methodType;
aoqi@0 303 private final Object fixedKey;
aoqi@0 304
attila@962 305 Binder(final LinkerServices linkerServices, final MethodType methodType, final Object fixedKey) {
aoqi@0 306 this.linkerServices = linkerServices;
aoqi@0 307 this.methodType = fixedKey == null ? methodType : methodType.insertParameterTypes(1, fixedKey.getClass());
aoqi@0 308 this.fixedKey = fixedKey;
aoqi@0 309 }
aoqi@0 310
attila@962 311 /*private*/ MethodHandle bind(final MethodHandle handle) {
attila@963 312 return bindToFixedKey(linkerServices.asTypeLosslessReturn(handle, methodType));
aoqi@0 313 }
aoqi@0 314
attila@962 315 /*private*/ MethodHandle bindTest(final MethodHandle handle) {
aoqi@0 316 return bindToFixedKey(Guards.asType(handle, methodType));
aoqi@0 317 }
aoqi@0 318
attila@962 319 private MethodHandle bindToFixedKey(final MethodHandle handle) {
aoqi@0 320 return fixedKey == null ? handle : MethodHandles.insertArguments(handle, 1, fixedKey);
aoqi@0 321 }
aoqi@0 322 }
aoqi@0 323
aoqi@0 324 private static MethodHandle RANGE_CHECK_ARRAY = findRangeCheck(Object.class);
aoqi@0 325 private static MethodHandle RANGE_CHECK_LIST = findRangeCheck(List.class);
aoqi@0 326 private static MethodHandle CONTAINS_MAP = Lookup.PUBLIC.findVirtual(Map.class, "containsKey",
aoqi@0 327 MethodType.methodType(boolean.class, Object.class));
aoqi@0 328
attila@962 329 private static MethodHandle findRangeCheck(final Class<?> collectionType) {
aoqi@0 330 return Lookup.findOwnStatic(MethodHandles.lookup(), "rangeCheck", boolean.class, collectionType, Object.class);
aoqi@0 331 }
aoqi@0 332
aoqi@0 333 @SuppressWarnings("unused")
attila@962 334 private static final boolean rangeCheck(final Object array, final Object index) {
aoqi@0 335 if(!(index instanceof Number)) {
aoqi@0 336 return false;
aoqi@0 337 }
aoqi@0 338 final Number n = (Number)index;
aoqi@0 339 final int intIndex = n.intValue();
aoqi@0 340 final double doubleValue = n.doubleValue();
aoqi@0 341 if(intIndex != doubleValue && !Double.isInfinite(doubleValue)) { // let infinite trigger IOOBE
aoqi@0 342 return false;
aoqi@0 343 }
aoqi@0 344 if(0 <= intIndex && intIndex < Array.getLength(array)) {
aoqi@0 345 return true;
aoqi@0 346 }
aoqi@0 347 throw new ArrayIndexOutOfBoundsException("Array index out of range: " + n);
aoqi@0 348 }
aoqi@0 349
aoqi@0 350 @SuppressWarnings("unused")
attila@962 351 private static final boolean rangeCheck(final List<?> list, final Object index) {
aoqi@0 352 if(!(index instanceof Number)) {
aoqi@0 353 return false;
aoqi@0 354 }
aoqi@0 355 final Number n = (Number)index;
aoqi@0 356 final int intIndex = n.intValue();
aoqi@0 357 final double doubleValue = n.doubleValue();
aoqi@0 358 if(intIndex != doubleValue && !Double.isInfinite(doubleValue)) { // let infinite trigger IOOBE
aoqi@0 359 return false;
aoqi@0 360 }
aoqi@0 361 if(0 <= intIndex && intIndex < list.size()) {
aoqi@0 362 return true;
aoqi@0 363 }
aoqi@0 364 throw new IndexOutOfBoundsException("Index: " + n + ", Size: " + list.size());
aoqi@0 365 }
aoqi@0 366
aoqi@0 367 private static MethodHandle SET_LIST_ELEMENT = Lookup.PUBLIC.findVirtual(List.class, "set",
aoqi@0 368 MethodType.methodType(Object.class, int.class, Object.class));
aoqi@0 369
aoqi@0 370 private static MethodHandle PUT_MAP_ELEMENT = Lookup.PUBLIC.findVirtual(Map.class, "put",
aoqi@0 371 MethodType.methodType(Object.class, Object.class, Object.class));
aoqi@0 372
attila@962 373 private GuardedInvocationComponent getElementSetter(final CallSiteDescriptor callSiteDescriptor,
attila@962 374 final LinkerServices linkerServices, final List<String> operations) throws Exception {
aoqi@0 375 final MethodType callSiteType = callSiteDescriptor.getMethodType();
aoqi@0 376 final Class<?> declaredType = callSiteType.parameterType(0);
aoqi@0 377
aoqi@0 378 final GuardedInvocationComponent gic;
aoqi@0 379 // If declared type of receiver at the call site is already an array, a list or map, bind without guard. Thing
aoqi@0 380 // is, it'd be quite stupid of a call site creator to go though invokedynamic when it knows in advance they're
aoqi@0 381 // dealing with an array, or a list or map, but hey...
aoqi@0 382 // Note that for arrays and lists, using LinkerServices.asType() will ensure that any language specific linkers
aoqi@0 383 // in use will get a chance to perform any (if there's any) implicit conversion to integer for the indices.
aoqi@0 384 final boolean isMap;
aoqi@0 385 if(declaredType.isArray()) {
aoqi@0 386 gic = new GuardedInvocationComponent(MethodHandles.arrayElementSetter(declaredType));
aoqi@0 387 isMap = false;
aoqi@0 388 } else if(List.class.isAssignableFrom(declaredType)) {
aoqi@0 389 gic = new GuardedInvocationComponent(SET_LIST_ELEMENT);
aoqi@0 390 isMap = false;
aoqi@0 391 } else if(Map.class.isAssignableFrom(declaredType)) {
aoqi@0 392 gic = new GuardedInvocationComponent(PUT_MAP_ELEMENT);
aoqi@0 393 isMap = true;
aoqi@0 394 } else if(clazz.isArray()) {
aoqi@0 395 gic = getClassGuardedInvocationComponent(MethodHandles.arrayElementSetter(clazz), callSiteType);
aoqi@0 396 isMap = false;
aoqi@0 397 } else if(List.class.isAssignableFrom(clazz)) {
aoqi@0 398 gic = new GuardedInvocationComponent(SET_LIST_ELEMENT, Guards.asType(LIST_GUARD, callSiteType), List.class,
aoqi@0 399 ValidationType.INSTANCE_OF);
aoqi@0 400 isMap = false;
aoqi@0 401 } else if(Map.class.isAssignableFrom(clazz)) {
aoqi@0 402 gic = new GuardedInvocationComponent(PUT_MAP_ELEMENT, Guards.asType(MAP_GUARD, callSiteType), Map.class,
aoqi@0 403 ValidationType.INSTANCE_OF);
aoqi@0 404 isMap = true;
aoqi@0 405 } else {
aoqi@0 406 // Can't set elements for objects that are neither arrays, nor list, nor maps.
aoqi@0 407 gic = null;
aoqi@0 408 isMap = false;
aoqi@0 409 }
aoqi@0 410
aoqi@0 411 // In contrast to, say, getElementGetter, we only compute the nextComponent if the target object is not a map,
aoqi@0 412 // as maps will always succeed in setting the element and will never need to fall back to the next component
aoqi@0 413 // operation.
aoqi@0 414 final GuardedInvocationComponent nextComponent = isMap ? null : getGuardedInvocationComponent(
aoqi@0 415 callSiteDescriptor, linkerServices, operations);
aoqi@0 416 if(gic == null) {
aoqi@0 417 return nextComponent;
aoqi@0 418 }
aoqi@0 419
aoqi@0 420 // We can have "dyn:setElem:foo", especially in composites, i.e. "dyn:setElem|setProp:foo"
aoqi@0 421 final String fixedKey = getFixedKey(callSiteDescriptor);
aoqi@0 422 // Convert the key to a number if we're working with a list or array
aoqi@0 423 final Object typedFixedKey;
aoqi@0 424 if(!isMap && fixedKey != null) {
aoqi@0 425 typedFixedKey = convertKeyToInteger(fixedKey, linkerServices);
aoqi@0 426 if(typedFixedKey == null) {
aoqi@0 427 // key is not numeric, it can never succeed
aoqi@0 428 return nextComponent;
aoqi@0 429 }
aoqi@0 430 } else {
aoqi@0 431 typedFixedKey = fixedKey;
aoqi@0 432 }
aoqi@0 433
aoqi@0 434 final GuardedInvocation gi = gic.getGuardedInvocation();
aoqi@0 435 final Binder binder = new Binder(linkerServices, callSiteType, typedFixedKey);
aoqi@0 436 final MethodHandle invocation = gi.getInvocation();
aoqi@0 437
aoqi@0 438 if(nextComponent == null) {
aoqi@0 439 return gic.replaceInvocation(binder.bind(invocation));
aoqi@0 440 }
aoqi@0 441
aoqi@0 442 final MethodHandle checkGuard = convertArgToInt(invocation == SET_LIST_ELEMENT ? RANGE_CHECK_LIST :
aoqi@0 443 RANGE_CHECK_ARRAY, linkerServices, callSiteDescriptor);
attila@963 444 final MethodPair matchedInvocations = matchReturnTypes(binder.bind(invocation),
attila@963 445 nextComponent.getGuardedInvocation().getInvocation());
attila@963 446 return nextComponent.compose(matchedInvocations.guardWithTest(binder.bindTest(checkGuard)), gi.getGuard(),
aoqi@0 447 gic.getValidatorClass(), gic.getValidationType());
aoqi@0 448 }
aoqi@0 449
aoqi@0 450 private static MethodHandle GET_ARRAY_LENGTH = Lookup.PUBLIC.findStatic(Array.class, "getLength",
aoqi@0 451 MethodType.methodType(int.class, Object.class));
aoqi@0 452
aoqi@0 453 private static MethodHandle GET_COLLECTION_LENGTH = Lookup.PUBLIC.findVirtual(Collection.class, "size",
aoqi@0 454 MethodType.methodType(int.class));
aoqi@0 455
aoqi@0 456 private static MethodHandle GET_MAP_LENGTH = Lookup.PUBLIC.findVirtual(Map.class, "size",
aoqi@0 457 MethodType.methodType(int.class));
aoqi@0 458
aoqi@0 459 private static MethodHandle COLLECTION_GUARD = Guards.getInstanceOfGuard(Collection.class);
aoqi@0 460
attila@962 461 private GuardedInvocationComponent getLengthGetter(final CallSiteDescriptor callSiteDescriptor) {
aoqi@0 462 assertParameterCount(callSiteDescriptor, 1);
aoqi@0 463 final MethodType callSiteType = callSiteDescriptor.getMethodType();
aoqi@0 464 final Class<?> declaredType = callSiteType.parameterType(0);
aoqi@0 465 // If declared type of receiver at the call site is already an array, collection, or map, bind without guard.
aoqi@0 466 // Thing is, it'd be quite stupid of a call site creator to go though invokedynamic when it knows in advance
aoqi@0 467 // they're dealing with an array, collection, or map, but hey...
aoqi@0 468 if(declaredType.isArray()) {
aoqi@0 469 return new GuardedInvocationComponent(GET_ARRAY_LENGTH.asType(callSiteType));
aoqi@0 470 } else if(Collection.class.isAssignableFrom(declaredType)) {
aoqi@0 471 return new GuardedInvocationComponent(GET_COLLECTION_LENGTH.asType(callSiteType));
aoqi@0 472 } else if(Map.class.isAssignableFrom(declaredType)) {
aoqi@0 473 return new GuardedInvocationComponent(GET_MAP_LENGTH.asType(callSiteType));
aoqi@0 474 }
aoqi@0 475
aoqi@0 476 // Otherwise, create a binding based on the actual type of the argument with an appropriate guard.
aoqi@0 477 if(clazz.isArray()) {
aoqi@0 478 return new GuardedInvocationComponent(GET_ARRAY_LENGTH.asType(callSiteType), Guards.isArray(0,
aoqi@0 479 callSiteType), ValidationType.IS_ARRAY);
aoqi@0 480 } if(Collection.class.isAssignableFrom(clazz)) {
aoqi@0 481 return new GuardedInvocationComponent(GET_COLLECTION_LENGTH.asType(callSiteType), Guards.asType(
aoqi@0 482 COLLECTION_GUARD, callSiteType), Collection.class, ValidationType.INSTANCE_OF);
aoqi@0 483 } if(Map.class.isAssignableFrom(clazz)) {
aoqi@0 484 return new GuardedInvocationComponent(GET_MAP_LENGTH.asType(callSiteType), Guards.asType(MAP_GUARD,
aoqi@0 485 callSiteType), Map.class, ValidationType.INSTANCE_OF);
aoqi@0 486 }
aoqi@0 487 // Can't retrieve length for objects that are neither arrays, nor collections, nor maps.
aoqi@0 488 return null;
aoqi@0 489 }
aoqi@0 490
attila@962 491 private static void assertParameterCount(final CallSiteDescriptor descriptor, final int paramCount) {
aoqi@0 492 if(descriptor.getMethodType().parameterCount() != paramCount) {
aoqi@0 493 throw new BootstrapMethodError(descriptor.getName() + " must have exactly " + paramCount + " parameters.");
aoqi@0 494 }
aoqi@0 495 }
aoqi@0 496 }

mercurial