2016-09-22 9 views
1

Ich habe ein Problem mit Java-Annotationen, weil einige Funktionen veraltet sind und ich nicht weiß, wie man es mit "Annotation.tree" schreibt, was vorgeschlagen wird.Java-Annotationen javaArgs und LiteralArguments sind veraltet

hier Code veraltet:

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] 

hier ist neuer Code, der nicht funktioniert:

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

Fehler:

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 

Antwort

0

ich nun eine Lösung gefunden und es funktioniert .

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] 
Verwandte Themen