티스토리 뷰
반응형
자바는 클래스와 인터페이스의 메타 데이터를 java.lang 패키지에 소속된 Class 클래스로 관리한다.
* 메타 데이터 : 클래스 이름, 생성자 정보, 플드 정보, 메소드 정보를 의미함
1. 클래스 객체 얻기
클래스 객체를 얻기 위해서는 Object 클래스가 가지고 있는 getClass() 메소드를 이용하면 된다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Main { | |
public static void main(String[] args) { | |
Book book = new Book(); | |
Class class1 = book.getClass(); | |
System.out.println(class1.getName()); | |
} | |
} |
2. 리플렉션
Class 객체를 이용하면 클래스 생성자, 필드, 메소드 의 정보를 알 수 있다. 이 과정을 리플렉션 이라고 한다.
리플렉션에서는 다음과 같은 메소드를 제공하고있다.
object.getDeclaredClasses();
object.getDeclaredConstructors();
object.getDeclaredFields();
object.getDeclaredMethods();
상속된 멤버를 알고싶다면 .getFields() / .getMethods 를 사용한다.
단, 위 메소드는 public 멤버만 가져오게 된다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Book { | |
public String getBookName() { | |
return bookName; | |
} | |
public void setBookName(String bookName) { | |
this.bookName = bookName; | |
} | |
public Book(String bookName){ | |
this.bookName=bookName; | |
} | |
public Book(){} | |
private String bookName; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Constructor; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.Method; | |
class Main { | |
public static void main(String[] args) { | |
Book book = new Book(); | |
System.out.println("Class Name"); | |
Class class1 = book.getClass(); | |
System.out.println(class1.getName()); | |
System.out.println(); | |
System.out.println("[Constructor]"); | |
Constructor[] constructors = class1.getDeclaredConstructors(); | |
for(Constructor constructor : constructors){ | |
System.out.println(constructor.getName()); | |
Class[] parameters = constructor.getParameterTypes(); | |
PrintParameters(parameters); | |
} | |
System.out.println(); | |
System.out.println("[Field]"); | |
Field[] fields = class1.getDeclaredFields(); | |
for(Field field : fields){ | |
System.out.println(field.getName()); | |
System.out.println(field.getType().getSimpleName() + " " + field.getName()); | |
} | |
System.out.println(); | |
System.out.println("[Method]"); | |
Method[] methods = class1.getDeclaredMethods(); | |
for(Method method :methods){ | |
System.out.println(method.getName()); | |
Class[] parameters = method.getParameterTypes(); | |
PrintParameters(parameters); | |
} | |
} | |
private static void PrintParameters(Class[] parameters) { | |
for(int i=0; i< parameters.length; i++){ | |
System.out.print(parameters[i].getName()); | |
if(i< parameters.length-1){ | |
System.out.println(","); | |
} | |
System.out.println(); | |
} | |
} | |
} |
반응형
'프로그래밍 언어 > JAVA' 카테고리의 다른 글
[JAVA]Vector vs ArrayList vs LinkedList (0) | 2022.02.26 |
---|---|
[JAVA] String[] args 의 의미 (0) | 2021.04.25 |
[JAVA] 객체 복제 clone() (0) | 2021.03.23 |
[JAVA] 오버로딩(Overloading) VS 오버라이딩(Overriding) (0) | 2021.02.24 |
[JAVA] OOP 객체 지향 프로그래밍 (0) | 2021.02.23 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 백준
- 알고리즘
- Solid
- CS
- spring-cloud
- nginx
- kakao
- OOP
- 그래프
- docker
- springboot
- JPA
- C언어
- security
- java
- 자바
- 수학
- Matlab
- 디자인패턴
- 자격증
- Algorithm
- 면접
- Spring
- 스프링부트
- 릿코드
- interview
- ajax
- 매트랩
- 프로그래머스
- 스프링
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함