Skip to content
This repository was archived by the owner on Feb 1, 2024. It is now read-only.

Commit a2c7437

Browse files
修复 ArgsReader.kt 的一个严重问题
1 parent a57ca7f commit a2c7437

File tree

4 files changed

+65
-20
lines changed

4 files changed

+65
-20
lines changed

core/src/main/kotlin/com/github/open_edgn/data/format/Annotations.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,28 @@ package com.github.open_edgn.data.format
1111
* @constructor
1212
*/
1313
@Retention(AnnotationRetention.RUNTIME)
14-
@Target(AnnotationTarget.FUNCTION,AnnotationTarget.PROPERTY,AnnotationTarget.FIELD)
14+
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY, AnnotationTarget.FIELD)
1515
annotation class ArgsItem(
16-
val defaultValue:String = "",
16+
val defaultValue: String = "",
1717
vararg val alias: String
1818
)
1919

20+
/**
21+
* 忽略字段,打上此字段后将忽略对此字段的一切事情
22+
*/
23+
@Retention(AnnotationRetention.RUNTIME)
24+
@Target(AnnotationTarget.FUNCTION,
25+
AnnotationTarget.CLASS,
26+
AnnotationTarget.FILE,
27+
AnnotationTarget.PROPERTY,
28+
AnnotationTarget.FIELD)
29+
annotation class Ignore()
30+
2031
/**
2132
* 标记此方法为 BETA 方法,后期可能会更改
2233
*
2334
* 更改会在发行日志下注明
2435
*/
2536
@Retention(AnnotationRetention.SOURCE)
26-
@Target(AnnotationTarget.CLASS,AnnotationTarget.FUNCTION)
37+
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
2738
annotation class Beta()

core/src/main/kotlin/com/github/open_edgn/data/format/io/BaseArgsLoader.kt

Lines changed: 0 additions & 4 deletions
This file was deleted.

