From 94edef15b00853d76f43ae9a404b774dbc35e309 Mon Sep 17 00:00:00 2001 From: AUTplayed Date: Sat, 11 May 2019 22:54:34 +0200 Subject: [PATCH] removed static requirement --- README.md | 8 ++++---- build.gradle | 4 ++-- .../codes/fepi/ldfspark/AnnotationDiscover.java | 15 ++++++++------- src/main/java/codes/fepi/ldfspark/LdfSpark.java | 4 ++-- .../java/codes/fepi/ldfspark/PageHandler.java | 2 +- src/main/java/codes/fepi/ldfspark/PageLogic.java | 11 ++++++----- src/test/java/codes/fepi/libuser/TestHandler.java | 4 ++-- 7 files changed, 25 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index cc078a0..b55e9d1 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ A java library for bootstrapping a web application with java backend, and ldf+mu codes.fepi ldfSpark - 1.0 + 1.1 ``` **gradle** @@ -23,15 +23,15 @@ The user only has to implement a Handler Class for handling the page content (an @PageHandler public class Handler { - public static Object index() { + public Object index() { return "page content"; } - public static Object otherPage(Request req) { + public Object otherPage(Request req) { return "other page content"; } - public static Object login(Request req, Response res) { + public Object login(Request req, Response res) { return "login page content"; } } diff --git a/build.gradle b/build.gradle index 5998c5f..6d3385a 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { } group 'codes.fepi' -version '1.0' +version '1.1' sourceCompatibility = 1.8 @@ -18,7 +18,7 @@ dependencies { testImplementation group: 'junit', name: 'junit', version: '4.12' api group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25' implementation group: 'eu.infomas', name: 'annotation-detector', version: '3.0.5' - api 'com.sparkjava:spark-core:2.7.2' + api 'com.sparkjava:spark-core:2.9.0' api "com.sparkjava:spark-template-mustache:2.7.1" } diff --git a/src/main/java/codes/fepi/ldfspark/AnnotationDiscover.java b/src/main/java/codes/fepi/ldfspark/AnnotationDiscover.java index 43852cc..4c8f265 100644 --- a/src/main/java/codes/fepi/ldfspark/AnnotationDiscover.java +++ b/src/main/java/codes/fepi/ldfspark/AnnotationDiscover.java @@ -1,17 +1,16 @@ package codes.fepi.ldfspark; -import codes.fepi.ldfspark.PageHandler; import eu.infomas.annotation.AnnotationDetector; import java.lang.annotation.Annotation; import java.lang.reflect.Method; -import java.util.function.Consumer; +import java.util.function.BiConsumer; public class AnnotationDiscover implements AnnotationDetector.TypeReporter { - private Consumer consumer; + private BiConsumer consumer; - public AnnotationDiscover(Consumer consumer) { + public AnnotationDiscover(BiConsumer consumer) { super(); this.consumer = consumer; } @@ -20,12 +19,14 @@ public AnnotationDiscover(Consumer consumer) { public void reportTypeAnnotation(Class annotation, String className) { try { Class clazz = Class.forName(className); + Object pageHandler = clazz.newInstance(); for (Method method : clazz.getMethods()) { if (method.getDeclaringClass() == Object.class && !method.isAccessible()) continue; - consumer.accept(method); + consumer.accept(method, pageHandler); } - } catch (ClassNotFoundException e) { - System.out.println("something went horribly wrong with the library: " + e.getMessage()); + } catch (Exception e) { + System.out.println("something went wrong with the reflection: "); + e.printStackTrace(); } } diff --git a/src/main/java/codes/fepi/ldfspark/LdfSpark.java b/src/main/java/codes/fepi/ldfspark/LdfSpark.java index 963a408..0b9c7bd 100644 --- a/src/main/java/codes/fepi/ldfspark/LdfSpark.java +++ b/src/main/java/codes/fepi/ldfspark/LdfSpark.java @@ -56,8 +56,8 @@ public static void stop() { spark.Spark.stop(); } - private static void handleHandlerMethod(Method method) { + private static void handleHandlerMethod(Method method, Object pageHandler) { pages.add(method.getName()); - pageLogic.page(method.getName(), method); + pageLogic.page(method, pageHandler); } } diff --git a/src/main/java/codes/fepi/ldfspark/PageHandler.java b/src/main/java/codes/fepi/ldfspark/PageHandler.java index 7d3dd6a..3e148e6 100644 --- a/src/main/java/codes/fepi/ldfspark/PageHandler.java +++ b/src/main/java/codes/fepi/ldfspark/PageHandler.java @@ -7,7 +7,7 @@ /** * Annotate a class with this annotation.
- * All methods of the annotated class have to be public static to be detected, and have to return something.
+ * All methods of the annotated class have to be public to be detected, and have to return something.
* Possible Parameters are: *
    *
  • nothing
  • diff --git a/src/main/java/codes/fepi/ldfspark/PageLogic.java b/src/main/java/codes/fepi/ldfspark/PageLogic.java index 1cc53cd..bd5a02c 100644 --- a/src/main/java/codes/fepi/ldfspark/PageLogic.java +++ b/src/main/java/codes/fepi/ldfspark/PageLogic.java @@ -19,18 +19,19 @@ class PageLogic { LOGGER = LoggerFactory.getLogger("LdfPageLogic"); } - void page(String page, Method handler) { + void page(Method method, Object handler) { + String page = method.getName(); String route = String.format("/pages/%s/%s.html", page, page); LOGGER.info("registered page: {}", route); get(route, (req, res) -> { Object entity = null; - switch (handler.getParameterCount()) { + switch (method.getParameterCount()) { case 2: - entity = handler.invoke(null, req, res); break; + entity = method.invoke(handler, req, res); break; case 1: - entity = handler.invoke(null, req); break; + entity = method.invoke(handler, req); break; case 0: - entity = handler.invoke(null); break; + entity = method.invoke(handler); break; } return mte.render(new ModelAndView(entity, page + ".html")); }); diff --git a/src/test/java/codes/fepi/libuser/TestHandler.java b/src/test/java/codes/fepi/libuser/TestHandler.java index 1f7c30c..8ed4604 100644 --- a/src/test/java/codes/fepi/libuser/TestHandler.java +++ b/src/test/java/codes/fepi/libuser/TestHandler.java @@ -8,11 +8,11 @@ @PageHandler public class TestHandler { - public static Object index() { + public Object index() { return new TestEntity("ldfSpark", Arrays.asList("static rendering", "single page application")); } - public static Object other(Request req) { + public Object other(Request req) { return req.userAgent(); } }