SlideShare a Scribd company logo
Reflection and Introspection
REFLECTION AND INTROSPECTION
Muhammad Adil Raja
Roaming Researchers, Inc.
cbna
April 24, 2015
Reflection and Introspection
OUTLINE I
1 INTRODUCTION
2 REFERENCES
Reflection and Introspection
Introduction
INTRODUCTION
Reflection and Introspection imply a program that can
dynamically learn about or modify itself.
Learn the class of objects.
Learn what methods an object responds to.
Creating instances from class objects.
Dynamically adding new classes.
Dynamically adding new methods to existing classes.
Not all abilities are found in all languages.
Some languages do not have any support for reflection or
introspection.
Reflection and Introspection
Introduction
CLASS OBJECTS
In most languages, introspection begins with an object that
represents the class.
Typically this object has class Class.
Reflection and Introspection
Introduction
THINGS YOU CAN DO WITH A CLASS VARIABLE
One operation that can be performed with a value of type class
is to get its parent class
EXAMPLE
Class parentClass = aClass . getSuperclass ( ) ; / / Java
Another operation is to get the list of all subclasses for a class.
aSet <− aClass subclasses " Smalltalk "
You can also get the name of a class as a string.
char ∗ name = typeinfo ( aVariable ) . name ( ) ; / / C++
Reflection and Introspection
Introduction
GETTING THE CLASS FROM A STRING
In some languages, you can take the name of a class (a string)
and from this get the class object.
EXAMPLE
Class aClass = Class . forName ( " classname " ) ; / / Java
The class does not even have to be loaded.
The class will automatically be loaded for you.
Reflection and Introspection
Introduction
TESTING IF AN OBJECT IS AN INSTANCE OF A CLASS
We have seen already that most languages provide a way to
test if an object is an instance of a class.
EXAMPLE
i f Member( aVariable , Child ) then
aChild = Child ( aVariable ) (∗ Object Pascal ∗)
This is needed for downcasting.
However, if you find yourself using this a lot, then you
aren’t thinking in an object-oriented way, since often such
situations can be better written using polymorphism.
Reflection and Introspection
Introduction
METHODS AS OBJECTS
The next level of complexity is treating a method as an
object.
First step is to get a collection of methods from a class
EXAMPLE
Method [ ] methods = aClass . getDeclaredMethods ( ) ; / / Java
aSet <− aClass selectors . / / Smalltalk
Reflection and Introspection
Introduction
USING A METHOD
EXAMPLE
System . out . p r i n t l n ( methods [ 0 ] . getName ( ) ) ;
Class c = methods [ 0 ] . getReturnType ( ) ;
Reflection and Introspection
Introduction
DYNAMIC EXECUTION
You can also invoke the method, passing it the receiver and an
array of arguments:
EXAMPLE
Class sc = String . class ;
Class [ ] paramTypes = new Class [ 1 ] ;
paramTypes [ 0 ] = sc ;
try {
Method mt = sc . getMethod ( " concat " , paramTypes ) ;
Object mtArgs [ ] = { " xyz " } ;
Object r e s u l t = mt . invoke ( " abc " , mtArgs ) ;
System . out . p r i n t l n ( " r e s u l t i s " + r e s u l t ) ;
} catch ( Exception e ) {
System . out . p r i n t l n ( " Exception " + e ) ;
}
Here we dynamically look up a method, based both on name
and type signature, then create an array of arguments, then
execute the method.
Reflection and Introspection
Introduction
CLASS LOADING
Java has a class named ClassLoader that allows you to
define a class from an array of bytes:
EXAMPLE
class SimpleClassLoader extends ClassLoader {
public Class getClass ( String name) {
Class theClass = null ;
try {
F i l e f = new F i l e (name ) ;
InputStream i s = new FileInputStream ( f ) ;
int bufsize = ( int ) f . length ( ) ;
byte buf [ ] = new byte [ bufsize ] ;
i s . read ( buf , 0 , bufsize ) ;
i s . close ( ) ;
theClass = defineClass ( null , buf , 0 , buf . length ) ;
} catch ( Exception e ) {
System . err . p r i n t l n ( " Error during load " + e ) ;
System . e x i t ( 1 ) ;
}
return theClass ;
}
}
Reflection and Introspection
Introduction
METACLASSES – STRANGE LOOPS
You can get into some interesting places by asking the
right questions.
A class is an object.
All objects are instances of a class.
What is the class of a class?
In Java this path circles back on itself very quickly; Class is
an intance of itself.
In Smalltalk, the path goes on a bit longer before it circles
back on itself.
Reflection and Introspection
Introduction
RATIONALE FOR METACLASSES
Remember that by introducing new classes that represent
classes, Smalltalk was able to solve the following problem
How do you give unique behavior to just one instance of a
class?
(For example, the behavior to initialize newly created
instances of a class).
The answer was, you don’t.
You add a new child class that defines the behavior you
want, and put this between the object and the true parent.
Reflection and Introspection
Introduction
RELATIONSHIP TO COMPONENTS
Relection and Introspection have taken on increasing
importance as the field moves from object-based
computing to component-based computing (COM, Corba,
JavaBeans, etc).
This is because components must often be dynamically
loaded and executed.
And reflection is the underlying mechanism that permits
this.
Reflection and Introspection
References
REFERENCES
Images and content for developing these slides have been
taken from the follwoing book with the permission of the
author.
An Introduction to Object Oriented Programming, Timothy
A. Budd.
This presentation is developed with Beamer:
JuanLesPins, beetle.

