NutzCN Logo
问答 咨询个JAVA注解问题
发布于 2908天前 作者 threefish 1662 次浏览 复制 上一个帖子 下一个帖子
标签:
package com.test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class TestMain {
    public void MyTestParamAnnotation(@Param("myname") String name,int id) {
        System.out.println(name);
    }
    public static void main(String[] args) {
        Class clsss = TestMain.class;
        Method[] clsssMethods = clsss.getMethods();
        for (Method method : clsssMethods) {
            System.out.println("方法名:"+method.getName());
            Annotation[][] annotations=method.getParameterAnnotations();
            for(Annotation[] annotation:annotations){
                if(annotation.length>0){
                    System.out.println(annotation[0].annotationType());
                }
            }
        }
    }
}

请问如何获得

@Param("myname") 

中的myname值?

2 回复

这样:

	public static void main(String[] args) throws Exception {
        Method method = YvrModule.class.getMethod("upload", new Class<?>[]{TempFile.class});
        Annotation[][] annss = method.getParameterAnnotations();
        for (Annotation[] annos: annss) {
            for (Annotation anno : annos) {
                if (anno instanceof Param) {
                    System.out.println(((Param)anno).value());
                }
            }
        }
    }

YvrModule的upload方法长这样:

	public Object upload(@Param("file") TempFile tmp) throws IOException {
		int userId = Toolkit.uid();
		return yvrService.upload(tmp, userId);
	}

上述main方法的输出值是

file
添加回复
请先登陆
回到顶部