SlideShare a Scribd company logo
1 of 18
ⓒSaebyeol Yu. Saebyeol’s PowerPoint
Reflection
강임남
ⓒSaebyeol Yu. Saebyeol’s PowerPoint
목차
1 Reflection이란?
2 언제Reflection이필요할까?
3 Java 바탕지식
4 ReflectionAPI와예시
ⓒSaebyeol Yu. Saebyeol’s PowerPoint
5 정리
ⓒSaebyeol Yu. Saebyeol’s PowerPoint
1
Reflection 이란?
ⓒSaebyeol Yu. Saebyeol’s PowerPoint
Part 1 Reflection이란?
Reflect:
1. 비추다. 2. 반사하다. 3. 나타내다
ⓒSaebyeol Yu. Saebyeol’s PowerPoint
Part 1 Reflection이란?
Reflection
모르는 대상을 사용해야할 때,
대상에 대해 정보를 알려주고 사용할 수 있도록 도와주는 기술
Runtime 시점에, 컴파일 시점엔 몰랐던
Class에 대한 정보를 검사해 파악하고,
클래스를 조작할 수 있도록 지원하는 기술
ⓒSaebyeol Yu. Saebyeol’s PowerPoint
2
언제 Reflection 필요?
ⓒSaebyeol Yu. Saebyeol’s PowerPoint
Part 2 언제Reflection이필요할까?
언제?
대상인 Class Type을
모르는 채로 객체를 생성해야하는 경우가 언제일까?
Code 작성 시점에는 어떤 Class 를 사용 해야할지 모르지만,
실행시점에는 Class를 사용해 실행해야 하는 경우
즉,
동적으로 Class를 사용해야할 경우
ⓒSaebyeol Yu. Saebyeol’s PowerPoint
Part 2 언제Reflection이필요할까?
어디에서?
프레임워크나 라이브러리:
Spring Annotation, Spring Dependency Injection,
Hibernate 의 default constructor,
JUnit, jackson library 등
기타:
정적 분석 툴, IDE 의 java code 자동 완성 기능 등
ⓒSaebyeol Yu. Saebyeol’s PowerPoint
3
Java 바탕지식
ⓒSaebyeol Yu. Saebyeol’s PowerPoint
Part 3 JA
V
A바탕지식-1
>
바인딩
컴퓨터 프로그래밍에서
각종 값들이
확정되어(결정)
변경할 수 없는
구속(bind) 상태가
되는 것.
자바는 동적 바인딩을 통해 상속, 다형성과 같은 다양
한 기능을 사용할 수 있다.
정적 바인딩
Static
Binding
- compile 시점에 값이 확정
- 실행 시 효율이 높고, 안정적
- Overloading, final, static
동적 바인딩
Dynamic
Binding
- Runtime 시점에 값이 결정
- 많은 메모리 공간 필요
- 속도 저하(타입 checking)
- Overriding, java 의 다형성, 상속이 가능한
이유
ⓒSaebyeol Yu. Saebyeol’s PowerPoint
Part 3 JA
V
A바탕지식-2
>
JVM 구조와 동적 바인딩,
Reflection
- 실체: 사용자가 작성한 클래
스를 뜻함
- 투영체: Java 코드가 실행이
되면서 Method Area 에 올라
간 클래스 정보
- 매개체(물 또는 거울): java.l
ang.Class Class Type인 Clas
s 에 대입(java.lang.reflect)
ⓒSaebyeol Yu. Saebyeol’s PowerPoint
Part 3 JA
V
A바탕지식-3
누가
프레임 워크
언제
런타임
어디에서
JVM
무엇을
Method Area에
있는
Class 정보
왜
Code 작성시점
에 Class 정보
알 수 없음
어떻게
Class Type 인
Class
ⓒSaebyeol Yu. Saebyeol’s PowerPoint
4
ReflectionAPI 와 예시
ⓒSaebyeol Yu. Saebyeol’s PowerPoint
Part 4 ReflectionAPI와예시-1
Reflection API
(java.lang.reflect)
- Class(Interface, Enum)
- Constructor
- Method
- Field
...
클래스 정보를 활용한 Reflection API
가 제공하는 기능
1. 객체생성
2. 메서드 호출
3. 변수 값 설정 등
Reflection 을 통해 클래스의 메소드,
변수, 생성자를 파악할 수 있고 동적
으로 사용 가능하여, 여러 가지 기능
을 구현할 수 있다.
ⓒSaebyeol Yu. Saebyeol’s PowerPoint
Part 4 ReflectionAPI와예시-2
Reflection API
상세 설명
- getClasses, getField …
- getXX: 해당 XX(클래스, 필드, 메소
드 등) 이나 super 클래스에 선언 (또
는 구현) 되어 있고 public 접근 제한
자를 가진 대상
- getDeclaredClasses …
- getDeclaredXX: 해당 XX(클래스, 필
드, 메소드 등) 선언 (또는 구현)만 되
어 있으면 됨. 접근 제어자와 상관 없
음.
- newInstance: Class type Class 의
메소드로 해당 클래스 타입의 인스턴
스를 생성해주는 메소드.
newInstance() 구현부를 보면, 대상의
생성자의 접근제어자가 Public 여야
만 조회 및 사용이 가능함.
ⓒSaebyeol Yu. Saebyeol’s PowerPoint
Part 4 ReflectionAPI와예시-3
성능 확인
- 생성자를 이용한 인스턴스 생성과
Reflection API 를 활용한 인스턴스 생
성의 시간을 비교
- 0.0018845 VS 0.027349
- 14 ~ 15배 차이
ⓒSaebyeol Yu. Saebyeol’s PowerPoint
5
정리
ⓒSaebyeol Yu. Saebyeol’s PowerPoint
Part 6 마무리
Reflection 정리
Java 특징인 Reflection이란?
프레임 워크처럼 Class 정보를
동적으로 바인딩하여 기능을 제공해
주는 대상이 JVM 에서 Runtime 할 때,
프레임워크 기능 코드를 작성할 땐 알
수 없던 Class 정보들을 JVM의
메모리 Method Area 영역에서 알아와
동적으로 처리하여 사용자에게
기능을 제공해주는 것
Reflection 정리
- 컴파일 시 확인하지 않고 런타임 시
에만 발생하는 문제가 발생할 수 있다.
- 접근 지시자를 무시할 수 있어, 추상
화를 깰 수 있다.
- Java Reflection의 다양한 기능이
Java 언어의 경계를 넘나드는데 도움
을 주지만, 성능 부하 또한 분명히 존
재하기 때문에 주의해서 사용해야 한
다.

