Programming Languages (CS302 2006S)

Notes on Reflection in Java

Comments on:

Green, Dale. Trail: The Reflection API. In The JavaTM Tutorial. Sun Microsystems. Available online at http://java.sun.com/docs/books/tutorial/reflect/index.html

Comments from: Peter Brown, Michael Claveria, Davis Hart (*), Alex Leach, Angeline Namai, Mark Nettling, Dimitar Tasev, Dave Ventresca.

No Comments from: Brad Miller (x).


I found the readings to be pretty straight forward overall. One specific I found interesting was with regard to retrieving component types in arrays. I understand that usually a two- dimensional array is basically an array of arrays, however, it would seem more logical to me that when you getComponentType on a 2-D array of ints, the program should tell you that the component is an int instead of an array.

That strategy makes it harder for the program to determine whether something is a one-dimensional array of int values, a two-dimensional array of int values, or .... Yes, we could add other methods to the Array reflection class, but that makes things even less uniform.

The reflection API seems to be a way of creating GUI's that allow the user to create, manipulate and utilize Java-like things (objects, arrays, methods, etc) during run time of a program. My question is, if the user needs this much control over a program, why not just code program themselves? If they're competent enough to use the GUI to manipulate objects, arrays, etc... you would think they'd be able to code as well. Other than the few items the mainpage lists, what do people use the reflection API for in regular practice?

Many things other than GUIs. We'll cover a few in class. Also, note that in many cases, programmers don't have access to the original source code.

This is not really new information for me considering we used the java.lang.reflect package in Software Design. What I have learned about them though is that it is a powerful tool that can be used, but also a complicated one that should only be used when necessary. Though, as Sun suggests, it is best to use interfaces and abstract classes to type multiple similar classes, it is sometimes necessary to "open" an object to determine what it can or cannot do.

Based on Green's introduction, reflection enables one to inspect or manipulate the properties of a field, method, or class whose name can only be determined at runtime. It seems that reflection is a form of metaprogramming, since a reflective program handles itself or another program as data. There is no mention of the ability to alter method behavior, though. Does reflection support this?

The Proxy class suppors the alteration of method behavior. (Not as richly as you can in a language like Scheme, but still fairly richly.)

I am also curious as to whether there is any relationship between anonymous inner classes, e.g. event listeners, and reflection. I suppose anonymous classes come to mind because the API deals mainly with structures that remain anonymous until runtime. Could we say that reflection, like an anonymous class, is useful in an event-driven program?

While reflection may be useful in event-driven programs, it is useful in a host of other situations, too.

Having read Beyond Java, I found myself picking out the complications of the Java language exhibited in the reading on reflection. For example, I noticed the variety of set methods (for primitive types and objects) referenced by the authors. Ultimately, the reflection API seems like a very complicated way of dealing with Java's strict typing system. I noticed that a lot of the operations outlined in the reading, particularly object creation and field setting, would be simpler in a dynamic or weakly typed language with an eval function. However, I do like the way in which the reflection API allows a programmer to think about code in terms of a meta-hierarchy of class/method/field "descriptions" rather than a hierarchy of actual code. In the sense that it allows a programmer (although pretty much only those writing debugging tools) to step away from actual code, it serves as an example of Bruce Tate's call for languages that support "meta-programming".

Reflection is certainly a form of meta-programming. One of the reasons we're considering reflection in Java is Tate's emphasis on this idea.

Why are is there a different method to use a no-argument constructor versus a constructor with arguments? Could the authors have not simply overloaded the getConstructor method and then required that newInstance be called on a constructor for all objects?

You don't need to overload getConstructor. As far as I can tell, newInstance is just a shorthand for getting the zero-parameter constructor and then calling it. I expect that the oepration was common enough that the folks at Sun just decided to include it explicitly, rather than forcing each person to reimplement it.

I thought the reading was fairly straightforward in terms of showing how to use particular methods to do simple things like check if an object is an array, determine the class of an object, etc...

I was curious about what buttons do in Java. All I know is that button is a subclass of component. How do components differ from objects?

Components are objects that can be shoved in a GUI.

I was also unsure about the class modifiers abstract and final.

Abstract classes are classes that do not provide all of their methods. Think of them as a hybrid of interfaces and classes. Final classes cannot be subclassed (I think).

Why is it that in the sample program that invokes a method dynamically, the illegal access exception needs to be called?

You might try to call a method which you cannot legally call (e.g., because it is private or because it is protected and you are not in the same package or a subclass).

The reading was pretty straightforward, although the section on "Discovering Class Constructors" was very confusing in terms of how the arrays were created.

In what sense?

One thing I noticed in the code: Isn't it wasteful memory-wise to declare a variable inside a loop? Or does Java automatically take care of that?

The Java compiler is smart enough to handle that.

I was completely unaware of the existence of the Java features discussed in the Reflection API. However, I am not sure what exactly their point of use might be; normally the user is aware of the classes he/she created (I suppose they can be intesively used in the process of reverse engineering).

We'll cover applications in class.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Wed May 10 09:03:16 2006.
The source to the document was last modified on Mon Mar 6 09:48:57 2006.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS302/2006S/Readings/java-reflection.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu