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

Mon, 18 Feb 2013 16:00:15 +0100

author
attila
date
Mon, 18 Feb 2013 16:00:15 +0100
changeset 101
f8221ce53c2e
parent 90
5a820fb11814
child 830
3cb09c560108
permissions
-rw-r--r--

8008371: Fix Dynalink compiler warnings and whitespace
Reviewed-by: jlaskey, sundar

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

mercurial