APIdia Launch Partner
Top Description Inners Fields Constructors Methods
jdk.dynalink

pack-priv final Class TypeConverterFactory

extends Object
Class Inheritance
Imports
java.lang.invoke.MethodHandle, .MethodHandles, .MethodHandles.Lookup, .MethodType, .WrongMethodTypeException, java.util.LinkedList, .List, java.util.function.Supplier, jdk.dynalink.linker.ConversionComparator, .ConversionComparator.Comparison, .GuardedInvocation, .GuardingTypeConverterFactory, .LinkerServices, .MethodTypeConversionStrategy, jdk.dynalink.linker.support.TypeUtilities

A factory for type converters. This class is the main implementation behind the LinkerServices#asType(MethodHandle, MethodType). It manages the known GuardingTypeConverterFactory instances and creates appropriate converters for method handles.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class

Field Summary

Modifier and TypeField and Description
private final MethodTypeConversionStrategy
private final BiClassValue<Boolean>
private final ConversionComparator[]
private final BiClassValue<MethodHandle>
private final BiClassValue<MethodHandle>
pack-priv static final MethodHandle

Constructor Summary

AccessConstructor and Description
pack-priv
TypeConverterFactory(final Iterable<? extends GuardingTypeConverterFactory>
the GuardingTypeConverterFactory instances to compose.
factories
,
final MethodTypeConversionStrategy
conversion strategy for automatic type conversions. After asType(java.lang.invoke.MethodHandle, java.lang.invoke.MethodType) has applied all custom conversions to a method handle, it still needs to effect method invocation conversions that can usually be automatically applied as per java.lang.invoke.MethodHandle#asType(java.lang.invoke.MethodType). However, sometimes language runtimes will want to customize even those conversions for their own call sites. A typical example is allowing unboxing of null return values, which is by default prohibited by ordinary MethodHandles.asType. In this case, a language runtime can install its own custom automatic conversion strategy, that can deal with null values. Note that when the strategy's MethodTypeConversionStrategy#asType(java.lang.invoke.MethodHandle, java.lang.invoke.MethodType) is invoked, the custom language conversions will already have been applied to the method handle, so by design the difference between the handle's current method type and the desired final type will always only be ones that can be subjected to method invocation conversions. Can be null, in which case no custom strategy is employed.
autoConversionStrategy
)

Creates a new type converter factory from the available GuardingTypeConverterFactory instances.

Method Summary

Modifier and TypeMethod and Description
private static MethodHandle
applyConverters(final MethodHandle handle, final int pos, final List<MethodHandle> converters)

pack-priv MethodHandle
asType(final MethodHandle
target method handle
handle
,
final MethodType
the types of source arguments
fromType
)

Similar to MethodHandle#asType(MethodType) except it also hooks in method handles produced by GuardingTypeConverterFactory implementations, providing for language-specific type coercing of parameters.

pack-priv static boolean

Returns:

true if it's safe to let MethodHandles.convertArguments() to handle this conversion.
canAutoConvert
(final Class<?>
convert from this class
fromType
,
final Class<?>
convert to this class
toType
)

Determines whether it's safe to perform an automatic conversion between the source and target class.

pack-priv boolean

Returns:

true if there can be a conversion, false if there can not.
canConvert
(final Class<?>
the source type for the conversion
from
,
final Class<?>
the target type for the conversion
to
)

Returns true if there might exist a conversion between the requested types (either an automatic JVM conversion, or one provided by any available GuardingTypeConverterFactory), or false if there definitely does not exist a conversion between the requested types.

pack-priv ConversionComparator.Comparison

Returns:

one of Comparison constants that establish which - if any - of the target types is preferable for the conversion.
compareConversion
(final Class<?>
the source type.
sourceType
,
final Class<?>
one potential target type
targetType1
,
final Class<?>
another potential target type.
targetType2
)

Determines which of the two type conversions from a source type to the two target types is preferred.

pack-priv static MethodHandle
createConverter(final Class<?> sourceType, final Class<?> targetType, GuardingTypeConverterFactory[] factories)

