@@ -3,6 +3,7 @@ package com.github.open_edgn.data.format.utils
33import com.github.open_edgn.data.format.ArgsItem
44import com.github.open_edgn.data.format.Beta
55import com.github.open_edgn.data.format.FormatErrorException
6+ import com.github.open_edgn.data.format.Ignore
67import org.slf4j.LoggerFactory
78import java.lang.RuntimeException
89import 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
0 commit comments