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

@ -18,7 +18,6 @@ import static com.google.common.truth.Truth.assertThat;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import google.registry.testing.ExceptionRule;
import java.util.concurrent.Callable;
import org.junit.Rule;
@ -33,18 +32,14 @@ public final class RouterTest {
@Rule
public final ExceptionRule thrown = new ExceptionRule();
private Router create(Class<?> clazz) {
return Router.create(ImmutableList.copyOf(clazz.getMethods()));
}
////////////////////////////////////////////////////////////////////////////////////////////////
public interface Empty {}
@Test
public void testRoute_noRoutes_returnsAbsent() throws Exception {
assertThat(create(Empty.class).route("")).isAbsent();
assertThat(create(Empty.class).route("/")).isAbsent();
assertThat(Router.create(Empty.class).route("")).isAbsent();
assertThat(Router.create(Empty.class).route("/")).isAbsent();
}
////////////////////////////////////////////////////////////////////////////////////////////////
@ -61,7 +56,7 @@ public final class RouterTest {
@Test
public void testRoute_pathMatch_returnsRoute() throws Exception {
Optional<Route> route = create(SlothComponent.class).route("/sloth");
Optional<Route> route = Router.create(SlothComponent.class).route("/sloth");
assertThat(route).isPresent();
assertThat(route.get().action().path()).isEqualTo("/sloth");
assertThat(route.get().instantiator()).isInstanceOf(Function.class);
@ -69,12 +64,12 @@ public final class RouterTest {
@Test
public void testRoute_pathMismatch_returnsAbsent() throws Exception {
assertThat(create(SlothComponent.class).route("/doge")).isAbsent();
assertThat(Router.create(SlothComponent.class).route("/doge")).isAbsent();
}
@Test
public void testRoute_pathIsAPrefix_notAllowedByDefault() throws Exception {
assertThat(create(SlothComponent.class).route("/sloth/extra")).isAbsent();
assertThat(Router.create(SlothComponent.class).route("/sloth/extra")).isAbsent();
}
////////////////////////////////////////////////////////////////////////////////////////////////
@ -91,16 +86,16 @@ public final class RouterTest {
@Test
public void testRoute_prefixMatches_returnsRoute() throws Exception {
assertThat(create(PrefixComponent.class).route("/prefix")).isPresent();
assertThat(create(PrefixComponent.class).route("/prefix/extra")).isPresent();
assertThat(Router.create(PrefixComponent.class).route("/prefix")).isPresent();
assertThat(Router.create(PrefixComponent.class).route("/prefix/extra")).isPresent();
}
@Test
public void testRoute_prefixDoesNotMatch_returnsAbsent() throws Exception {
assertThat(create(PrefixComponent.class).route("")).isAbsent();
assertThat(create(PrefixComponent.class).route("/")).isAbsent();
assertThat(create(PrefixComponent.class).route("/ulysses")).isAbsent();
assertThat(create(PrefixComponent.class).route("/man/of/sadness")).isAbsent();
assertThat(Router.create(PrefixComponent.class).route("")).isAbsent();
assertThat(Router.create(PrefixComponent.class).route("/")).isAbsent();
assertThat(Router.create(PrefixComponent.class).route("/ulysses")).isAbsent();
assertThat(Router.create(PrefixComponent.class).route("/man/of/sadness")).isAbsent();
}
////////////////////////////////////////////////////////////////////////////////////////////////
@ -118,21 +113,21 @@ public final class RouterTest {
@Test
public void testRoute_prefixAndLongPathMatch_returnsLongerPath() throws Exception {
Optional<Route> route = create(LongPathComponent.class).route("/prefix/long");
Optional<Route> route = Router.create(LongPathComponent.class).route("/prefix/long");
assertThat(route).isPresent();
assertThat(route.get().action().path()).isEqualTo("/prefix/long");
}
@Test
public void testRoute_prefixAndLongerPrefixMatch_returnsLongerPrefix() throws Exception {
Optional<Route> route = create(LongPathComponent.class).route("/prefix/longer");
Optional<Route> route = Router.create(LongPathComponent.class).route("/prefix/longer");
assertThat(route).isPresent();
assertThat(route.get().action().path()).isEqualTo("/prefix/long");
}
@Test
public void testRoute_onlyShortPrefixMatches_returnsShortPrefix() throws Exception {
Optional<Route> route = create(LongPathComponent.class).route("/prefix/cat");
Optional<Route> route = Router.create(LongPathComponent.class).route("/prefix/cat");
assertThat(route).isPresent();
assertThat(route.get().action().path()).isEqualTo("/prefix");
}
@ -146,7 +141,7 @@ public final class RouterTest {
@Test
public void testRoute_methodsInComponentAreIgnored_noRoutes() throws Exception {
assertThat(create(WeirdMethodsComponent.class).route("/sloth")).isAbsent();
assertThat(Router.create(WeirdMethodsComponent.class).route("/sloth")).isAbsent();
}
////////////////////////////////////////////////////////////////////////////////////////////////
@ -171,6 +166,6 @@ public final class RouterTest {
@Test
public void testCreate_twoTasksWithSameMethodAndPath_resultsInError() throws Exception {
thrown.expect(IllegalArgumentException.class, "Multiple entries with same key");
create(DuplicateComponent.class);
Router.create(DuplicateComponent.class);
}
}