user6719582 user6719582 - 2 months ago 4x
Java Question

How to annotate java package and reflect metadata

I would like to annotite java package using simple annotation:

public @interface Related {
int apiVersion() default 0;

However when I try to add it to any package I've got compilation error

Error:(1, 14) java: cannot find symbol
symbol: class Related
location: package

Any help appreciated!


After searching a bit I found also this kind of error

Error:(1, 1) java: package annotations should be in file


To be able to place annotation on package you should create file which should contain package definition like this:


To be able to reflect reflect package you also need to set up RetentionPolicy.RUNTIME

public @interface Related {
    // stuff...

And then you can finally reflect package:


You can also reflect package by name using java.lang.Package