More Related Content

PDF
Python Class | Python Programming | Python Tutorial | Edureka
PDF
Python - object oriented
PPTX
Introduction to OOP(in java) BY Govind Singh
PPTX
OOP Basic Concepts
PPT
Oop java
PPT
Oops Concept Java
Python Class | Python Programming | Python Tutorial | Edureka
Python - object oriented
Introduction to OOP(in java) BY Govind Singh
OOP Basic Concepts
Oop java
Oops Concept Java

What's hot (20)

PDF
Core java complete notes - Contact at +91-814-614-5674
PPT
Oops in Java
PPT
INTRODUCTION TO JAVA
PPTX
OOPs in Java
PPT
11 Using classes and objects
PDF
Object oriented programming With C#
PDF
Classes and Nested Classes in Java
PPTX
Inner Classes & Multi Threading in JAVA
PPTX
Inheritance in JAVA PPT
PPTX
Classes, objects in JAVA
PPTX
Static keyword ppt
PPTX
oops concept in java | object oriented programming in java
PPTX
Java Inheritance - sub class constructors - Method overriding
PPTX
Chapter 07 inheritance
PPTX
Java(inheritance)
PPTX
Pj01 x-classes and objects
PPT
4. Classes and Methods
PDF
java-06inheritance
PPTX
Object oriented programming concepts
PPT
Java: Inheritance
Core java complete notes - Contact at +91-814-614-5674
Oops in Java
INTRODUCTION TO JAVA
OOPs in Java
11 Using classes and objects
Object oriented programming With C#
Classes and Nested Classes in Java
Inner Classes & Multi Threading in JAVA
Inheritance in JAVA PPT
Classes, objects in JAVA
Static keyword ppt
oops concept in java | object oriented programming in java
Java Inheritance - sub class constructors - Method overriding
Chapter 07 inheritance
Java(inheritance)
Pj01 x-classes and objects
4. Classes and Methods
java-06inheritance
Object oriented programming concepts
Java: Inheritance
Ad

Similar to Reflection and Introspection (20)

