You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by us...@apache.org on 2016/02/20 01:06:15 UTC

lucene-solr git commit: LUCENE-6989: Make casting to Runnable interface in cleaner hack easier to understand

Repository: lucene-solr
Updated Branches:
  refs/heads/master 9418369b4 -> 0f29b3ec7


LUCENE-6989: Make casting to Runnable interface in cleaner hack easier to understand


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/0f29b3ec
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/0f29b3ec
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/0f29b3ec

Branch: refs/heads/master
Commit: 0f29b3ec7fd638341915f83384656e72dff868ec
Parents: 9418369
Author: Uwe Schindler <us...@apache.org>
Authored: Sat Feb 20 01:05:45 2016 +0100
Committer: Uwe Schindler <us...@apache.org>
Committed: Sat Feb 20 01:05:45 2016 +0100

----------------------------------------------------------------------
 .../org/apache/lucene/store/MMapDirectory.java  | 21 +++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/0f29b3ec/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java b/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java
index 348b36b..ff16324 100644
--- a/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java
+++ b/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java
@@ -343,8 +343,8 @@ public class MMapDirectory extends FSDirectory {
       
       final Method m = directBufferClass.getMethod("cleaner");
       m.setAccessible(true);
-      final MethodHandle directBufferCleanerMethod = lookup.unreflect(m);
-      final Class<?> cleanerClass = directBufferCleanerMethod.type().returnType();
+      MethodHandle directBufferCleanerMethod = lookup.unreflect(m);
+      Class<?> cleanerClass = directBufferCleanerMethod.type().returnType();
       
       final MethodHandle cleanMethod;
       if (Runnable.class.isAssignableFrom(cleanerClass)) {
@@ -353,19 +353,22 @@ public class MMapDirectory extends FSDirectory {
         if (sm != null) {
           sm.checkPackageAccess("jdk.internal.ref");
         }
-        cleanMethod = explicitCastArguments(lookup.findVirtual(Runnable.class, "run", methodType(void.class)),
-            methodType(void.class, cleanerClass));
+        // cast return value of cleaner() to Runnable:
+        directBufferCleanerMethod = directBufferCleanerMethod.asType(directBufferCleanerMethod.type().changeReturnType(Runnable.class));
+        cleanerClass = Runnable.class;
+        // lookup run() method on the interface instead of Cleaner:
+        cleanMethod = lookup.findVirtual(cleanerClass, "run", methodType(void.class));
       } else {
         // can be either the old internal "sun.misc.Cleaner" or
         // the new Java 9 "java.lang.ref.Cleaner$Cleanable":
         cleanMethod = lookup.findVirtual(cleanerClass, "clean", methodType(void.class));
       }
       
-      final MethodHandle nonNullTest = explicitCastArguments(lookup.findStatic(Objects.class, "nonNull", methodType(boolean.class, Object.class)),
-          methodType(boolean.class, cleanerClass));
-      final MethodHandle noop = dropArguments(explicitCastArguments(constant(Void.class, null), methodType(void.class)), 0, cleanerClass);
-      final MethodHandle unmapper = explicitCastArguments(filterReturnValue(directBufferCleanerMethod, guardWithTest(nonNullTest, cleanMethod, noop)),
-          methodType(void.class, ByteBuffer.class));
+      final MethodHandle nonNullTest = lookup.findStatic(Objects.class, "nonNull", methodType(boolean.class, Object.class))
+          .asType(methodType(boolean.class, cleanerClass));
+      final MethodHandle noop = dropArguments(constant(Void.class, null).asType(methodType(void.class)), 0, cleanerClass);
+      final MethodHandle unmapper = filterReturnValue(directBufferCleanerMethod, guardWithTest(nonNullTest, cleanMethod, noop))
+          .asType(methodType(void.class, ByteBuffer.class));
       
       return (BufferCleaner) (ByteBufferIndexInput parent, ByteBuffer buffer) -> {
         if (directBufferClass.isInstance(buffer)) {