pack-priv static MethodHandle
getCacheableTypeConverter(final Class<?> sourceType, final Class<?> targetType, final BiClassValue<MethodHandle> converterMap)

pack-priv static MethodHandle
getCacheableTypeConverterNull(final Class<?> sourceType, final Class<?> targetType, final BiClassValue<MethodHandle> converterMap)

pack-priv MethodHandle

Returns:

a method handle performing the conversion.
getTypeConverter
(final Class<?>
the type to convert from
sourceType
,
final Class<?>
the type to convert to
targetType
)

Given a source and target type, returns a method handle that converts between them.

pack-priv static MethodHandle
getTypeConverterNull(final Class<?> sourceType, final Class<?> targetType, final BiClassValue<MethodHandle> converterMap)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

autoConversionStrategyback to summary
private final MethodTypeConversionStrategy autoConversionStrategy
canConvertback to summary
private final BiClassValue<Boolean> canConvert
comparatorsback to summary
private final ConversionComparator[] comparators
converterIdentityMapback to summary
private final BiClassValue<MethodHandle> converterIdentityMap
converterMapback to summary
private final BiClassValue<MethodHandle> converterMap
IDENTITY_CONVERSIONback to summary
pack-priv static final MethodHandle IDENTITY_CONVERSION

Constructor Detail

TypeConverterFactoryback to summary
pack-priv TypeConverterFactory(final Iterable<? extends GuardingTypeConverterFactory> factories, final MethodTypeConversionStrategy autoConversionStrategy)

Creates a new type converter factory from the available GuardingTypeConverterFactory instances.

Parameters
factories:Iterable<? extends GuardingTypeConverterFactory>

the GuardingTypeConverterFactory instances to compose.

autoConversionStrategy:MethodTypeConversionStrategy

conversion strategy for automatic type conversions. After asType(java.lang.invoke.MethodHandle, java.lang.invoke.MethodType) has applied all custom conversions to a method handle, it still needs to effect method invocation conversions that can usually be automatically applied as per java.lang.invoke.MethodHandle#asType(java.lang.invoke.MethodType). However, sometimes language runtimes will want to customize even those conversions for their own call sites. A typical example is allowing unboxing of null return values, which is by default prohibited by ordinary MethodHandles.asType. In this case, a language runtime can install its own custom automatic conversion strategy, that can deal with null values. Note that when the strategy's MethodTypeConversionStrategy#asType(java.lang.invoke.MethodHandle, java.lang.invoke.MethodType) is invoked, the custom language conversions will already have been applied to the method handle, so by design the difference between the handle's current method type and the desired final type will always only be ones that can be subjected to method invocation conversions. Can be null, in which case no custom strategy is employed.

Method Detail

applyConvertersback to summary
private static MethodHandle applyConverters(final MethodHandle handle, final int pos, final List<MethodHandle> converters)
asTypeback to summary
pack-priv MethodHandle asType(final MethodHandle handle, final MethodType fromType)

Similar to MethodHandle#asType(MethodType) except it also hooks in method handles produced by GuardingTypeConverterFactory implementations, providing for language-specific type coercing of parameters. For all conversions that are not a JLS method invocation conversion it'll insert MethodHandles#filterArguments(MethodHandle, int, MethodHandle...) with composite filters provided by GuardingTypeConverterFactory implementations. For the remaining JLS method invocation conversions, it will invoke MethodTypeConversionStrategy#asType(MethodHandle, MethodType) first if an automatic conversion strategy was specified in the constructor, and finally apply MethodHandle#asType(MethodType) for any remaining conversions.

Parameters
handle:MethodHandle

target method handle

fromType:MethodType

the types of source arguments

Returns:MethodHandle

a method handle that is a suitable combination of MethodHandle#asType(MethodType), MethodTypeConversionStrategy#asType(MethodHandle, MethodType), and MethodHandles#filterArguments(MethodHandle, int, MethodHandle...) with GuardingTypeConverterFactory produced type converters as filters.

canAutoConvertback to summary
pack-priv static boolean canAutoConvert(final Class<?> fromType, final Class<?> toType)

Determines whether it's safe to perform an automatic conversion between the source and target class.

