import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Count100 {
}
사용
public class MyHello {
@Count100
public void hello(){
System.out.println("hello");
}
}
실행
메소드가 어노테이션이 설정되어 있는 경우 행동을 하게 만듬
import java.lang.reflect.Method;
public class MyHelloExam {
public static void main(String[] args) {
MyHello hello = new MyHello();
try{
Method method = hello.getClass().getDeclaredMethod("hello");
if(method.isAnnotationPresent(Count100.class)){
for(int i = 0; i < 100; i++){
hello.hello();
}
}else{
hello.hello();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}