2

我正在尝试编写检查以防止返回带有特定注释的类型。

如,

const val TEST_CONTENT = 
        """
        | package sample.test
        |
        | @Target(AnnotationTarget.ANNOTATION_CLASS, AnnotationTarget.CLASS)
        | annotation class SampleAnnotation
        | 
        | @SampleAnnotation
        | internal interface ConditionalReturnedType 
        |
        | interface TestCase {
        |   // this is not allowed
        |   fun someFunction(whatever: String): ConditionalReturnedType
        | }
        """.trimIndent()

到目前为止我的规则如下

override fun visitNamedFunction(function: KtNamedFunction) {
    super.visitNamedFunction(function)
    if (BindingContext.EMPTY == bindingContext) {
      return
    }

    val returnType = function.createTypeBindingForReturnType(bindingContext)?.type ?: return

    // HERE Annotations is always EMPTY
    val annotations = returnType.annotations
    val hasRequiredAnnotation = annotations.hasAnnotation(FqName("SampleAnnotation"))
    if (!hasRequiredAnnotation) return

    if (isAnAllowedCondition(function, returnType)) {
      // Allow returning the type for this condition
      return
    }

    report(CodeSmell(/** */))
  }

我可以验证这returnType是正确的,但annotations类型始终为空。是否有另一种获取注释的方法,或者我在这里犯了一些新手错误?:)

我的测试如下,

@Test
fun `negative cuz it doesnt match allowed conditions`() {
  val actual = subject.compileAndLintWithContext(ENVIRONMENT.env, TEST_CONTENT)
  assertThat(actual).hasSize(1)
  assertThat(actual[0].message)
      .isEqualTo("Ops. You shouldn't return that.")
}
4

1 回答 1

1

所以,有2个问题。

  1. 我在那里添加visitClass并检查了注释,它总是空的——这很奇怪,让我意识到问题出在测试上。|出于某种原因,在编译时要删除带有注释行的代码块。| @SampleAnnotation这根本不存在于编译文件中。删除|解决了这个问题。
  2. 出于某种原因,看起来没有KotlinType为 returnType 填充注释字段。所以相反,我需要以某种方式到达 KtClass 对象,添加以下我能够获得注释。
val returnType = function.createTypeBindingForReturnType(bindingContext)?.type ?: return
val annotations = returnType.constructor.declarationDescriptor?.annotations ?: return
val isAnnotated = annotations.any { it.type.toString() == "SampleAnnotation" }
于 2021-04-23T12:46:51.093 回答