Parameters
fromType:Class<?>

convert from this class

toType:Class<?>

convert to this class

Returns:boolean

true if it's safe to let MethodHandles.convertArguments() to handle this conversion.

canConvertback to summary
pack-priv boolean canConvert(final Class<?> from, final Class<?> to)

Returns true if there might exist a conversion between the requested types (either an automatic JVM conversion, or one provided by any available GuardingTypeConverterFactory), or false if there definitely does not exist a conversion between the requested types. Note that returning true does not guarantee that the conversion will succeed at runtime (notably, if the "from" or "to" types are sufficiently generic), but returning false guarantees that it would fail.

Parameters
from:Class<?>

the source type for the conversion

to:Class<?>

the target type for the conversion

Returns:boolean

true if there can be a conversion, false if there can not.

compareConversionback to summary
pack-priv ConversionComparator.Comparison compareConversion(final Class<?> sourceType, final Class<?> targetType1, final Class<?> targetType2)

Determines which of the two type conversions from a source type to the two target types is preferred. This is used for dynamic overloaded method resolution. If the source type is convertible to exactly one target type with a method invocation conversion, it is chosen, otherwise available ConversionComparators are consulted.

Parameters
sourceType:Class<?>

the source type.

targetType1:Class<?>

one potential target type

targetType2:Class<?>

another potential target type.

Returns:ConversionComparator.Comparison

one of Comparison constants that establish which - if any - of the target types is preferable for the conversion.

createConverterback to summary
pack-priv static MethodHandle createConverter(final Class<?> sourceType, final Class<?> targetType, GuardingTypeConverterFactory[] factories)
getCacheableTypeConverterback to summary
pack-priv static MethodHandle getCacheableTypeConverter(final Class<?> sourceType, final Class<?> targetType, final BiClassValue<MethodHandle> converterMap)
getCacheableTypeConverterNullback to summary
pack-priv static MethodHandle getCacheableTypeConverterNull(final Class<?> sourceType, final Class<?> targetType, final BiClassValue<MethodHandle> converterMap)
getTypeConverterback to summary
pack-priv MethodHandle getTypeConverter(final Class<?> sourceType, final Class<?> targetType)

Given a source and target type, returns a method handle that converts between them. Never returns null; in worst case it will return an identity conversion (that might fail for some values at runtime). You can use this method if you have a piece of your program that is written in Java, and you need to reuse existing type conversion machinery in a non-invokedynamic context.

Parameters
sourceType:Class<?>

the type to convert from

targetType:Class<?>

the type to convert to

Returns:MethodHandle

a method handle performing the conversion.

getTypeConverterNullback to summary
pack-priv static MethodHandle getTypeConverterNull(final Class<?> sourceType, final Class<?> targetType, final BiClassValue<MethodHandle> converterMap)
jdk.dynalink back to summary

private Class TypeConverterFactory.LookupSupplier

extends Object
implements Supplier<MethodHandles.Lookup>
Class Inheritance
All Implemented Interfaces
java.util.function.Supplier

Field Summary

Modifier and TypeField and Description
pack-priv volatile boolean
pack-priv volatile boolean

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public MethodHandles.Lookup
get()

Implements java.util.function.Supplier.get.

Gets a result.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

closedback to summary
pack-priv volatile boolean closed
returnedLookupback to summary
pack-priv volatile boolean returnedLookup

Constructor Detail

LookupSupplierback to summary
private LookupSupplier()

Method Detail

getback to summary
public MethodHandles.Lookup get()

Implements java.util.function.Supplier.get.

Doc from java.util.function.Supplier.get.

Gets a result.

Annotations
@Override
jdk.dynalink back to summary

private Class TypeConverterFactory.NotCacheableConverter

extends RuntimeException
Class Inheritance
Annotations
@SuppressWarnings:serial

Field Summary

Modifier and TypeField and Description
pack-priv final MethodHandle

Constructor Summary

AccessConstructor and Description
pack-priv

Field Detail

converterback to summary
pack-priv final MethodHandle converter

Constructor Detail

NotCacheableConverterback to summary
pack-priv NotCacheableConverter(final MethodHandle converter)