You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@bookkeeper.apache.org by GitBox <gi...@apache.org> on 2018/02/17 07:06:08 UTC

[GitHub] sijie closed pull request #1156: Call static methods and build callbacks in MavenClassLoader

sijie closed pull request #1156: Call static methods and build callbacks in MavenClassLoader
URL: https://github.com/apache/bookkeeper/pull/1156
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

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 a7091bec1..d25dbc2ff 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
@@ -23,10 +23,14 @@
 import com.google.common.collect.Lists;
 
 import java.io.File;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Optional;
@@ -102,6 +106,47 @@ public static MavenClassLoader forBookKeeperVersion(String version) throws Excep
         return forArtifact("org.apache.bookkeeper:bookkeeper-server:" +  version);
     }
 
+    public Object getClass(String className) throws Exception {
+        return Class.forName(className, true, classloader);
+    }
+
+    public Object callStaticMethod(String className, String methodName, ArrayList<?> args) throws Exception {
+        Class<?> klass = Class.forName(className, true, classloader);
+
+        try {
+            Class<?>[] paramTypes = args.stream().map((a)-> a.getClass()).toArray(Class[]::new);
+            return klass.getMethod(methodName, paramTypes).invoke(null, args.stream().toArray(Object[]::new));
+        } catch (NoSuchMethodException nsme) {
+            // maybe the params are primitives
+            Class<?>[] paramTypes = args.stream().map((a) -> {
+                    Class<?> k = a.getClass();
+                    try {
+                        Object type = k.getField("TYPE").get(null);
+                        if (type instanceof Class<?>) {
+                            return (Class<?>)type;
+                        } else {
+                            return k;
+                        }
+                    } catch (IllegalAccessException | NoSuchFieldException nsfe) {
+                        return k;
+                    }
+                }).toArray(Class[]::new);
+            return klass.getMethod(methodName, paramTypes).invoke(null, args.stream().toArray(Object[]::new));
+        }
+    }
+
+    public Object createCallback(String interfaceName, Object closure) throws Exception {
+        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);
+                                          }
+                                      });
+    }
+
     public Object newInstance(String className, Object... args) throws Exception {
         Class<?> klass = Class.forName(className, true, classloader);
         return klass.getConstructor(Arrays.stream(args).map((a)-> a.getClass()).toArray(Class[]::new))
diff --git a/tests/integration-tests-utils/src/test/java/org/apache/bookkeeper/tests/MavenClassLoaderTest.java b/tests/integration-tests-utils/src/test/java/org/apache/bookkeeper/tests/MavenClassLoaderTest.java
index 930355787..e4d05336a 100644
--- a/tests/integration-tests-utils/src/test/java/org/apache/bookkeeper/tests/MavenClassLoaderTest.java
+++ b/tests/integration-tests-utils/src/test/java/org/apache/bookkeeper/tests/MavenClassLoaderTest.java
@@ -20,6 +20,8 @@
  */
 package org.apache.bookkeeper.tests;
 
+import com.google.common.collect.Lists;
+
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -39,4 +41,11 @@ public void testNoZooKeeperInterference() throws Exception {
                                    org.apache.zookeeper.KeeperException.NoNodeException.class));
     }
 
+    @Test
+    public void testStaticMethod() throws Exception {
+        Object o = MavenClassLoader.forBookKeeperVersion("4.6.0").callStaticMethod(
+                "org.apache.bookkeeper.client.BKException", "create", Lists.newArrayList(-101));
+        Assert.assertEquals(o.getClass().getName(),
+                            "org.apache.bookkeeper.client.BKException$BKLedgerFencedException");
+    }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services