Java 5부터 추가 된 기능. 간단하게 메타코드(추가 정보) 제공
알맹이만 가볍고 빠르게 알아보자!
기본으로 존재하는 어노테이션은 무엇이 있을까?
너무 깊이 있게 다루진 않을 꺼다!
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();
}
}
}