Skip to content

Commit

Permalink
removed static requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
AUTplayed committed May 11, 2019
1 parent 7fb2834 commit 94edef1
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 23 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ A java library for bootstrapping a web application with java backend, and ldf+mu
<dependency>
<groupId>codes.fepi</groupId>
<artifactId>ldfSpark</artifactId>
<version>1.0</version>
<version>1.1</version>
</dependency>
```
**gradle**
Expand All @@ -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";
}
}
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group 'codes.fepi'
version '1.0'
version '1.1'

sourceCompatibility = 1.8

Expand All @@ -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"
}

Expand Down
15 changes: 8 additions & 7 deletions src/main/java/codes/fepi/ldfspark/AnnotationDiscover.java
Original file line number Diff line number Diff line change
@@ -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<Method> consumer;
private BiConsumer<Method, Object> consumer;

public AnnotationDiscover(Consumer<Method> consumer) {
public AnnotationDiscover(BiConsumer<Method, Object> consumer) {
super();
this.consumer = consumer;
}
Expand All @@ -20,12 +19,14 @@ public AnnotationDiscover(Consumer<Method> consumer) {
public void reportTypeAnnotation(Class<? extends Annotation> 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();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/codes/fepi/ldfspark/LdfSpark.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/main/java/codes/fepi/ldfspark/PageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

/**
* Annotate a class with this annotation. <br/>
* All methods of the annotated class have to be public static to be detected, and have to return something. <br/>
* All methods of the annotated class have to be public to be detected, and have to return something. <br/>
* Possible Parameters are:
* <ul>
* <li>nothing</li>
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/codes/fepi/ldfspark/PageLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
});
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/codes/fepi/libuser/TestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit 94edef1

Please sign in to comment.