-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/kotlin/top/ntutn/starseasdk/proxy/AliasProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package top.ntutn.starseasdk.proxy | ||
|
||
import com.google.auto.service.AutoService | ||
import javax.annotation.processing.AbstractProcessor | ||
import javax.annotation.processing.Processor | ||
import javax.annotation.processing.RoundEnvironment | ||
import javax.annotation.processing.SupportedAnnotationTypes | ||
import javax.lang.model.element.TypeElement | ||
import javax.tools.FileObject | ||
import javax.tools.StandardLocation | ||
|
||
@AutoService(Processor::class) | ||
@SupportedAnnotationTypes("top.ntutn.starseasdk.proxy.NewestApi") | ||
class AliasProcessor : AbstractProcessor() { | ||
|
||
override fun process(set: MutableSet<out TypeElement>?, roundEnvironment: RoundEnvironment?): Boolean { | ||
if (set.isNullOrEmpty() || roundEnvironment == null) { | ||
return false | ||
} | ||
val outputPackageName = processingEnv.options["aliasprocessor.package"]!! | ||
val suffix = processingEnv.options["aliasprocessor.suffix"]?:"" | ||
var outputContent = """ | ||
package $outputPackageName | ||
/** | ||
* generated by AliasProcessor | ||
* 这个类是对tg机器人框架starsea提供的最新版本API的alias | ||
* 用于固定包名,避免升级版本号后有一大片包名要改 | ||
* have a nice day. | ||
*/ | ||
""".trimIndent() | ||
roundEnvironment.getElementsAnnotatedWith(NewestApi::class.java).forEach { | ||
it as TypeElement | ||
outputContent += "\ntypealias ${it.simpleName}${suffix} = ${it.qualifiedName}\n" | ||
} | ||
val filerSourceFile: FileObject = processingEnv.filer.createResource( | ||
StandardLocation.SOURCE_OUTPUT, | ||
outputPackageName, "CompatAlias.kt" | ||
) | ||
with(filerSourceFile.openOutputStream()) { | ||
write(outputContent.toByteArray()) | ||
flush() | ||
close() | ||
} | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package top.ntutn.starseasdk.proxy | ||
|
||
/** | ||
* 标记当前类是最新API版本的类,用于自动生成typealias | ||
*/ | ||
@Target(AnnotationTarget.CLASS) | ||
@Retention(AnnotationRetention.SOURCE) | ||
annotation class NewestApi |
3 changes: 3 additions & 0 deletions
3
src/main/kotlin/top/ntutn/starseasdk/v2/BotContentProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
package top.ntutn.starseasdk.v2 | ||
|
||
import top.ntutn.starseasdk.proxy.NewestApi | ||
import java.util.ServiceLoader | ||
|
||
/** | ||
* 用于访问机器人的全局上下文。 | ||
*/ | ||
@NewestApi | ||
object BotContext: IBotContext by ServiceLoader.load(IBotContext::class.java).first() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters