Skip to content

Commit

Permalink
MethodCall.getAllMethods() now also returns default methods from inte…
Browse files Browse the repository at this point in the history
  • Loading branch information
belaban committed Jan 24, 2018
1 parent 6011ff1 commit 1440816
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 29 deletions.
42 changes: 26 additions & 16 deletions src/org/jgroups/blocks/MethodCall.java
Expand Up @@ -2,14 +2,16 @@


import org.jgroups.Constructable;
import org.jgroups.util.*;
import org.jgroups.util.Bits;
import org.jgroups.util.Streamable;
import org.jgroups.util.Util;

import java.io.*;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.*;
import java.util.function.Supplier;


Expand Down Expand Up @@ -366,15 +368,26 @@ protected void readMethod(DataInput in) throws Exception {
* and those inherited from superclasses and superinterfaces.
*/
protected static Method[] getAllMethods(Class target) {
Class superclass = target;
List methods = new ArrayList();
int size = 0;
Class superclass = target;
Set<Method> methods = new HashSet<>();

while(superclass != null) {
try {
Method[] m = superclass.getDeclaredMethods();
methods.add(m);
size += m.length;
Collections.addAll(methods, m);

// find the default methods of all interfaces (https://issues.jboss.org/browse/JGRP-2247)
Class[] interfaces=superclass.getInterfaces();
if(interfaces != null) {
for(Class cl: interfaces) {
Method[] tmp=getAllMethods(cl);
if(tmp != null) {
for(Method mm: tmp)
if(mm.isDefault())
methods.add(mm);
}
}
}
superclass = superclass.getSuperclass();
}
catch(SecurityException e) {
Expand All @@ -384,13 +397,10 @@ protected static Method[] getAllMethods(Class target) {
}
}

Method[] result = new Method[size];
Method[] result = new Method[methods.size()];
int index = 0;
for(Iterator i = methods.iterator(); i.hasNext();) {
Method[] m = (Method[])i.next();
System.arraycopy(m, 0, result, index, m.length);
index += m.length;
}
for(Method m: methods)
result[index++]=m;
return result;
}

Expand Down
84 changes: 76 additions & 8 deletions tests/junit/org/jgroups/blocks/RpcDispatcherUnitTest.java
Expand Up @@ -3,6 +3,7 @@

import org.jgroups.*;
import org.jgroups.tests.ChannelTestBase;
import org.jgroups.util.Rsp;
import org.jgroups.util.RspList;
import org.jgroups.util.Util;
import org.testng.annotations.AfterClass;
Expand Down Expand Up @@ -167,25 +168,92 @@ public void testInvocationOnEmptyTargetSet() throws Exception {
}


public void testInvocationOfDefaultMethodInParentInterface() throws Exception {
RspList<Integer> rsps=d1.callRemoteMethods(null, "bar", null, null, RequestOptions.SYNC());
System.out.println("rsps:\n" + rsps);
assert rsps.size() == 3;
for(Rsp<Integer> rsp: rsps)
assert rsp.getValue() == 22;
}

private static class ServerObject {
boolean called=false;
int num_calls=0;

public boolean wasCalled() {
return called;
public void testInvocationOfDefaultMethod() throws Exception {
RspList<Integer> rsps=d1.callRemoteMethods(null, "bar2", null, null, RequestOptions.SYNC());
System.out.println("rsps:\n" + rsps);
assert rsps.size() == 3;
for(Rsp<Integer> rsp: rsps)
assert rsp.getValue() == 44;
}

public void testInvocationOnSubclass() throws Exception {
Object obj1=d1.server_obj, obj2=d2.server_obj, obj3=d3.server_obj;
try {
d1.server_obj=new Subclass();
d2.server_obj=new Subclass();
d3.server_obj=new Subclass();
RspList<Integer> rsps=d1.callRemoteMethods(null, "foobar", null, null, RequestOptions.SYNC());
System.out.println("rsps:\n" + rsps);
assert rsps.size() == 3;
for(Rsp<Integer> rsp : rsps)
assert rsp.getValue() == 33;
}
finally {
d1.server_obj=obj1;
d2.server_obj=obj2;
d3.server_obj=obj3;
}
}

public int getNumCalls() {return num_calls;}
public void testInvocationOnObject() throws Exception {
RspList<Integer> rsps=d1.callRemoteMethods(null, "hashCode", null, null, RequestOptions.SYNC());
System.out.println("rsps:\n" + rsps);
assert rsps.size() == 3;
for(Rsp<Integer> rsp: rsps)
assert rsp.getValue() > 0;
}

public void reset() {
called=false; num_calls=0;
public void testInvocationOfProtectedMethod() throws Exception {
RspList<Boolean> rsps=d1.callRemoteMethods(null, "protectedMethod", null, null, RequestOptions.SYNC());
System.out.println("rsps:\n" + rsps);
assert rsps.size() == 3;
for(Rsp<Boolean> rsp: rsps)
assert rsp.getValue();
}


protected interface ParentInterface {
boolean wasCalled();
default int bar() {return 22;}
}

protected interface MyInterface extends ParentInterface {
int getNumCalls();
void reset();
boolean foo();
default int bar2() {return 44;}
}

protected static class ServerObject implements MyInterface {
boolean called;
int num_calls;

public boolean wasCalled() {
return called;
}
public int getNumCalls() {return num_calls;}
public void reset() {called=false; num_calls=0;}

public boolean foo() {
num_calls++;
return called=true;
}

protected static boolean protectedMethod() {return true;}
}


protected static class Subclass extends ServerObject {
public static int foobar() {return 33;}
}


Expand Down
10 changes: 7 additions & 3 deletions tests/junit/org/jgroups/tests/GossipRouterTest.java
Expand Up @@ -108,12 +108,16 @@ protected JChannel createTunnelChannel(String name) throws Exception {
}

protected JChannel createTunnelChannel(String name, boolean include_failure_detection) throws Exception {
TUNNEL tunnel=(TUNNEL)new TUNNEL().setValue("bind_addr", bind_addr).setValue("reconnect_interval", 1000);
TUNNEL tunnel=new TUNNEL().setValue("bind_addr", bind_addr).setValue("reconnect_interval", 1000);
tunnel.setGossipRouterHosts(gossip_router_hosts);
List<Protocol> protocols=new ArrayList<>();
protocols.addAll(Arrays.asList(tunnel, new PING(), new MERGE3().setValue("min_interval", 1000).setValue("max_interval", 3000)));
if(include_failure_detection)
protocols.addAll(Arrays.asList(new FD().setValue("timeout", 2000).setValue("max_tries", 2), new VERIFY_SUSPECT()));
if(include_failure_detection) {
List<Protocol> tmp=new ArrayList<>(2);
tmp.add(new FD().setValue("timeout", 2000).setValue("max_tries", 2));
tmp.add(new VERIFY_SUSPECT());
protocols.addAll(tmp);
}
protocols.addAll(Arrays.asList(new NAKACK2().setValue("use_mcast_xmit", false), new UNICAST3(), new STABLE(),
new GMS().joinTimeout(10)));
JChannel ch=new JChannel(protocols);
Expand Down
8 changes: 6 additions & 2 deletions tests/junit/org/jgroups/tests/TUNNEL_Test.java
Expand Up @@ -256,8 +256,12 @@ protected JChannel createTunnelChannel(String name, boolean include_failure_dete
tunnel.setGossipRouterHosts(gossip_router_hosts);
List<Protocol> protocols=new ArrayList<>();
protocols.addAll(Arrays.asList(tunnel, new PING(), new MERGE3().setValue("min_interval", 1000).setValue("max_interval", 3000)));
if(include_failure_detection)
protocols.addAll(Arrays.asList(new FD().setValue("timeout", 2000).setValue("max_tries", 2), new VERIFY_SUSPECT()));
if(include_failure_detection) {
List<Protocol> tmp=new ArrayList<>(2);
tmp.add(new FD().setValue("timeout", 2000).setValue("max_tries", 2));
tmp.add(new VERIFY_SUSPECT());
protocols.addAll(tmp);
}
protocols.addAll(Arrays.asList(new NAKACK2().setValue("use_mcast_xmit", false), new UNICAST3(), new STABLE(),
new GMS().joinTimeout(1000)));
JChannel ch=new JChannel(protocols);
Expand Down

0 comments on commit 1440816

Please sign in to comment.