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

@ -25,7 +25,7 @@ import java.util.Map;
import java.util.TreeMap;
/**
* Path prefix request router for Nomulus.
* Path prefix request router.
*
* <p>See the documentation of {@link RequestHandler} for more information.
*
@ -37,14 +37,15 @@ import java.util.TreeMap;
*/
final class Router {
static Router create(Iterable<Method> componentMethods) {
return new Router(extractRoutesFromComponent(componentMethods));
/** Create a new Router for the given component class. */
static Router create(Class<?> componentClass) {
return new Router(componentClass);
}
private final ImmutableSortedMap<String, Route> routes;
private Router(ImmutableSortedMap<String, Route> routes) {
this.routes = routes;
private Router(Class<?> componentClass) {
this.routes = extractRoutesFromComponent(componentClass);
}
/** Returns the appropriate action route for a request. */
@ -61,10 +62,12 @@ final class Router {
}
private static
ImmutableSortedMap<String, Route> extractRoutesFromComponent(Iterable<Method> methods) {
ImmutableSortedMap<String, Route> extractRoutesFromComponent(Class<?> componentClass) {
ImmutableSortedMap.Builder<String, Route> routes =
new ImmutableSortedMap.Builder<>(Ordering.natural());
for (Method method : methods) {
for (Method method : componentClass.getMethods()) {
// Make App Engine's security manager happy.
method.setAccessible(true);
if (!isDaggerInstantiatorOfType(Runnable.class, method)) {
continue;
}