If I were in your place, I'd just learn enough Java to be proficient. Java is a very powerful language and certainly better suited for mundane tasks than your preprocessor.
There's a couple of extensible preprocessors that aim to do exactly this. They're called 'clojure', 'scala', and 'kawa'.
ImmutableSet<Foo> foos = ImmutableSet.of(a, few, elements);
ImmutableMap<Key, Value> map = ImmutableMap.of(k1, v1, k2, v2);You don't have to do it even if you are writing Java. This is a perfect example of clever for the sake of being clever.
http://jcp.org/en/jsr/detail?id=337
Today Java lets you do the following:
List<String> myList = Arrays.asList("one", "two", "three");
Scala and Groovy which target the JVM let you do things like:
Scala: val myList = List("one", "two", "three")
Groovy: def myList = ["one", "two", "three"]
Then there's JRuby, Jython etc.
This is often not good enough, as Arrays.asList returns some internal immutable List type. If you then tried to do something like myList.add("four"); you'd get an exception.
What you'd actually need to do is something more like:
> List<String> myList = new ArrayList<String>(Arrays.asList("one", "two", "three"));
Compare that to more dynamic languages, where that example would be:
> myList = ["one", "two", "three"]
Yes, it's a trivial example, but it's something many programs involve all over the place, and the boilerplate adds up a tedious task.
Most of the examples given are creating static objects but the last example a non-static object is created in the same way. Imagine you are doing this a couple of times then you end up with a lot of anonymous classes. Isn't there a limit on the number of classes you can have? At least it makes debugging problematic.
But I might be wrong about this since I am not a Java expert...
You just stuff a hundred of jars full of classes into your classpath and never look back.
JVM hums happily and does not care about however much classes you have. No problems with debugging either because why would it be? Anonymous classes are as introspective as non-anonymous.
The cases in which to worry about stuff like this is if you need to load the code on someone else's machine, like with an applet or Android.
I consider myself pretty liberal when it comes to coding standards and being clever, but this is something that will badly piss me off. I can't quite put my finger on why I feel so, considering I am perfectly fine with most of the perl tricks. For example, https://gist.github.com/3392206 looks good to me(you are using Perl, you should know about list and scalar contexts).