Skip to content

Latest commit

ย 

History

History
85 lines (72 loc) ยท 3.67 KB

README.md

File metadata and controls

85 lines (72 loc) ยท 3.67 KB

Reflection

๋ฆฌํ”Œ๋ ‰์…˜์ด๋ž€ ๊ตฌ์ฒด์ ์ธ ํด๋ž˜์Šค ํƒ€์ž…์„ ์•Œ์ง€ ๋ชปํ•˜๋”๋ผ๋„ ๊ทธ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ, ํƒ€์ž…, ๋ณ€์ˆ˜๋“ค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ์ž๋ฐ” API์ž…๋‹ˆ๋‹ค.
์ปดํŒŒ์ผ ์‹œ๊ฐ„์ด ์•„๋‹Œ ์‹คํ–‰ ์‹œ๊ฐ„์— ๋™์ ์œผ๋กœ ํŠน์ • ํด๋ž˜์Šค์˜ ์ •๋ณด๋ฅผ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค.
๋ฆฌํ”Œ๋ ‰์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋Š” ์ •๋ณด๋Š” Class, Constructor, Method, Field ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฆฌํ”Œ๋ ‰์…˜์€ ๋™์ ์œผ๋กœ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•  ๊ฒฝ์šฐ, ์ฆ‰ ์ž‘์„ฑ ์‹œ์ ์—๋Š” ์–ด๋–ค ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ• ์ง€ ๋ชจ๋ฅด์ง€๋งŒ ๋Ÿฐํƒ€์ž„ ์‹œ์ ์—์„œ ๊ฐ€์ ธ์™€ ์‹คํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
ํ”„๋ ˆ์ž„์›Œํฌ๋‚˜ IDE์—์„œ Spring Annotation, IntelliJ ์ž๋™ ์™„์„ฑ ๊ธฐ๋Šฅ ๋“ฑ์˜ ์ด๋Ÿฐ ๋™์  ๋ฐ”์ธ๋”ฉ์„ ์ด์šฉํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

Class.forName("ํด๋ž˜์Šค์ด๋ฆ„")์„ ํ†ตํ•ด ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๊ณ  ํด๋ž˜์Šค์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

public class DoHee {
    public String name;
    public int number;
    public void setDoHee (String name, int number) {
      this.name = name;
      this.number = number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public void sayHello(String name) {
      System.out.println("Hello, " + name);
  }
}
import java.lang.reflect.Method;
import java.lang.reflect.Field;

public class ReflectionTest {
    public void reflectionTest() {
        try {
            Class myClass = Class.forName("DoHee");
            Method[] methods = myClass.getDeclaredMethods();

            /* ํด๋ž˜์Šค ๋‚ด ์„ ์–ธ๋œ ๋ฉ”์„œ๋“œ์˜ ๋ชฉ๋ก ์ถœ๋ ฅ */
            /* ์ถœ๋ ฅ : public void DoHee.setDoHee(java.lang.String,int)
                     public void DoHee.setNumber(int)
                     public void DoHee.sayHello(java.lang.String) */
            for (Method method : methods) {
                System.out.println(method.toString());
            }

            /* ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ๋ฐ˜ํ™˜ ํƒ€์ž… ํ™•์ธ */
            /* ์ถœ๋ ฅ : Class Name : class DoHee
                     Method Name : setDoHee
                     Return Type : void */
            Method method = methods[0];
            System.out.println("Class Name : " + method.getDeclaringClass());
            System.out.println("Method Name : " + method.getName());
            System.out.println("Return Type : " + method.getReturnType());

            /* ์ถœ๋ ฅ : Param Type : class java.lang.String
                     Param Type : int */
            Class[] paramTypes = method.getParameterTypes();
            for(Class paramType : paramTypes) {
                System.out.println("Param Type : " + paramType);
            }

            /* ๋ฉ”์„œ๋“œ ์ด๋ฆ„์œผ๋กœ ํ˜ธ์ถœ */
            Method sayHelloMethod = myClass.getMethod("sayHello", String.class);
            sayHelloMethod.invoke(myClass.newInstance(), new String("DoHee")); // ์ถœ๋ ฅ : Hello, DoHee

            /* ๋‹ค๋ฅธ ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„ ํ•„๋“œ์˜ ๊ฐ’ ์ˆ˜์ • */
            Field field = myClass.getField("number");
            DoHee obj = (DoHee) myClass.newInstance();
            obj.setNumber(5);
            System.out.println("Before Number : " + field.get(obj)); // ์ถœ๋ ฅ : Before Number : 5
            field.set(obj, 10);
            System.out.println("After Number : " + field.get(obj)); // ์ถœ๋ ฅ : After Number : 10
        } catch (Exception e) {
            // Exception Handling
        }
    }

    public static void main(String[] args) {
        new ReflectionTest().reflectionTest();
    }
}