아래와 같은 정보들을 가져올 수 있으며 해당 정보들을 가져와서 객체를 생성하거나 메서드를 호출하거나 변수의 값을 변경할 수 있다.
package test;
public class Animal {
public String name = "myName ?";
private String city = "mycity ?";
public Animal() {
}
private void sleep() {
System.out.println("sleep");
}
private void eat(String x) {
System.out.println("eat: " + x);
}
private void walk() {
System.out.println("walk");
}
}
package test;
import test.Animal;
public class Dog extends Animal {
private String myName = "뽀삐";
public String myCity = "서울";
public Dog() {
}
private Dog(String myName) {
this.myName = myName;
}
private void myName(String name) {
System.out.println("myName : " + name);
}
private void myCity(String city) {
System.out.println("myCity : " + city);
}
private void hello() {
System.out.println("hello~");
}
}
package test;
public class Main {
public static void main(String[] args) {
}
}
클래스 Class 객체는 클래스 또는 인터페이스를 가리킨다. java.lang.Class이며 import를 하지 않고 사용할 수 있다.
Case01 - class를 알고 있다는 전제
public class Main {
public static void main(String[] args) {
Class cls = Dog.class;
System.out.println("Class Name : " + cls.getName());
// Class Name : test.Dog
}
}
Case02 - class를 참조할 수 없고 이름만 알고 있는 상황
public class Main {
public static void main(String[] args) throws Exception {
Class cls = Class.forName("test.Dog");
System.out.println("Class Name : " + cls.getName());
// Class Name : test.Dog
}
}
Case01 - 인자가 없는 생성자 가져오기
import java.lang.reflect.Constructor;
public class Main {
public static void main(String[] args) throws Exception {
Class cls = Class.forName("test.Dog");
Constructor constructor = cls.getDeclaredConstructor();
System.out.println("Constructor : " + constructor.getName());
// Constructor : test.Dog
}
}
Case02 - 인자가 있는 생성자 가져오기
import java.lang.reflect.Constructor;
public class Main {
public static void main(String[] args) throws Exception {
Class cls = Class.forName("test.Dog");
Constructor constructor = cls.getDeclaredConstructor(String.class);
System.out.println("Constructor : " + constructor.getName());
// Constructor : test.Dog
}
}
Case03 - 모든 생성자 가져오기
import java.lang.reflect.Constructor;
public class Main {
public static void main(String[] args) throws Exception {
Class cls = Class.forName("test.Dog");
Constructor constructors[] = cls.getDeclaredConstructors();
for (Constructor item : constructors){
System.out.println("Get constructors : " + item);
}
// Get constructors : public test.Dog()
// Get constructors : private test.Dog(java.lang.String)
}
}
Case04 - public 생성자만 가져오기
import java.lang.reflect.Constructor;
public class Main {
public static void main(String[] args) throws Exception {
Class cls = Class.forName("test.Dog");
Constructor constructors[] = cls.getConstructors();
for (Constructor item : constructors){
System.out.println("Get public constructors : " + item);
}
// Get public constructors : public test.Dog()
}
}
getDeclaredMethod() 메서드를 사용하여 인자로 메서드의 파라미터 정보를 넘겨주면 일치하는 것을 찾을 수 있습니다.
Case01 - 인자가 있는 메서드 가져오는 방법
import java.lang.reflect.Method;
public class Main {
public static void main(String[] args) throws Exception {
Class cls = Class.forName("test.Dog");
Method method = cls.getDeclaredMethod("myName", String.class);
System.out.println("Method : " + method);
// Method : private void test.Dog.myName(java.lang.String)
}
}
import java.lang.reflect.Method;
public class Main {
public static void main(String[] args) throws Exception {
Class cls = Class.forName("test.Dog");
Method method = cls.getDeclaredMethod("hello", null);
System.out.println("Method : " + method);
// Method : private void test.Dog.hello()
}
}
import java.lang.reflect.Method;
public class Main {
public static void main(String[] args) throws Exception {
Class cls = Class.forName("test.Dog");
Method methods[] = cls.getDeclaredMethods();
for (Method item : methods) {
System.out.println("Method : " + item);
// Method : private void test.Dog.myName(java.lang.String)
// Method : private void test.Dog.myCity(java.lang.String)
// Method : private void test.Dog.hello()
}
}
}
import java.lang.reflect.Method;
public class Main {
public static void main(String[] args) throws Exception {
Class cls = Class.forName("test.Dog");
Method methods[] = cls.getMethods();
for (Method item : methods) {
System.out.println("Method : " + item);
}
/*
* Method : public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
* Method : public final void java.lang.Object.wait() throws java.lang.InterruptedException
* Method : public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
* Method : public boolean java.lang.Object.equals(java.lang.Object)
* Method : public java.lang.String java.lang.Object.toString()
* Method : public native int java.lang.Object.hashCode()
* Method : public final native java.lang.Class java.lang.Object.getClass()
* Method : public final native void java.lang.Object.notify()
* Method : public final native void java.lang.Object.notifyAll()
*/
}
}
import java.lang.reflect.Field;
public class Main {
public static void main(String[] args) throws Exception {
Class cls = Class.forName("test.Dog");
Field field = cls.getDeclaredField("myName");
System.out.println(field);
// private java.lang.String test.Dog.myName
}
}
import java.lang.reflect.Field;
public class Main {
public static void main(String[] args) throws Exception {
Class cls = Class.forName("test.Dog");
Field fields[] = cls.getDeclaredFields();
for (Field item : fields) {
System.out.println(item);
}
// private java.lang.String test.Dog.myName
// public java.lang.String test.Dog.myCity
}
}
import java.lang.reflect.Field;
public class Main {
public static void main(String[] args) throws Exception {
Class cls = Class.forName("test.Dog");
Field fields[] = cls.getFields();
for (Field item : fields) {
System.out.println(item);
}
// public java.lang.String test.Dog.myCity
// public java.lang.String test.Animal.name
}
}
클래스로부터 변수 정보를 가져와 객체의 변수 값을 변경할 수 있다.
Case01 - getField() 메서드를 사용하면 객체의 public 필드를 찾아서 값을 변경할 수 있다.
import java.lang.reflect.Field;
public class Main {
public static void main(String[] args) throws Exception {
Dog dog = new Dog();
Class cls = Class.forName("test.Dog");
Field field = cls.getField("myCity");
System.out.println("default field : " + field.get(dog));
// default field : 서울
field.set(dog, "제주도");
System.out.println("update field : " + field.get(dog));
// update field : 제주도
}
}
Case 02 - setAccessible() 메서드를 사용하면 private로 선언한 필드에 접근
import java.lang.reflect.Field;
public class Main {
public static void main(String[] args) throws Exception {
Dog dog = new Dog();
Class cls = Class.forName("test.Dog");
Field field = cls.getDeclaredField("myName");
field.setAccessible(true);
System.out.println("default field : " + field.get(dog));
// default field : 뽀삐
field.set(dog, "펩시");
System.out.println("update field : " + field.get(dog));
// update field : 펩시
}
}