시소당
Reflection : 객체를 통해 클래스의 정보를 분석해 내는 프로그램 기법
Java는 Class정보 자체를 인스턴스화 하여 메모리에 적재한 뒤, 그 메타 정보를 실시간으로 이용할 수 있게 한다. 클래스 정보 자체를 정의한 자바클래스가 바로 java.lang.Class입니다
java.lang.Class 의 인스턴스(즉, new)는 ClassLoader라는 또다른 클래스 인스턴스에 의해
생겨난다. ClassLoader는 특정 위치(classpath라고도 함) 배열(여러 classpath가 하나의 ClassLoader에 매핑될 수 있는 이유)에 매핑되어 그 위치에 존재하는 클래스 정의 파일들(*.class)을 읽어서 메모리에 동적으로(실시간 로딩 요청에 따라) 적재한다.
자바는 클래스정보를 인스턴스화하여 가질 수 있다 보니 리플렉션(reflection)이라는 개념이 가능하다. 리플렉션은 원래 인터프리터언어(tcl/tk, php, perl, python등)에서 발달한 개념인데, 자바 가상머신도 이렇듯 클래스 메타 정보를 메모리에 가지고 있는 반-인터프리터 언어이기 때문에 리플렉션이 가능하다.
리플렉션은, 객체의 필드명, 메소드명...등 C언어등의 컴파일언어에서의 obj에는 주소값으로밖에 남아있지 않은 정보를... 프로그램 수행시간중에 얻을 수 있게 하는 기능이다.
컴파일 언어에서는 당연히 이런것이 불가능하다.
그러나 VM이 클래스 정보를 메모리에 들고 있으므로 메소드명, 필드명...등을 알아낼 수 있는 것이다
-> 역방향 자바 프로그래밍
: 만약 반대로 객체의 메모리만을 알고 있고, 그 객체의 형을 모른다면???
모든 객체는 Object 클래스를 상속하므로 기본적으로 Object형의 객체로 인식할 수 있지만
그 클래스 내부의 메소드에 접근할 수가 없다.
!!! Reflection 기법으로 클래스의 형을 모르는 상태에서도 메소드에 접근할 수 있다!!!
1. Class 클래스 : 클래스의 정보를 분석하기 위한 도구.
.class 파일의 등록 클래스, 바이트 코드의 등록 클래스로 표현할 수 있다.
2. getMethod()를 이용하여 해당 클래스의 메소드 정보를 알아낼 수 있다.
리턴 값은 Method형의 배열
Data라는 class 파일이 있다고 하면...
Class c = Data.class;
Method[] m = c.getMethod();
메소드가 여러 개 존재할 수 있기 때문에 배열 형태가 된다.
* Method라는 클래스는 메소드에 관련된 작업을 할 수 있는 클래스이다.
3. getField()를 이용하여 해당 클래스의 멤버 필드 정보를 알아낼 수있다.
리턴 값은 Field형의 배열
역시 Data라는 class 파일이 있다고 하면...
Class c = Data.class;
Field[] f = c.getField();
* Field 클래스는 멤버 필드에 대한 작업을 할 수 있도록 고안된 클래스이다.
4. getConstructors()를 이용하여 해당 클래스의 생성자 정보를 알아낼 수 있다.
리턴 값은 Constructor형의 배열
Class c = Data.class;
Constructor[] con = c.getConstructors();
5. getInterfaces()/getSuperClass()를 이용, 클래스의 상속구조에 대한 정보를 알아 낼 수 있다.
리턴 값은 Class[] getInterfaces() / Class getSuperClass()
Class c = Data.class;
Class[] interfaces = c.getInterfaces();
Class superClass = c.getSuperClass();
인터페이스나 상위 클래스의 경우에는 얻을 수 있는 것 자체가 클래스이다.
6. Class 클래스는 java.lang 팩키지
Method, Field, Constructor는 java.lang.reflect 팩키지
예) Integer 클래스의 등록정보 알아내기
import java.lang.reflect.*;
public class ShowClassInfoMain {
public static void main(String[] args) {
Class c = Integer.class;
Class[] iface = c.getInterfaces();
Constructor[] con = c.getConstructors();
Method[] m = c.getMethods();
Field[] f = c.getFields();
Class temp = c;
System.out.println("****Integer class Information****");
System.out.println("*******************************");
System.out.println("SuperClass --->>>");
while((temp=temp.getSuperclass()) != null) {
System.out.println(temp);
}
System.out.println();
System.out.println("Interface --->>>");
for(int i=0; i<iface.length; i++) {
System.out.println("interface[" + i + "] : " + iface[i]);
}
System.out.println();
System.out.println("Constructor --->>>");
for(int i=0; i<con.length; i++) {
System.out.println("Constructor[" + i + "] : " + con[i]);
}
System.out.println();
System.out.println("Method --->>>");
for(int i=0; i<m.length; i++) {
System.out.println("Method[" + i + "] : " + m[i]);
}
System.out.println();
System.out.println("Member Field --->>>");
for(int i=0; i<f.length; i++) {
System.out.println("Field[" + i + "] : " + f[i]);
}
}
}
출처 : jabook.org
[출처] 객체를 통해 클래스의 정보를 분석해 내는 프로그램 기법 Reflection :Class |작성자 현지기
http://blog.naver.com/galhan?Redirect=Log&logNo=100046364299