Groovy 如何获取注释值';使用';
我想从Groovy 如何获取注释值';使用';,groovy,annotations,Groovy,Annotations,我想从MyAnnot检索注释值used。我在列表中得到了3个注释,尽管只有2个注释。此外,我还尝试获取MyAnnot的used字段,但没有成功。我想返回一个map,其中MyAnnot的used是键,type是map的值 // Then, define your class with it's annotated Fields class MyClass { @MyAnnot(used = "Hey", type = "There") String fielda @MyAnno
MyAnnot
检索注释值used
。我在列表中得到了3个注释,尽管只有2个注释。此外,我还尝试获取MyAnnot
的used
字段,但没有成功。我想返回一个map,其中MyAnnot
的used
是键,type
是map的值
// Then, define your class with it's annotated Fields
class MyClass {
@MyAnnot(used = "Hey", type = "There")
String fielda
@MyAnnot(used = "denn", type = "Ton")
String fieldc
}
def findAllPropertiesForClassWithAnotation(obj, annotClass) {
def op = []
def annos = []
def i = 0
obj.properties.findAll { prop ->
obj.getClass().declaredFields.find {
it.name == prop.key && annotClass in it.declaredAnnotations*.annotationType()
annos=it.declaredAnnotations
i++
if(annos)
op << annos[0] as Set
// println"Props ${annos[0]}"
}
}
op.each{ println "${it} and i is ${i}"}
}
// Then, define an instance of our class
MyClass a = new MyClass(fielda: 'tim', fieldc: 'dennisStar')
// And print the results of calling our method
println findAllPropertiesForClassWithAnotation(a, MyAnnot)
//然后,用类的注释字段定义类
类MyClass{
@MyAnnot(使用“嘿”,type=“There”)
弦场
@MyAnnot(使用“denn”,type=“Ton”)
字符串字段C
}
def FindAllProperties for ClassWithAnotation(对象,注释类){
def op=[]
def annos=[]
DEFI=0
obj.properties.findAll{prop->
obj.getClass().declaredFields.find{
it.name==其中的prop.key&&annotClass.declaredAnnotations*.annotationType()
annos=it.declaredAnnotations
我++
如果(annos)
op首先,您需要将注释标记为:@Retention(RetentionPolicy.RUNTIME)
,以便在运行时对其进行处理,因此它将是:
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnot {
String used()
String type()
}
import java.lang.annotation.RetentionPolicy
import java.lang.annotation.Retention
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnot {
String used()
String type()
}
class MyClass {
@MyAnnot(used="Hey" ,type="There")
String fielda
@MyAnnot(used="denn", type="Ton")
String fieldc
}
def findAllPropertiesForClassWithAnotation( obj, annotClass ) {
def c = obj.getClass()
c.declaredFields.findAll { field ->
field.isAnnotationPresent(annotClass)
}.collect { found ->
def a = found.getAnnotation(annotClass)
[(a.used()): a.type()]
}.sum()
}
MyClass a = new MyClass(fielda: 'tim', fieldc: 'dennisStar')
println findAllPropertiesForClassWithAnotation(a, MyAnnot)
然后,used
和type
不是字段,也不是属性,而是方法,因此必须调用并获取它们
脚本将是:
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnot {
String used()
String type()
}
import java.lang.annotation.RetentionPolicy
import java.lang.annotation.Retention
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnot {
String used()
String type()
}
class MyClass {
@MyAnnot(used="Hey" ,type="There")
String fielda
@MyAnnot(used="denn", type="Ton")
String fieldc
}
def findAllPropertiesForClassWithAnotation( obj, annotClass ) {
def c = obj.getClass()
c.declaredFields.findAll { field ->
field.isAnnotationPresent(annotClass)
}.collect { found ->
def a = found.getAnnotation(annotClass)
[(a.used()): a.type()]
}.sum()
}
MyClass a = new MyClass(fielda: 'tim', fieldc: 'dennisStar')
println findAllPropertiesForClassWithAnotation(a, MyAnnot)
请注意,仅传递注释类是不够的,因为您不知道要在注释上调用的方法(used
和type
)。以下方法仅适用于MyAnnot
类