Turned 'getDeprecationAnnotation' and 'getReplacementText' to functions for reusability

This commit is contained in:
Yair Morgenstern 2022-02-02 10:48:18 +02:00
parent ff2fc02961
commit fe14918545
5 changed files with 31 additions and 24 deletions

View File

@ -375,25 +375,10 @@ class Ruleset {
// The tests are RulesetInvariant in nature, but RulesetSpecific is called for _all_ objects, invariant is not.
continue
val deprecationAnnotation = unique.type.declaringClass.getField(unique.type.name)
.getAnnotation(Deprecated::class.java)
val deprecationAnnotation = unique.getDeprecationAnnotation()
if (deprecationAnnotation != null) {
var replacementUniqueText = deprecationAnnotation.replaceWith.expression
val deprecatedUniquePlaceholders = unique.type.text.getPlaceholderParameters()
// Here, for once, we DO want the conditional placeholder parameters together with the regular ones,
// so we cheat the conditional detector by removing the '<'
for (parameter in replacementUniqueText.replace('<',' ').getPlaceholderParameters()) {
val parameterNumberInDeprecatedUnique =
deprecatedUniquePlaceholders.indexOf(parameter.removePrefix("+").removePrefix("-"))
if (parameterNumberInDeprecatedUnique == -1) continue
var replacementText = unique.params[parameterNumberInDeprecatedUnique]
if (parameter.startsWith('+')) replacementText = "+$replacementText"
else if(parameter.startsWith('-')) replacementText = "-$replacementText"
replacementUniqueText =
replacementUniqueText.replace("[$parameter]", "[$replacementText]")
}
val replacementUniqueText = unique.getReplacementText()
val deprecationText =
"$name's unique \"${unique.text}\" is deprecated ${deprecationAnnotation.message}," +
if (deprecationAnnotation.replaceWith.expression != "") " replace with \"${replacementUniqueText}\"" else ""

View File

@ -46,6 +46,28 @@ class Unique(val text: String, val sourceObjectType: UniqueTarget? = null, val s
return true
}
fun getDeprecationAnnotation(): Deprecated? = type?.getDeprecationAnnotation()
fun getReplacementText(): String {
val deprecationAnnotation = getDeprecationAnnotation() ?: return ""
var replacementUniqueText = deprecationAnnotation.replaceWith.expression
val deprecatedUniquePlaceholders = type!!.text.getPlaceholderParameters()
// Here, for once, we DO want the conditional placeholder parameters together with the regular ones,
// so we cheat the conditional detector by removing the '<'
for (parameter in replacementUniqueText.replace('<',' ').getPlaceholderParameters()) {
val parameterNumberInDeprecatedUnique =
deprecatedUniquePlaceholders.indexOf(parameter.removePrefix("+").removePrefix("-"))
if (parameterNumberInDeprecatedUnique == -1) continue
var replacementText = params[parameterNumberInDeprecatedUnique]
if (parameter.startsWith('+')) replacementText = "+$replacementText"
else if(parameter.startsWith('-')) replacementText = "-$replacementText"
replacementUniqueText =
replacementUniqueText.replace("[$parameter]", "[$replacementText]")
}
return replacementUniqueText
}
private fun conditionalApplies(
condition: Unique,
state: StateForConditionals

View File

@ -777,5 +777,8 @@ enum class UniqueType(val text: String, vararg targets: UniqueTarget, val flags:
}
return errorList
}
fun getDeprecationAnnotation(): Deprecated? = declaringClass.getField(name)
.getAnnotation(Deprecated::class.java)
}

View File

@ -95,8 +95,7 @@ object TranslationFileWriter {
linesToTranslate.add("\n\n#################### Lines from Unique Types #######################\n")
for (unique in UniqueType.values()) {
val deprecationAnnotation = unique.declaringClass.getField(unique.name)
.getAnnotation(Deprecated::class.java)
val deprecationAnnotation = unique.getDeprecationAnnotation()
if (deprecationAnnotation != null) continue
if (unique.flags.contains(UniqueFlag.HiddenToUsers)) continue

View File

@ -64,8 +64,7 @@ class UniqueDocsWriter {
lines += "## " + targetType.key.name + " uniques"
for (uniqueType in targetType.value) {
val deprecationAnnotation = uniqueType.declaringClass.getField(uniqueType.name)
.getAnnotation(Deprecated::class.java)
val deprecationAnnotation = uniqueType.getDeprecationAnnotation()
if (deprecationAnnotation != null) {
deprecatedUniques += uniqueType
continue
@ -83,8 +82,7 @@ class UniqueDocsWriter {
lines += "## Deprecated uniques"
for (deprecatedUnique in deprecatedUniques) {
val deprecationAnnotation =
deprecatedUnique.declaringClass.getField(deprecatedUnique.name)
.getAnnotation(Deprecated::class.java)
deprecatedUnique.getDeprecationAnnotation()!!
val deprecationText = "Deprecated ${deprecationAnnotation.message}," +
if (deprecationAnnotation.replaceWith.expression != "") " replace with \"${deprecationAnnotation.replaceWith.expression}\"" else ""