Amnesiac Java: language features I forgot existed 19
I've been developing in Java for pretty much since Java 1.0. While working on the ANTLR Java 5 grammar I was suprised to rediscover some features of the language I'd just plain forgot.
First up, while working on annotation array initialisers, I was pleasantly reminded that the final element of a regular array can have a trailing comma:
String[] foo =
new String[]
{
"qwerty1",
"qwerty2",
"qwerty3",
};
This is a nice little convenience for when you need to expand or contract the array elements in the initialiser - it means you don't have to fiddle with the final element to make sure there is no trailing comma.
Later I was reminded that interfaces may be package protected. For some reason i'd deluded myself into thinking interfaces could only be public:
package foo;
interface Bar {}
...
package foo2;
class Bar2
{
foo.Bar b;
//compilation error: foo.Bar can't
//be accessed from outside its package
}
Duh! How could I have lived without that one? I often complained to co-workers that I couldn't do this - by their response they obviously didn't know you could either.
Finally, I was recently reminded that you can use the .class 'field' on primitive types:
Class c = int.class;
I've long been aware of this 'field' on regular classes and interfaces (e.g. String.class) but when faced with the need to get the class of a primitve type I'd use the helper fields (e.g. Integer.TYPE). This forgotten feature is nicely consistent.
I use quotations around the word field here because it's not really a field, just a few grains of syntactic sugar (decompile a class that uses the .class 'field' to see what I mean).
What's your forgotten language feature?
My Java 5 Antlr grammar 6
After years of toying with Antlr but never actually doing anything useful with it; and after months of merely skimming articles on new Java 5 language features I thought I’d deepen my understanding of both and update the Antlr Java 1.4 grammar to recognize the new language features.
Suprisingly it only took about a week to polish off, and most of that was spent just cleaning up the grammar and deciding between different ways to structure the output AST (rather than strugling with Antlr tool itself).
For anyone interested, my grammar can be found on my site and it’s also hosted at antlr.org
As for how well tested it is: I’ve used it to successfully recognise the entire Java 5 source base, jdigraph’s source base (an early adopter of generics) and some custom tests I developed. I found the accompanying AST recogniser (as opposed to the token stream recogniser) is a good tool for confirming your tree is structured as you expected it to be. I also visually inspected the AST to ensure its structure was what I expected. An alternative to visual inspection would be great to automatically ensure the grammar stays correct over a lifetime of modifications. I’d love to hear from anyone who’s worked on such a tool. A testing framework that used XQuery or XPath querying/assertions on DOM-adapted AST (similar to PMD’s AST interface) would be my first approach. Comments or experience anyone?
More comments on Antlr and Java 5’s new language features later.
I'm a Grammar God
According to BaalObsidian , I’m a grammar God!
Here’s their HTML badge to prove it:

You are a GRAMMAR GOD!
If your mission in life is not already to
preserve the English tongue, it should be.
Congratulations and thank you!
How grammatically sound are you?
brought to you by Quizilla
I think they mean the American English tongue, though.