2. Annotation Basics
Java annotation은 다음과 같이 표현됩니다.
@Deprecated, @Override, @SuppressWarings
위 3가지 Annotation의 경우에 Compiler instructions으로 이미 Java에서 제공되는 것 입니다.
2.1. @Deprecated
Class, Method, Field에 해당 annotation을 표시해두면 해당되는 것들은 사용하지 마라 라고 권고하는 것 입니다. 해당 annotation은 compile시에 warning(경고)를 표시해줍니다.
@Deprecated
public class DeprecatedClass {
public DeprecatedClass() {
}
@Deprecated
public static final String deprecatedStr = "Deprecated"
@Deprecated
public void deprecatedMethod(){
}
}
public class Main {
public static void main(String[] args) {
// Class Call
DeprecatedClass deprecatedClass = new DeprecatedClass();
// Method Call
deprecatedClass.deprecatedMethod();
// Field Call
String str = DeprecatedClass.deprecatedStr;
}
}
2.2. @Override
@Override annotation는 superclass(부모 클래스)에 대해 상속 받아 override 할 경우에 사용됩니다. 만약 superclass에 해당 method가 없다면, compile시 error를 발생시킵니다.
public class ParentClass {
public void extendMethod(){
}
}
package com.example.annotation;
public class ChildClass extends ParentClass{
@Override
public void extendMethod() {
super.extendMethod();
}
@Override
public void notExtendMethod(){
}
}
2.3 @SuppressWarnings
@SuppressWarning 는 warning을 발생하는 것들에 대하여 warning을 제거하는 용도로 사용됩니다. Deprecated된 명령어를 사용하거나, 보장되지 않는 형변환을 할 때 등 컴파일 시점에 warning을 발생하는 경우에 대해서 warning을 제거할 수 있도록 합니다.
@SuppressWarnings("deprecation")
public class SuppressWarningsClass {
public void method(){
DeprecatedClass deprecatedClass = new DeprecatedClass();
}
}
참고 논문 및 사이트
1. Java Annotation: 인터페이스 강요로부터 자유를… - http://www.nextree.co.kr/p5864/
2. Java Annotations - http://tutorials.jenkov.com/java/annotations.html
No comments:
Post a Comment