Upload
adil-raja
View
90
Download
1
Embed Size (px)
Citation preview
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 candynamically 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 orintrospection.
Reflection and Introspection
Introduction
CLASS OBJECTS
In most languages, introspection begins with an object thatrepresents 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 classis to get its parent class
EXAMPLE
Class parentClass = aClass . getSuperc lass ( ) ; / / Java
Another operation is to get the list of all subclasses for a class.
aSet <− aClass subclasses " Sma l l t a l k "
You can also get the name of a class as a string.
char ∗ name = t y p e i n f o ( aVar iab le ) . 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 totest if an object is an instance of a class.
EXAMPLE
i f Member ( aVar iable , Ch i ld ) thenaChi ld = Chi ld ( aVar iab le ) (∗ Object Pascal ∗)
This is needed for downcasting.However, if you find yourself using this a lot, then youaren’t thinking in an object-oriented way, since often suchsituations can be better written using polymorphism.
Reflection and Introspection
Introduction
METHODS AS OBJECTS
The next level of complexity is treating a method as anobject.First step is to get a collection of methods from a class
EXAMPLE
Method [ ] methods = aClass . getDeclaredMethods ( ) ; / / Java
aSet <− aClass s e l e c t o r s . / / Sma l l t a l k
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 anarray of arguments:
EXAMPLE
Class sc = S t r i n g . class ;Class [ ] paramTypes = new Class [ 1 ] ;paramTypes [ 0 ] = sc ;t ry {
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 ( Except ion e ) {System . out . p r i n t l n ( " Except ion " + e ) ;
}
Here we dynamically look up a method, based both on nameand type signature, then create an array of arguments, thenexecute the method.
Reflection and Introspection
Introduction
CLASS LOADING
Java has a class named ClassLoader that allows you todefine a class from an array of bytes:
EXAMPLE
class SimpleClassLoader extends ClassLoader {public Class getClass ( S t r i n g name) {
Class theClass = nul l ;t ry {
F i l e f = new F i l e (name ) ;InputStream i s = new Fi le InputSt ream ( f ) ;i n t bu fs i ze = ( i n t ) f . l eng th ( ) ;byte buf [ ] = new byte [ bu fs i ze ] ;i s . read ( buf , 0 , bu fs i ze ) ;i s . c lose ( ) ;theClass = def ineClass ( null , buf , 0 , buf . leng th ) ;
} catch ( Except ion e ) {System . e r r . p r i n t l n ( " E r ro r dur ing load " + e ) ;System . e x i t ( 1 ) ;
}return theClass ;
}}
Once you have a class, you can create instances, or executemethods.
Reflection and Introspection
Introduction
METACLASSES – STRANGE LOOPS
You can get into some interesting places by asking theright 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 isan intance of itself.In Smalltalk, the path goes on a bit longer before it circlesback on itself.
Reflection and Introspection
Introduction
RATIONALE FOR METACLASSES
Remember that by introducing new classes that representclasses, Smalltalk was able to solve the following problemHow do you give unique behavior to just one instance of aclass?(For example, the behavior to initialize newly createdinstances of a class).The answer was, you don’t.You add a new child class that defines the behavior youwant, and put this between the object and the true parent.
Reflection and Introspection
Introduction
RELATIONSHIP TO COMPONENTS
Relection and Introspection have taken on increasingimportance as the field moves from object-basedcomputing to component-based computing (COM, Corba,JavaBeans, etc).This is because components must often be dynamicallyloaded and executed.And reflection is the underlying mechanism that permitsthis.
Reflection and Introspection
References
REFERENCES
Images and content for developing these slides have beentaken from the follwoing book with the permission of theauthor.An Introduction to Object Oriented Programming, TimothyA. Budd.This presentation is developed with Beamer:
JuanLesPins, beetle.