50,530
社区成员
发帖
与我相关
我的任务
分享
public class MethodDemo {
public static void main(String[] args){
Method[] methods = SampleClass.class.getMethods();
Annotation[][] annotations = methods[1].getParameterAnnotations();
for (Annotation[] annotation1 : annotations) {
for (Annotation annotation : annotation1) {
if (annotation instanceof CustomAnnotation) {
CustomAnnotation customAnnotation = (CustomAnnotation) annotation;
System.out.println("name: " + customAnnotation.name());
System.out.println("value: " + customAnnotation.value());
}
}
}
}
}
@CustomAnnotation(name = "SampleClass", value = "Sample Class Annotation")
class SampleClass {
private String sampleField;
public String getSampleField() {
return sampleField;
}
public void setSampleField(
@CustomAnnotation(name = "sampleClassMethod", value = "Sample Method Annotation") String sampleField) {
this.sampleField = sampleField;
}
}
@Retention(RetentionPolicy.RUNTIME)
@interface CustomAnnotation {
public String name();
public String value();
}