Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

finding classes with specific annotation

Thread view: 
Leonard Milcin Jr. - 05 Jul 2007 17:19 GMT
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
Joshua Cranmer - 05 Jul 2007 21:43 GMT
> Hi!
>
[quoted text clipped - 7 lines]
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.