More Related Content

Featured

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 

java_reflection.pptx

  • 1. ⓒSaebyeol Yu. Saebyeol’s PowerPoint Reflection 강임남
  • 2. ⓒSaebyeol Yu. Saebyeol’s PowerPoint 목차 1 Reflection이란? 2 언제Reflection이필요할까? 3 Java 바탕지식 4 ReflectionAPI와예시 ⓒSaebyeol Yu. Saebyeol’s PowerPoint 5 정리
  • 3. ⓒSaebyeol Yu. Saebyeol’s PowerPoint 1 Reflection 이란?
  • 4. ⓒSaebyeol Yu. Saebyeol’s PowerPoint Part 1 Reflection이란? Reflect: 1. 비추다. 2. 반사하다. 3. 나타내다
  • 5. ⓒSaebyeol Yu. Saebyeol’s PowerPoint Part 1 Reflection이란? Reflection 모르는 대상을 사용해야할 때, 대상에 대해 정보를 알려주고 사용할 수 있도록 도와주는 기술 Runtime 시점에, 컴파일 시점엔 몰랐던 Class에 대한 정보를 검사해 파악하고, 클래스를 조작할 수 있도록 지원하는 기술
  • 6. ⓒSaebyeol Yu. Saebyeol’s PowerPoint 2 언제 Reflection 필요?
  • 7. ⓒSaebyeol Yu. Saebyeol’s PowerPoint Part 2 언제Reflection이필요할까? 언제? 대상인 Class Type을 모르는 채로 객체를 생성해야하는 경우가 언제일까? Code 작성 시점에는 어떤 Class 를 사용 해야할지 모르지만, 실행시점에는 Class를 사용해 실행해야 하는 경우 즉, 동적으로 Class를 사용해야할 경우
  • 8. ⓒSaebyeol Yu. Saebyeol’s PowerPoint Part 2 언제Reflection이필요할까? 어디에서? 프레임워크나 라이브러리: Spring Annotation, Spring Dependency Injection, Hibernate 의 default constructor, JUnit, jackson library 등 기타: 정적 분석 툴, IDE 의 java code 자동 완성 기능 등
  • 9. ⓒSaebyeol Yu. Saebyeol’s PowerPoint 3 Java 바탕지식
  • 10. ⓒSaebyeol Yu. Saebyeol’s PowerPoint Part 3 JA V A바탕지식-1 > 바인딩 컴퓨터 프로그래밍에서 각종 값들이 확정되어(결정) 변경할 수 없는 구속(bind) 상태가 되는 것. 자바는 동적 바인딩을 통해 상속, 다형성과 같은 다양 한 기능을 사용할 수 있다. 정적 바인딩 Static Binding - compile 시점에 값이 확정 - 실행 시 효율이 높고, 안정적 - Overloading, final, static 동적 바인딩 Dynamic Binding - Runtime 시점에 값이 결정 - 많은 메모리 공간 필요 - 속도 저하(타입 checking) - Overriding, java 의 다형성, 상속이 가능한 이유
  • 11. ⓒSaebyeol Yu. Saebyeol’s PowerPoint Part 3 JA V A바탕지식-2 > JVM 구조와 동적 바인딩, Reflection - 실체: 사용자가 작성한 클래 스를 뜻함 - 투영체: Java 코드가 실행이 되면서 Method Area 에 올라 간 클래스 정보 - 매개체(물 또는 거울): java.l ang.Class Class Type인 Clas s 에 대입(java.lang.reflect)
  • 12. ⓒSaebyeol Yu. Saebyeol’s PowerPoint Part 3 JA V A바탕지식-3 누가 프레임 워크 언제 런타임 어디에서 JVM 무엇을 Method Area에 있는 Class 정보 왜 Code 작성시점 에 Class 정보 알 수 없음 어떻게 Class Type 인 Class
  • 13. ⓒSaebyeol Yu. Saebyeol’s PowerPoint 4 ReflectionAPI 와 예시
  • 14. ⓒSaebyeol Yu. Saebyeol’s PowerPoint Part 4 ReflectionAPI와예시-1 Reflection API (java.lang.reflect) - Class(Interface, Enum) - Constructor - Method - Field ... 클래스 정보를 활용한 Reflection API 가 제공하는 기능 1. 객체생성 2. 메서드 호출 3. 변수 값 설정 등 Reflection 을 통해 클래스의 메소드, 변수, 생성자를 파악할 수 있고 동적 으로 사용 가능하여, 여러 가지 기능 을 구현할 수 있다.
  • 15. ⓒSaebyeol Yu. Saebyeol’s PowerPoint Part 4 ReflectionAPI와예시-2 Reflection API 상세 설명 - getClasses, getField … - getXX: 해당 XX(클래스, 필드, 메소 드 등) 이나 super 클래스에 선언 (또 는 구현) 되어 있고 public 접근 제한 자를 가진 대상 - getDeclaredClasses … - getDeclaredXX: 해당 XX(클래스, 필 드, 메소드 등) 선언 (또는 구현)만 되 어 있으면 됨. 접근 제어자와 상관 없 음. - newInstance: Class type Class 의 메소드로 해당 클래스 타입의 인스턴 스를 생성해주는 메소드. newInstance() 구현부를 보면, 대상의 생성자의 접근제어자가 Public 여야 만 조회 및 사용이 가능함.
  • 16. ⓒSaebyeol Yu. Saebyeol’s PowerPoint Part 4 ReflectionAPI와예시-3 성능 확인 - 생성자를 이용한 인스턴스 생성과 Reflection API 를 활용한 인스턴스 생 성의 시간을 비교 - 0.0018845 VS 0.027349 - 14 ~ 15배 차이
  • 17. ⓒSaebyeol Yu. Saebyeol’s PowerPoint 5 정리
  • 18. ⓒSaebyeol Yu. Saebyeol’s PowerPoint Part 6 마무리 Reflection 정리 Java 특징인 Reflection이란? 프레임 워크처럼 Class 정보를 동적으로 바인딩하여 기능을 제공해 주는 대상이 JVM 에서 Runtime 할 때, 프레임워크 기능 코드를 작성할 땐 알 수 없던 Class 정보들을 JVM의 메모리 Method Area 영역에서 알아와 동적으로 처리하여 사용자에게 기능을 제공해주는 것 Reflection 정리 - 컴파일 시 확인하지 않고 런타임 시 에만 발생하는 문제가 발생할 수 있다. - 접근 지시자를 무시할 수 있어, 추상 화를 깰 수 있다. - Java Reflection의 다양한 기능이 Java 언어의 경계를 넘나드는데 도움 을 주지만, 성능 부하 또한 분명히 존 재하기 때문에 주의해서 사용해야 한 다.

