Skip to content

Commit

Permalink
Merge pull request #1106 from codeborne/support-controller-subclasses
Browse files Browse the repository at this point in the history
Fixes #1105 invoke non-static method in child controller, not in pare…
  • Loading branch information
asolntsev authored Feb 13, 2017
2 parents 2dcb3ae + aa3c307 commit 3f739e8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion framework/src/play/mvc/ActionInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ public static Object invokeControllerMethod(Method method, Object[] forceArgs) t
}

Object methodClassInstance = isStatic ? null :
(method.getDeclaringClass().equals(request.controllerClass)) ? request.controllerInstance :
(method.getDeclaringClass().isAssignableFrom(request.controllerClass)) ? request.controllerInstance :
method.getDeclaringClass().newInstance();

return invoke(method, methodClassInstance, args);
Expand Down
22 changes: 20 additions & 2 deletions framework/test-src/play/mvc/ActionInvokerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,21 @@ public void invokeStaticJavaMethod() throws Exception {
@Test
public void invokeNonStaticJavaMethod() throws Exception {
Http.Request.current().controllerClass = TestController.class;
assertEquals("non-static", ActionInvoker.invokeControllerMethod(TestController.class.getMethod("nonStaticJavaMethod"), noArgs));
assertEquals("non-static-parent", ActionInvoker.invokeControllerMethod(TestController.class.getMethod("nonStaticJavaMethod"), noArgs));
}

@Test
public void invokeNonStaticJavaMethodInChildController() throws Exception {
Http.Request.current().controllerClass = TestChildController.class;
assertEquals("non-static-child", ActionInvoker.invokeControllerMethod(TestChildController.class.getMethod("nonStaticJavaMethod"), noArgs));
}

@Test
public void invokeNonStaticJavaMethodWithNonStaticWith() throws Exception {
Http.Request request = Http.Request.current();
request.controllerClass = TestControllerWithWith.class;
executeMethod("handleBefores", Http.Request.class, request);
assertEquals("non-static", ActionInvoker.invokeControllerMethod(TestControllerWithWith.class.getMethod("nonStaticJavaMethod")));
executeMethod("handleAfters", Http.Request.class, request);
assertEquals(1, beforesCounter);
assertEquals(1, aftersCounter);
Expand Down Expand Up @@ -180,7 +187,18 @@ public static String staticJavaMethod() {
}

public String nonStaticJavaMethod() {
return "non-static";
return "non-static-" + getPrefix();
}

protected String getPrefix() {
return "parent";
}
}

public static class TestChildController extends TestController {
@Override
protected String getPrefix() {
return "child";
}
}

Expand Down

0 comments on commit 3f739e8

Please sign in to comment.