如何在Android中实现LintFix以向类定义添加缺少的注释
我正在研究在我当前的Android应用程序中自定义Lint规则的开发 我的检测器查找项目中未使用特定注释注释的所有活动 我想创建一个LintFix,添加缺少的注释,如下所示:-如何在Android中实现LintFix以向类定义添加缺少的注释,android,lint,Android,Lint,我正在研究在我当前的Android应用程序中自定义Lint规则的开发 我的检测器查找项目中未使用特定注释注释的所有活动 我想创建一个LintFix,添加缺少的注释,如下所示:- val fix = LintFix.create() .replace() .text("class ") .with("@MyMissingAnnotation \nclass ") .ra
val fix = LintFix.create()
.replace()
.text("class ")
.with("@MyMissingAnnotation \nclass ")
.range(context.getLocation(node as UElement))
.build()
错误活动
class MyActivity : AppCompatActivity() {
...
}
活动固定
@MyMissingAnnotation
class MyActivity : AppCompatActivity() {
...
}
我制定的代码是:-
val fix = LintFix.create()
.replace()
.text("")
.with("@MyMissingAnnotation")
.build()
但是,这会导致以下损坏的代码
class @MyMissingAnnotationMyActivity : AppCompatActivity() {
...
}
因为我的报告类似于此
context.report(
ISSUE, node,
context.getNameLocation(node),
"Activities require the @MyMissingAnnotation annotation.",
fix
)
如何在类中的正确位置添加所需的注释
更新
我已设法解决我的问题,如下所示:-
val fix = LintFix.create()
.replace()
.text("class ")
.with("@MyMissingAnnotation \nclass ")
.range(context.getLocation(node as UElement))
.build()
但是,我也希望在修复注释的同时导入注释
如何正确设置位置以添加关联的注释导入语句?您可以使用shortenNames、start和reformattrue使修复更合理
val fix = LintFix.create()
.replace()
.text("")
.with("@com.foo.bar2.MyMissingAnnotation")
.beginning()
.shortenNames()
.reformat(true)
.range(context.getLocation(node as UElement))
.build()
您可以使用shortenNames、start和reformattrue使您的修复更合理
val fix = LintFix.create()
.replace()
.text("")
.with("@com.foo.bar2.MyMissingAnnotation")
.beginning()
.shortenNames()
.reformat(true)
.range(context.getLocation(node as UElement))
.build()