주석 종류 Annotations Types
11521 단어 Annotations
Annotations are notes in Java programs to instruct the Java compiler to do something. Java provides three standard annotations and four standard meta-annotations.
주 해 는 자바 프로그램의 주석 으로 자바 컴 파일 러 가 어떤 일 을 하도록 지시 하 는 데 사 용 됩 니 다.자바 제공
3 가지 표준 주해 와 4 가지 표준 원 주해.
An annotation type is a special interface type.
An annotation is an instance of an annotation type.
An annotation type has a name and members.
주해 형식 은 특수 한 인터페이스 형식 입 니 다.
주 해 는 주해 형식의 인 스 턴 스 입 니 다.
주해 유형 은 이름과(방법)구성원 이 있 습 니 다.
The information contained in an annotation takes the form of key/value pairs.
There can be zero or multiple pairs and each key has a specific type.
It can be a String, int, or other Java types.
주해 에 포 함 된 정 보 는 키/값 쌍 으로 표 시 됩 니 다.
0 개 이상 의 쌍 이 있 을 수 있 습 니 다.키 마다 구체 적 인 유형 이 있 습 니 다.
키 는 String,int 또는 다른 자바 형식 일 수 있 습 니 다.
Annotation types with no key/value pairs are called marker annotation types.
Those with one key/value pair are referred to single-value annotation types.
There are three annotation types in Java 5: Deprecated, Override, and Suppress Warnings.
key/value 가 없 는 주해 형식 을 주석 형식 을 표시 합 니 다.
키/value 쌍 을 가 진 단일 값 주석 형식 입 니 다.
자바 5 에는 세 가지 주해 유형 이 있 습 니 다. 시간 이 지 났 습 니 다.덮어 쓰기 와... 경 고 를 억누르다.
There are four other annotation types that are part of the java.lang.annotation package: Documented, Inherited, Retention, and Target.
These four annotation types are used to annotate annotations,
자바.lang.annotation 패키지 의 일부분 으로 4 가지 원 주해 유형 이 있 습 니 다.
문서,계승,보존,목표.
1.13.2 The Annotation Interface : 주석 인터페이스
An annotation type is a Java interface.
All annotation types are subinterfaces of the java.lang.annotation.Annotation interface.
It has one method, annotation Type, that returns an java.lang.Class object.
하나의 주석 형식 은 자바 인터페이스 입 니 다.
모든 주해 유형 은 java.lang.annotation.annotation 인터페이스의 하위 인터페이스 입 니 다.
annotation Type()방법 이 있 습 니 다.자바.lang.Class 대상 을 되 돌려 줍 니 다.
java.lang.Class<? extends Annotation> annotationType()
In addition, any implementation of Annotation will override the equals, hashCode, and
toString methods from the java.lang.Object class.
또한,Annotation 의 모든 실현 은 자바.lang.Object 류 의 equals,hashCode 와 toString 방법 을 덮어 씁 니 다.
1.13.3 기본 값 사용 기본 값 사용
Annotation default values is used if no value is specified.
A default value is specified by adding a default clause.
Default value must be of a type compatible with type.
값 을 지정 하지 않 으 면 주해 의 기본 값 이 사 용 됩 니 다.
default 자 구 를 추가 하여 기본 값 을 지정 합 니 다.
기본 값 은 형식 호 환 이 필요 합 니 다.
다음 @MyAnnotation 은 기본 값 을 포함 합 니 다.
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
String stringValue() default "defaultString";
int intValue() default 101;
}
다음 4 가지 사용 방식 은 @MyAnnotation 은 모두 합 법 적 입 니 다.
@MyAnnotation() // both str and val default
@MyAnnotation(stringValue = "some string") // val defaults
@MyAnnotation(intValue = 100) // str defaults
@MyAnnotation(stringValue = "Testing", intValue = 100) // no defaults
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
// A simple annotation type.
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
String stringValue() default "defaultString";
int intValue() default 101;
}
@MyAnnotation(stringValue = "for class", intValue = 100)
public class MainClass {
// Annotate a method.
@MyAnnotation(intValue = 100)
public static void myMethod() {
}
public static void main(String[] arg) {
try {
MainClass ob = new MainClass();
Method m = ob.getClass( ).getMethod("myMethod");
Annotation[] annos = m.getAnnotations();
System.out.println("All annotations for myMeth:");
for(Annotation a : annos)
System.out.println(a);
} catch (Exception exc) {
}
}
}
/*
All annotations for myMeth:
@MyAnnotation(stringValue=defaultString, intValue=100)
*/
1.13.4 Marker Annotations 태그 형 주석
Marker Annotations are used to mark a declaration.
A marker annotation is a special kind of annotation.
A marker annotation contains no members.
Using isAnnotationPresent( ) to determine if the marker is present.
태그 형 주 해 는 성명 을 표시 하 는 데 사 용 됩 니 다.
표기 형 주 해 는 주해 중의 특수 한 일종 이다.
태그 형 주 해 는 구성원 을 포함 하지 않 습 니 다.
isAnnotationPresent()를 사용 하여 marker 가 존재 하 는 지 확인 합 니 다.
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
}
@MyAnnotation
public class MainClass {
// Annotate a method.
@MyAnnotation()
public static void myMethod() {
}
public static void main(String[] arg) {
try {
MainClass ob = new MainClass();
Method m = ob.getClass( ).getMethod("myMethod");
Annotation[] annos = m.getAnnotations();
System.out.println("All annotations for myMeth:");
for(Annotation a : annos)
System.out.println(a);
} catch (Exception exc) {
}
}
}
/*
All annotations for myMeth:
@MyAnnotation()
*/
설명:태그 형 주 해 를 사용 할 때 뒤의 괄호()는 있 으 나 마 나 합 니 다.
1.13.5 Single-Member Annotations 단일 값 형 주석
Single-Member Annotations contains only one member.
Single-Member Annotations allow a shorthand form of specifying the value.
The name of the member must be value.
단일 값 주 해 는 한 구성원 만 포함 합 니 다.
단일 값 형 주 해 는 간단 한 형식 으로 값 을 실행 할 수 있 습 니 다.
멤버 이름 은 value 여야 합 니 다.
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
int value();
}
@MyAnnotation(102)
public class MainClass {
// Annotate a method.
@MyAnnotation(101)
public static void myMethod() {
}
public static void main(String[] arg) {
try {
MainClass ob = new MainClass();
Method m = ob.getClass( ).getMethod("myMethod");
Annotation[] annos = m.getAnnotations();
System.out.println("All annotations for myMeth:");
for(Annotation a : annos)
System.out.println(a);
} catch (Exception exc) {
}
}
}
/*
All annotations for myMeth:
@MyAnnotation(value=101)
*/
1.13.6 Creates and uses a single-member annotation 단일 값 주석 생 성 및 사용
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
@Retention(RetentionPolicy.RUNTIME)
@interface MySingle {
int value(); // this variable name must be value
}
class Single {
@MySingle(100)
public static void myMeth() {
Single ob = new Single();
try {
Method m = ob.getClass().getMethod("myMeth");
MySingle anno = m.getAnnotation(MySingle.class);
System.out.println(anno.value()); // displays 100
} catch (NoSuchMethodException exc) {
System.out.println("Method Not Found.");
}
}
public static void main(String args[]) {
myMeth();
}
}
//100
1.13.7 단일 값 문법
Using the single-value syntax when applying an annotation that has other members, if other members all have default values.
다른 구성원 이 있 는 단일 값 형 주 해 를 사용 할 때(다른 구성원 마다 기본 값 이 있 음)단일 값 문법 을 사용 할 수 있 습 니 다.
예 를 들 면:
@interface SomeAnno {
int value();
int xyz() default 0;
}
@SomeAnno(88)
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
int value();
int defaultValue() default 100;
}
@MyAnnotation(102)
public class MainClass {
// Annotate a method.
@MyAnnotation(101)
public static void myMethod() {
}
public static void main(String[] arg) {
try {
MainClass ob = new MainClass();
Method m = ob.getClass( ).getMethod("myMethod");
Annotation[] annos = m.getAnnotations();
System.out.println("All annotations for myMeth:");
for(Annotation a : annos)
System.out.println(a);
} catch (Exception exc) {
}
}
}
/*
All annotations for myMeth:
@MyAnnotation(defaultValue=100, value=101)
*/
1.13.8 일부 제한 사항
No annotation can inherit another.
All methods declared by an annotation must be without parameters.
Annotations cannot be generic.
They cannot specify a throws clause.
They must return one of the following:
하나의 주 해 는 다른 주 해 를 계승 할 수 없다.
주석 에서 설명 한 모든 방법 은 인자 가 없어 야 합 니 다.
주 해 는 일반적인 것 이 어 서 는 안 된다.
주 해 는 throws 자 구 를 지정 할 수 없습니다.
주 해 는 다음 중 하 나 를 되 돌려 야 합 니 다.
A simple type, such as int or double,
An object of type String or Class
An enum type
Another annotation type
An array of one of the preceding types
간단 한 대답 유형 예 를 들 어 int 나 double,
형식 String 또는 Class 의 대상,
매 거 진(enum)형식,
다른 주석 형식
이상 의 특정한 유형의 배열.
1.13.9 예:하나의 표기 형 주해
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
@Retention(RetentionPolicy.RUNTIME)
@interface MyMarker {
}
class Marker {
@MyMarker
public static void myMeth() {
Marker ob = new Marker();
try {
Method m = ob.getClass().getMethod("myMeth");
if (m.isAnnotationPresent(MyMarker.class))
System.out.println("MyMarker is present.");
} catch (NoSuchMethodException exc) {
System.out.println("Method Not Found.");
}
}
public static void main(String args[]) {
myMeth();
}
}
//MyMarker is present.