8139274: Use JDK 8 default method for LinkerServices.asTypeLosslessReturn
authorattila
Mon, 12 Oct 2015 10:28:27 +0200
changeset 33004 e4de5829b493
parent 33003 f1e00197031f
child 33005 19c2fe67d821
8139274: Use JDK 8 default method for LinkerServices.asTypeLosslessReturn Reviewed-by: lagergren, sundar
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/internal/dynalink/linker/LinkerServices.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/internal/dynalink/support/LinkerServicesImpl.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/NashornBeansLinker.java
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/internal/dynalink/linker/LinkerServices.java	Mon Oct 12 10:27:32 2015 +0200
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/internal/dynalink/linker/LinkerServices.java	Mon Oct 12 10:28:27 2015 +0200
@@ -130,7 +130,11 @@
      * {@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 @@
      * @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));
-        }
-    }
 }
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/internal/dynalink/support/LinkerServicesImpl.java	Mon Oct 12 10:27:32 2015 +0200
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/internal/dynalink/support/LinkerServicesImpl.java	Mon Oct 12 10:28:27 2015 +0200
@@ -132,11 +132,6 @@
     }
 
     @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);
     }
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/NashornBeansLinker.java	Mon Oct 12 10:27:32 2015 +0200
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/NashornBeansLinker.java	Mon Oct 12 10:28:27 2015 +0200
@@ -211,11 +211,6 @@
         }
 
         @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);
         }