Reputation: 23
I would like to annotite java package using simple annotation:
@Target(ElementType.PACKAGE)
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 com.test.xxx
Any help appreciated!
EDIT
After searching a bit I found also this kind of error
Error:(1, 1) java: package annotations should be in file package-info.java
Upvotes: 2
Views: 449
Reputation: 11250
To be able to place annotation on package you should create package-info.java
file which should contain package definition like this:
@Related
package tld.some.name;
To be able to reflect reflect package you also need to set up RetentionPolicy.RUNTIME
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PACKAGE)
public @interface Related {
// stuff...
}
And then you can finally reflect package:
SomeClassInPackage.class.getPackage().getAnnotation(Related.class)
You can also reflect package by name using java.lang.Package
Package.getPackage("tld.some.name").getAnnotation(Related.class)
Upvotes: 1