The Curious Schemer

The following sentence is false. The preceding sentence is true.

Pain-Free Exploratory Programming with JavaSE 6.0

with 2 comments

You know how sometimes you’re exploring a new Java API, and you don’t want to create a class (public static void main blah blah), compile it, (or create project if you’re using an IDE), typing System.out.println to see the output, just to find out how that API works? I really hate that. Especially if it’s a 3rd party library

Java SE 6.0 in this regard. It makes it really easy to explore an API without having to compile a class just to see how it works.

Trying Out Regular Expressions

For instance, let’s say we want to escape all single quotation marks, double quotation marks, and backslashes in a Java String (say because you want to pass it to JavaScript). A Perl guru can probably write the regex in 3 seconds, but my regex is rusty. I want to be able to try it out, and then paste the result into my code with just a few modifications.

Since I have Java 6.0, I just have to start jrunscript. By default it gives me a JavaScript console. Now it’s easy to try out different regular expressions:

js> var s = new java.lang.String("Testin' this thing: Test' test \" test \\ ");

The objective is to add an escaping backslash in front of ‘, “, and \ within the string. First trial:

js> s.replaceAll("[\\"'\\\\]", "\\$1");
script error: sun.org.mozilla.javascript.internal.EvaluatorException: unterminated string literal (<STDIN>#1) in <STDIN> at line number 1

Hmmm. That didn’t go so well. Oh, too many backslashes for the double quotation marks! Second trial–I just pressed the up arrow, edit the expression, and press Enter:

js> s.replaceAll("[\"'\\\\]", "\\$1");
Testin$1 this thing: Test$1 test $1 test $1

Meh. Third trial:

js> s.replaceAll("[\"'\\\\]", "\\$0");
Testin$0 this thing: Test$0 test $0 test $0

Ah, right, the $ needs to be escaped:

js> s.replaceAll("[\"'\\\\]", "\\\\$0");
Testin\' this thing: Test\' test \" test \\

Sweet! No class, no compiling, no System.out.println, just trying immediately the API you want.

Trying Out New Third-Party Libraries

Java’s Calendar and Date are quite… emasculating. I believe that using them too much shorten your lifespan and in all probability has disastrous effect on your sex life. Self-respecting developers who care about sex use Joda Time. Joda Time has been submitted as a JSR, and I’m wishing it every success. May java.util.Date and java.util.Calendar die a horrible death ASAP.

So, let’s say you’ve downloaded Joda’s JAR and would like to explore. Just run jrunscript this way (obviously change the location of the Joda’s JAR accordingly:

jrunscript -cp C:\apps\jars\joda-time-1.4.jar

and import the package:

js> importPackage(org.joda.time);

and we can start exploring right away:

js> var now = new DateTime();
js> now
2007-03-17T09:14:43.656+08:00
js> var oneMonth = new Period().withMonths(1);
js> oneMonth
P1M
js> now.plus(oneMonth);
2007-04-17T09:14:43.656+08:00
js> now.plus(oneMonth).plus(new Period().withDays(31));
2007-05-18T09:14:43.656+08:00

It’s easy to get all the static members of, say, Period:

js> for(m in Period) { println(m); }
days
months
millis
.
.
.

and the instance members:

js> for(m in (new Period())) { println(m); }
getMonths
withMinutes
withFieldAdded
.
.
.

Nice! Trying out Swing also becomes real easy with this. Frankly my Java development is all Web these days, though.

Written by rayfd

March 17, 2007 at 1:23 am

Posted in Java, JavaScript, Technology

2 Responses

Subscribe to comments with RSS.

  1. [...] I dug deeper, and I found something that programmers coming from other languages may find surprising. Examples will illustrate this best (I’m using the jrunscript console that I blogged about here): [...]

  2. [...] Most of them were arrays that contained objects, and it worked perfectly, like demonstrated in my favourite tool jrunscript [...]


Leave a Reply