Skip to content

Commit

Permalink
完成 After
Browse files Browse the repository at this point in the history
  • Loading branch information
IceCream-QAQ committed Jul 7, 2020
1 parent fdbb2f9 commit 8ef0e16
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<packaging>jar</packaging>

<modelVersion>4.0.0</modelVersion>
<version>0.1.3.0-SNAPSHOTS</version>
<version>0.1.3.0</version>

<parent>
<groupId>com.IceCreamQAQ</groupId>
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/IceCreamQAQ/Yu/annotation/After.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
package com.IceCreamQAQ.Yu.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface After {

int value() default 0;

String[] except() default "";

String[] only() default "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface NewActionContext {
operator fun set(name: String, obj: Any)

fun onError(e: Throwable): Throwable?
fun onSuccess(result: Any): Any?
fun onSuccess(result: Any?): Any?

}

Expand All @@ -29,7 +29,7 @@ class NewActionContextImpl : NewActionContext {

override fun onError(e: Throwable): Throwable = e

override fun onSuccess(result: Any): Any? {
override fun onSuccess(result: Any?): Any? {
this.result = result
return null
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.IceCreamQAQ.Yu.controller

import com.IceCreamQAQ.Yu.annotation.Action
import com.IceCreamQAQ.Yu.annotation.Before
import com.IceCreamQAQ.Yu.annotation.Path
import com.IceCreamQAQ.Yu.annotation.With
import com.IceCreamQAQ.Yu.annotation.*
import com.IceCreamQAQ.Yu.controller.router.*
import com.IceCreamQAQ.Yu.di.YuContext
import com.IceCreamQAQ.Yu.loader.LoadItem
Expand Down Expand Up @@ -69,11 +66,18 @@ abstract class NewControllerLoader : Loader {

val methods = controllerClass.methods
val befores = HashMap<Before, NewMethodInvoker>()
val afters = HashMap<After,NewMethodInvoker>()

for (method in allMethods) {
val before = method.getAnnotation(Before::class.java)
if (before != null) {
val beforeInvoker = createMethodInvoker(instance, method)
befores[before] = (beforeInvoker)
befores[before] = beforeInvoker
}
val after = method.getAnnotation(After::class.java)
if (after != null) {
val afterInvoker = createMethodInvoker(instance, method)
afters[after] = afterInvoker
}
}
// val before = befores.toTypedArray()
Expand Down Expand Up @@ -101,8 +105,19 @@ abstract class NewControllerLoader : Loader {
}
abs.add(invoker)
}
val aas = ArrayList<NewMethodInvoker>()
w@ for ((after, invoker) in afters) {
if (after.except.size != 1 || after.except[0] != "") for (s in after.except) {
if (s == actionMethodName) continue@w
}
if (after.only.size != 1 || after.only[0] != "") for (s in after.only) {
if (s != actionMethodName) continue@w
}
aas.add(invoker)
}

actionInvoker.befores = abs.toTypedArray()
actionInvoker.afters = aas.toTypedArray()

val mi = getMatchItem(actionPath, actionInvoker)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,12 @@ open class NewActionInvoker(level: Int, method: Method, instance: Any) : NewRout
val o = before.invoke(context)
if (o != null) context[o::class.java.simpleName.toLowerCaseFirstOne()] = o
}
val result = invoker.invoke(context) ?: return true
val result = invoker.invoke(context)
context.onSuccess(result)
for (after in afters) {
val o = after.invoke(context)
if (o != null) context[o::class.java.simpleName.toLowerCaseFirstOne()] = o
}
} catch (e: Exception) {
throw context.onError(e) ?: return true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.icecreamqaq.test.yu.controller

import com.IceCreamQAQ.Yu.annotation.Action
import com.IceCreamQAQ.Yu.annotation.After
import com.IceCreamQAQ.Yu.annotation.NewDefaultController
import com.IceCreamQAQ.Yu.annotation.Path
import com.icecreamqaq.test.yu.annotation.TestHook
Expand All @@ -14,6 +15,11 @@ class TestNewController {
@Action("m{abc}b{bbc}")
fun mb(abc: String, bbc: String) = "mb: abc = $abc, bbc = $bbc."

@After
fun after(){
println("TestNewControllerAfter")
}

}

@Path("11")
Expand Down

0 comments on commit 8ef0e16

Please sign in to comment.