8139274: Use JDK 8 default method for LinkerServices.asTypeLosslessReturn
Reviewed-by: lagergren, sundar
This commit is contained in:
parent
26f00c8261
commit
5dd8a5bcfe
@ -130,7 +130,11 @@ public interface LinkerServices {
|
||||
* {@link MethodHandles#filterArguments(MethodHandle, int, MethodHandle...)} with
|
||||
* {@link GuardingTypeConverterFactory}-produced type converters as filters.
|
||||
*/
|
||||
public MethodHandle asTypeLosslessReturn(MethodHandle handle, MethodType fromType);
|
||||
public default MethodHandle asTypeLosslessReturn(final MethodHandle handle, final MethodType fromType) {
|
||||
final Class<?> handleReturnType = handle.type().returnType();
|
||||
return asType(handle, TypeUtilities.isConvertibleWithoutLoss(handleReturnType, fromType.returnType()) ?
|
||||
fromType : fromType.changeReturnType(handleReturnType));
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a source and target type, returns a method handle that converts between them. Never returns null; in worst
|
||||
@ -188,23 +192,4 @@ public interface LinkerServices {
|
||||
* @return a method handle with parameters and/or return type potentially filtered for wrapping and unwrapping.
|
||||
*/
|
||||
public MethodHandle filterInternalObjects(final MethodHandle target);
|
||||
|
||||
/**
|
||||
* If we could just use Java 8 constructs, then {@code asTypeSafeReturn} would be a method with default
|
||||
* implementation. Since we can't do that, we extract common default implementations into this static class.
|
||||
*/
|
||||
public static class Implementation {
|
||||
/**
|
||||
* Default implementation for {@link LinkerServices#asTypeLosslessReturn(MethodHandle, MethodType)}.
|
||||
* @param linkerServices the linker services that delegates to this implementation
|
||||
* @param handle the passed handle
|
||||
* @param fromType the passed type
|
||||
* @return the converted method handle, as per the {@code asTypeSafeReturn} semantics.
|
||||
*/
|
||||
public static MethodHandle asTypeLosslessReturn(final LinkerServices linkerServices, final MethodHandle handle, final MethodType fromType) {
|
||||
final Class<?> handleReturnType = handle.type().returnType();
|
||||
return linkerServices.asType(handle, TypeUtilities.isConvertibleWithoutLoss(handleReturnType, fromType.returnType()) ?
|
||||
fromType : fromType.changeReturnType(handleReturnType));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -131,11 +131,6 @@ public class LinkerServicesImpl implements LinkerServices {
|
||||
return typeConverterFactory.asType(handle, fromType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MethodHandle asTypeLosslessReturn(final MethodHandle handle, final MethodType fromType) {
|
||||
return Implementation.asTypeLosslessReturn(this, handle, fromType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MethodHandle getTypeConverter(final Class<?> sourceType, final Class<?> targetType) {
|
||||
return typeConverterFactory.getTypeConverter(sourceType, targetType);
|
||||
|
@ -210,11 +210,6 @@ public class NashornBeansLinker implements GuardingDynamicLinker {
|
||||
return linkerServices.asType(handle, fromType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MethodHandle asTypeLosslessReturn(final MethodHandle handle, final MethodType fromType) {
|
||||
return Implementation.asTypeLosslessReturn(this, handle, fromType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MethodHandle getTypeConverter(final Class<?> sourceType, final Class<?> targetType) {
|
||||
return linkerServices.getTypeConverter(sourceType, targetType);
|
||||
|
Loading…
Reference in New Issue
Block a user