feat(utils): 添加数组工具类和国际化支持
- 添加ByteArray.toHexString和String.toHexByteArray扩展函数 - 实现多种数组类型的copyTo和copyFrom扩展方法 - 在AutoConfiguration中集成I18N国际化功能 - 添加AutoService注解及处理器用于自动服务注册 - 新增BadGatewayException HTTP状态异常类 - 更新启动横幅格式并添加国际化文本支持
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2026 mingliqiye
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile AutoService.kt
|
||||
* LastUpdate 2026-02-07 17:00:39
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.annotation
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
/**
|
||||
* 标记一个类为其提供自动服务注册功能的注解。
|
||||
*
|
||||
* 此注解用于标识那些需要被自动注册为服务的类。通过指定[value]参数,
|
||||
* 可以声明该类实现的服务接口类型。注解的作用范围限定为类(CLASS),
|
||||
* 并且仅在源码级别保留(SOURCE),不会编译到字节码中。
|
||||
* @since 4.6.3
|
||||
*
|
||||
* @param value 需要注册的服务接口类型数组,默认为空数组。
|
||||
*/
|
||||
@Target(AnnotationTarget.CLASS)
|
||||
@Retention(AnnotationRetention.SOURCE)
|
||||
annotation class AutoService(
|
||||
val value: Array<KClass<*>> = []
|
||||
)
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2026 mingliqiye
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile DateTimeJsonFormat.kt
|
||||
* LastUpdate 2026-02-07 08:05:39
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.annotation
|
||||
|
||||
import com.mingliqiye.utils.time.Formatter
|
||||
|
||||
/**
|
||||
* 日期时间JSON格式化注解,用于标注字段的日期时间格式化方式
|
||||
*
|
||||
* @property value 格式化器类型,默认为NONE
|
||||
* @property formatter 自定义格式化字符串,默认为空字符串
|
||||
* @property repcZero 是否替换零值,默认为true
|
||||
*/
|
||||
@Target(AnnotationTarget.FIELD)
|
||||
annotation class DateTimeJsonFormat(
|
||||
val value: Formatter = Formatter.NONE,
|
||||
val formatter: String = "",
|
||||
val repcZero: Boolean = true,
|
||||
)
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2026 mingliqiye
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile TestAnnotation.kt
|
||||
* LastUpdate 2026-02-07 08:44:25
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.annotation
|
||||
|
||||
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY)
|
||||
@Retention(AnnotationRetention.SOURCE)
|
||||
annotation class TestAnnotation(val value: String = "test")
|
||||
|
||||
@Target(AnnotationTarget.FIELD)
|
||||
@Retention(AnnotationRetention.SOURCE)
|
||||
annotation class TestFieldAnnotation(val required: Boolean = true)
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2026 mingliqiye
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile UUIDJsonFormat.kt
|
||||
* LastUpdate 2026-02-07 08:05:58
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.annotation
|
||||
|
||||
import com.mingliqiye.utils.base.BaseType
|
||||
import com.mingliqiye.utils.uuid.UUIDFormatType
|
||||
|
||||
/**
|
||||
* UUID JSON格式化注解,用于指定UUID字段在JSON序列化/反序列化时的格式
|
||||
*
|
||||
* @property value UUID格式类型,默认为 NO_UPPER_SPACE
|
||||
* @property base 基数类型,默认为 BASE16
|
||||
*/
|
||||
@Target(AnnotationTarget.FIELD)
|
||||
annotation class UUIDJsonFormat(
|
||||
val value: UUIDFormatType = UUIDFormatType.NO_UPPER_SPACE,
|
||||
val base: BaseType = BaseType.BASE16
|
||||
)
|
||||
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Copyright 2026 mingliqiye
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile AutoServiceProcessor.kt
|
||||
* LastUpdate 2026-02-08 01:24:16
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.annotation.processor
|
||||
|
||||
import com.mingliqiye.utils.annotation.AutoService
|
||||
import com.mingliqiye.utils.io.IO.println
|
||||
import javax.annotation.processing.AbstractProcessor
|
||||
import javax.annotation.processing.ProcessingEnvironment
|
||||
import javax.annotation.processing.RoundEnvironment
|
||||
import javax.annotation.processing.SupportedAnnotationTypes
|
||||
import javax.lang.model.element.TypeElement
|
||||
import javax.lang.model.type.MirroredTypesException
|
||||
import javax.lang.model.type.TypeMirror
|
||||
import javax.tools.StandardLocation
|
||||
|
||||
|
||||
@SupportedAnnotationTypes("com.mingliqiye.utils.annotation.AutoService")
|
||||
class AutoServiceProcessor : AbstractProcessor() {
|
||||
override fun init(processingEnv: ProcessingEnvironment) {
|
||||
super.init(processingEnv)
|
||||
}
|
||||
|
||||
override fun process(
|
||||
annotations: Set<TypeElement>,
|
||||
roundEnv: RoundEnvironment
|
||||
): Boolean {
|
||||
|
||||
val service = mutableMapOf<String, MutableList<String>>()
|
||||
val elements = roundEnv.getElementsAnnotatedWith(AutoService::class.java)
|
||||
if (elements.isEmpty()) return false
|
||||
for (element in elements) {
|
||||
val autoServiceAnnotation = element.getAnnotation(AutoService::class.java)
|
||||
var asd = (try {
|
||||
autoServiceAnnotation!!.value.map { it.java.name }
|
||||
} catch (e: MirroredTypesException) {
|
||||
e.typeMirrors.map(TypeMirror::toString)
|
||||
})
|
||||
|
||||
if (asd.isEmpty()) {
|
||||
if (element is TypeElement) {
|
||||
asd = element.interfaces.map { it.toString() }
|
||||
}
|
||||
}
|
||||
for (data in asd) {
|
||||
var ldata: MutableList<String>? = service[data]
|
||||
if (ldata == null) {
|
||||
ldata = mutableListOf()
|
||||
service[data] = ldata
|
||||
}
|
||||
ldata.add(element.toString())
|
||||
}
|
||||
}
|
||||
processClassAnnotation(service)
|
||||
return true
|
||||
}
|
||||
|
||||
fun processClassAnnotation(map: Map<String, MutableList<String>>) {
|
||||
map.forEach { (interfaceName, implementations) ->
|
||||
if (implementations.isEmpty()) {
|
||||
println("警告: $interfaceName 的实现列表为空!")
|
||||
return@forEach
|
||||
}
|
||||
try {
|
||||
val serviceFile = processingEnv.filer.createResource(
|
||||
StandardLocation.CLASS_OUTPUT,
|
||||
"",
|
||||
"META-INF/services/$interfaceName"
|
||||
)
|
||||
val content = StringBuilder()
|
||||
serviceFile.openWriter().use { writer ->
|
||||
implementations.forEach { impl ->
|
||||
val line = "$impl\n"
|
||||
content.append(line)
|
||||
writer.write(line)
|
||||
}
|
||||
writer.flush()
|
||||
}
|
||||
|
||||
} catch (e: Exception) {
|
||||
println("文件创建失败: ${e.javaClass.name} - ${e.message}")
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user