Redandwhite
Redandwhite

Reputation: 2549

Get javadoc version for a class?

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.

Upvotes: 1

Views: 1879

Answers (2)

Joachim Sauer
Joachim Sauer

Reputation: 308021

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();

Upvotes: 1

liwp
liwp

Reputation: 6926

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()).

Upvotes: 5

Related Questions