Friday, September 23, 2011

Java Reflection Tutorial


Java's Reflection API's makes it possible to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time. It is also possible to instantiate new objects, invoke methods and get/set field values using reflection.
Java Reflection is a quite powerful and can be very useful. For instance, when mapping objects to tables in a database at runtime, like Butterfly Persistence does. Or, when mapping the statements in a script language to method calls on real objects at runtime, like Butterfly Container does when parsing its configuration scripts.
There are already numerous Java Reflection Tutorials on the internet. However, most of them, including Sun's own Java Reflection tutorial, only scratch the surface of Java Reflection and its possibilities.
This tutorial will get into Java reflection in more depth than most of the tutorials I have seen. It will explain the basics of Java Reflection including how to work with arrays, annotations, generics and dynamic proxies, and do dynamic class loading and reloading. It will also show you how to do more specific tasks, like reading all getter methods of a class, or accessing private fields and methods of a class. This tutorial will also clear up some of the confusion out there about what Generics information is available at runtime. Some people claim that all Generics information is lost at runtime. This is not true.
This tutorial is work in progress, so please be patient and check back later, if your topic isn't yet covered, or subscribe to the <. Here is a list of the topics covered so far:
Java Reflection
Introduction to Java Reflection
Classes
Constructors
Fields
Methods
Getters and Setters
Private Fields and Methods
Annotations
Generics
Arrays
Dynamic Proxies
Dynamic Class Loading and Reloading

No comments:

Post a Comment