finding classes with specific annotation

  • Thread starter Leonard Milcin Jr.
  • Start date
L

Leonard Milcin Jr.

Hi!

Is it possible to find classes that are annotated with specific annotation?

I'm writing an application and I'd like to create very simple mechanism
that would allow me to use plugins. I would like to have a name for each
plugin and I would like to be able to find a class given this name (and
not the class name...). For example, I have an annotation like this:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Plugin {

String value();

}

and somewhere in the classpath there is a class TestTask:

@Plugin("testTask")
public class TestTask extends BaseTask {

static final Logger logger = Logger.getLogger(TestTask.class);

public void execute() throws TaskExecutionException {
logger.info("message from a plugin");
}
}

I'd like to be able to find class TestTask given the string testTask so
I could instantiate it and run whatever it does (in this case print
message to Log4J). How to do this?

Best Regards,
Leonard Milcin
 
J

Joshua Cranmer

Hi!

Is it possible to find classes that are annotated with specific
annotation?

I'd like to be able to find class TestTask given the string testTask so
I could instantiate it and run whatever it does (in this case print
message to Log4J). How to do this?

Best Regards,
Leonard Milcin

Your best bet is to manually parse the classpath to find all classes, and
then test each class for that annotation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top