use of ninja.utils.MethodReference in project ninja by ninjaframework.
the class RouterImpl method getRouteForControllerClassAndMethod.
@Override
public Optional<Route> getRouteForControllerClassAndMethod(Class<?> controllerClass, String controllerMethodName) {
verifyRoutesCompiled();
MethodReference reverseRouteKey = new MethodReference(controllerClass, controllerMethodName);
Route route = this.reverseRoutes.get(reverseRouteKey);
return Optional.ofNullable(route);
}
use of ninja.utils.MethodReference in project ninja by ninjaframework.
the class RouterImpl method compileRoutes.
@Override
public void compileRoutes() {
if (routes != null) {
throw new IllegalStateException("Routes already compiled");
}
List<Route> routesLocal = new ArrayList<>();
for (RouteBuilderImpl routeBuilder : allRouteBuilders) {
routesLocal.add(routeBuilder.buildRoute(injector));
}
this.routes = ImmutableList.copyOf(routesLocal);
// compile reverse routes for O(1) lookups
this.reverseRoutes = new HashMap<>(this.routes.size());
for (Route route : this.routes) {
// its possible this route is a Result instead of a controller method
if (route.getControllerClass() != null) {
MethodReference controllerMethodRef = new MethodReference(route.getControllerClass(), route.getControllerMethod().getName());
if (this.reverseRoutes.containsKey(controllerMethodRef)) {
// the first one wins with reverse routing so we ignore this route
} else {
this.reverseRoutes.put(controllerMethodRef, route);
}
}
}
logRoutes();
}
use of ninja.utils.MethodReference in project ninja by ninjaframework.
the class RouteBuilderImplTest method routeWithMethodReference.
@Test
public void routeWithMethodReference() throws Exception {
routeBuilder.GET().route("/method_reference").with(new MethodReference(MockController.class, "execute"));
Route route = routeBuilder.buildRoute(injector);
assertTrue(route.matches("GET", "/method_reference"));
assertThat(route.getControllerClass(), is(MockController.class));
}
use of ninja.utils.MethodReference in project ninja by ninjaframework.
the class RouterImplTest method before.
@Before
@SuppressWarnings("Convert2Lambda")
public void before() {
this.ninjaBaseDirectoryResolver = new NinjaBaseDirectoryResolver(ninjaProperties);
when(testControllerProvider.get()).thenReturn(new TestController());
when(injector.getProvider(TestController.class)).thenReturn(testControllerProvider);
when(injector.getInstance(ParamParsers.class)).thenReturn(new ParamParsers(Collections.emptySet()));
Provider<RouteBuilderImpl> routeBuilderImplProvider = mock(Provider.class);
when(routeBuilderImplProvider.get()).thenAnswer((invocation) -> new RouteBuilderImpl(ninjaProperties, ninjaBaseDirectoryResolver));
router = new RouterImpl(injector, ninjaProperties, routeBuilderImplProvider);
// add route:
router.GET().route("/testroute").with(TestController.class, "index");
router.GET().route("/user/{email}/{id: .*}").with(TestController.class, "user");
router.GET().route("/u{userId: .*}/entries/{entryId: .*}").with(TestController.class, "entry");
// second route to index should not break reverse routing matching the first
router.GET().route("/testroute/another_url_by_index").with(TestController.class, "index");
router.GET().route("/ref").with(new MethodReference(TestController.class, "ref"));
// functional interface / lambda routing
TestController testController1 = new TestController("Hi!");
router.GET().route("/any_instance_method_ref").with(TestController::home);
router.GET().route("/any_instance_method_ref_exception").with(TestController::exception);
router.GET().route("/any_instance_method_ref2").with(ControllerMethods.of(TestController::home));
router.GET().route("/specific_instance_method_ref").with(testController1::message);
router.GET().route("/specific_instance_method_ref_annotations").with(testController1::status);
router.GET().route("/anonymous_method_ref").with(() -> Results.status(202));
Result staticResult = Results.status(208);
router.GET().route("/anonymous_method_ref_captured").with(() -> staticResult);
router.GET().route("/anonymous_method_ref_context").with((Context context) -> Results.status(context.getParameterAsInteger("status")));
router.GET().route("/anonymous_class").with(new ControllerMethod0() {
@Override
public Result apply() {
return Results.status(203);
}
});
router.GET().route("/anonymous_class_annotations").with(new ControllerMethod1<Integer>() {
@Override
public Result apply(@Param("status") Integer status) {
return Results.status(status);
}
});
router.compileRoutes();
}
use of ninja.utils.MethodReference in project ninja by ninjaframework.
the class ReverseRouterTest method simpleWithMethodReference.
@Test
public void simpleWithMethodReference() {
MethodReference methodRef = new MethodReference(TestController.class, "home");
String route = reverseRouter.with(methodRef).build();
assertThat(route, is("/home"));
}
Aggregations