Java의 정석

12장. 어노테이션, 메타 어노테이션,타입 정의 및 요소

simplism 2023. 2. 19. 17:04

어노테이션(@)

주석처럼 프로그래밍 언어에 영향을 미치지 않으며, 유용한 정보를 제공

※ 어노테이션은 JUnit이라는 특정프로그램의 정보제공(설정정보)을 위한 것
javadoc.exe : 주석을 추출하여 문서를 만들어냄.
xml :설정파일 (안쓰는추세)
 설정파일은 1개 인데 많은사람이 공유해야함.

 

 


표준 어노테이션

Java에서 제공하는 어노테이션

 


@Override 

 


@Deprecated

 


@FunctionalInterface


@SuppressWarnings


메타 어노테이션


@Target

 


@Retention

 


@Documented, @Inherited


@Repeatable

 


어노테이션 타입 정의하기

 


어노테이션 요소

 


모든 어노테이션의 조상 -  java.lang.annotation.Annotaion

 


마커 어노테이션 - Maker Annotation


어노테이션 요소의 규칙

 

 

 

import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Deprecated
@SuppressWarnings("1111") //유효하지 않은 어노테이션은 무시된다.
@TestInfo(testedBy="aaa", testTools= {"JUnit","JUnit5"},testDate=@DateTime(yymmdd="160101", hhmmss="225959"))
public class ex12_8 {
	public static void main(String[] args) {
	
		//ex12_8의 class객체를 얻는다.
		Class<ex12_8> cls = ex12_8.class;
		
		TestInfo anno = cls.getAnnotation(TestInfo.class);
		System.out.println("anno.testedBy() : " + anno.testedBy());
		System.out.println("anno.testDate().yymmdd() : " + anno.testDate().yymmdd());
		System.out.println("anno.testDate().hhmmss() : " + anno.testDate().hhmmss());
		
		for(String str : anno.testTools())
			System.out.println("testTools = " + str);
	
		System.out.println();
		
		//ex12_8에 적용된 모든 어노테이션을 가져온다.
		Annotation[] annoArr = cls.getAnnotations();
		
		for(Annotation a : annoArr)
			System.out.println(a);
	}
}

@Retention(RetentionPolicy.RUNTIME) //실행시에 사용가능하도록 지정
@interface TestInfo{
	int 		count() 		default 1;
	String 		testedBy();
	String[]	testTools()		default {"JUnit","JUnit5"}; //default요소가 1개인경우 {} 생략가능
	TestType	testType()		default TestType.FIRST;
	DateTime	testDate();
}

@Retention(RetentionPolicy.RUNTIME) //실행시에 사용가능하도록 지정
@interface DateTime{
	String yymmdd();
	String hhmmss();
}

enum TestType { FIRST, FINAL }


출처 : 남궁성의 정석코딩

https://www.youtube.com/@MasterNKS