Editor's Notes

  1. JVM의 ClassLoader는. class를 Perm Gen (8+ : Metaspace)에 Load 한다. Class 형식의 Instance가 생성된다. Type.class 형식으로 Heap에 저장 https://www.youtube.com/watch?v=RZB7_6sAtC4&t=49s&pp=ygUZcmVmbGVjdGlvbiBqYXZhIO2FjOy7pO2GoQ%3D%3D
  2. Java 특징인 Reflection이란? 프레임 워크처럼 Class 정보를 동적으로 바인딩하여 기능을 제공해 주는 대상이 JVM 에서 Runtime 할 때, 프레임워크 기능 코드를 작성할 땐 알 수 없던 Class 정보들을 Method Area 에서 알아와 동적으로 처리하여 사용자에게 기능을 제공해주는 것
  3. https://velog.io/@suyeon-jin/리플렉션-스프링의-DI는-어떻게-동작하는걸까 sss.reflection_test.plain.basic 의 BasicMain 클래스
  4. sss.reflection_test.plain.basic 의 BasicMain java.lang.Class 의 getConstructor0 메소드
  5. 참고 사이트: https://lob-dev.tistory.com/entry/Java의-Reflection-API Java 특징인 Reflection이란? 프레임 워크처럼 Class 정보를 동적으로 바인딩하여 기능을 제공해 주는 대상이 JVM 에서 Runtime 할 때, 프레임워크 기능 코드를 작성할 땐 알 수 없던 Class 정보들을 Method Area 에서 알아와 동적으로 처리하여 사용자에게 기능을 제공해주는 것