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

Wed, 20 Aug 2014 10:25:28 +0200

author
attila
date
Wed, 20 Aug 2014 10:25:28 +0200
changeset 962
ac62e33a99b0
parent 830
3cb09c560108
child 963
e2497b11a021
permissions
-rw-r--r--

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

mercurial