Design with runtime class information
How to make use of runtime class information in Java programs
By Bill Venners, JavaWorld.com, 02/01/99
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
One of the cool things about Java's object model is that Java objects are "conscious": given a reference to an object, you
can get information about that object's class. This "runtime class information" makes possible all kinds of interesting designs
and implementation techniques. But how are we to use this capability?
When I think about Java's support for runtime class information, two clichés come to my mind:
1. Information is power.
In our case, runtime class information gives power to the programmer because he or she has more information to work with.
This is a good thing.
2. Power corrupts.
Alas, in our case, runtime class information can be abused in designs.
The question I attempt to answer in this article is: How can you take advantage of the cool runtime class information offered
by Java objects without being accused of abuse of power?
How runtime class information works
Before I delve into guidelines, I'd like to give you some background on how runtime class information works in Java.
Every class or interface you write in the Java programming language defines a new type for your program to use. Once you define a new type, you can declare variables of that type. A type defines a set of operations
that may be performed on variables of that kind and the meaning of those operations.
If you define a class Hippopotamus, for example, you then can declare variables of type Hippopotamus. On such variables, the Java virtual machine (JVM) will allow you to perform operations defined for type Hippopotamus, but won't allow any other operations. If class Hippopotamus declares a public takeBath() method, for example, the JVM will allow you to invoke takeBath() on a variable of type Hippopotamus.
When you compile your class or interface, the Java compiler (if it is fully pleased with your work) will give you a class
file. The class file is an intermediate-compiled binary format for describing a Java type (a class or interface). The class
file for Hippopotamus, for example, would contain all information needed to define that type, including things like:
- The type's name (
Hippopotamus)
- The superclass's name
- Any modifiers (such as
public or abstract)
- The number of interfaces the type directly implements
- The names of those interfaces
- The number of fields
- For each field, a field descriptor (field name, type, modifiers)
- The number of methods
- For each method:
- A method descriptor (method name, return type or void, number and types of parameters)
- Modifiers (
static, abstract, private, and so on)
- The bytecodes
- An exception table
When a JVM loads a type (a class or interface), the JVM stores information about that type in a logical portion of its memory
called the method area. The type information stored in the method area corresponds to the information stored in the class file, but unlike the class
file, the structure and organization of the information in the method area is not defined by the Java specifications. The
designers of each JVM decide how to store the information they parse from Java class files in the method area of their JVM
implementation.
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- Bill's next book is Flexible Java http://www.artima.com/flexiblejava/index.html
- A complete online reprint of "Chapter 7, The Linking Model," of Bill's book Inside the Java Virtual Machine http://www.artima.com/insidejvm/linkmod.html
- The handout and slides for Bill's "Dynamic Extension in Java" talk. http://www.artima.com/javaseminars/modules/DynaExt/index.html
- Bill recently returned from his European bike trip. Read about it at
http://www.artima.com/bv/travel/bike98/index.html
- The discussion forum devoted to the material presented in this article http://www.artima.com/flexiblejava/fjf/rtci/index.html
- Links to all previous Design Techniques articles http://www.artima.com/designtechniques/index.html
- Recommended books on Java design http://www.artima.com/designtechniques/booklist.html
- A transcript of an e-mail debate between Bill Venners, Mark Johnson (JavaWorld's JavaBeans columnist), and Mark Balbe on whether all objects should be made into beans http://www.artima.com/flexiblejava/comments/beandebate.html
- Object orientation FAQ http://www.cyberdyne-object-sys.com/oofaq/
- A hearty collection of links on object orientation (numbering 7,237 at last count) http://www.rhein-neckar.de/~cetus/software.html
- The Object-Oriented Page http://www.well.com/user/ritchie/oo.html
- Collection of information on OO approach http://arkhp1.kek.jp:80/managers/computing/activities/OO_CollectInfor/OO_CollectInfo.html
- Design Patterns home page http://hillside.net/patterns/patterns.html
- A Comparison of OOA and OOD Methods http://www.iconcomp.com/papers/comp/comp_1.html
- "Object-Oriented Analysis and Design MethodsA Comparative Review" http://wwwis.cs.utwente.nl:8080/dmrg/OODOC/oodoc/oo.html
- Patterns discussion FAQ http://gee.cs.oswego.edu/dl/pd-FAQ/pd-FAQ.html
- Patterns in Java AWT http://mordor.cs.hut.fi/tik-76.278/group6/awtpat.html
- Software Technology's Design Patterns page http://www.sw-technologies.com/dpattern/
- Previous Design Techniques articles http://www.javaworld.com/topicalindex/jw-ti-techniques.html
excellent code! By Anonymous on March 7, 2010, 4:30 pmexcellent code!
Reply | Read entire comment
View all comments