java.lang.reflect.Field ํด๋์ค
Field ํด๋์ค ๋ ํด๋์ค ๋๋ ์ธํฐํ์ด์ค์ ๋จ์ผ ํ๋์ ๋ํ ์ ๋ณด ๋ฐ ๋์ ์ก์ธ์ค๋ฅผ ์ ๊ณตํฉ๋๋ค. Field๋ get ๋๋ set ์ก์ธ์ค ์์ ์ค์ ํ์ฅ ์ ํ ๋ณํ์ ํ์ฉํ์ง๋ง ์ถ์๊ฐ ๋ฐ์ํ๋ฉด IllegalArgumentException์ด ๋ฐ์ํฉ๋๋ค.
Field ๊ฐ์ฒด๋ฅผ ์ป์ผ๋ ค๋ฉด ๋จผ์ ํด๋์ค๋ฅผ ์์ฑํฉ๋๋ค.
public class Person {
private String name;
private int age;
public boolean isMale;
protected String address;
public static final int MAX_AGE = 120;
public static final int MIN_AGE = 0;
}
๋ค์์ ํด๋น ํด๋์ค๋ก ์์ ํ๊ธฐ ์ํ ์ฝ๋์ ๋๋ค.
public class Main {
public static void main(String[] args) {
Field[] fields = Person.class.getDeclaredFields();
List<Field> actualFields = getFieldNames(fields);
System.out.println(actualFields);
}
static List<Field> getFieldNames(Field[] fields) {
return List.of(fields);
}
}
์ด๊ฒ์ด ๋์ค์ ์์ ํ ํด๋์ค์ ํ๋ ๋ชฉ๋ก์ ์ป๋ ๋ฐฉ๋ฒ์ ๋๋ค. ๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
์ด์ ์ด ๋ฐ์ดํฐ๋ก ๋ฌด์์ ํ ์ ์๋์ง ์์๋ด ์๋ค. Field ํด๋์ค ์ ๋ฉ์๋์ ๋ํด ์ด์ผ๊ธฐํด ๋ด ์๋ค .
๋ฐฉ๋ฒ | ์ค๋ช |
---|---|
getType() | ์ด ํ๋๊ฐ ๋ํ๋ด๋ ํ๋์ ์ ์ธ๋ ์ ํ์ ์๋ณํ๋ ํด๋์ค ๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค.ํ๋๋ฌผ์ฒด. |
getAnnotatedType() | ๋ฐํ์ฃผ์ ์ ํ์ด Field๊ฐ ๋ํ๋ด๋ ํ๋์ ์ ์ธ๋ ์ ํ์ ์ง์ ํ๊ธฐ ์ํด ์ ํ์ ์ฌ์ฉ์ ๋ํ๋ด๋ ๊ฐ์ฒด์ ๋๋ค. |
getGenericType() | ๋ฐํ์ ํthis๊ฐ ๋ํ๋ด๋ ํ๋์ ์ ์ธ๋ ์ ํ์ ๋ํ๋ด๋ ๊ฐ์ฒดํ๋๋ฌผ์ฒด. |
getName() | ์ด ํ์๋๋ ํ๋์ ์ด๋ฆ์ ๋ฐํํฉ๋๋ค.ํ๋๋ฌผ์ฒด. |
getModifiers() | ์ด ํญ๋ชฉ์ด ๋ํ๋ด๋ ํ๋์ ๋ํ Java ์ธ์ด ์์ ์๋ฅผ ์ธ์ฝ๋ฉํ๋ int๋ฅผ ๋ฐํํฉ๋๋ค.ํ๋๋ฌผ์ฒด. |
getAnnotations() | ์ด ํ๋์ ๋ํ ์ฃผ์์ ๋ฐํํฉ๋๋ค. ์ฃผ์์ด ์์ผ๋ฉด ๋น ๋ฐฐ์ด์ ๋ฐํํฉ๋๋ค. |
getType(), getName() ๋ฐ getModifiers() ๋ฉ์๋
getType() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ํ๋์ ์ ํ์ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค. ๋ฐฉ๋ฒ์ ์์ฑํด ๋ด ์๋ค:
static void printTypes(List<Field> fields){
fields.forEach(e -> System.out.println(e.getType()));
}
๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
int
๋ถ์ธ
ํด๋์ค java.lang.String
int
int
์ด์ ํด๋์ค์ ํ๋ ์ด๋ฆ์ ๊ฐ์ ธ์ค๋ ๋ฉ์๋๋ฅผ ์ถ๊ฐํด ๋ณด๊ฒ ์ต๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด ํด๋์ค์ ํ๋๋ฅผ ๋ ์ฝ๊ฒ ํ์ํ ์ ์์ต๋๋ค.
static void printTypesAndNames(List<Field> fields){
fields.forEach(e -> System.out.printf("Field type - %s\nField name - %s\n\n", e.getType(), e.getName()));
}
์ด์ ๊ฒฐ๊ณผ๊ฐ ๋ ์ดํดํ๊ธฐ ์ฝ์ต๋๋ค.
ํ๋ ์ด๋ฆ - name
ํ๋ ์ ํ - int
ํ๋ ์ด๋ฆ - age
ํ๋ ์ ํ - boolean
ํ๋ ์ด๋ฆ - isMale
ํ๋ ์ ํ - class java.lang.String
ํ๋ ์ด๋ฆ - address
ํ๋ ์ ํ - int
ํ๋ ์ด๋ฆ - MAX_AGE
ํ๋ ์ ํ - int
ํ๋ ์ด๋ฆ - MIN_AGE
์์ฒญ๋! ๋ฐฉ๋ฒ์ ์ข ๋ ์์ ํด ๋ด ์๋ค! ์ฌ๊ธฐ์ ์ก์ธ์ค ์์ ์๋ฅผ ์ถ๊ฐํฉ๋๋ค.
static void printFieldInfo(List<Field> fields){
fields.forEach(e -> System.out.printf("Field type - %s\nField name - %s\nModifiers - %s\n\n", e.getType(), e.getName(), Modifier.toString(e.getModifiers())));
}
๊ทธ๋ฆฌ๊ณ e.getModifiers() ๊ฐ ๋ฐํํ๋ ๊ฒ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค . ์ด ๋ฉ์๋๋ ํ๋์ ์ก์ธ์ค ํ์ ์์ ์ํ๋ฅผ ๊ฒฐ์ ํ ์ ์๋ int๋ฅผ ๋ฐํํฉ๋๋ค. Modifier ํด๋์ค์๋ ํ๋์ ๊ฐ ํน์ ์์ ์๋ฅผ ๋ด๋นํ๋ ์ ์ ๋ณ์๊ฐ ํฌํจ๋์ด ์์ต๋๋ค .
![](https://cdn.codegym.cc/images/article/3f20ce0b-dbef-450b-b6d1-31324ee45efa/800.jpeg)
๋ฐํ ๊ฐ์ Modifier.toString() ์ ๋ํ ํ๊ณ ์ฆ์ ๊ฐ์ ํ ์คํธ๋ก ๊ฐ์ ธ์ต๋๋ค.
ํ๋ ์ด๋ฆ - ์ด๋ฆ
์์ ์ - ๊ฐ์ธ
ํ๋ ์ ํ - int
ํ๋ ์ด๋ฆ - ๋์ด
์์ ์ - ๊ฐ์ธ
ํ๋ ์ ํ - ๋ถ์ธ
ํ๋ ์ด๋ฆ - isMale
์์ ์ - ๊ณต๊ฐ
ํ๋ ์ ํ - ํด๋์ค java.lang.String
ํ๋ ์ด๋ฆ - ์ฃผ์
์์ ์ - ๋ณดํธ๋
ํ๋ ์ ํ - int
ํ๋ ์ด๋ฆ - MAX_AGE
์์ ์ - public static final
ํ๋ ์ ํ - int
ํ๋ ์ด๋ฆ - MIN_AGE
์์ ์ - public static final
Modifier.toString() ์ด ์๋ ๋ชจ์ต์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค .
ํ๋ ์ด๋ฆ - ์ด๋ฆ
์์ ์ - 2
ํ๋ ์ ํ - int
ํ๋ ์ด๋ฆ - ์ฐ๋ น
์์ ์ - 2
ํ๋ ์ ํ - ๋ถ์ธ
ํ๋ ์ด๋ฆ - isMale
์์ ์ - 1
ํ๋ ์ ํ - ํด๋์ค java.lang.String
ํ๋ ์ด๋ฆ - ์ฃผ์
์์ ์ - 4
ํ๋ ์ ํ - int
ํ๋ ์ด๋ฆ - MAX_AGE
์์ ์ - 25
ํ๋ ์ ํ - int
ํ๋ ์ด๋ฆ - MIN_AGE
์์ ์ - 25
getAnnotations(), getAnnotatedType() ๋ฐ getGenericType() ๋ฉ์๋
์ด๋ฌํ ๋ฉ์๋์ ํจ๊ป ์๋ํ๋๋ก Person ํด๋์ค๋ฅผ ์์ ํด ๋ณด๊ฒ ์ต๋๋ค . ์์ฒด ์ฃผ์์ ์์ฑํ์ฌ ํ๋์ ์ ์ฉํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ ํ๋๋ฅผ ๋ช ๊ฐ ๋ ์ถ๊ฐํฉ๋๋ค.
๋ ๊ฐ์ ์ฃผ์์ ๋ง๋ค์ด ๋ณด๊ฒ ์ต๋๋ค. Pig Latin์ ๋ณ์ ์ด๋ฆ์ ํ๋์ ์ ๋ฌํ๊ณ ๋ ๋ฒ์งธ๋ ์์์ ์ฌ์ฉํฉ๋๋ค.
@Target(value=ElementType.FIELD)
@Retention(value= RetentionPolicy.RUNTIME)
public @interface Name {
String name();
}
@Target({ ElementType.TYPE_USE })
@Retention(RetentionPolicy.RUNTIME)
public @interface Number {
}
๊ทธ๋ฆฌ๊ณ ๋ฉ์ธ ํด๋์ค์ Person ํด๋์ค๋ฅผ ๋ณ๊ฒฝํฉ๋๋ค .
public class Person {
@Name(name = "Ame-nay")
private String name;
@Name(name = "User nicknames")
List<String> nicknames;
private final Class<Object> type;
private int @Number[] number;
public Person(Class<Object> type) {
this.type = type;
}
}
public static void main(String[] args) {
Field[] fields = Person.class.getDeclaredFields();
List<Field> actualFields = getFieldNames(fields);
printAdditionalInfo(actualFields);
}
static void printAdditionalInfo(List<Field> fields) {
System.out.println("\ngetAnnotatedType:");
fields.forEach(e -> System.out.println(e.getAnnotatedType()));
System.out.println("\ngetGenericType:");
fields.forEach(e -> System.out.println(e.getGenericType()));
System.out.println("\ngetAnnotations:");
fields.forEach(e -> System.out.println(Arrays.toString(e.getAnnotations())));
}
์ฐ๋ฆฌ ๋ฐฉ๋ฒ์ ๊ฒฐ๊ณผ๋ฅผ ์ดํด๋ณด๊ณ ๊ทธ๊ฒ์ด ๋ฌด์์ ์ํ ๊ฒ์ธ์ง ์์๋ผ ๋์ ๋๋ค.
java.lang.Class<java.lang.Object>
java.util.List<java.lang.String>
java.lang.String
int @Number()[]
getGenericType:
java.lang.Class<java.lang. ๊ฐ์ฒด>
java.util.List<java.lang.String>
class java.lang.String
class [I
getAnnotations:
[]
[@Name(name="\u0055\u0073\u0065\u0072\u0020\u006e\u0069\u0063 \u006b\u006e\u0061\u006d\u0065\u0073")]
[@์ด๋ฆ(์ด๋ฆ="\u0041\u006d\u0065\u002d\u006e\u0061\u0079")] [
]
-
getAnnotatedType์ ์ฃผ์ด์ง ํ๋์ ๋ํ ์ฃผ์์ ๋ฐํํฉ๋๋ค(์๋ ๊ฒฝ์ฐ). ํ๋์ ๋ํ ์ฃผ์์ ์ป์๊ณ ์๋ฒฝํ๊ฒ ๋ณผ ์ ์์ต๋๋ค.
-
getGenericType์ ์ฌ์ฉํ๋ฉด ๋งค๊ฐ๋ณ์ํ๋ ์ ํ์ ์ฌ๋ฐ๋ฅด๊ฒ ํ์ํ ์ ์์ต๋๋ค.
-
getAnnotations๋ ๊ฐ์ฒด์ ์ ์ฉ๋ ์ฃผ์์ ๋ฐํํฉ๋๋ค.
์ด๊ฒ์ ํด๋์ค์ ๊ฐ ํ๋์ ๋ํ ๋ชจ๋ ๋ฐ์ดํฐ์ ์ก์ธ์ค ์์ ์, ์ฃผ์ ๋ฐ ๋ฐ์ดํฐ ์ ํ์ ์ฝ๊ฒ ์ป์ ์ ์๋ ๋ฐฉ๋ฒ์ ๋๋ค.
java.lang.reflect.Method ํด๋์ค
๊ฐ๋ ์! ์ฐ๋ฆฌ๋ ์ฐ๋ฆฌ ์์ ์ ๋ถ์ผ์ ๋ํด ์ด์ผ๊ธฐํ์ต๋๋ค. ์ด์ ๋ฐฉ๋ฒ์ ๋ํด ์ด์ผ๊ธฐํ ์๊ฐ์ ๋๋ค.
Method ๊ฐ์ฒด๋ฅผ ์ป์ผ๋ ค๋ฉด getMethod ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ๋ฉ์๋ ์ด๋ฆ์ ์ ๋ฌํฉ๋๋ค. ์ด๊ฒ์ Method ๊ฐ์ฒด๋ฅผ ์ป๋ ๊ธฐ๋ณธ ๋ฐฉ๋ฒ์ ๋๋ค .
Method getNameMethod = Person.class.getMethod("getName");
์ฐ๋ฆฌ๋ ๊ณ์ํด์ ์ฐ๋ฆฌ ํ๊ธ๊ณผ ํจ๊ป ์ผํ ๊ฒ์ ๋๋ค. getter ๋ฐ setter, hashCode, equals ๋ฐ toString ๋ฉ์๋๋ฅผ ์ถ๊ฐํด ๋ณด๊ฒ ์ต๋๋ค .
public class Person {
private String name;
private int age;
public boolean isMale;
protected String address;
public static final int MAX_AGE = 120;
public static final int MIN_AGE = 0;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isMale() {
return isMale;
}
public void setMale(boolean male) {
isMale = male;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", isMale=" + isMale +
", address='" + address + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age && isMale == person.isMale && Objects.equals(name, person.name) && Objects.equals(address, person.address);
}
@Override
public int hashCode() {
return Objects.hash(name, age, isMale, address);
}
}
์ด์ Method ํด๋์ค ๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒ์ฌํ ์ผ๋ จ์ ๋ฉ์๋๋ฅผ ์ค๋นํ๊ฒ ์ต๋๋ค . ๋ค์์ ๊ฐ์ฅ ์ค์ํ ๋ฐฉ๋ฒ ๋ชฉ๋ก์ ๋๋ค.
๋ฐฉ๋ฒ | ์ค๋ช |
---|---|
getName() | ๋ฉ์๋์ ์ด๋ฆ์ ๋ฐํํฉ๋๋ค. |
getModifiers() | ์ด ๋ฉ์๋์ ์ก์ธ์ค ํ์ ์๋ฅผ ๋ฐํํฉ๋๋ค. |
getReturnType() | ๋ฉ์๋์ ๋ฐํ ์ ํ์ ๋ฐํํฉ๋๋ค. |
getGenericReturnType() | ์ ๋ค๋ฆญ ๋ฉ์๋๋ฅผ ์ค๋ช ํ๋ ๋ฉ์๋์ ๋ฐํ ์ ํ์ ๋ฐํํฉ๋๋ค. |
getParameterTypes() | ๋ฉ์๋ ๋งค๊ฐ๋ณ์์ ๋ฐฐ์ด์ ๋ฐํํฉ๋๋ค. |
getGenericParameterTypes() | ์ ๋ค๋ฆญ ๋ฉ์๋๋ฅผ ์ค๋ช ํ๋ ๋ฉ์๋ ๋งค๊ฐ ๋ณ์ ๋ฐฐ์ด์ ๋ฐํํฉ๋๋ค. |
getExceptionTypes() | ๋ฉ์๋๊ฐ throwํ ์ ์๋ ์์ธ๋ฅผ ๋ฐํํฉ๋๋ค. |
getGenericExceptionTypes() | ๋งค๊ฐ๋ณ์ํ๋ ์ ํ์ ๊ณ ๋ คํ์ฌ ๋ฉ์๋๊ฐ throwํ ์ ์๋ ์์ธ๋ฅผ ๋ฐํํฉ๋๋ค. |
getAnnotations() | ๋ถ๋ชจ ์ฃผ์์ ํฌํจํ์ฌ ๋ฉ์๋์ ๋ํ ์ฃผ์์ ๋ฐํํฉ๋๋ค. |
getDeclaredAnnotations() | ๋ถ๋ชจ ์ฃผ์์ ์ ์ธํ ๋ฉ์๋์ ์ฃผ์์ ๋ฐํํฉ๋๋ค. |
ํด๋์ค์ ๋ฉ์๋ ๋ฐฐ์ด์ ์ป์ผ๋ ค๋ฉด ๋ค์ ๋ฉ์๋๋ฅผ ํธ์ถํ ์ ์์ต๋๋ค.
Method[] methods = Person.class.getDeclaredMethods();
ํด๋์ค์ ๋ชจ๋ ๋ฉ์๋๋ฅผ ์ ๊ณตํฉ๋๋ค.
getName() ๋ฐ getModifiers() ๋ฉ์๋
getName์ ์ฌ์ฉํ์ฌ ๊ฐ ๋ฉ์๋์ ์ด๋ฆ์ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค .
static List<String> getMethodsName(Method[] methods) {
return Arrays.asList(methods)
.stream()
.map(Method::getName)
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
}
์ด์ ์์ ์๋ฅผ ๊ฐ์ ธ์ค๊ธฐ ์ํด getModifiers๋ฅผ ์ฌ์ฉํ๋ ๋ฉ์๋๋ฅผ ์์ฑํด ๋ณด๊ฒ ์ต๋๋ค .
static List<String> getModifiers(Method[] methods) {
return Arrays
.stream(methods)
.map(Method::getModifiers)
.map(String::valueOf)
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
}
์ฃผ์ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค .
public static void main(String[] args) {
Method[] methods = Person.class.getDeclaredMethods();
System.out.println(getMethodsName(methods));
System.out.println(getModifiers(methods));
}
์ฐ๋ฆฌ์ ๊ฒฐ๊ณผ:
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
๋ชจ๋ ๋ฉ์๋์๋ public ํ์ ์๊ฐ ์์ผ๋ฏ๋ก ๋ง์ง๋ง ๋ฉ์๋๋ 1์ ๋ฐฐ์ด์ ๋ฐํํฉ๋๋ค. ์ฝ๋๋ฅผ ์์ ํ๋ฉด ์์ ์ ์์ฒด๊ฐ ํ์๋ฉ๋๋ค.
public static void main(String[] args) {
Method[] methods = Person.class.getDeclaredMethods();
System.out.println(getMethodsName(methods));
System.out.println(modifyModifiers(getModifiers(methods)));
}
๊ณต๊ฐ, ๊ณต๊ฐ, ๊ณต๊ฐ, ๊ณต๊ฐ, ๊ณต๊ฐ, ๊ณต๊ฐ, ๊ณต๊ฐ, ๊ณต๊ฐ, ๊ณต๊ฐ, ๊ณต๊ฐ, ๊ณต๊ฐ]
getReturnedType()
์ด ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ์๋์ ๋ฐํ ์ ํ์ ์ป์ ์ ์์ต๋๋ค.
static void getReturnedType(Method[] methods) {
Arrays.stream(methods)
.map(Method::getReturnType)
.forEach(System.out::println);
}
๋ถ์ธ
ํด๋์ค java.lang.String
int
void
ํด๋์ค java.lang.String
๋ถ์ธ
int
void
void
void
getGenericReturnType()
Person ํด๋์ค์ ๋งค๊ฐ๋ณ์ํ๋ ์ ํ์ผ๋ก ๋ํ๋ ์ ํ์ ๋ฐํํ๋ ๋ฉ์๋๋ฅผ ์ ๊ณต ํ๊ณ ๋ฐํ ๊ฐ์ ๊ฐ์ ธ์ค๋๋ก ํฉ์๋ค.
public List<String> someMethod() {
// Very useful and important method
return null;
}
๊ทธ๋ฆฌ๊ณ ์ฐ๋ฆฌ๋ ์ฐ๋ฆฌ์ ์ฃผ์ ๋ฐฉ๋ฒ์ ์ ๋ฐ์ดํธํ ๊ฒ์ ๋๋ค:
static void getGenericReturnType(Method[] methods) {
Arrays.stream(methods)
.map(Method::getGenericReturnType)
.forEach(System.out::println);
}
์ฐ๋ฆฌ ๋ฐฉ๋ฒ์ ๊ฒฐ๊ณผ:
๋ถ์ธ
ํด๋์ค java.lang.String
int
void
ํด๋์ค java.lang.String
๋ถ์ธ
int
void
void
void
java.util.List<java.lang.String>
getParameterTypes() ๋ฐ getGenericParameterTypes() ๋ฉ์๋
์ฐ๋ฆฌ๋ ๊ณ์ํด์ Person ํด๋์ค๋ฅผ ์์ ํ์ฌ ๋ ๊ฐ์ง ๋ฉ์๋๋ฅผ ๋ ์ถ๊ฐํฉ๋๋ค.
public List<String> someMethod(List<String> list, String s) {
// Very useful and important method
return null;
}
์ฒซ ๋ฒ์งธ๋ ๋ฉ์๋์ ๋งค๊ฐ๋ณ์๋ฅผ ๊ฐ์ ธ์ค๊ณ ๋ ๋ฒ์งธ๋ ๋งค๊ฐ๋ณ์ํ๋ ์ ํ๋ ์ ๊ณตํฉ๋๋ค.
static void getParameterTypes(Method[] methods) {
Class<?>[] types = method.getParameterTypes();
for (Class<?> type : types) {
System.out.println(type);
}
}
static void getGenericParameterTypes(Method[] methods) {
Type[] types = method.getGenericParameterTypes();
for (Type type : types) {
System.out.println(type);
}
}
์ฐ๋ฆฌ๋ ์ฐ๋ฆฌ์ ๋ฐฉ๋ฒ ์ค ํ๋๋ง ์ก์ธ์คํ ๊ฒ์ ๋๋ค. ํน์ ์ด๋ฆ์ผ๋ก ๋ฉ์๋์ ์ก์ธ์คํ๋ ค๋ฉด getMethod๋ฅผ ํธ์ถ ํ๊ณ ์ํ๋ ๋ฉ์๋์ ์ด๋ฆ๊ณผ ๋งค๊ฐ ๋ณ์๋ฅผ ์ ๋ฌํฉ๋๋ค.
public static void main(String[] args) throws NoSuchMethodException {
Method currentMethod = Person.class.getMethod("someMethod", List.class, String.class);
getParameterTypes(currentMethod);
System.out.println();
getGenericParameterTypes(currentMethod);
}
์ฝ๋๋ฅผ ์คํํ๋ฉด ๋ฉ์๋๊ฐ ์ด๋ป๊ฒ ๋ค๋ฅธ์ง, ๋ฐํ๋๋ ๋ด์ฉ์ด ๋ฌด์์ธ์ง ํ์ธํ ์ ์์ต๋๋ค.
ํด๋์ค java.lang.String
java.util.List<java.lang.String>
ํด๋์ค java.lang.String
getExceptionTypes() ๋ฐ getGenericExceptionTypes() ๋ฉ์๋
์ด๋ฌํ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ฉ์๋๊ฐ ๋์ง ์ ์๋ ์์ธ ๋ฐฐ์ด๊ณผ ๋งค๊ฐ๋ณ์ํ๋ ์ ํ์ด ์๋ ์์ธ(์๋ ๊ฒฝ์ฐ)๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค. ์จ๊ฒจ์ง ์ ์ ํด๋์ค๊ฐ ์๋ ์ ์์ ๋ฅผ ์ฌ์ฉํ๊ฒ ์ต๋๋ค.
private static class Processor {
private void init() {}
private void process() throws IOException {}
}
๊ทธ๋ฆฌ๊ณ Processor ํด๋์ค ์์ ๋ฉ์๋๋ฅผ ํธ์ถํฉ๋๋ค .
public static void main(String... args) throws NoSuchMethodException {
Method method = Processor.class.getDeclaredMethod("process");
Type[] type = method.getExceptionTypes();
System.out.println(Arrays.toString(type));
}
์ด์ ์์ธ๋ฅผ ๋ณผ ์ ์์ต๋๋ค.
์ด์ ์ ํ์ ๋งค๊ฐ๋ณ์ํํ๊ฒ ์ต๋๋ค. ๊ธฐ๋ณธ ํด๋์ค๋ฅผ ์์ ํฉ๋๋ค.
private static class Processor<E extends IOException> {
private void process() throws E {
}
}
๊ทธ๋ฆฌ๊ณ ์ฃผ์ ๋ฐฉ๋ฒ ์ ์ฝ๋ :
public static void main(String... args) throws NoSuchMethodException {
Method m = Processor.class.getDeclaredMethod("process");
Type[] t = m.getGenericExceptionTypes();
System.out.println(Arrays.toString(t));
for (Type type : t) {
if (type instanceof TypeVariable) {
for (Type type1 : ((TypeVariable) type).getBounds()) {
System.out.println(type1);
}
}
}
}
์ด ๋ฉ์๋ ์์๋ ์ ํ ๋ณ์์ ๋ํ ์ผ๋ฐ ๋ถ๋ชจ ์ธํฐํ์ด์ค์ธ TypeVariables ๊ฐ์ฒด๊ฐ ์์ต๋๋ค . ๊ทธ ์์ ์ด์ ๋ด๋ถ ๋งค๊ฐ๋ณ์, ์ฆ ์ค์ฒฉ ์์ธ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.
ํด๋์ค java.io.IOException
getAnnotations() ๋ฐ getDeclaredAnnotations() ๋ฉ์๋
์ด ์ ํด๋์ค๋ฅผ ๊ณ์ ์ฌ์ฉํ๊ณ ๋ช ๊ฐ์ง ์ฃผ์์ ์ถ๊ฐํด ๋ณด๊ฒ ์ต๋๋ค. ์์ฒด Annotation ์ฃผ์์ ์์ฑํฉ๋๋ค .
@Retention(RetentionPolicy.RUNTIME)
@interface Annotation {
public String key();
public String value();
}
๊ทธ๋ฆฌ๊ณ ๊ทธ๊ฒ์ ์ฐ๋ฆฌ์ ๋ฐฉ๋ฒ์ ์ ์ฉํ์ญ์์ค:
@Annotation(key = "key", value = "value")
private void process() throws E{
}
๋ฌผ๋ก ๋ชจ๋ ์ฃผ์์ ํ์ํ๋ ๋ฐฉ๋ฒ์ ์ถ๊ฐํ ๊ฒ์ ๋๋ค.
static void getMethodAnnotations(Class<?> clazz) {
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
System.out.println(method.getName());
System.out.println(Arrays.toString(method.getAnnotations()));
System.out.println();
}
}
์ฃผ์ ๋ฐฉ๋ฒ ๊ตฌํ :
public static void main(String... args) {
Class clazz = Processor.class;
getMethodAnnotations(clazz);
}
๊ฒฐ๊ณผ ํ๋ฉด ์ถ๋ ฅ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
[@com.company.Main&Annotation(key="key", value="value")]
์ด๊ฒ์ด ๋ฉ์๋์ ์ ์ฉ๋ ์ฃผ์์ ๊ฐ์ ธ์ฌ ์ ์๋ ๋ฐฉ๋ฒ์ด๋ฉฐ getAnnotations ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ํด๋์ค์ ์์ ์ฃผ์์๋ ์ก์ธ์คํ ์ ์์ต๋๋ค.
์ค๋ ์ฐ๋ฆฌ๋ ๋ฆฌํ๋ ์ ์ด ๋ฉ์๋ ๋ฐ ํ๋์ ํจ๊ป ์๋ํ๋ ๋ฐฉ์๊ณผ ์ด๋ฅผ ํตํด ์ป์ ์ ์๋ ๋ฐ์ดํฐ์ ๋ํด ์๊ฒ ๋์์ต๋๋ค!
GO TO FULL VERSION