|
Introducing reflection
Reflection gives your code access to internal information for classes loaded into the JVM and allows you to write code that works with classes selected during execution, not in the source code
|
|
Applied reflection
Command line argument processing is one of those nasty chores that seems to keep coming around no matter how many times you've dealt with it in the past.
|
|
Class transformation with Javassist
Bored with Java classes that execute just the way the source code was written? Then cheer up, because you're about to find out about twisting classes into shapes never intended by the compiler!
|
|
The Reflection API
The reflection API represents, or reflects, the classes, interfaces, and objects in the current Java Virtual Machine. You'll want to use the reflection API if you are writing development tools such as debuggers, class browsers, and GUI builders. With the reflection API you can:
|
|
Examining Classes
If you are writing a class browser, you need a way to get information about classes at runtime. For example, you might want to display the names of the class fields, methods, and constructors. Or, you might want to show which interfaces are implemented by a class. To get this information you need to get the Class object that reflects the class.
|
|
Retrieving Class Objects
You can retrieve a Class object in several ways
|
|
Getting the Class Name
Every class in the Java programming language has a name.
|
|
Discovering Class Modifiers
Every class in the Java programming language has a name.
|
|
Finding Superclasses
Because the Java programming language supports inheritance, an application such as a class browser must be able to identify superclasses.
|
|
Identifying the Interfaces Implemented by a Class
The type of an object is determined by not only its class and superclass, but also by its interfaces.
|
|
Discovering Class Constructors
To create an instance of a class, you invoke a special method called a constructor
|
|
Manipulating Objects
Software development tools, such as GUI builders and debuggers, need to manipulate objects at runtime
|
|
Manipulating Objects
Software development tools, such as GUI builders and debuggers, need to manipulate objects at runtime
|
|
Summary of Classes
The following table summarizes the classes that compose the reflection API
|
|
Working with Arrays
Software development tools, such as GUI builders and debuggers, need to manipulate objects at runtime
|
|
Obtaining Method Information
To find out what public methods belong to a class, invoke the method named getMethods.
|
|
Examining Interfaces
Class objects represent interfaces as well as classes.
|
|
Eliminate tedious programming: Recover data with XML and Reflection
The parsing of ResultSets forms one of the most significant tasks involved in retrieving data from a database. But, as a repetitious and uninteresting assignment, it is not a favorite among developers
|
|
Take an in-depth look at the Java Reflection API
When you are creating tools for a programming language that are written in that programming language, sometimes you have to look "under the covers."
|
|
Using Java Reflection
Reflection is a feature in the JavaTM programming language. It allows an executing Java program to examine or "introspect" upon itself, and manipulate internal properties of the program.
|
|
Java Reflection
The JDK 1.1 Reflection API provides Java code with a means of introspection. Using Reflection, you can not only discover the fields, methods, and constructors of loaded classes, but you can dynamically manipulate them within the basic security framework as well.
|
|
Reflecting, introspecting, and customizing JavaBeans
The Sun JDK JavaBeans API provides a framework for defining reusable software components that can be manipulated in a visual builder tool.
|
|
Speed up batch file processing using generic programming and core reflection
Approaching conventional batch file processing with Java in a naive way can be very inefficient. Trying to speed up the process without modifying the actual Java utility used in your batch file is not as straightforward as you may think.
|
|
Java Tip 57: Applet parameterization via class reflection
Parameterizing an applet is usually a tedious chore, involving many repetitive lines of code in the applet's init() method. But all these lines can be replaced by a single one.
|
|
Make an EJB from any Java class with Java Reflection
Do you have a Java class whose functionality would be useful across the entire enterprise? Do you have many classes with enterprise potential and existing applications that use them?
|
|
Untangle your servlet code with reflection
You can enlist the Reflection API to unravel an all-too-common problem in servlet development: doGet() and doPost() methods that grow long, complex, and hard to extend and debug
|
|
Reflection vs. code generation
Developers try to avoid tedious, redundant programming at all costs. Solid coding principles such as inheritance, polymorphism, and design patterns help developers avoid code redundancy. But in the uncertain realm of software development, these principles cannot eliminate the need for code maintenance and rewrites.
|
|
Java Tip 98: Reflect on the Visitor design pattern
The Visitor pattern is often used to separate the structure of an object collection from the operations performed on that collection. For example, it can separate the parsing logic in a compiler from the code generation logic
|
|
Reflection: A new way to discover information about Java classes
Most people think of reflection as the return of an image in a mirror or as serious thought. Developers using the Java programming language can include another meaning: a way to discover information about Java classes.
|
|
Using java.lang.reflect.Proxy to Interpose on JavaTM Class Methods
Library interposition has proven to be a very useful technique in C language programming. With this approach, it is not necessary to recompile any existing source code--or even have access to the source code--in order to trace, profile or debug function calls. The goal of this technical article is to explore a technique for interposing on JavaTM methods.
|
|
Java Tip 113: Identify subclasses at runtime
Java Reflection provides a lot of information about a given class at runtime; you can easily know all its super classes, implemented interfaces, methods, constructors, fields, and so on. But in some cases, you may want to know all the classes implementing a given interface, or subclassing a given class
|