PPT
10 reflection
PPT
Reflection in java
PDF
Pharo: a reflective language A first systematic analysis of reflective APIs
PPTX
Java Reflection Concept and Working
PDF
Java Reflection Explained Simply
PPT
Java Reflection
PPT
Object and Classes in Java
DOCX
Question and answer Programming
PDF
Java reflection
PPTX
L04 Software Design 2
PDF
Messages, Instances and Initialization
PDF
Understanding And Using Reflection
PPT
Chapter 5 declaring classes & oop
PPT
Stoop 305-reflective programming5
PPTX
JAVA-PPT'S-complete-chrome.pptx
PPTX
JAVA-PPT'S.pptx
PDF
Metaprograms and metadata (as part of the the PTT lecture)
PDF
Java Reflection
PPTX
Java reflection
PDF
JAVA-PPT'S.pdf
10 reflection
Reflection in java
Pharo: a reflective language A first systematic analysis of reflective APIs
Java Reflection Concept and Working
Java Reflection Explained Simply
Java Reflection
Object and Classes in Java
Question and answer Programming
Java reflection
L04 Software Design 2
Messages, Instances and Initialization
Understanding And Using Reflection
Chapter 5 declaring classes & oop
Stoop 305-reflective programming5
JAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S.pptx
Metaprograms and metadata (as part of the the PTT lecture)
Java Reflection
Java reflection
JAVA-PPT'S.pdf
Ad

More from adil raja (20)

PDF
ANNs.pdf
PDF
A Software Requirements Specification
PDF
NUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
PDF
DevOps Demystified
PDF
On Research (And Development)
PDF
Simulators as Drivers of Cutting Edge Research
PDF
The Knock Knock Protocol
PDF
File Transfer Through Sockets
PDF
Remote Command Execution
PDF
Thesis
PDF
CMM Level 3 Assessment of Xavor Pakistan
PDF
Data Warehousing
PDF
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
PDF
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
PDF
Real-Time Non-Intrusive Speech Quality Estimation for VoIP
PDF
VoIP
PDF
ULMAN GUI Specifications
PDF
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
PDF
ULMAN-GUI
PDF
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
ANNs.pdf
A Software Requirements Specification
NUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
DevOps Demystified
On Research (And Development)
Simulators as Drivers of Cutting Edge Research
The Knock Knock Protocol
File Transfer Through Sockets
Remote Command Execution
Thesis
CMM Level 3 Assessment of Xavor Pakistan
Data Warehousing
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Real-Time Non-Intrusive Speech Quality Estimation for VoIP
VoIP
ULMAN GUI Specifications
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
ULMAN-GUI
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...

Recently uploaded (20)

PDF
Nekopoi APK 2025 free lastest update
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
System and Network Administration Chapter 2
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
Transform Your Business with a Software ERP System
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
ai tools demonstartion for schools and inter college
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Essential Infomation Tech presentation.pptx
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Digital Strategies for Manufacturing Companies
Nekopoi APK 2025 free lastest update
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
System and Network Administration Chapter 2
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Transform Your Business with a Software ERP System
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
ai tools demonstartion for schools and inter college
Understanding Forklifts - TECH EHS Solution
Odoo Companies in India – Driving Business Transformation.pdf
Softaken Excel to vCard Converter Software.pdf
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
How to Migrate SBCGlobal Email to Yahoo Easily
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Essential Infomation Tech presentation.pptx
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
How to Choose the Right IT Partner for Your Business in Malaysia
Digital Strategies for Manufacturing Companies

