gpt4 book ai didi

Java 注解 javaArgs 和 LiteralArguments 已弃用

转载 作者:太空宇宙 更新时间:2023-11-04 12:13:38 24 4
gpt4 key购买 nike

我在使用 Java 注释时遇到问题,因为某些函数已被弃用,而且我不知道如何使用建议的“Annotation.tree”来编写它。

这里是已弃用的代码:

lazy val lengthOfAnnotation = p.annotations.find(_.tpe == ru.typeOf[Len])
lazy val maxLength = lengthOfAnnotation.get.javaArgs.head._2.asInstanceOf[ru.LiteralArgument].value.value.asInstanceOf[Int]

这是不起作用的新代码:

lazy val lengthOfAnnotation = p.annotations.find(_.tree.tpe == ru.typeOf[Len])
lazy val maxLength = lengthOfAnnotation.get.tree.children.tail.head.tpe.value.asInstanceOf[Int]

错误:

method javaArgs in trait AnnotationApi is deprecated: Use tree.children.tail instead

type LiteralArgument in trait Annotations is deprecated: Use Annotation.tree to inspect annotation arguments

method value in trait LiteralArgumentApi is deprecated: Use Annotation.tree to inspect annotation arguments

最佳答案

我找到了一个解决方案,它现在正在运行。

    import scala.reflect.runtime._
import scala.reflect.runtime.universe._
lazy val lengthOfAnnotation = p.annotations.find(_.tree.tpe == ru.typeOf[Len])
val result = lengthOfAnnotation.flatMap { a =>
a.tree.children.tail.head.collect({ case Literal(Constant(value)) => value }).headOption
}
lazy val maxLength = result.get.value.asInstanceOf[Int]

关于Java 注解 javaArgs 和 LiteralArguments 已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39632100/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com