Skip to content

Commit

Permalink
refactor: replace nullable properties with lateinit for improved init…
Browse files Browse the repository at this point in the history
…ialization in CommentResolver (#1178)
  • Loading branch information
tangcent authored Dec 15, 2024
1 parent a020e49 commit c92a73b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,49 +22,49 @@ import com.itangcent.utils.emptyIf
class CommentResolver {

@Inject
protected val logger: Logger? = null
protected lateinit var logger: Logger

@Inject
protected val ruleComputer: RuleComputer? = null
protected lateinit var ruleComputer: RuleComputer

@Inject
protected val jvmClassHelper: JvmClassHelper? = null
protected lateinit var jvmClassHelper: JvmClassHelper

@Inject
protected val psiClassHelper: PsiClassHelper? = null
protected lateinit var psiClassHelper: PsiClassHelper

@Inject
protected val duckTypeHelper: DuckTypeHelper? = null
protected lateinit var duckTypeHelper: DuckTypeHelper

fun resolveCommentForType(duckType: DuckType, context: PsiElement): String? {

if (!duckType.isSingle()) {
return null
}

if (jvmClassHelper!!.isEnum(duckType)) {
if (jvmClassHelper.isEnum(duckType)) {

if (duckType is SingleUnresolvedDuckType) {
return resolveCommentForType(duckType.psiType(), context)
}

val convertTo = ruleComputer!!.computer(ClassRuleKeys.ENUM_CONVERT, duckType, context)
val convertTo = ruleComputer.computer(ClassRuleKeys.ENUM_CONVERT, duckType, context)

if (convertTo.notNullOrBlank()) {
if (convertTo!!.contains("#")) {
val options = psiClassHelper!!.resolveEnumOrStatic(convertTo, context, "")
val options = psiClassHelper.resolveEnumOrStatic(convertTo, context, "")
if (options.notNullOrEmpty()) {
return KVUtils.getOptionDesc(options!!)
}
} else {
val resolveClass = duckTypeHelper!!.resolveClass(convertTo, context)
val resolveClass = duckTypeHelper.resolveClass(convertTo, context)
if (resolveClass == null) {
logger!!.error("failed to resolve class:$convertTo")
logger.warn("Failed to resolve class: $convertTo")
return null
}
val constants = psiClassHelper!!.parseEnumConstant(resolveClass)
val constants = psiClassHelper.parseEnumConstant(resolveClass)
if (constants.isEmpty()) {
logger!!.error("nothing be found at:$convertTo")
logger.debug("No constants found at: $convertTo")
return null
}

Expand All @@ -75,10 +75,10 @@ class CommentResolver {
if (duckType is SingleDuckType) {
val enumClass = duckType.psiClass()

val constants = psiClassHelper!!.resolveEnumOrStatic(context, enumClass, null, "") {}
val constants = psiClassHelper.resolveEnumOrStatic(context, enumClass, null, "") {}
.emptyIf { psiClassHelper.parseEnumConstant(enumClass) }
if (constants.isEmpty()) {
logger!!.error("nothing be found for:${duckType.canonicalText()}")
logger.debug("No constants found for: ${duckType.canonicalText()}")
return null
}

Expand All @@ -91,25 +91,25 @@ class CommentResolver {

fun resolveCommentForType(psiType: PsiType, context: PsiElement): String? {

if (jvmClassHelper!!.isEnum(psiType)) {
if (jvmClassHelper.isEnum(psiType)) {

val convertTo = ruleComputer!!.computer(ClassRuleKeys.ENUM_CONVERT, psiType, context)
val convertTo = ruleComputer.computer(ClassRuleKeys.ENUM_CONVERT, psiType, context)

if (convertTo.notNullOrBlank()) {
if (convertTo!!.contains("#")) {
val options = psiClassHelper!!.resolveEnumOrStatic(convertTo, context, "")
val options = psiClassHelper.resolveEnumOrStatic(convertTo, context, "")
if (options.notNullOrEmpty()) {
return KVUtils.getOptionDesc(options!!)
}
} else {
val resolveClass = duckTypeHelper!!.resolveClass(convertTo, context)
val resolveClass = duckTypeHelper.resolveClass(convertTo, context)
if (resolveClass == null) {
logger!!.error("failed to resolve class:$convertTo")
logger.warn("Failed to resolve class: $convertTo")
return null
}
val constants = psiClassHelper!!.parseEnumConstant(resolveClass)
val constants = psiClassHelper.parseEnumConstant(resolveClass)
if (constants.isEmpty()) {
logger!!.error("nothing be found at:$convertTo")
logger.debug("No constants found at: $convertTo")
return null
}

Expand All @@ -118,9 +118,9 @@ class CommentResolver {
}

val enumClass = jvmClassHelper.resolveClassInType(psiType)!!
val constants = psiClassHelper!!.resolveEnumOrStatic(context, enumClass, null, "") {}
val constants = psiClassHelper.resolveEnumOrStatic(context, enumClass, null, "") {}
if (constants.isNullOrEmpty()) {
logger!!.error("nothing be found for:${psiType.canonicalText}")
logger.debug("No constants found for: ${psiType.canonicalText}")
return null
}

Expand Down
Empty file.
Binary file added plugin-adapter/build/libs/plugin-adapter.jar
Binary file not shown.
2 changes: 2 additions & 0 deletions plugin-adapter/build/tmp/jar/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Manifest-Version: 1.0

0 comments on commit c92a73b

Please sign in to comment.