Skip to content
Snippets Groups Projects
Commit 050bc7aa authored by Paul Gregoire's avatar Paul Gregoire
Browse files

Add and update for reflection testing

parent 76eec104
No related branches found
No related tags found
No related merge requests found
...@@ -136,15 +136,17 @@ public class ReflectionUtils { ...@@ -136,15 +136,17 @@ public class ReflectionUtils {
log.trace("Args / parameters count: {}", numParams); log.trace("Args / parameters count: {}", numParams);
} }
// convert the args first // convert the args first
Object[] params = ConversionUtils.convertParams(args); Class<?>[] params = ConversionUtils.convertParams(args);
if (params[0] != null && IConnection.class.isInstance(params[0])) {
// if it does implement IConnection use the interface
params[0] = IConnection.class;
}
if (isDebug) { if (isDebug) {
for (Object clazz : params) { Arrays.asList(params).forEach(param -> log.debug("Parameter: {}", param));
log.debug("Parameter: {}", clazz);
}
} }
// try to skip the listing of all the methods by checking for exactly what we want first // try to skip the listing of all the methods by checking for exactly what we want first
try { try {
Method method = service.getClass().getMethod(methodName, (Class<?>[]) params); Method method = service.getClass().getMethod(methodName, params);
if (isDebug) { if (isDebug) {
log.debug("Exact method found, skipping list: {}", methodName); log.debug("Exact method found, skipping list: {}", methodName);
} }
...@@ -208,29 +210,29 @@ public class ReflectionUtils { ...@@ -208,29 +210,29 @@ public class ReflectionUtils {
public static Object[] findMethodWithListParameters(Object service, String methodName, Object[] args) { public static Object[] findMethodWithListParameters(Object service, String methodName, Object[] args) {
Method method = null; Method method = null;
try { try {
//try to skip the listing of all the methods by checking for exactly what // convert the args first
//we want first Class<?>[] params = ConversionUtils.convertParams(args);
method = service.getClass().getMethod(methodName, ConversionUtils.convertParams(args)); if (params[0] != null && IConnection.class.isInstance(params[0])) {
// if it does implement IConnection use the interface
params[0] = IConnection.class;
}
// try to skip the listing of all the methods by checking for exactly what we want first
method = service.getClass().getMethod(methodName, params);
log.debug("Exact method found (skipping list): {}", methodName); log.debug("Exact method found (skipping list): {}", methodName);
return new Object[] { method, args }; return new Object[] { method, args };
} catch (NoSuchMethodException nsme) { } catch (NoSuchMethodException nsme) {
log.debug("Method not found using exact parameter types", nsme); log.debug("Method not found using exact parameter types", nsme);
} }
List<Method> methods = findMethodsByNameAndNumParams(service, methodName, 1); List<Method> methods = findMethodsByNameAndNumParams(service, methodName, args.length);
if (isTrace) { if (isTrace) {
log.trace("Found {} methods", methods.size()); log.trace("Found {} methods", methods.size());
} }
if (!methods.isEmpty()) { if (!methods.isEmpty()) {
log.debug("Multiple methods found with same name and parameter count; parameter conversion will be attempted in order"); log.debug("Multiple methods found with same name and parameter count; parameter conversion will be attempted in order");
Object[] params = null;
for (int i = 0; i < methods.size(); i++) { for (int i = 0; i < methods.size(); i++) {
try { try {
method = methods.get(i); method = methods.get(i);
params = ConversionUtils.convertParams(args, method.getParameterTypes()); Object[] params = ConversionUtils.convertParams(args, method.getParameterTypes());
if (args.length > 0 && (args[0] instanceof IConnection) && (!(params[0] instanceof IConnection))) {
// Don't convert first IConnection parameter
continue;
}
return new Object[] { method, params }; return new Object[] { method, params };
} catch (Exception ex) { } catch (Exception ex) {
log.debug("Parameter conversion failed", ex); log.debug("Parameter conversion failed", ex);
......
...@@ -312,24 +312,16 @@ public class ConversionUtils { ...@@ -312,24 +312,16 @@ public class ConversionUtils {
* @return Array of converted objects * @return Array of converted objects
*/ */
public static Class<?>[] convertParams(Object[] source) { public static Class<?>[] convertParams(Object[] source) {
Class<?>[] converted = null; Class<?>[] converted = new Class<?>[0];
if (source != null) { if (source != null) {
converted = new Class<?>[source.length]; converted = new Class<?>[source.length];
for (int i = 0; i < source.length; i++) { for (int i = 0; i < source.length; i++) {
if (source[i] != null) { if (source[i] != null) {
// if the class is not an instance of IConnection use its class
//if (!IConnection.class.isInstance(source[i])) {
converted[i] = source[i].getClass(); converted[i] = source[i].getClass();
//} else {
// if it does implement IConnection use the interface
//converted[i] = IConnection.class;
//}
} else { } else {
converted[i] = null; converted[i] = null;
} }
} }
} else {
converted = new Class<?>[0];
} }
if (log.isTraceEnabled()) { if (log.isTraceEnabled()) {
log.trace("Converted parameters: {}", Arrays.toString(converted)); log.trace("Converted parameters: {}", Arrays.toString(converted));
......
...@@ -32,7 +32,7 @@ public class ReflectionUtilsTest { ...@@ -32,7 +32,7 @@ public class ReflectionUtilsTest {
log.info("Result: {}", Arrays.asList(result)); log.info("Result: {}", Arrays.asList(result));
} }
assertNotEquals(NULL_RETURN, result); assertNotEquals(NULL_RETURN, result);
// // call with two parameters string and int
call = new PendingCall("TestService.doTest", new Object[] { "test", 42 }); call = new PendingCall("TestService.doTest", new Object[] { "test", 42 });
result = ReflectionUtils.findMethod(conn, call, service, methodName); result = ReflectionUtils.findMethod(conn, call, service, methodName);
if (result == null) { if (result == null) {
...@@ -42,6 +42,19 @@ public class ReflectionUtilsTest { ...@@ -42,6 +42,19 @@ public class ReflectionUtilsTest {
log.info("Result 2: {}", Arrays.asList(result)); log.info("Result 2: {}", Arrays.asList(result));
} }
assertNotEquals(NULL_RETURN, result); assertNotEquals(NULL_RETURN, result);
// call with two parameters string and int but expecting Connection as first parameter to hit on 2nd call
methodName = "doTestWithConn";
call = new PendingCall("TestService.doTestWithConn", new Object[] { "test", 42 });
result = ReflectionUtils.findMethod(conn, call, service, methodName);
if (result == null) {
log.info("Result is null");
fail("Result is null, method not found");
} else {
log.info("Result 2: {}", Arrays.asList(result));
}
assertNotEquals(NULL_RETURN, result);
} }
private class DummyConnection extends RTMPMinaConnection { private class DummyConnection extends RTMPMinaConnection {
...@@ -54,9 +67,8 @@ public class ReflectionUtilsTest { ...@@ -54,9 +67,8 @@ public class ReflectionUtilsTest {
log.info("doTest: {}", param); log.info("doTest: {}", param);
} }
// method with IConnection as first parameter isn't found public void doTestWithConn(IConnection conn, String param0, Integer param1) {
public void doTest(IConnection conn, String param) { log.info("doTestWithConn: {} {} {}", conn, param0, param1);
log.info("doTest: {} {}", conn, param);
} }
public void doTest(String param0, Integer param1) { public void doTest(String param0, Integer param1) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment