Change Router to do reflective setAccessible() calls itself

This change moves the reflective setAccessible() calls on the request component
methods (needed so that they can be invoked reflectively from RequestHandler)
to within Router itself, eliminating the need to manually call this from each
Servlet class and then pass in the resulting Method objects.  Instead, we just
pass in the request component class and let Router do the rest.

Old comments say that cross-package reflection is not allowed on AppEngine, but
while it's quite possible this was once the case, I can't reproduce that
limitation, and the documentation seems to contradict any such restriction:

"""
An application is allowed full, unrestricted, reflective access to its own
classes.  It can query any private members, call the method
java.lang.reflect.AccessibleObject.setAccessible(), and read/set private
members.
"""
https://cloud.google.com/appengine/docs/java/runtime#reflection

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=138693006
This commit is contained in:
nickfelt 2016-11-09 15:23:47 -08:00 committed by Ben McIlwain
parent 59c213c66f
commit 9122372e38
7 changed files with 34 additions and 87 deletions

View file

@ -31,7 +31,6 @@ import com.google.common.base.Optional;
import google.registry.util.FormattingLogger;
import google.registry.util.NonFinalForTesting;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.joda.time.Duration;
@ -76,20 +75,9 @@ public final class RequestHandler<C> {
@NonFinalForTesting
private static UserService userService = UserServiceFactory.getUserService();
/**
* Creates a new request processor based off your component methods.
*
* <p><b>Warning:</b> When using the App Engine platform, you must call
* {@link Method#setAccessible(boolean) setAccessible(true)} on all your component {@link Method}
* instances, from within the same package as the component. This is due to cross-package
* reflection restrictions.
*
* @param methods is the result of calling {@link Class#getMethods()} on {@code component}, which
* are filtered to only include those with no arguments returning a {@link Runnable} with an
* {@link Action} annotation
*/
public static <C> RequestHandler<C> create(Class<C> component, Iterable<Method> methods) {
return new RequestHandler<>(component, Router.create(methods));
/** Creates a new request processor based off your component methods. */
public static <C> RequestHandler<C> create(Class<C> component) {
return new RequestHandler<>(component, Router.create(component));
}
private final Router router;
@ -102,8 +90,7 @@ public final class RequestHandler<C> {
/**
* Runs the appropriate action for a servlet request.
*
* @param component is an instance of the component type whose methods were passed to
* {@link #create(Class, Iterable)}
* @param component is an instance of the component type passed to {@link #create(Class)}
*/
public void handleRequest(HttpServletRequest req, HttpServletResponse rsp, C component)
throws IOException {