
#Java reflection use case code#
Again, Introspection in Java is really done using the Reflection API import name reflection is used to describe code which is able to inspect other code in the same system (or itself).įor example, say you have an object of an unknown type in Java, and you would like to call a 'doSomething' method on it if one exists. This is short example of Type Introspection in Java, where we discover the fields of an object and show their values dynamically.


Type introspection is the ability of a program to examine the type or properties of an object at runtime. Still, conceptually, introspection and reflection are different things: Introspection is performed as well using the Java Reflection API. In java there is no specific introspection API available natively. For example, there is no way in a Pascal, C, or C++ program to obtain detailed information about the functions defined within that program. The ability to examine and manipulate a Java class from within itself may not sound like very much, but in other programming languages this feature simply doesn't exist. Reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible. Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. Runtime reflection is the ability of a computer program to examine, introspect, and modify its own structure and behavior at runtime. In this article we'll dig into the library Javassist which is a bytecode manipulation framework that can help in achieving all of the above mechanisms.īut before, let's describe three different, unrelated but complementary techniques: Introspection, Reflection and Bytecode Manipulation. Classes can be generated entirely dynamically at runtime using bytecode manipulation techniques.Classes bytecode can be modified at runtime without an agent as long as the class has not been loaded yet by a classloader.The java classes bytecode can be modified before being loaded by the classloader through the usage of an agent.In principle the bytecode is read only and cannot be change once loaded. This bytecode is loaded by the JVM to execute the Java program. These Java classes take the form of bytecode. Java source files are compiled to Java class files by the Java Compiler. It is used extensively for instance by frameworks such as Spring (IoC) and Hibernate (ORM) to inject dynamic behaviour to Java objects at runtime.īut first let's look at a very summarized view of the Java toolchain to remind a few concepts: 4.0 SCIF : Simple and Cute IoC Frameworkīytecode manipulation consists in modifying the classes - represented by bytecode - compiled by the Java compiler, at runtime.3.2 IoC frameworks (Spring / Pico / Google).

You might also want to have a look at the second article in this serie available here : Bytecode manipulation with Javassist for fun and profit part II: Generating toString and getter/setters using bytecode manipulation. Part of this article is available as a slideshare presentation here. The goal of this article is to present Javassist in the light of a concrete use case: the implementation in a little more than 300 lines of code of a lightweight, simple but cute IoC Container: SCIF - Simple and Cute IoC Framework.Ī new version of comet-tennis demo app with the SCIF framework integrated is available here. And mastering bytecode manipulation, opens a whole new world of approaches and possibilities. It takes a few minutes to every initiated Java developer to understand and be able to use Javassist efficiently. Of all the libraries and tools providing advanced bytecode manipulation features, Javassist is the easiest to use and the quickest to master. require to understand Java bytecode thoroughly and come up with means of manipulating it at runtime.Įach and every of these advanced features of what is nowadays standard approaches when programming with Java require a sound understanding of the Java bytecode, not to mention completely new languages running on the JVM such as Scala or Clojure.īytecode manipulation is not easy though. Profilers, mocking tools, AOP, ORM frameworks, IoC Containers, boilerplate code generators, etc.

Understanding the bytecode, however, is essential to the areas of tooling and program analysis, where the applications can modify the bytecode to adjust the behavior according to the application's domain. Java bytecode is the form of instructions that the JVM executes.Ī Java programmer, normally, does not need to be aware of how Java bytecode works.
