Annotation
Annotation์ ํน์ง
Annotation์ด๋ ๋ณธ๋ ์ฃผ์์ด๋ ๋ป์ผ๋ก, interface๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ ๋ฌธ๋ฒ์ด๋ค
์ฃผ์๊ณผ๋ ๊ทธ ์ญํ ์ด ๋ค๋ฅด์ง๋ง ์ฃผ์์ฒ๋ผ ์ฝ๋์ ๋ฌ์ class์ ํน๋ณํ ์๋ฏธ๋ฅผ ๋ถ์ฌํ๊ฑฐ๋ ๊ธฐ๋ฅ์ ์ฃผ์ ํ ์ ์๋ค
Annotation์ ์ฌ์ฉํ๋ฉด ๋ฐ์ดํฐ์ ๋ํ
์ ํจ์ฑ ๊ฒ์ฌ์กฐ๊ฑด
์ annotation์ ์ฌ์ฉํ์ฌ ์ง์ ๋ช ์ํจ์ผ๋ก์จ ์ ํจ ์กฐ๊ฑด์ ์ฝ๊ฒ ํ์ ํ ์ ์๊ฒ ๋๋ฉฐ, ์ฝ๋๊ฐ ๊น๋ํด์ง๋คAnnotation์ ํฌ๊ฒ
๋ฌธ์ํ
,compiler ์ฒดํฌ
,์ฝ๋ ๋ถ์
์ ์ํ ์ฉ๋๋ก ์ฌ์ฉ๋๋ค๋ฌธ์ํ ๋ถ๋ถ์
JavaDoc
์ด ์๊ธฐ ๋๋ฌธ์ ๋ง์ด ์ฌ์ฉ๋์ง๋ ์๋๋คAnnotation์ ๋ณธ์ง์ ์ธ ๋ชฉ์ ์ ์์ค ์ฝ๋์ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ํํํ๋ ๊ฒ์ด๋ค
ํด์๋๋ ์์ ์ ์ ํ ์๋ ์๋ค (
Retention Policy
)
Annotation์ ์ข
๋ฅ
Annotation์๋ ํฌ๊ฒ ์ธ ๊ฐ์ง ์ข ๋ฅ๊ฐ ์กด์ฐํ๋ค
1. JDK ์ ๋ด์ฅ๋์ด ์๋ Built-in annotation
Built-in annotation
์ฃผ๋ก compiler๋ฅผ ์ํ ๊ฒ์ผ๋ก, compiler์๊ฒ ์ ์ฉํ ์ ๋ณด๋ฅผ ์ ๊ณตํ๋ค
ex)
@Override
์์๋ฐ์์ ๋ฉ์๋๋ฅผ override ํ ๋ ๋ํ๋๋
@Override
annotationmethod ์์๋ง ๋ถ์ผ ์ ์์ผ๋ฉฐ, ํ์ฌ method๊ฐ super class์ method๋ฅผ overrideํ method์์ compiler์๊ฒ ๋ช ์ํ๋ค
overriding ํ ๋ method ๋ช ์์ ์คํ๊ฐ ๋ฐ์ํ ์ ์๋๋ฐ compiler ์ ์ฅ์์๋ ์๋ก์ด method๋ฅผ ์์ฑํ๋ ๊ฒ์ธ์ง overriding ํ๋ ๊ฒ์ธ์ง ๋ชจ๋ฅธ๋ค
์ด๋ฐ ๊ฒฝ์ฐ annotation์ ํตํด์ ์คํ๊ฐ ๋ฐ์ํ ์ ์๋ ๋ถ๋ถ์ ์ก์์ค ์ ์๋ค
@Deprecated
์ฐจํ ๋ฒ์ ์ ์ง์๋์ง ์์ ์ ์๊ธฐ ๋๋ฌธ์ ๋ ์ด์ ์ฌ์ฉ๋์ง ๋ง์์ผ ํ ๋ฉ์๋๋ฅผ ๋ํ๋ธ๋ค.
@SupressWarning
ํ๋ก๊ทธ๋๋จธ์ ์๋๋ฅผ compiler์๊ฒ ์ ๋ฌํ์ฌ ๊ฒฝ๊ณ ๋ฅผ ์ ๊ฑฐํ๋ค
@FunctionalInterface
compiler์๊ฒ ๋ค์์ ์ธํฐํ์ด์ค๋ ํจ์ํ ์ธํฐํ์ด์ค๋ผ๋ ๊ฒ์ ์๋ฆฐ๋ค
overriding annotation๊ณผ ๊ฐ์ ์ด์ ๋ก ์ค์๋ฅผ ๋ฏธ์ฐ์ ๋ฐฉ์งํ๊ธฐ ์ํด ์ฌ์ฉํ๋ค
2. annotation์ ๋ํ ์ ๋ณด๋ฅผ ๋ํ๋ด๊ธฐ ์ํ annotation์ธ Meta annotation
Meta annotation
annotation์ ์ฌ์ฉ๋๋ annotation์ผ๋ก ํด๋น annotation์ ๋์๋์์ ๊ฒฐ์ ํ๋ค
์ฃผ๋ก ์๋ก์ด annotation์ ์ ์ํ ๋ ์ฌ์ฉํ๋ค
ex)
@Target
annotation์ด ์ ์ฉ ๊ฐ๋ฅํ ๋์์ ์ง์ ํ๋๋ฐ ์ฌ์ฉํ๋ค
์ฌ๋ฌ ๊ฐ์ ๊ฐ์ ์ง์ ํ ๋๋ ๋ฐฐ์ด์์ ์ฒ๋ผ ๊ดํธ { }๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค
@Retention
์ปค์คํ annotation์ ์์ฑํ ๋ ์ฃผ๋ก ์ฌ์ฉํ๋ annotation์ผ๋ก, ์ด๋ ์์ ๊น์ง annotation์ memory๋ฅผ ๊ฐ์ ธ๊ฐ ์ง ์ค์ ํ ์ ์๋ค
annotation์
life cycle
์ ์ค์ ํ๋ ๊ฒ์ผ๋ก, ์ฆ, annotation์ด ์ธ์ ๊น์ง ์ด์ ์์์ง๋ฅผ ์ ํ๋ ๊ฒ!
์์ฑ
RetentionPolicy
Retention Policy ๋?
Annotation์ ์๋ช ์ฃผ๊ธฐ๋ฅผ ์ ์ํ๋ ์ ์ฑ
Compile ๋ class file์์ annotation ์ ๋ณด๋ฅผ ์ ์งํ ์ง ์ฌ๋ถ๋ฅผ ์ง์ ํ๋๋ฐ ์ฌ์ฉ๋๋ค
Retention Policy์ ์ข ๋ฅ
RetentionPolicy.SOURCE
source code (.java)๊น์ง ๋จ์์๋๋ค
compile ๊ณผ์ ์์ annotation ์ ๋ณด๊ฐ ์ฌ๋ผ์ง๋ค
์ฃผ๋ก compiler ๊ฒฝ๊ณ ๋ error message๋ฅผ ์์ฑํ๋๋ฐ ์ฌ์ฉ๋๋ค
RetentionPolicy.CLASS
class ํ์ผ(.class)๊น์ง ๋จ์์๋๋ค (=bytecode)
runtime ์ ์ ์ง๋์ง ์๋๋ค
์ฃผ๋ก compile ์์ ์ฝ๋ ์์ฑ์ ํ๊ฑฐ๋, compiler์ ์ฒ๋ฆฌ๋ฅผ ์ํด ์ฌ์ฉ๋๋ค
RetentionPolicy.RUNTIME
runtime๊น์ง ๋จ์์๋๋ค (=์ฌ์ค์ ์ ์ฌ๋ผ์ง๋ค)
์ฃผ๋ก runtime ์์
reflection
์ ํตํด annotation ์ ๋ณด๋ฅผ ์ฝ๊ฑฐ๋ ์์ ํ๋๋ฐ ์ฌ์ฉ๋๋คex) ํน์ annotation์ ์ฌ์ฉํ์ฌ runtime ์์ class์ ๋์์ ๋ณ๊ฒฝํ๊ฑฐ๋ ์ถ๊ฐํ๋ ๊ธฐ๋ฅ์ ์ ๊ณต
@Inherited
annotation์ด ์์ class์๋ ์์๋๋๋ก ํ๋ annotation์ด๋ค
์ด annotation์ ๋ถ๋ชจ class์ ๋ถ์ด๋ฉด ์์ class๋ ์ด annotation์ด ๋ถ์ ๊ฒ๊ณผ ๊ฐ์ด ์ธ์๋๋ค
@Native
Native method์ ์ํด ์ฐธ์กฐ๋๋ ์์ ํ๋์ ๋ถ์ด๋ annotation์ด๋ค
Native method๋ JVM์ด ์ค์น๋ OS์ method๋ฅผ ๋งํ๋ค
Native method๋ ๋ณดํต C์ธ์ด๋ก ์์ฑ๋์ด ์๊ณ , Java์์๋ method์ ์ ์ธ๋ถ๋ง ์ ์ํ๊ณ ๊ตฌํ์ ํ์ง ์๋๋ค
Object class์ method๋ค์ ๋๋ถ๋ถ Native method์ด๋ค
์ฐ๋ฆฌ๋ Java๋ผ๋ ์ธ์ด๋ฅผ ํตํด OS์ method๋ฅผ ํธ์ถํ๊ณ ์์๋ ๊ฒ์ด๋ค
Native method์ Java์ ์ ์๋ method๋ฅผ ์ฐ๊ฒฐํ๋ ๊ฒ์
JNI(Java Native Interface)
๋ผ๊ณ ํ๋ค
3. ๊ฐ๋ฐ์๊ฐ ์ง์ ๋ง๋ค์ด ๋ด๋ Custom Annotation
Custom Annotation
Last updated
Was this helpful?