Pain-Free Exploratory Programming with JavaSE 6.0
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.
[...] 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): [...]
Really Understanding JavaScript's Equality and Identity « The Curious Schemer
March 18, 2007 at 3:35 pm
[...] Most of them were arrays that contained objects, and it worked perfectly, like demonstrated in my favourite tool jrunscript [...]
Why Won't eval() Eval My JSON? (Or: JSON Object !== Object Literal) « The Curious Schemer
March 28, 2007 at 6:14 pm