package com.javafor.java.lang.annotation.test;
import java.lang.reflect.Method;
import com.javafor.java.lang.annotation.Column;
import com.javafor.java.lang.annotation.Student;
/**
*
*
* @author Harjoe; if you have any questions, please contact me with my blog or
* email, thanks.<br>
* blog: <a href="http://www.javafor.com">www.javafor.com</a><br>
* github: <a href="https://github.com/harjoe">www.github.com/harjoe</a><br>
* email: <a href="harjoe@hotmail.com">harjoe@hotmail.com</a><br>
**/
public class Testor {
public static void main(String[] args) throws ClassNotFoundException {
Class<?> studentClass = Student.class;
//获取注释以及值, 取方法上的注释
Method[] methods = studentClass.getMethods();
for(Method method: methods) {
if (method.isAnnotationPresent(Column.class)) {
Column column = method.getAnnotation(Column.class);
System.out.println(column.name());
}
}
//取类的注注释
if (studentClass.isAnnotationPresent(Column.class)) {
Column column = studentClass.getAnnotation(Column.class);
System.out.println(column.name());
}
}
}
FDA
最新推荐文章于 2018-12-26 20:20:11 发布