注解
package annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//哪些类型可以使用
@Target({ElementType.METHOD})
//保留的方式
@Retention(RetentionPolicy.RUNTIME)
public @interface Misquotation {
String userName() ;
String password() ;
}
package annotation;
import java.lang.reflect.Method;
public class MisquotationTest01 {
@Misquotation(userName = "admin",password = "12345")
public void doSome(){
}
public static void main(String[] args) throws Exception {
//获取这个类
Class misquotation = Class.forName("annotation.MisquotationTest01");
//获取doSome方法
Method doSomeMethod = misquotation.getDeclaredMethod("doSome");
//判断这个方法上是否含有这个注解 misquotation.isAnnotatonPresent(Misquotation.class)
//判断这个类中是否含有这个注解if(doSomeMethod.isAnnotationPresent(Misquotation.class)){
Misquotation misquotation1 = doSomeMethod.getAnnotation(Misquotation.class);
System.out.println(misquotation1.userName());
System.out.println(misquotation1.password());
}
}
}