ν°μ€ν 리 λ·°
π μ λν μ΄μ
: μ£Όμμ²λΌ μμ€μ½λμ μν₯μ λ―ΈμΉμ§ μμΌλ©΄μ μ»΄νμΌλ¬μκ² μ μ©ν μ 보λ₯Ό μ 곡νλ κ² ('@'κ° λΆμ νκ·Έ)
https://docs.oracle.com/javase/8/docs/api/java/lang/annotation/Annotation.html
Annotation (Java Platform SE 8 )
Returns the hash code of this annotation, as defined below: The hash code of an annotation is the sum of the hash codes of its members (including those with default values), as defined below: The hash code of an annotation member is (127 times the hash cod
docs.oracle.com
π νμ€ μ λν μ΄μ
: μλ°μμ κΈ°λ³Έμ μΌλ‘ μ 곡νλ μ λν μ΄μ
βοΈ @Override
: μ»΄νμΌλ¬μκ² μ€λ²λΌμ΄λ©νλ λ©μλλΌλ κ²μ μλ¦Ό → μ»΄νμΌλ¬κ° κ°μ μ΄λ¦μ μ‘°μλ©μλκ° μμΌλ©΄ μ€λ₯λ₯Ό λ°μμν΄!
: λ©μλ μμλ§ λΆμΌ μ μμ
class MyClass extends Object {
@Override
public String toString() { ... } // Object ν΄λμ€μ toString() λ©μλλ₯Ό μ€λ²λΌμ΄λ
}
β @Override μ λν μ΄μ μ λΆμ΄μ§ μμμΌλ©΄, toString()μ tostring()μΌλ‘ μλͺ»μ¨λ μ€λ₯ λ°μX → μ€ν ν μ΄λμ μλͺ»λμλμ§ μμλ΄κΈ° μ΄λ €μ!
βοΈ @Deprecated
: λ μ΄μ μ¬μ©λμ§ μλ νλλ λ©μλμ μ¬μ©νλ μ λν μ΄μ , μ΄ μ λν μ΄μ μ΄ λΆμ λμμ λ€λ₯Έ κ²μΌλ‘ λ체λμμΌλ λ μ΄μ μ¬μ©νμ§ μμ κ²μ κΆνλ€(κ°μ X)λ μλ―Έ!
: @Deprecated μ λν μ΄μ μ΄ λΆμ νλλ λ©μλλ₯Ό μ¬μ©νλ©΄, μ»΄νμΌ μλ¬ X, ν΄λΉ νλ λλ λ©μλκ° Deprecatedλμλ€λ λ©μμ§λ§!
:λ³΄ν΅ JDK λ²μ μ΄ μ κ·Έλ λ λ λ μ¬μ©!
βοΈ @FunctionallInterface
: 'ν¨μν μΈν°νμ΄μ€'λ₯Ό μ μΈν λ, μ΄ μ λν μ΄μ μ λΆμ΄λ©΄ μ»΄νμΌλ¬κ° 'ν¨μν μΈν°νμ΄μ€'λ₯Ό μ¬λ°λ₯΄κ² μ μΈνλμ§ νμΈνκ³ , μλͺ»λ κ²½μ° μλ¬λ₯Ό λ°μ
(ν¨μν μΈν°νμ΄μ€λ λ·μͺ½μ λμ΄)
βοΈ @SuppressWarnings
: μ»΄νμΌλ¬κ° 보μ¬μ£Όλ κ²½κ³ λ©μμ§κ° λνλμ§ μκ² μ΅μ !
β μΌλ°μ μΌλ‘ μ»΄νμΌλ¬μ κ²½κ³ λ©μμ§λ λͺ¨λ νμΈνκ³ ν΄κ²°ν΄μ μ»΄νμΌ νμ μ΄λ ν λ©μμ§λ λνλμ§ μκ² ν΄μΌν¨! κ·Έλμ μκ³ λ 무μνκ³ μΆμ λ @SuppressWarningsλ₯Ό μ¬μ©!
@SuppressWarnings("deprecation") // deprecation κ΄λ ¨ κ²½κ³ λ₯Ό μ΅μ
public static void main(String args[]) {
...
@SuppressWarnings("unchecked") // μ§λ€λ¦μ€ κ΄λ ¨ κ²½κ³ λ₯Ό μ΅μ
ArrayList<Integer> list = new ArrayList();
}
: @SuppressWarnings({"deprecation", "unchecked"})μ κ°μ΄ νλ²μ μ΅μ κ°λ₯! but ν΄λΉ λμμλ§ μ λν μ΄μ μ λΆμ¬μ κ²½κ³ μ μ΅μ λ²μλ₯Ό μ΅μννλ κ²μ΄ best! → λμ€μ mainν¨μμ μΆκ°λλ μ½λκΉμ§ κ²½κ³ κ° μ΅μ λ μλ μκΈ° λλ¬Έ
ππ»βοΈ μ΅μ ν μ μλ κ²½κ³ λ©μμ§λ μ΄λ€ κ²μ΄ μλμ? (μ£Όλ‘ μ¬μ©λλ κ²)
βοΈ deprecation : @Deprecatedκ° λΆμ λμμ μ¬μ©ν΄μ λ°μνλ κ²½κ³ μ΅μ
βοΈ unchecked : μ§λ€λ¦μ€λ‘ νμ μ μ§μ νμ§ μμμ λ λ°μνλ κ²½κ³ μ΅μ
βοΈ rawtypes : μ§λ€λ¦μ€λ₯Ό μ¬μ©νμ§ μμμ λ°μνλ κ²½κ³ μ΅μ
βοΈ varargs : κ°λ³μΈμμ νμ μ΄ μ§λ€λ¦ νμ μΌ λ λ°μνλ κ²½κ³ λ₯Ό μ΅μ
β μ€λ₯ λ©μμ§μμ λκ΄νΈ [] μμ μλ κ²μ΄ λ©μμ§μ μ’ λ₯
βοΈ @SafeVarargs
: λ©μλμ μ μΈλ κ°λ³μΈμμ νμ μ΄ non-reifiableμΌ κ²½μ°, ν΄λΉ λ©μλλ₯Ό μ μΈ, νΈμΆνλ λΆλΆμμ "unchecked" κ²½κ³ κ° λ°μνλλ°, μ΄ κ²½κ³ λ₯Ό μ΅μ νκΈ° μν΄μ μ¬μ©
ππ»βοΈ non-reifiable?
π©π»π« μ»΄νμΌ ν μ κ±°λλ νμ (ex. T) λ°λλ‘ reifiable νμ μ μ»΄νμΌ ν μ κ±°X. λλΆλΆμ μ§λ€λ¦ νμ μ non-reifiable νμ !
@SafeVarargs // unchecked κ²½κ³ μ΅μ
@SuppressWarnings("varargs") // varargs κ²½κ³ μ΅μ
public static <T> List<T> asList(T... a) {
return new ArrayList<T>(a); // List<Integer> list = asList(new int[3]);
}
β @SafeVarargsμΌλ‘ 'unchecked' κ²½κ³ λ μ΅μ ν μ μμ§λ§ 'varargs' κ²½κ³ λ μ΅μ ν μ μκΈ° λλ¬Έμ λ³΄ν΅ @SuppressWarnings("varargs")μ ν¨κ» μ¬μ©
ππ»βοΈ κ·ΈλΌ κ·Έλ₯ @SuppressWarnings("unchecked")λ₯Ό μ°λ©΄ λμ§ μλμ?
π©π»π« λ©μλμ μ μΈλ κ°λ³μΈμμ νμ μ΄ non-reifiable(ex. μ§λ€λ¦ νμ )μΌ κ²½μ°, ν΄λΉ λ©μλλ₯Ό μ μΈ, νΈμΆνλ λΆλΆμμ "unchecked" κ²½κ³ κ° λ°μ! λ°λΌμ ν΄λΉ λ©μλλ₯Ό μ μΈλΆ + ν΄λΉ λ©μλλ₯Ό νΈμΆν λλ§λ€ @SuppressWarnings("unchecked")λ₯Ό λΆμ¬μ€μΌν¨ but @SafeVarargsλ₯Ό μ¬μ©νλ©΄ μ μΈλΆμλ§ ν΄λΉ μ λν μ΄μ μ λΆμ΄λ©΄ νΈμΆν λ μλμΌλ‘ κ²½κ³ κ° μ΅μ !
π λ©ν μ λν μ΄μ
: μ λν μ΄μ μ μν μ λν μ΄μ , μ λν μ΄μ μ μ μν λ μ λν μ΄μ μ μ μ© λμ(target)μ΄λ μ μ§κΈ°κ°(retention)λ±μ μ§μ νλλ° μ¬μ©
: λ©ν μ λν μ΄μ λ νμ€ μ λν μ΄μ !
βοΈ @Target
: μ λν μ΄μ μ΄ μ μ©κ°λ₯ν λμμ μ§μ νλλ° μ¬μ©
import static java.long.annotation.ElementType.*;
@Target({FIELD, TYPE, TYPE_USE})
public @interface MyAnnotation { ... }
π @TargetμΌλ‘ μ§μ ν μ μλ μ λν μ΄μ μ μ©λμμ μ’ λ₯
βͺ ANNOTATION_TYPE : μ λν μ΄μ
βͺ CONSTRUCTOR : μμ±μ
βͺ FIELD : νλ(λ©€λ²λ³μ, enumμμ)
βͺ LOCAL_VARIABLE : μ§μλ³μ
βͺ METHOD : λ©μλ
βͺ PACKAGE : ν¨ν€μ§
βͺ PARAMETER : λ§€κ°λ³μ
βͺ TYPE : νμ (ν΄λμ€, μΈν°νμ΄μ€, enum)
βͺ TYPE_PARAMETER : νμ λ§€κ°λ³μ
βͺ TYPE_USE : ν°μ μ΄ μ¬μ©λλ λͺ¨λ κ³³
@MyAnnotation // TYPE
class MyClass {
@MyAnnotation // FIELD
int i;
@MyAnnotation // TYPE_USE
MyClass mc
}
βοΈ @Retention
: μ λν μ΄μ μ΄ μ μ§λλ κΈ°κ°μ μ§μ νλλ° μ¬μ©
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface FunctionalInterface { ... }
π @RetentionμΌλ‘ μ§μ ν μ μλ μ λν μ΄μ μ μ§μ μ± μ μ’ λ₯
βͺ SOURCE : μμ€ νμΌμλ§ μ‘΄μ¬ ← μ»΄νμΌλ¬λ₯Ό μ§μ μμ±ν κ²μ΄ μλλ©΄ μ¬μ©ν μΌμ΄ μμ!
βͺ CLASS : ν΄λμ€ νμΌμ μ‘΄μ¬ (κΈ°λ³Έκ°)
βͺ RUNTIME : λ°νμ(μ€ν μ)μλ μ¬μ©κ°λ₯ (by 리νλ μ )
ππ»βοΈ λ¦¬νλμ ?
π©π»π« ν΄λμ€μ λ©μλ, νμ , λ³μλ€μ μ κ·Όν μ μλλ‘ ν΄μ£Όλ μλ° API, Class κ°μ²΄λ₯Ό ν΅ν΄μ μ¬μ©
https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html
Class (Java Platform SE 8 )
Determines if the specified Class object represents a primitive type. There are nine predefined Class objects to represent the eight primitive types and void. These are created by the Java Virtual Machine, and have the same names as the primitive types tha
docs.oracle.com
import static java.long.annotation.ElementType.*;
@Target(TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value();
}
@MyAnnotation("you can put the value here")
class AnnotationResolver {
Class<AnnotationResolver> me = AnnotationResolver.class;
MyAnnotation anno = (MyAnnotation) me.getAnnotation(AnnotationResolver.class);
System.out.println(anno.value()); // you can put the value here
}
β getAnnotations()λ₯Ό ν΅ν΄ λͺ¨λ μ λν μ΄μ μ λ°°μ΄λ‘ λ°μ μλ μμ!
βοΈ @Documented
: μ λν μ΄μ μ λν μ λ³΄κ° javadocμΌλ‘ μμ±ν λ¬Έμμ ν¬ν¨λλλ‘ ν¨
https://www.geeksforgeeks.org/java-documented-annotations/
Java @Documented Annotations - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
www.geeksforgeeks.org
βοΈ @Inherited
: μ λν μ΄μ μ΄ μμ ν΄λμ€μλ μμλλλ‘ νλ κ²
@Inherited
public @interface MyAnnotation { ... }
@MyAnnotation
class MyClass { ... }
class MyLittleClass extends MyClass { ... } // @MyAnnotationμ΄ λΆμ κ²μΌλ‘ μΈμ
βοΈ @Repeatable
: νλμ λμμ κ°μ μ λν μ΄μ μ μ¬λ¬λ² λΆμΌ μ μκ² νλ κ²
@Repeatable(ToDos.class)
public @interface ToDo {
String value();
}
@ToDo("delete test codes")
@ToDo("override inherited method")
class MyClass { ... }
β μΌλ°μ μΈ μ λν μ΄μ κ³Ό λ¬λ¦¬ κ°μ μ΄λ¦μ μ λν μ΄μ μ΄ μ¬λ¬ κ°κ° νλμ λμμ μ μ©λ μ μκΈ° λλ¬Έμ, μ΄ μ λν μ΄μ λ€μ νλλ‘ λ¬Άμ΄μ λ€λ£° μ μλ 컨ν μ΄λ μ λν μ΄μ λ μΆκ°λ‘ μ μν΄μΌν¨! (ex. ToDos.class)
@interface ToDos {
ToDo[] value();
}
βοΈ @Native
: λ€μ΄ν°λΈ λ©μλμ μν΄ μ°Έμ‘°λλ μμ νλμ λΆμ΄λ μ λν μ΄μ
ππ»βοΈ λ€μ΄ν°λΈ λ©μλ?
π©π»π« JVMμ΄ μ€μΉλ OSμ λ©μλ, λ³΄ν΅ CμΈμ΄λ‘ μμ±λλ©° μλ°μμλ λ©μλμ μ μΈλΆλ§ μ μνκ³ κ΅¬νμX, νΈμΆνλ©΄ OSμ λ©μλκ° νΈμΆλ¨
β μ΄κ²μ μν΄μ μλ°μ μ μλ λ€μ΄ν°λΈ λ©μλμ OSμ λ©μλλ₯Ό μ°κ²°ν΄μ£Όλ μμ μ΄ μΆκ°λ‘ νμν¨(JNIμ μν )
π μ λν μ΄μ μ μ§μ λ§λ€μ΄λ³΄μ!
@interface μ λν
μ΄μ
μ΄λ¦ {
νμ
μμμ΄λ¦(); // μ λν
μ΄μ
μ μμ μ μΈ
...
}
βοΈ μ λν μ΄μ μμ
: μ λν μ΄μ λ΄μ μ μΈλ λ©μλ(λ°νκ°O, λ§€κ°λ³μX, μμμ ν΅ν΄ ꡬνν νμX)
: μ λν μ΄μ μ μ¬μ©ν λ μ΄ μμλ€μ κ°μ λΉ μ§μμ΄ μ§μ ν΄μ£Όμ΄μΌν¨! (λν΄νΈκ° μ§μ OK)
π μ λν μ΄μ μμ κ·μΉ
1οΈβ£ μμμ νμ μ κΈ°λ³Έν, String, enum, μ λν μ΄μ , Classλ§ νμ©
2οΈβ£ ()μμ λ§€κ°λ³μ μ μΈX
3οΈβ£ μμΈ μ μΈX
4οΈβ£ μμλ₯Ό νμ λ§€κ°λ³μ(T)λ‘ μ μX
@interface TestInfo {
int count() default 1;
String testedBy();
String[] testTools() default { "JUnit", "AutoTester" };
TestType testType() default TestType.FIRST; // enum TestType { FIRST, FINAL }
DataTime testDate(); //μμ μ΄ μλ λ€λ₯Έ μ λν
μ΄μ
μ ν¬ν¨O
}
@interface DataTime {
String yymmdd();
String hhmmss();
}
@TestInfo(testedBy="aaa",
testDate=@DateTime(yymmdd="230719", hhmmss="010859"))
β μ λν μ΄μ μμκ° νλλΏμ΄κ³ μ΄λ¦μ΄ valueμΈ κ²½μ°, μ λν μ΄μ μ μ¬μ©ν λ μμμ μ΄λ¦μ μλ΅ν΄λ λ¨
@interface TestInfo { String value(); }
@TestInfo("passed")
class MyTest() { ... }
β λͺ¨λ μ λν μ΄μ μ μ‘°μμ Annotationμ. νμ§λ§ μ λν μ΄μ μ μμ νμ©X
β μ λν μ΄μ μ μμκ° νλλ μ μλμ΄ μμ§ μμΌλ©΄ λ§μ»€ μ λν μ΄μ μ΄λΌκ³ ν¨!
'μΈμ΄ > μλ°' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
λλ€(lambda) (0) | 2023.08.06 |
---|---|
μ°λ λ (0) | 2023.07.30 |
μ΄κ±°ν (0) | 2023.07.17 |
μ§λ€λ¦μ€ (0) | 2023.07.14 |
컬λ μ νλ μμ (0) | 2023.07.13 |