Zharro
Zharro

Reputation: 809

Annotation for naming classes to be read by reflection

My application allows developers to create some plugins. Сompliance with the requirements determined by base abstract class. Each plugin must have a name. I want to solve this problem by using annotations. So, I defined my own annotation Name as follows:

@Retention(RetentionPolicy.RUNTIME)
public @interface Name {
public String value();
}

I put this annotaion and base abstract class CommonPlugin into separate module and build it as JAR-file to share with developers.

Then I import package and put this annotation before the defenition of me test plugin as follows:

@Name("Test plugin")
public class TestPlugin extends CommonPlugin {

Then I reflect all given plugins through URLClassLoader and can't find necessary annotation at TestPlugin class. But if I define Name annotation into the same package the TestPlugin class is, I can find it. It should be so or am I doing something wrong?

Upvotes: 1

Views: 143

Answers (1)

MvG
MvG

Reputation: 60988

Turning my coment into an answer so it can be accepted.

Make sure that the unqualified name Name refers to the same qualified name in all of your source files. In sources from different packages than the one containing the annotation, there should be a non-wildcard import for Name, and there should be no class with that name in that other package itself.

Upvotes: 1

Related Questions