Skip to content

Commit

Permalink
#51: 监听参数警告信息以及类型处理优化
Browse files Browse the repository at this point in the history
#52: 兼容kotlin的可空类型
#53: 兼容kotlin扩展函数写法

Closes #51
Closes #52
Closes #53
  • Loading branch information
ForteScarlet committed Jan 17, 2021
1 parent a28e470 commit 0ad73cc
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 175 deletions.
39 changes: 39 additions & 0 deletions UPDATE.MD
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,42 @@ simbot.core.sender.default.type=ERROR
- [44](https://github.com/ForteScarlet/simpler-robot/issues/44) 可爱猫json消息解析异常
- [45](https://github.com/ForteScarlet/simpler-robot/issues/45) 可爱猫 springboot starter无法触发监听



## RC.3

### 核心
- [#51](https://github.com/ForteScarlet/simpler-robot/issues/51) 优化监听函数监听参数与监听类型不一致而导致的错误,并增加解析期警告异常。
```java
// 监听函数解析期间会输出警告日志,因为你监听了 GroupMsg, 而参数中的 PrivateMsg 却没有被监听,它在触发监听时会抛出异常。
@OnGroup
public void listen(PrivateMsg msg) {
// ...
}
```

```java
// 监听函数解析期间会输出警告日志,因为你监听了 GroupMsg, 而参数中的 PrivateMsg 却没有被监听,它将永远为null。
@OnGroup
public void listen(@Depend(orIgnore = true) PrivateMsg msg) {
// ...
}
```

- [#52](https://github.com/ForteScarlet/simpler-robot/issues/52) 支持`kotlin``nullable`参数,使其等效于 `@Depend(orIgnore = true)`
```kotlin
// myBean 可以为null,因此如果依赖管理找不到此实例则会得到null值,而不是抛出异常。
@OnGroup
fun listen(msg: GroupMsg, myBean: MyBean?) {
// ...
}
```

- [#53](https://github.com/ForteScarlet/simpler-robot/issues/53) 支持`kotlin`的扩展函数写法。(注: 不支持顶级函数。需要有携带 @Beans 的类所包含。)
```kotlin
// In Java like: listen(PrivateMsg msg) { /* ... */ }
@OnPrivate
fun PrivateMsg.listen() {
println(this.text)
}
```

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
*
* * Copyright (c) 2021. ForteScarlet All rights reserved.
* * Project simple-robot
* * File MiraiAvatar.kt
* *
* * You can contact the author through the following channels:
* * github https://github.com/ForteScarlet
* * gitee https://gitee.com/ForteScarlet
* * email ForteScarlet@163.com
* * QQ 1149159218
*
*/

package love.forte.test.listener

import love.forte.common.ioc.annotation.Beans
import love.forte.simbot.annotation.OnPrivate
import love.forte.simbot.api.message.events.PrivateMsg

/**
* @author ForteScarlet
*/
@Beans
class TestListener {

@OnPrivate
fun PrivateMsg.listen() {
println("listen: ${this.text}")
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public interface ListenerFunction {

/**
* 当前监听函数的载体。例如一个 Class。
* 一般如果是个method,那么此即为[Class]。
* 例如如果是个method,那么此即为 method所在的 [Class]。
*/
val type: Type

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,6 @@ public class CoreMethodPostListenerRegistrar : PostListenerRegistrar {
logger.info("If you want to view the details, please enable log debug.")
}

// val scanner: Scanner<String, Class<*>> = HutoolClassesScanner()
//
// val scanPackages = packageScanEnvironment.scanPackages

// logger.debug("listener scan packages: {}", scanPackages.joinToString(", ", "[", "]"))

// 扫描所有的class, 然后筛选method
// scanPackages.forEach {
// scanner.scan(it)
// }

// 获取所有已经加载的依赖信息并扫描
val allBeans = dependBeanFactory.allBeans

Expand All @@ -84,15 +73,15 @@ public class CoreMethodPostListenerRegistrar : PostListenerRegistrar {
logger.debug("Get type from depend '$beanName' failed.", e)
null
}
}.distinct().flatMap {
}.distinct().flatMap { type ->
// 只获取public方法
it.methods.asSequence().filter { m ->
type.methods.asSequence().filter { m ->
AnnotationUtil.containsAnnotation(m.declaringClass, Listens::class.java) ||
(!AnnotationUtil.containsAnnotation(m, Ignore::class.java) &&
AnnotationUtil.containsAnnotation(m, Listens::class.java))
}.map {
MethodListenerFunction(it, type, dependBeanFactory, filterManager, converterManager, listenerResultFactory)
}
}.map {
MethodListenerFunction(it, dependBeanFactory, filterManager, converterManager, listenerResultFactory)
}.forEach {
registrar.register(it)
logger.debug(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@
package love.forte.simbot.core.listener

import love.forte.simbot.api.message.events.MsgGet
import love.forte.simbot.api.sender.Getter
import love.forte.simbot.api.sender.MsgSender
import love.forte.simbot.api.sender.Sender
import love.forte.simbot.api.sender.Setter
import love.forte.simbot.bot.Bot
import love.forte.simbot.filter.AtDetection
import love.forte.simbot.listener.ListenerContext
import love.forte.simbot.listener.ListenerFunctionInvokeData
import love.forte.simbot.listener.ListenerInterceptorChain

/**
*
* 监听函数触发所携带的参数接口默认数据实现。
* @author ForteScarlet -> https://github.com/ForteScarlet
*/
public data class ListenerFunctionInvokeDataImpl(
Expand All @@ -38,14 +35,14 @@ public data class ListenerFunctionInvokeDataImpl(
override val listenerInterceptorChain: ListenerInterceptorChain
) : ListenerFunctionInvokeData {
override fun get(type: Class<*>): Any? = when {
MsgSender::class.java.isAssignableFrom(type) -> msgSender
Sender::class.java.isAssignableFrom(type) -> msgSender.SENDER
Setter::class.java.isAssignableFrom(type) -> msgSender.SETTER
Getter::class.java.isAssignableFrom(type) -> msgSender.GETTER
Bot::class.java.isAssignableFrom(type) -> bot
AtDetection::class.java.isAssignableFrom(type) -> atDetection
ListenerContext::class.java.isAssignableFrom(type) -> context
MsgGet::class.java.isAssignableFrom(type) -> msgGet
type.isAssignableFrom(msgSender::class.java) -> msgSender
type.isAssignableFrom(msgSender.SENDER::class.java) -> msgSender.SENDER
type.isAssignableFrom(msgSender.SETTER::class.java) -> msgSender.SETTER
type.isAssignableFrom(msgSender.GETTER::class.java) -> msgSender.GETTER
type.isAssignableFrom(bot::class.java) -> bot
type.isAssignableFrom(atDetection::class.java) -> atDetection
type.isAssignableFrom(context::class.java) -> context
type.isAssignableFrom(msgGet::class.java) -> msgGet
else -> null
}
}
Loading

0 comments on commit 0ad73cc

Please sign in to comment.