Lambda the Ultimate

inactiveTopic A Simple Reflective Interpreter
started 10/23/2000; 8:52:23 AM - last post 10/27/2000; 7:47:29 AM
Ehud Lamm - A Simple Reflective Interpreter  blueArrow
10/23/2000; 8:52:23 AM (reads: 1377, responses: 10)
A Simple Reflective Interpreter
I've been working on reflection a bit, and came across this paper. It uses Scheme, to implement an interpreter supporting reflection (and reification).

It is very much in the EOPL style, giving the actual code.

I find the Java reflection support a bit limited in scope, but I haven't really studied it in depth. If anyone has links to good discussions/examples, I'd be glad to see them.

Simple question: Any way I can get from a class to its subclasses?
Posted to "" by Ehud Lamm on 10/23/00; 8:54:16 AM

Chris Rathman - Re: A Simple Reflective Interpreter  blueArrow
10/23/2000; 11:02:27 AM (reads: 1423, responses: 5)
Simple question: Any way I can get from a class to its subclasses?
Very simple in Smalltalk since the Smalltalk dictionary holds a list of all objects (classes in ST are also objects) in the image. Other than Smalltalk, I can't think of any other language that has the ability to traverse down through subclasses. In Java, specifically, a superclass is never cognizant of it's subclasses.

Ehud Lamm - Re: A Simple Reflective Interpreter  blueArrow
10/23/2000; 1:09:41 PM (reads: 1466, responses: 4)
In Java, specifically, a superclass is never cognizant of it's subclasses.

In the normal scheme of things this is, of course, a Good Thing. OCP and all that.

I just think that reflection breaks enough access checks, and information hiding, that this should also be possible...

Chris Rathman - Re: A Simple Reflective Interpreter  blueArrow
10/23/2000; 8:21:13 PM (reads: 1530, responses: 3)
As I said, this is a simple matter from Smalltalk. Specifically, you can get a collection of subclasses or superclasses just by sending a message to the class you want to know about (the example asks about the subclasses and superclasses for the String class).

| myCollection |

"immediate subclasses"
myCollection := String subclasses.

"all subclasses"
myCollection := String allSubclasses.

"immediate superclass"
myCollection := String superclass.

"all superclasses"
myCollection := String allSuperclasses.

The beauty of Smalltalk is that the image acts as a repository for objects - being sort of a object database. The image holds all the information for the defined classes and can be accessed via the main Smalltalk dictionary object which holds both a collection of all the metaclass definitions and any and all objects which have been instantiated. Beyond just asking about the hierarchy, you can even do stuff like count the number of instances of a particular object type (how many strings are loaded in memory?).

From the Java perspective (indeed from most languages that don't have the concept of the Smalltalk image), the question is a bit more difficult. The only thing that you possibly reflect on is the the classes and objects which have been loaded and instantiated up to that point in time. We could, theoretically, have the JVM return all subclasses which have been loaded into the JVM up to that point in time. Even when the JVM garbage collects objects, it has a tendency to retain the class definition in memory for a much longer period of time - making it possible to know the subclass, even if all the instances have gone through GC.

But, for some reason, Java is not friendly to the notion of reflecting on the classes which have been loaded by the classloader and reversing out the class hierarchy. The Security model tends to try to hide such things from user level classes. The only way to get around the security is to write your own class loader and then save off the class information as you go and then expose that information to whatever classes you want to give access.

So, yes, it is possible to extract and save off the information with a custom classloader, but the standard classloader (nor the default reflection methods) does not provide that facility.

Another alternative which IBM uses in the VAJ tool is to save off class metadata as part of the development environment. That is, you may not be able to get runtime reflection, but the development tool can provide that information to you for inspection. Indeed, the inspection tools for VAJ are very much based upon the inspection tools built into Visual Age for Smalltalk (much of VAJ is actually written in Smalltalk and runs on top of a Smalltalk VM).

andrew cooke - Re: A Simple Reflective Interpreter  blueArrow
10/24/2000; 6:38:04 AM (reads: 1598, responses: 2)
So, yes, it is possible to extract and save off the information with a custom classloader

And even then, I think you'd only be able to get subclasses that had already been referenced(?).

Reflection in Java is ugly and tedious.

Ehud Lamm - Re: A Simple Reflective Interpreter  blueArrow
10/24/2000; 9:22:50 AM (reads: 1661, responses: 1)
Reflection in Java is ugly and tedious.

I came to pretty much the same conclusion.

Any suggestions for better reflection support in a mainstream language, aside from Smalltalk?

andrew cooke - Re: A Simple Reflective Interpreter  blueArrow
10/25/2000; 3:48:09 AM (reads: 1720, responses: 0)
Any suggestions for better reflection support in a mainstream language

I suspect CLOS (Common Lisp's Object System) might be eaier to use (than Java) - I've never used reflection, but the index to AMOP (Kiczales et al) lists methods that return super and sub classes, and getting slot names is fairly simple. Dynamic typing probably helps a lot.

Common Lisp is always a good candidate for "does any language do...?" questions!

Ehud Lamm - Re: A Simple Reflective Interpreter  blueArrow
10/25/2000; 6:16:43 AM (reads: 1398, responses: 0)
When I phrased my question, I wasn't sure if I should write "aside from Smalltalk" or "aside from Smalltalk and CLOS"...

I haven't really used it, but reflection is quite connected to meta-object-proctocols (MOPs), and the classic here is indeed the CLOS book/implementation.

I haven't read the book, just keep seeing references to it... Guess I have to go find it. Is it worth the trouble, or is the book just a spec?

andrew cooke - Re: A Simple Reflective Interpreter  blueArrow
10/25/2000; 1:56:22 PM (reads: 1387, responses: 0)
Is it worth the trouble, or is the book just a spec?

Well, I was disappointed - I never finished it. I guess I was hoping for something more general, which was a bit silly, as it really is an advanced book about MOPs. The first third/half is a general discussion with code on MOP design and the second half/two-thirds is a detailed spec for CLOS. If you want to know about CLOS it's probably very good (although I think it covers a subset of the final ANSI standard). But it's not a great programming book that is also about CLOS...

In other words, if you want to spend you're time arguing with Erik, you might need it. But if you don't know who Erik is, I wouldn't bother ;-)

(I hope that comment still aplies - I've not read c.l.lisp lately!)

Ehud Lamm - Re: A Simple Reflective Interpreter  blueArrow
10/27/2000; 4:28:14 AM (reads: 1375, responses: 0)
I know what you are talking about.... ":->"

In fact I am more interested in the discussion of the MOP design, than I am interested in CLOS. At least at the moment.

Any better references?

andrew cooke - Re: A Simple Reflective Interpreter  blueArrow
10/27/2000; 7:47:29 AM (reads: 1381, responses: 0)
I don't know of anything better, but I'm no expert. Looking at AMOP again, with your requirements in mind, I guess about half of the first half/third (130 pages out of 310) is relevant. It is Lisp specific, with Lisp code, but the general approach must be fairly general (assuming that whatever language you are thinking of has something that matches "slots" and I guess all OO languages do).