core/src/main/kotlin/com/github/open_edgn/data/format/utils/ArgsReader.kt

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.github.open_edgn.data.format.utils
33
import com.github.open_edgn.data.format.ArgsItem
44
import com.github.open_edgn.data.format.Beta
55
import com.github.open_edgn.data.format.FormatErrorException
6+
import com.github.open_edgn.data.format.Ignore
67
import org.slf4j.LoggerFactory
78
import java.lang.RuntimeException
89
import kotlin.reflect.KClass
@@ -19,8 +20,9 @@ import kotlin.reflect.jvm.*
1920
*
2021
*/
2122
@Beta
22-
class ArgsReader (args: Array<String>, vararg argsBeans: KClass<*>) {
23+
class ArgsReader(args: Array<String>, vararg argsBeans: KClass<*>) {
2324
private val map = HashMap<String, Any>()
25+
private val logger = LoggerFactory.getLogger(javaClass)
2426

2527
init {
2628
for (bean in argsBeans) {
@@ -35,16 +37,27 @@ class ArgsReader (args: Array<String>, vararg argsBeans: KClass<*>) {
3537
val properties = bean.declaredMemberProperties.toList()
3638
val result = Array<Any?>(properties.size) { null }
3739
for ((index, property) in properties.withIndex()) {
40+
if (property.findAnnotation<Ignore>() != null) {
41+
logger.debug("忽略字段 {}.", property.name)
42+
continue
43+
}
3844
val argsItem = property.findAnnotation<ArgsItem>()
3945
try {
4046
if (argsItem != null) {
4147
val alias = argsItem.alias.toMutableList()
4248
if (alias.isEmpty()) {
4349
alias.add(property.name)
4450
}
51+
if (argsItem.defaultValue.isNotEmpty()) {
52+
53+
logger.debug("字段 {} 指定默认值为 {},在不存在可注入的数据时将使用此默认值.", property.name, argsItem.defaultValue)
54+
} else {
55+
logger.debug("字段 {} 不存在默认值.", property.name)
56+
}
4557
injection(result, index, property.returnType.jvmErasure, args, alias, argsItem.defaultValue)
4658
} else {
47-
injection(result, index, property.returnType.jvmErasure, args, listOf(property.name), "")
59+
logger.debug("字段 {} 不存在默认值.", property.name)
60+
injection(result, index, property.returnType.jvmErasure, args, listOf(property.name), null)
4861
}
4962
} catch (e: Exception) {
5063
throw FormatErrorException("解析 Arg 时出现错误![${e.message}]", e)
@@ -55,7 +68,7 @@ class ArgsReader (args: Array<String>, vararg argsBeans: KClass<*>) {
5568
}
5669
if (bean.isData) {
5770
map[bean.jvmName] =
58-
bean.javaObjectType.declaredConstructors[0].newInstance(*result)
71+
bean.javaObjectType.declaredConstructors[0].newInstance(*result)
5972
} else {
6073
val any = bean.createInstance()
6174
for ((index, property) in properties.withIndex()) {
@@ -87,23 +100,33 @@ class ArgsReader (args: Array<String>, vararg argsBeans: KClass<*>) {
87100
type: KClass<*>,
88101
args: Array<String>,
89102
alias: List<String>,
90-
defaultValue: String) {
103+
defaultValue: String?) {
91104
for (item in alias) {
92-
result[index] = when {
105+
val data = when {
93106
item.length == 1 -> {
94-
loadItem(args, "-$item", type, defaultValue)
107+
loadItem(args, "-$item", type)
95108
}
96109
item.length > 1 -> {
97-
loadItem(args, "--$item", type, defaultValue)
110+
loadItem(args, "--$item", type)
98111
}
99112
else -> {
100113
throw IndexOutOfBoundsException("alias 长度为 0 .")
101114
}
102115
}
116+
if (data != null) {
117+
result[index] = data
118+
return
119+
}
120+
}
121+
122+
if (defaultValue == null || defaultValue.isEmpty()) {
123+
throw FormatErrorException("未发现 Args 下存在可注入的数据且默认值.")
124+
} else {
125+
result[index] = StringFormatUtils.parse(defaultValue, type, true)
103126
}
104127
}
105128

106-
private fun loadItem(args: Array<String>, alias: String, type: KClass<*>, defaultValue: String): Any? {
129+
private fun loadItem(args: Array<String>, alias: String, type: KClass<*>): Any? {
107130
val argsLength = args.size
108131
for ((index, value) in args.withIndex()) {
109132
if (value == alias) {
@@ -112,18 +135,19 @@ class ArgsReader (args: Array<String>, vararg argsBeans: KClass<*>) {
112135
true
113136
} else {
114137
break
138+
// 返回指定字段的默认数值
115139
}
116140
} else {
117141
val nextItem = args[index + 1]
118142
if (type.javaObjectType == Boolean::class.javaObjectType) {
119143
nextItem.toUpperCase().trim() != "FALSE"
120144
} else {
121-
StringFormatUtils.parse(nextItem, type,true)
145+
StringFormatUtils.parse(nextItem, type, true)
122146
}
123147
}
124148
}
125149
}
126-
return StringFormatUtils.parse(defaultValue, type,true)
150+
return null
127151
}
128152
}
129153

core/src/test/kotlin/com/github/open_edgn/data/format/utils/ArgsReaderTest.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,24 @@ internal class ArgsReaderTest {
3030
class Data {
3131
@ArgsItem("%{user.dir}")
3232
lateinit var logger: File
33-
override fun toString(): String {
34-
return "Data(logger='$logger')"
35-
}
33+
}
34+
35+
@Test
36+
fun testSecond() {
37+
val test2 = ArgsReader(
38+
arrayOf("--work-dir","data/app", "-d", "--skip-args"),
39+
Test2::class
3640

41+
).getArgsBean(Test2::class)
42+
assertEquals(File("data/app"),test2.cfgPath)
43+
assertEquals(true,test2.debug)
44+
45+
}
46+
class Test2{
47+
@ArgsItem(alias = ["work-dir","w"],defaultValue = "/bat/dir")
48+
lateinit var cfgPath:File
49+
@ArgsItem(alias = ["d"])
50+
var debug:Boolean = false
3751

3852
}
3953
}

0 commit comments

Comments
 (0)