tags:

views:

124

answers:

2

I use the javadoc @version tag in my classes, however I am not sure how to get the version in the class itself. Let's look at an example...

/**
 * @version 1.0a
 * @author Redandwhite
 */
public class JClass extends JFrame implements ActionListener, KeyListener {
    String version = ...
}

Is there a method/class that can retrieve the version? Ideally, it's as simple as an API call which returns a simple String.

+5  A: 

The javadoc comments are not included in the generated byte code in any form, so there is no way to access the value of the @version tag from Java code (unless you parse the source code of course). There might be a version annotation that can be used to specify the class version instead of or in addition to the javadoc @version tag, and this annotation would be accessible via the Java reflection API (i.e. Class.getAnnotations()).

liwp
One little caveat: The `@deprecated` JavaDoc tag (not to be confused with the annotation called `@Deprecated`) is actually represented in the class file (only its existence, not the optional comment). That's the only place where the content of a comment influences the result of the compilation in Java.
Joachim Sauer
that is indeed unfortunate. Thanks for the helpful info
Redandwhite
A: 

As it's already been noted, it's not possible to get to that information.

An alternative solution is to read the Implementation-Version/Specification-Version property of the package.

That value can be defined in the MANIFEST.MF file of a jar file. If it is defined, then it can be queried for every package that's contained in that jar file.

So if you have a class org.example.MyClass and the jar it comes in has the relevant entries in the MANIFEST.MF file, then you can get the version like this:

Class<?> clazz = org.example.MyClass.class
Package pkg = clazz.getPackage();
String version = pkg.getImplementationVersion();
Joachim Sauer
this seems in line with what i'm looking for.. any idea how to set the VERSION value for the MANIFEST.MF file from within Netbeans?
Redandwhite