본문 바로가기

Spring

리플렉션 API: 클래스 정보 조회

반응형

https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html

 

Class (Java Platform SE 8 )

Determines if the specified Class object represents a primitive type. There are nine predefined Class objects to represent the eight primitive types and void. These are created by the Java Virtual Machine, and have the same names as the primitive types tha

docs.oracle.com

Class Class<T>

  • Type Parameters:T - the type of the class modeled by this Class object. For example, the type of String.class is Class<String>. Use Class<?> if the class being modeled is unknown.

Class 클래스를 사용해서 다른 클래스들의  접근 할 수 있다.

Class를 활용한 인스턴스 생성 방법 세 가지

  1. 모든 클래스를 로딩 한 다음 Class<T>의 인스턴스가 생긴다. “타입.class”로 접근할 수 있다.

  2. 모든 인스턴스는 getClass() 메소드를 가지고 있다. “인스턴스.getClass()”로 접근할 수 있다.

  3. 클래스를 문자열로 읽어오는 방법

    • Class.forName(“FQCN”)

    • 클래스패스에 해당 클래스가 없다면 ClassNotFoundException이 발생한다.


해당 클래스의 퍼블릭 필드들만 가져 옴
getDeclared~ 로 다른 모든 것 들을 가져올 수 있음

 

반응형

'Spring' 카테고리의 다른 글

Logback 사용하기  (0) 2022.03.06
AWS Amplify  (4) 2021.11.20
JUnit 5  (2) 2021.02.12
Spring AOP 개념  (3) 2020.12.02
Environment - 프로퍼티  (2) 2020.11.25