Joe Walnes
  Blog



Recent Entries

Creative uses of Hamcrest matchers

Hamcrest 1.1 released

Testing on the Toilet

Building testable AJAX apps (Does my button look big in this?)

QDox is back - 1.6 released

Java and .NET RESTful interoperability with XStream

I've joined Google

OSCon: SiteMesh, SiteMesh, SiteMesh, SiteMesh

Flexible JUnit assertions with assertThat()

SiteMesh and Content Management @ O'Reilly OpenSource Conference

XStream 1.1.2 released. Java 5 Enums, JavaBeans, field aliasing, StAX, and more...

VB.Net is the bestest

XStream 1.1.1 released

Accessing generic type information at runtime

XStream 1.1 released

JUnit tip: Setting the default timezone with a TestDecorator

XStream: how to serialize objects to non XML formats

How my backflip went...

Backflippin' in 4 hours.

Is 100% test coverage a BAD thing?

Looking back at the SiteMesh HTML parser

The road ahead for SiteMesh 3

Joe's Backflipping for Autistic Research - time is nearly up...

SiteMesh 2.2 Released

Advanced SiteMesh

More... [RSS | RDF]

About Joe Walnes

I am a software engineer for Google, based in London.

Open Source

WebStuff (coming soon)

XStream

ActiveMQ

SiteMesh

QDox

nMock

jMock

Pico Container

Nano Container

OpenSymphony

Squiggle

MockDoclet

MockObjects

Jelly

Groovy

PatternStitcher

XJB

Books

Java Open Source Programming, Wiley JSP Site Design, Wrox

Talks

Mock Roles, not Objects
October 26 2004, Vancouver, Canada. OOPSLA'04

Personal Development Practices Map
June 24 2004, Salt Lake City, Utah. Agile Development Conference

SiteMesh.NET and ASP.NET MasterPages
May 20 2004, Bangalore, India. Bangalore .NET User Group

Mock Objects: Driving Top Down Development
March 29 2004, St Neots, UK. OT2004

Mock Objects
December 2 2003, London, UK. XP Day 3


A simple way to access JavaDoc attributes at runtime

It's easy to gain access to JavaDoc attributes using QDox, however this requires you to have the source available. At runtime this isn't always possible, unless you're happy giving your source away.

A workaround to this is to use QDox to parse the source code at development time and serialize the resulting structures to make them available at runtime. This will work, but it's a bit nasty as big binary files aren't the most stable of things.

A more open approach is to bundle the source of your classes with the application, making them available at runtime, however without the method body implementations.

Class with method body implementations:

package stuff;

public class CheeseSlicer {

  /** @transaction mode=isolated */
  public Slice feedMe() {
    // some implementation specific stuff here.. don't waste your time reading it.
    if (System.currentTimeMillisMillis() % 2 == 0) {
      return new CheddarSlice();
    } else {
      return new BrieSlice();
    }
  }

}

Class WITHOUT method body implementations:

package stuff;

public class CheeseSlicer {

  /** @transaction mode=isolated */
  public Slice feedMe();

}

To do this is simple. First, QDox can be used to parse the source of a class into a JavaClass. Calling JavaClass.toString() will print out the class without the body implementations (as above). This is something done at development time and the result should be written to a file that is available at runtime.

At runtime, QDox can read this file just as if it were a full-blown Java source file, making the attributes available.

JDK 1.5, of course, makes this redundant, but in the mean time...

Comments

Carlos Villela

Another idea is to use XStream instead, so you don't get a huge binary file. But hey, you wrote XStre... ooooh... nevermind :))

peter royal

also, http://metaclass.codehaus.org/ :)

Joe-,
Wake up, buddy - J2SE5.0 is here! With the latest snapshot they have bundled "apt", which makes QDox very obsolete

Yes, please use a bloated XML format instead of a binary encoding. Java software is nowhere near slow enough yet.

Joe Walnes

Quick replies:

> Another idea is to use XStream instead,

That was considered too. But that's another dependency. And serialization is much slower and more bloated.

> also, http://metaclass.codehaus.org/

Yup. There's this too, which also provides a nice abstraction of meta-data, allowing another mechanism to be used than attributes. I was merely suggesting a lightweight approach that doesnt require another library and gives you access to the full JavaDoc model at runtime.

MetaClass is a more full featured solution.

> Wake up, buddy - J2SE5.0 is here! With the latest
> snapshot they have bundled "apt", which makes
> QDox very obsolete

Absolutely. QDox is obsolete if using J2SE5.0. However I am still working on lots of project that are not using this yet, as are many other people. I imagine this will be the case for a while longer as well.

> Yes, please use a bloated XML format instead of
> a binary encoding. Java software is nowhere near
> slow enough yet.

Ermm... I don't ever remember suggesting using XML :P. Using QDox to parse scan the source code is about TWICE as fast as native serialization, AND the file sizes are smaller, AND it's plain text! So nurrrr.

Mike Williams

> So nurrrr.

Nice conclusion.

Stuart Barlow

Does java 1.5 make javadoc available at runtime?

Name:
Email:
URL:

ThoughtBloggers

Martin Fowler

Dan North

Aslak Hellesoy

Darren Hobbs

Geoff Oliphant

Mike Roberts

Chris Stevenson

Jon Tirsen

Loads More...

Agile Bloggers

Ken Arnold

Ward Cunningham

Brian Marick

Robert Martin

Bret Pettichord

Java Bloggers

Ara Abrahamian

Mike Cannon-Brookes

Vincent Massol

Bob McWhirter

Rickard Oberg

Joseph Ottinger

James Strachan

Hani Suleiman

Communities

eXtreme Tuesday Club (XTC)

Thursday GeekSpeek

ThoughtWorks GeekNight

London Java Meetup

The Codehaus

[RSS | RDF]
© 2001-2004, Joe Walnes

Powered by SiteMesh and Moveable Type.