Mon, 03 Nov 2014 09:49:52 +0100
8059443: NPE when unboxing return values
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.support; |
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.invoke.WrongMethodTypeException; |
attila@488 | 90 | import java.security.AccessController; |
attila@488 | 91 | import java.security.PrivilegedAction; |
attila@90 | 92 | import java.util.LinkedList; |
attila@90 | 93 | import java.util.List; |
attila@90 | 94 | import jdk.internal.dynalink.linker.ConversionComparator; |
attila@90 | 95 | import jdk.internal.dynalink.linker.ConversionComparator.Comparison; |
attila@90 | 96 | import jdk.internal.dynalink.linker.GuardedInvocation; |
attila@719 | 97 | import jdk.internal.dynalink.linker.GuardedTypeConversion; |
attila@90 | 98 | import jdk.internal.dynalink.linker.GuardingTypeConverterFactory; |
attila@90 | 99 | import jdk.internal.dynalink.linker.LinkerServices; |
attila@1090 | 100 | import jdk.internal.dynalink.linker.MethodTypeConversionStrategy; |
attila@90 | 101 | |
attila@90 | 102 | /** |
attila@90 | 103 | * A factory for type converters. This class is the main implementation behind the |
attila@90 | 104 | * {@link LinkerServices#asType(MethodHandle, MethodType)}. It manages the known {@link GuardingTypeConverterFactory} |
attila@90 | 105 | * instances and creates appropriate converters for method handles. |
attila@90 | 106 | * |
attila@90 | 107 | * @author Attila Szegedi |
attila@90 | 108 | */ |
attila@90 | 109 | public class TypeConverterFactory { |
attila@90 | 110 | |
attila@90 | 111 | private final GuardingTypeConverterFactory[] factories; |
attila@90 | 112 | private final ConversionComparator[] comparators; |
attila@1090 | 113 | private final MethodTypeConversionStrategy autoConversionStrategy; |
attila@90 | 114 | |
attila@90 | 115 | private final ClassValue<ClassMap<MethodHandle>> converterMap = new ClassValue<ClassMap<MethodHandle>>() { |
attila@90 | 116 | @Override |
attila@90 | 117 | protected ClassMap<MethodHandle> computeValue(final Class<?> sourceType) { |
attila@488 | 118 | return new ClassMap<MethodHandle>(getClassLoader(sourceType)) { |
attila@90 | 119 | @Override |
attila@962 | 120 | protected MethodHandle computeValue(final Class<?> targetType) { |
attila@90 | 121 | try { |
attila@90 | 122 | return createConverter(sourceType, targetType); |
attila@962 | 123 | } catch (final RuntimeException e) { |
attila@90 | 124 | throw e; |
attila@962 | 125 | } catch (final Exception e) { |
attila@90 | 126 | throw new RuntimeException(e); |
attila@90 | 127 | } |
attila@90 | 128 | } |
attila@90 | 129 | }; |
attila@90 | 130 | } |
attila@90 | 131 | }; |
attila@90 | 132 | |
attila@90 | 133 | private final ClassValue<ClassMap<MethodHandle>> converterIdentityMap = new ClassValue<ClassMap<MethodHandle>>() { |
attila@90 | 134 | @Override |
attila@90 | 135 | protected ClassMap<MethodHandle> computeValue(final Class<?> sourceType) { |
attila@488 | 136 | return new ClassMap<MethodHandle>(getClassLoader(sourceType)) { |
attila@90 | 137 | @Override |
attila@962 | 138 | protected MethodHandle computeValue(final Class<?> targetType) { |
attila@90 | 139 | if(!canAutoConvert(sourceType, targetType)) { |
attila@719 | 140 | final MethodHandle converter = getCacheableTypeConverter(sourceType, targetType); |
attila@719 | 141 | if(converter != IDENTITY_CONVERSION) { |
attila@90 | 142 | return converter; |
attila@90 | 143 | } |
attila@90 | 144 | } |
attila@90 | 145 | return IDENTITY_CONVERSION.asType(MethodType.methodType(targetType, sourceType)); |
attila@90 | 146 | } |
attila@90 | 147 | }; |
attila@90 | 148 | } |
attila@90 | 149 | }; |
attila@90 | 150 | |
attila@719 | 151 | private final ClassValue<ClassMap<Boolean>> canConvert = new ClassValue<ClassMap<Boolean>>() { |
attila@719 | 152 | @Override |
attila@719 | 153 | protected ClassMap<Boolean> computeValue(final Class<?> sourceType) { |
attila@719 | 154 | return new ClassMap<Boolean>(getClassLoader(sourceType)) { |
attila@719 | 155 | @Override |
attila@962 | 156 | protected Boolean computeValue(final Class<?> targetType) { |
attila@719 | 157 | try { |
attila@719 | 158 | return getTypeConverterNull(sourceType, targetType) != null; |
attila@962 | 159 | } catch (final RuntimeException e) { |
attila@719 | 160 | throw e; |
attila@962 | 161 | } catch (final Exception e) { |
attila@719 | 162 | throw new RuntimeException(e); |
attila@719 | 163 | } |
attila@719 | 164 | } |
attila@719 | 165 | }; |
attila@719 | 166 | } |
attila@719 | 167 | }; |
attila@719 | 168 | |
attila@488 | 169 | private static final ClassLoader getClassLoader(final Class<?> clazz) { |
attila@488 | 170 | return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { |
attila@488 | 171 | @Override |
attila@488 | 172 | public ClassLoader run() { |
attila@488 | 173 | return clazz.getClassLoader(); |
attila@488 | 174 | } |
attila@494 | 175 | }, ClassLoaderGetterContextProvider.GET_CLASS_LOADER_CONTEXT); |
attila@488 | 176 | } |
attila@488 | 177 | |
attila@90 | 178 | /** |
attila@90 | 179 | * Creates a new type converter factory from the available {@link GuardingTypeConverterFactory} instances. |
attila@90 | 180 | * |
attila@90 | 181 | * @param factories the {@link GuardingTypeConverterFactory} instances to compose. |
attila@1090 | 182 | * @param autoConversionStrategy conversion strategy for automatic type conversions. After |
attila@1090 | 183 | * {@link #asType(java.lang.invoke.MethodHandle, java.lang.invoke.MethodType)} has applied all custom |
attila@1090 | 184 | * conversions to a method handle, it still needs to effect |
attila@1090 | 185 | * {@link TypeUtilities#isMethodInvocationConvertible(Class, Class) method invocation conversions} that |
attila@1090 | 186 | * can usually be automatically applied as per |
attila@1090 | 187 | * {@link java.lang.invoke.MethodHandle#asType(java.lang.invoke.MethodType)}. |
attila@1090 | 188 | * However, sometimes language runtimes will want to customize even those conversions for their own call |
attila@1090 | 189 | * sites. A typical example is allowing unboxing of null return values, which is by default prohibited by |
attila@1090 | 190 | * ordinary {@code MethodHandles.asType}. In this case, a language runtime can install its own custom |
attila@1090 | 191 | * automatic conversion strategy, that can deal with null values. Note that when the strategy's |
attila@1090 | 192 | * {@link MethodTypeConversionStrategy#asType(java.lang.invoke.MethodHandle, java.lang.invoke.MethodType)} |
attila@1090 | 193 | * is invoked, the custom language conversions will already have been applied to the method handle, so by |
attila@1090 | 194 | * design the difference between the handle's current method type and the desired final type will always |
attila@1090 | 195 | * only be ones that can be subjected to method invocation conversions. Can be null, in which case no |
attila@1090 | 196 | * custom strategy is employed. |
attila@90 | 197 | */ |
attila@1090 | 198 | public TypeConverterFactory(final Iterable<? extends GuardingTypeConverterFactory> factories, |
attila@1090 | 199 | final MethodTypeConversionStrategy autoConversionStrategy) { |
attila@90 | 200 | final List<GuardingTypeConverterFactory> l = new LinkedList<>(); |
attila@90 | 201 | final List<ConversionComparator> c = new LinkedList<>(); |
attila@962 | 202 | for(final GuardingTypeConverterFactory factory: factories) { |
attila@90 | 203 | l.add(factory); |
attila@90 | 204 | if(factory instanceof ConversionComparator) { |
attila@90 | 205 | c.add((ConversionComparator)factory); |
attila@90 | 206 | } |
attila@90 | 207 | } |
attila@90 | 208 | this.factories = l.toArray(new GuardingTypeConverterFactory[l.size()]); |
attila@90 | 209 | this.comparators = c.toArray(new ConversionComparator[c.size()]); |
attila@1090 | 210 | this.autoConversionStrategy = autoConversionStrategy; |
attila@90 | 211 | } |
attila@90 | 212 | |
attila@90 | 213 | /** |
attila@90 | 214 | * Similar to {@link MethodHandle#asType(MethodType)} except it also hooks in method handles produced by |
attila@90 | 215 | * {@link GuardingTypeConverterFactory} implementations, providing for language-specific type coercing of |
attila@1090 | 216 | * parameters. For all conversions that are not a JLS method invocation conversion it'll insert |
attila@1090 | 217 | * {@link MethodHandles#filterArguments(MethodHandle, int, MethodHandle...)} with composite filters |
attila@1090 | 218 | * provided by {@link GuardingTypeConverterFactory} implementations. For the remaining JLS method invocation |
attila@1090 | 219 | * conversions, it will invoke {@link MethodTypeConversionStrategy#asType(MethodHandle, MethodType)} first |
attila@1090 | 220 | * if an automatic conversion strategy was specified in the |
attila@1090 | 221 | * {@link #TypeConverterFactory(Iterable, MethodTypeConversionStrategy) constructor}, and finally apply |
attila@1090 | 222 | * {@link MethodHandle#asType(MethodType)} for any remaining conversions. |
attila@90 | 223 | * |
attila@90 | 224 | * @param handle target method handle |
attila@90 | 225 | * @param fromType the types of source arguments |
attila@1090 | 226 | * @return a method handle that is a suitable combination of {@link MethodHandle#asType(MethodType)}, |
attila@1090 | 227 | * {@link MethodTypeConversionStrategy#asType(MethodHandle, MethodType)}, and |
attila@90 | 228 | * {@link MethodHandles#filterArguments(MethodHandle, int, MethodHandle...)} with |
attila@90 | 229 | * {@link GuardingTypeConverterFactory} produced type converters as filters. |
attila@90 | 230 | */ |
attila@962 | 231 | public MethodHandle asType(final MethodHandle handle, final MethodType fromType) { |
attila@90 | 232 | MethodHandle newHandle = handle; |
attila@90 | 233 | final MethodType toType = newHandle.type(); |
attila@90 | 234 | final int l = toType.parameterCount(); |
attila@90 | 235 | if(l != fromType.parameterCount()) { |
attila@90 | 236 | throw new WrongMethodTypeException("Parameter counts differ: " + handle.type() + " vs. " + fromType); |
attila@90 | 237 | } |
attila@90 | 238 | int pos = 0; |
attila@90 | 239 | final List<MethodHandle> converters = new LinkedList<>(); |
attila@90 | 240 | for(int i = 0; i < l; ++i) { |
attila@90 | 241 | final Class<?> fromParamType = fromType.parameterType(i); |
attila@90 | 242 | final Class<?> toParamType = toType.parameterType(i); |
attila@90 | 243 | if(canAutoConvert(fromParamType, toParamType)) { |
attila@90 | 244 | newHandle = applyConverters(newHandle, pos, converters); |
attila@90 | 245 | } else { |
attila@90 | 246 | final MethodHandle converter = getTypeConverterNull(fromParamType, toParamType); |
attila@90 | 247 | if(converter != null) { |
attila@90 | 248 | if(converters.isEmpty()) { |
attila@90 | 249 | pos = i; |
attila@90 | 250 | } |
attila@90 | 251 | converters.add(converter); |
attila@90 | 252 | } else { |
attila@90 | 253 | newHandle = applyConverters(newHandle, pos, converters); |
attila@90 | 254 | } |
attila@90 | 255 | } |
attila@90 | 256 | } |
attila@90 | 257 | newHandle = applyConverters(newHandle, pos, converters); |
attila@90 | 258 | |
attila@90 | 259 | // Convert return type |
attila@90 | 260 | final Class<?> fromRetType = fromType.returnType(); |
attila@90 | 261 | final Class<?> toRetType = toType.returnType(); |
attila@90 | 262 | if(fromRetType != Void.TYPE && toRetType != Void.TYPE) { |
attila@90 | 263 | if(!canAutoConvert(toRetType, fromRetType)) { |
attila@90 | 264 | final MethodHandle converter = getTypeConverterNull(toRetType, fromRetType); |
attila@90 | 265 | if(converter != null) { |
attila@90 | 266 | newHandle = MethodHandles.filterReturnValue(newHandle, converter); |
attila@90 | 267 | } |
attila@90 | 268 | } |
attila@90 | 269 | } |
attila@90 | 270 | |
attila@1090 | 271 | // Give change to automatic conversion strategy, if one is present. |
attila@1090 | 272 | final MethodHandle autoConvertedHandle = |
attila@1090 | 273 | autoConversionStrategy != null ? autoConversionStrategy.asType(newHandle, fromType) : newHandle; |
attila@1090 | 274 | |
attila@1090 | 275 | // Do a final asType for any conversions that remain. |
attila@1090 | 276 | return autoConvertedHandle.asType(fromType); |
attila@90 | 277 | } |
attila@90 | 278 | |
attila@962 | 279 | private static MethodHandle applyConverters(final MethodHandle handle, final int pos, final List<MethodHandle> converters) { |
attila@90 | 280 | if(converters.isEmpty()) { |
attila@90 | 281 | return handle; |
attila@90 | 282 | } |
attila@90 | 283 | final MethodHandle newHandle = |
attila@90 | 284 | MethodHandles.filterArguments(handle, pos, converters.toArray(new MethodHandle[converters.size()])); |
attila@90 | 285 | converters.clear(); |
attila@90 | 286 | return newHandle; |
attila@90 | 287 | } |
attila@90 | 288 | |
attila@90 | 289 | /** |
attila@90 | 290 | * Returns true if there might exist a conversion between the requested types (either an automatic JVM conversion, |
attila@90 | 291 | * or one provided by any available {@link GuardingTypeConverterFactory}), or false if there definitely does not |
attila@90 | 292 | * exist a conversion between the requested types. Note that returning true does not guarantee that the conversion |
attila@90 | 293 | * will succeed at runtime (notably, if the "from" or "to" types are sufficiently generic), but returning false |
attila@90 | 294 | * guarantees that it would fail. |
attila@90 | 295 | * |
attila@90 | 296 | * @param from the source type for the conversion |
attila@90 | 297 | * @param to the target type for the conversion |
attila@90 | 298 | * @return true if there can be a conversion, false if there can not. |
attila@90 | 299 | */ |
attila@90 | 300 | public boolean canConvert(final Class<?> from, final Class<?> to) { |
attila@719 | 301 | return canAutoConvert(from, to) || canConvert.get(from).get(to).booleanValue(); |
attila@90 | 302 | } |
attila@90 | 303 | |
attila@90 | 304 | /** |
attila@90 | 305 | * Determines which of the two type conversions from a source type to the two target types is preferred. This is |
attila@90 | 306 | * used for dynamic overloaded method resolution. If the source type is convertible to exactly one target type with |
attila@90 | 307 | * a method invocation conversion, it is chosen, otherwise available {@link ConversionComparator}s are consulted. |
attila@90 | 308 | * @param sourceType the source type. |
attila@90 | 309 | * @param targetType1 one potential target type |
attila@90 | 310 | * @param targetType2 another potential target type. |
attila@90 | 311 | * @return one of Comparison constants that establish which - if any - of the target types is preferable for the |
attila@90 | 312 | * conversion. |
attila@90 | 313 | */ |
attila@962 | 314 | public Comparison compareConversion(final Class<?> sourceType, final Class<?> targetType1, final Class<?> targetType2) { |
attila@962 | 315 | for(final ConversionComparator comparator: comparators) { |
attila@90 | 316 | final Comparison result = comparator.compareConversion(sourceType, targetType1, targetType2); |
attila@90 | 317 | if(result != Comparison.INDETERMINATE) { |
attila@90 | 318 | return result; |
attila@90 | 319 | } |
attila@90 | 320 | } |
attila@90 | 321 | if(TypeUtilities.isMethodInvocationConvertible(sourceType, targetType1)) { |
attila@90 | 322 | if(!TypeUtilities.isMethodInvocationConvertible(sourceType, targetType2)) { |
attila@90 | 323 | return Comparison.TYPE_1_BETTER; |
attila@90 | 324 | } |
attila@90 | 325 | } else if(TypeUtilities.isMethodInvocationConvertible(sourceType, targetType2)) { |
attila@90 | 326 | return Comparison.TYPE_2_BETTER; |
attila@90 | 327 | } |
attila@90 | 328 | return Comparison.INDETERMINATE; |
attila@90 | 329 | } |
attila@90 | 330 | |
attila@90 | 331 | /** |
attila@90 | 332 | * Determines whether it's safe to perform an automatic conversion between the source and target class. |
attila@90 | 333 | * |
attila@90 | 334 | * @param fromType convert from this class |
attila@90 | 335 | * @param toType convert to this class |
attila@90 | 336 | * @return true if it's safe to let MethodHandles.convertArguments() to handle this conversion. |
attila@90 | 337 | */ |
attila@90 | 338 | /*private*/ static boolean canAutoConvert(final Class<?> fromType, final Class<?> toType) { |
attila@90 | 339 | return TypeUtilities.isMethodInvocationConvertible(fromType, toType); |
attila@90 | 340 | } |
attila@90 | 341 | |
attila@962 | 342 | /*private*/ MethodHandle getCacheableTypeConverterNull(final Class<?> sourceType, final Class<?> targetType) { |
attila@719 | 343 | final MethodHandle converter = getCacheableTypeConverter(sourceType, targetType); |
attila@719 | 344 | return converter == IDENTITY_CONVERSION ? null : converter; |
attila@719 | 345 | } |
attila@719 | 346 | |
attila@962 | 347 | /*private*/ MethodHandle getTypeConverterNull(final Class<?> sourceType, final Class<?> targetType) { |
attila@719 | 348 | try { |
attila@719 | 349 | return getCacheableTypeConverterNull(sourceType, targetType); |
attila@962 | 350 | } catch(final NotCacheableConverter e) { |
attila@719 | 351 | return e.converter; |
attila@719 | 352 | } |
attila@719 | 353 | } |
attila@719 | 354 | |
attila@962 | 355 | /*private*/ MethodHandle getCacheableTypeConverter(final Class<?> sourceType, final Class<?> targetType) { |
attila@719 | 356 | return converterMap.get(sourceType).get(targetType); |
attila@90 | 357 | } |
attila@90 | 358 | |
attila@90 | 359 | /** |
attila@90 | 360 | * Given a source and target type, returns a method handle that converts between them. Never returns null; in worst |
attila@90 | 361 | * case it will return an identity conversion (that might fail for some values at runtime). You can use this method |
attila@90 | 362 | * if you have a piece of your program that is written in Java, and you need to reuse existing type conversion |
attila@90 | 363 | * machinery in a non-invokedynamic context. |
attila@90 | 364 | * @param sourceType the type to convert from |
attila@90 | 365 | * @param targetType the type to convert to |
attila@90 | 366 | * @return a method handle performing the conversion. |
attila@90 | 367 | */ |
attila@962 | 368 | public MethodHandle getTypeConverter(final Class<?> sourceType, final Class<?> targetType) { |
attila@719 | 369 | try { |
attila@719 | 370 | return converterIdentityMap.get(sourceType).get(targetType); |
attila@962 | 371 | } catch(final NotCacheableConverter e) { |
attila@719 | 372 | return e.converter; |
attila@719 | 373 | } |
attila@90 | 374 | } |
attila@90 | 375 | |
attila@962 | 376 | /*private*/ MethodHandle createConverter(final Class<?> sourceType, final Class<?> targetType) throws Exception { |
attila@90 | 377 | final MethodType type = MethodType.methodType(targetType, sourceType); |
attila@90 | 378 | final MethodHandle identity = IDENTITY_CONVERSION.asType(type); |
attila@90 | 379 | MethodHandle last = identity; |
attila@719 | 380 | boolean cacheable = true; |
attila@90 | 381 | for(int i = factories.length; i-- > 0;) { |
attila@719 | 382 | final GuardedTypeConversion next = factories[i].convertToType(sourceType, targetType); |
attila@90 | 383 | if(next != null) { |
attila@719 | 384 | cacheable = cacheable && next.isCacheable(); |
attila@719 | 385 | final GuardedInvocation conversionInvocation = next.getConversionInvocation(); |
attila@719 | 386 | conversionInvocation.assertType(type); |
attila@719 | 387 | last = conversionInvocation.compose(last); |
attila@90 | 388 | } |
attila@90 | 389 | } |
attila@719 | 390 | if(last == identity) { |
attila@719 | 391 | return IDENTITY_CONVERSION; |
attila@719 | 392 | } |
attila@719 | 393 | if(cacheable) { |
attila@719 | 394 | return last; |
attila@719 | 395 | } |
attila@719 | 396 | throw new NotCacheableConverter(last); |
attila@90 | 397 | } |
attila@90 | 398 | |
attila@90 | 399 | /*private*/ static final MethodHandle IDENTITY_CONVERSION = MethodHandles.identity(Object.class); |
attila@719 | 400 | |
attila@963 | 401 | @SuppressWarnings("serial") |
attila@719 | 402 | private static class NotCacheableConverter extends RuntimeException { |
attila@719 | 403 | final MethodHandle converter; |
attila@719 | 404 | |
attila@719 | 405 | NotCacheableConverter(final MethodHandle converter) { |
attila@719 | 406 | super("", null, false, false); |
attila@719 | 407 | this.converter = converter; |
attila@719 | 408 | } |
attila@719 | 409 | } |
attila@101 | 410 | } |