Monday, September 15, 2014

Building a Java 8 JavaDocs link

One of those features I definitely prize in my open source app for Java developers called Class Viewer is the ability to go to JavaDocs to the method. And for that to happen the app has to build the link for you, like here's an example that I just did with it:

And that works for Java 8, where you can see the '8' in the link, where that is in the base which is set in ClassViewerConfig.xml--where you can edit with a text editor so easy to make changes--and the app builds out the rest of the link where I picked a method from the String class for demonstration purposes:

static String format(Locale,String,Object[])

Putting in italics so it stands out. And with that as a reference you can see what the app has to do, which is just loop through that, which it gets from Java Reflections and is what it has after it strips off package information, and switch that to the format that was just implemented for getting to that method in JavaDocs.

So yeah, I kind of had a bit of a freak out a few months ago as I knew Java 8 was coming out and had changes but didn't really dig into them, which I guess was a mistake, as I made some changes to Class Viewer, and thought that was it, until I checked going to JavaDocs and it just didn't work! Then I realized there had been a change.

For the curious you can go to JavaDocs before Java 8 and see the old format. What I'll say is that I like this new format as it's MUCH easier to code, which is what matters to me.

Oh yeah, it had some quirky stuff and maybe I missed something so if anyone finds something PLEASE let me know, as like Object arrays go to Object..., but check out how other arrays are handled with this example:

And that's from:

static String copyValueOf(char[],int,int)

So you can see that here the char[] turns into char:A, which I noticed immediately and coded up, only to thankfully try something with an Object array and notice it didn't work! So yeah, not completely sure I got everything, but...well it works for everything I've tried.

One of the best things with this new approach is it really is more fun to code and removes issues with handling spaces, where if you wonder you can go back to earlier versions.

At first I was going to go to the newer version without support for older JavaDocs and realized that was silly since the app could just check your Java version, which it does from the System. So if you have an older version before Java 8, it will use the old way to get to JavaDocs.

James Harris
Post a Comment