This is an automated email from the ASF dual-hosted git repository.

ivank pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bookkeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new e5af4a2  Allow default methods in MavenClassloader callbacks
e5af4a2 is described below

commit e5af4a20f240f38c8ce712a5e3bba4976eb89a41
Author: Ivan Kelly <iv...@apache.org>
AuthorDate: Tue Feb 20 18:07:54 2018 +0100

    Allow default methods in MavenClassloader callbacks
    
    AsyncCallback.AddCallback has recently acquired a default method,
    which breaks the assumption that there is only one method on these
    callbacks. However, if does have a default method that calls the one
    method. This patch takes advantage of that to allow us to use
    AddCallback in tests.
    
    Author: Ivan Kelly <iv...@apache.org>
    
    Reviewers: Enrico Olivelli <eolive...@gmail.com>
    
    This closes #1187 from ivankelly/groovy-callback-default
---
 tests/integration-tests-base-groovy/pom.xml         |  1 -
 tests/integration-tests-utils/pom.xml               |  6 ++++++
 .../apache/bookkeeper/tests/MavenClassLoader.java   | 21 ++++++++++++++++++---
 tests/pom.xml                                       |  5 +++++
 4 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/tests/integration-tests-base-groovy/pom.xml 
b/tests/integration-tests-base-groovy/pom.xml
index 9bf71b0..738e813 100644
--- a/tests/integration-tests-base-groovy/pom.xml
+++ b/tests/integration-tests-base-groovy/pom.xml
@@ -35,7 +35,6 @@
   <name>Apache BookKeeper :: Tests :: Base module for Arquillian based 
integration tests using groovy</name>
 
   <properties>
-    <groovy.version>2.4.13</groovy.version>
     <groovy-eclipse-compiler.version>2.9.2-04</groovy-eclipse-compiler.version>
     <groovy-eclipse-batch.version>2.4.13-02</groovy-eclipse-batch.version>
   </properties>
diff --git a/tests/integration-tests-utils/pom.xml 
b/tests/integration-tests-utils/pom.xml
index 35fb4c8..811efe2 100644
--- a/tests/integration-tests-utils/pom.xml
+++ b/tests/integration-tests-utils/pom.xml
@@ -92,6 +92,12 @@
       <version>${arquillian-cube.version}</version>
     </dependency>
 
+    <dependency>
+      <groupId>org.codehaus.groovy</groupId>
+      <artifactId>groovy-all</artifactId>
+      <version>${groovy.version}</version>
+    </dependency>
+
   </dependencies>
 
   <build>
diff --git 
a/tests/integration-tests-utils/src/main/java/org/apache/bookkeeper/tests/MavenClassLoader.java
 
b/tests/integration-tests-utils/src/main/java/org/apache/bookkeeper/tests/MavenClassLoader.java
index d25dbc2..2c3bb30 100644
--- 
a/tests/integration-tests-utils/src/main/java/org/apache/bookkeeper/tests/MavenClassLoader.java
+++ 
b/tests/integration-tests-utils/src/main/java/org/apache/bookkeeper/tests/MavenClassLoader.java
@@ -22,7 +22,11 @@ package org.apache.bookkeeper.tests;
 
 import com.google.common.collect.Lists;
 
+import groovy.lang.Closure;
+
 import java.io.File;
+import java.lang.invoke.MethodHandles;
+import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
@@ -135,14 +139,25 @@ public class MavenClassLoader implements AutoCloseable {
         }
     }
 
-    public Object createCallback(String interfaceName, Object closure) throws 
Exception {
+    public Object createCallback(String interfaceName, Closure closure) throws 
Exception {
+        final Constructor<MethodHandles.Lookup> constructor = 
MethodHandles.Lookup.class.getDeclaredConstructor(
+                Class.class, int.class);
+        constructor.setAccessible(true);
         return Proxy.newProxyInstance(classloader,
                                       new Class<?>[]{ 
Class.forName(interfaceName, true, classloader) },
                                       new InvocationHandler() {
+
                                           @Override
                                           public Object invoke(Object proxy, 
Method m, Object[] args) throws Throwable {
-                                              Method call = 
closure.getClass().getMethod("call", Object[].class);
-                                              return call.invoke(closure, 
(Object)args);
+                                              if (args.length == 
closure.getMaximumNumberOfParameters()) {
+                                                  return closure.call(args);
+                                              } else {
+                                                  final Class<?> 
declaringClass = m.getDeclaringClass();
+                                                  return 
constructor.newInstance(declaringClass, MethodHandles.Lookup.PRIVATE)
+                                                      .unreflectSpecial(m, 
declaringClass)
+                                                      .bindTo(proxy)
+                                                      
.invokeWithArguments(args);
+                                              }
                                           }
                                       });
     }
diff --git a/tests/pom.xml b/tests/pom.xml
index fa8484c..db0d46e 100644
--- a/tests/pom.xml
+++ b/tests/pom.xml
@@ -27,6 +27,11 @@
   <groupId>org.apache.bookkeeper.tests</groupId>
   <artifactId>tests-parent</artifactId>
   <name>Apache BookKeeper :: Tests</name>
+
+  <properties>
+    <groovy.version>2.4.13</groovy.version>
+  </properties>
+
   <modules>
     <module>shaded</module>
     <module>docker-images</module>

-- 
To stop receiving notification emails like this one, please contact
iv...@apache.org.

Reply via email to