Friday, January 30, 2009

Javasaurus

Nothing about dinosaurs; apologies to any 6-year-olds I've misled.

There's an interface in the Java libraries called "Runnable", that just packages up the idea of "some code that you might want to run." This is handy when writing algorithms like "do some preparation; then run whatever it is the client wants to run; then do some clean-up." It's a way to hand a series of program steps from one module to another without having to know in advance what those steps are. ("Closures," much debated in Java, are another way of doing this.)

Runnable defines one method, "run". But the "run" method doesn't allow for the possibility of failure. I needed something similar, that was allowed to communicate failure (by throwing an exception). I knew there was something, but what? A search of the likely spots didn't turn up what I was looking for.

It would be really cool if there was a Thesaurus of Java, a tool or a web site that would let me type in "Runnable" and would come back with all the other things that were kind of like "Runnable." In this case the answer, provided by my colleague Hung, is "Callable." Doh.

A similar task that comes up for me a lot is that I've got a This, and I know it's possible to convert it to a That, but I'm not sure how. For instance, let's say I've got an Eclipse IFile, and I want to convert it to an ordinary Java File. The mapping isn't perfect, but basically, given an IFile there is a chain of methods you can call that will either get you the corresponding File or tell you it doesn't exist. But what is that chain of methods?

There's a finite number of methods that take an IFile as an argument (or receiver). There's a finite number of methods that produce a File. So there's a finite, although very large, possible graph between them - for instance, you could imagine calling something like IFile.getURL() and then URL.getFileDescriptor() and then FileDescriptor.getFile(). (I just made those names up, that's not the real answer.)

Most of the paths through the graph will be wrong, and some will be long and some will be short. But you could use the same sort of semantic analysis tools that are used for natural language translation, feeding off existing code (such as the Eclipse code base, in this example), to inform the graph of common versus uncommon pairings. I'd enter my start and end types, and I'd see a view of the top ten paths through the graph of possible method calls connecting them, perhaps even with links to existing code examples where something like that path had been used before.

I tried Googling for "Java Thesaurus" to see if this existed already, but all that comes up is Java code for writing thesauri.

2 comments:

Mike Morearty said...

Walter, a couple of years ago I came across a project that does exactly what you describe -- and even better, their proof of concept was targeted specifically toward the Eclipse codebase, and included an Eclipse plugin!

I tried Prospector at the time, and it did work fairly well. You could use either the Eclipse plugin or the website; I typically used the website. If I typed in "org.eclipse.core.resources.IFile" in the From box, and "java.io.File" in the To box, it would then give a long list of code fragments that it had found in various places, that appeared to the do the mapping. Some of them were wrong, but it was good enough -- it was easy for a person to read the results and find the one he was looking for.

And yet sadly, it seems to be gone. It was a UC Berkeley research project called Prospector. It used to live at http://snobol.cs.berkeley.edu .

However, I did find http://www.cs.berkeley.edu/~bodik/research/prospector.html , which links to a PDF that describes the project in great detail. Even if you just glance at the first page, and the code sample in the right-hand column, I know you'll be saying, "Yes, that's what I want!"

Maybe you should ping the author and ask about the state of the project.

Walter Harley said...

Yes, that's what I want! That's exactly it. Wow, I feel like I'm in a parallel universe now.

Looks like the advisor, Ras Bodik, is on sabbatical through this spring. I'll try to track down David Mandelin, the student whose project it was - evidently he's now working at Mozilla.