Reflection and Introspection

  • 1. Reflection and Introspection REFLECTION AND INTROSPECTION Muhammad Adil Raja Roaming Researchers, Inc. cbna April 24, 2015
  • 2. Reflection and Introspection OUTLINE I 1 INTRODUCTION 2 REFERENCES
  • 3. Reflection and Introspection Introduction INTRODUCTION Reflection and Introspection imply a program that can dynamically learn about or modify itself. Learn the class of objects. Learn what methods an object responds to. Creating instances from class objects. Dynamically adding new classes. Dynamically adding new methods to existing classes. Not all abilities are found in all languages. Some languages do not have any support for reflection or introspection.
  • 4. Reflection and Introspection Introduction CLASS OBJECTS In most languages, introspection begins with an object that represents the class. Typically this object has class Class.
  • 5. Reflection and Introspection Introduction THINGS YOU CAN DO WITH A CLASS VARIABLE One operation that can be performed with a value of type class is to get its parent class EXAMPLE Class parentClass = aClass . getSuperclass ( ) ; / / Java Another operation is to get the list of all subclasses for a class. aSet <− aClass subclasses " Smalltalk " You can also get the name of a class as a string. char ∗ name = typeinfo ( aVariable ) . name ( ) ; / / C++
  • 6. Reflection and Introspection Introduction GETTING THE CLASS FROM A STRING In some languages, you can take the name of a class (a string) and from this get the class object. EXAMPLE Class aClass = Class . forName ( " classname " ) ; / / Java The class does not even have to be loaded. The class will automatically be loaded for you.
  • 7. Reflection and Introspection Introduction TESTING IF AN OBJECT IS AN INSTANCE OF A CLASS We have seen already that most languages provide a way to test if an object is an instance of a class. EXAMPLE i f Member( aVariable , Child ) then aChild = Child ( aVariable ) (∗ Object Pascal ∗) This is needed for downcasting. However, if you find yourself using this a lot, then you aren’t thinking in an object-oriented way, since often such situations can be better written using polymorphism.
  • 8. Reflection and Introspection Introduction METHODS AS OBJECTS The next level of complexity is treating a method as an object. First step is to get a collection of methods from a class EXAMPLE Method [ ] methods = aClass . getDeclaredMethods ( ) ; / / Java aSet <− aClass selectors . / / Smalltalk
  • 9. Reflection and Introspection Introduction USING A METHOD EXAMPLE System . out . p r i n t l n ( methods [ 0 ] . getName ( ) ) ; Class c = methods [ 0 ] . getReturnType ( ) ;
  • 10. Reflection and Introspection Introduction DYNAMIC EXECUTION You can also invoke the method, passing it the receiver and an array of arguments: EXAMPLE Class sc = String . class ; Class [ ] paramTypes = new Class [ 1 ] ; paramTypes [ 0 ] = sc ; try { Method mt = sc . getMethod ( " concat " , paramTypes ) ; Object mtArgs [ ] = { " xyz " } ; Object r e s u l t = mt . invoke ( " abc " , mtArgs ) ; System . out . p r i n t l n ( " r e s u l t i s " + r e s u l t ) ; } catch ( Exception e ) { System . out . p r i n t l n ( " Exception " + e ) ; } Here we dynamically look up a method, based both on name and type signature, then create an array of arguments, then execute the method.
  • 11. Reflection and Introspection Introduction CLASS LOADING Java has a class named ClassLoader that allows you to define a class from an array of bytes: EXAMPLE class SimpleClassLoader extends ClassLoader { public Class getClass ( String name) { Class theClass = null ; try { F i l e f = new F i l e (name ) ; InputStream i s = new FileInputStream ( f ) ; int bufsize = ( int ) f . length ( ) ; byte buf [ ] = new byte [ bufsize ] ; i s . read ( buf , 0 , bufsize ) ; i s . close ( ) ; theClass = defineClass ( null , buf , 0 , buf . length ) ; } catch ( Exception e ) { System . err . p r i n t l n ( " Error during load " + e ) ; System . e x i t ( 1 ) ; } return theClass ; } }
  • 12. Reflection and Introspection Introduction METACLASSES – STRANGE LOOPS You can get into some interesting places by asking the right questions. A class is an object. All objects are instances of a class. What is the class of a class? In Java this path circles back on itself very quickly; Class is an intance of itself. In Smalltalk, the path goes on a bit longer before it circles back on itself.
  • 13. Reflection and Introspection Introduction RATIONALE FOR METACLASSES Remember that by introducing new classes that represent classes, Smalltalk was able to solve the following problem How do you give unique behavior to just one instance of a class? (For example, the behavior to initialize newly created instances of a class). The answer was, you don’t. You add a new child class that defines the behavior you want, and put this between the object and the true parent.
  • 14. Reflection and Introspection Introduction RELATIONSHIP TO COMPONENTS Relection and Introspection have taken on increasing importance as the field moves from object-based computing to component-based computing (COM, Corba, JavaBeans, etc). This is because components must often be dynamically loaded and executed. And reflection is the underlying mechanism that permits this.
  • 15. Reflection and Introspection References REFERENCES Images and content for developing these slides have been taken from the follwoing book with the permission of the author. An Introduction to Object Oriented Programming, Timothy A. Budd. This presentation is developed with Beamer: JuanLesPins, beetle.