Combining Java Reflection API with Java Annotation Types for Thread Safety

Discussion in 'Java' started by pek, Oct 22, 2007.

  1. pek

    pek Guest

    Hello everyone. I have a really complicated question (and I don't
    really know if this is even possible).

    I am currently developing a program that is multithreaded. I have a
    lot of methods that start a thread and call other methods from other
    classes. All the time, I have to make sure that the class's method
    that I am calling is correctly synchronized (if needed) or not. So
    here is what I was looking the last couple of days.

    I know Java has an Annotation Type system that it can help me mark
    various methods in a class for a particular purpose (such as a @Test
    or @FixThis). So I was thinking, this could also work if every method
    that inside the code a thread is initiated, I could Annotate it as
    @ThreadCreator. Also, I could annotate a method that is being called
    from a thread as @ThreadSafe (making sure of course that it is indeed
    a thread-safe method). This is the first part.

    The second part is to use Java Reflection API. I saw that I can use
    this API to find what annotations a class uses and where. So I know I
    could find out that a particular method in a class is annotated as
    @ThreadCreator.

    Now, combining this together, I was wondering if I could create a
    "system" (I don't really know how to call it) that would take a class
    that I give it, find which methods are annotated with @ThreadCreator,
    invoke those methods, and see if the methods call other methods that
    are not annotated as @ThreadSafe, in which case the "system" will warn
    me that this should be @ThreadSafe.

    Is this even possible? Is there any other solution to this? Is there
    already an implementation of this?

    Thank you very much for your time.
    pek, Oct 22, 2007
    #1
    1. Advertising

  2. Re: Combining Java Reflection API with Java Annotation Types forThread Safety

    pek wrote:
    > I know Java has an Annotation Type system that it can help me mark
    > various methods in a class for a particular purpose (such as a @Test
    > or @FixThis). So I was thinking, this could also work if every method
    > that inside the code a thread is initiated, I could Annotate it as
    > @ThreadCreator. Also, I could annotate a method that is being called
    > from a thread as @ThreadSafe (making sure of course that it is indeed
    > a thread-safe method). This is the first part.


    Using annotations for static analysis? I would like to see how this
    works out: I've been considering using some for moving some internal
    precondition checks to pseudo-compile time.

    > The second part is to use Java Reflection API. I saw that I can use
    > this API to find what annotations a class uses and where. So I know I
    > could find out that a particular method in a class is annotated as
    > @ThreadCreator.
    >
    > Now, combining this together, I was wondering if I could create a
    > "system" (I don't really know how to call it) that would take a class
    > that I give it, find which methods are annotated with @ThreadCreator,
    > invoke those methods, and see if the methods call other methods that
    > are not annotated as @ThreadSafe, in which case the "system" will warn
    > me that this should be @ThreadSafe.
    >
    > Is this even possible? Is there any other solution to this? Is there
    > already an implementation of this?


    I *think* the best way to proceed would be to use the annotation
    processing tool. I have not used this tool at all (then again, my use of
    annotations is rather limited at present), but it seems it would be far
    easier than processing runtime loading components. Speaking from my
    limited experience, processing annotations at runtime loading of classes
    is a nightmare in terms of initial implementation.

    As far other implementations, I know of none, but I would not be
    surprised to see an existing implementation (soliciting help from any
    resident concurrent programming gurus lurking about).

    I also have reservations about your verification algorithm as stated,
    but since I am not an expert in concurrent programming, and since it
    appears to me that this is merely a conceptual-level design, I am sure
    that you have already handled the objects of my fears, so I will not
    mention them.

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
    Joshua Cranmer, Oct 23, 2007
    #2
    1. Advertising

  3. pek

    pek Guest

    On Oct 23, 5:25 am, Joshua Cranmer <> wrote:
    > pek wrote:
    > > I know Java has an Annotation Type system that it can help me mark
    > > various methods in a class for a particular purpose (such as a @Test
    > > or @FixThis). So I was thinking, this could also work if every method
    > > that inside the code a thread is initiated, I could Annotate it as
    > > @ThreadCreator. Also, I could annotate a method that is being called
    > > from a thread as @ThreadSafe (making sure of course that it is indeed
    > > a thread-safe method). This is the first part.

    >
    > Using annotations for static analysis? I would like to see how this
    > works out: I've been considering using some for moving some internal
    > precondition checks to pseudo-compile time.
    >
    > > The second part is to use Java Reflection API. I saw that I can use
    > > this API to find what annotations a class uses and where. So I know I
    > > could find out that a particular method in a class is annotated as
    > > @ThreadCreator.

    >
    > > Now, combining this together, I was wondering if I could create a
    > > "system" (I don't really know how to call it) that would take a class
    > > that I give it, find which methods are annotated with @ThreadCreator,
    > > invoke those methods, and see if the methods call other methods that
    > > are not annotated as @ThreadSafe, in which case the "system" will warn
    > > me that this should be @ThreadSafe.

    >
    > > Is this even possible? Is there any other solution to this? Is there
    > > already an implementation of this?

    >
    > I *think* the best way to proceed would be to use the annotation
    > processing tool. I have not used this tool at all (then again, my use of
    > annotations is rather limited at present), but it seems it would be far
    > easier than processing runtime loading components. Speaking from my
    > limited experience, processing annotations at runtime loading of classes
    > is a nightmare in terms of initial implementation.
    >
    > As far other implementations, I know of none, but I would not be
    > surprised to see an existing implementation (soliciting help from any
    > resident concurrent programming gurus lurking about).
    >
    > I also have reservations about your verification algorithm as stated,
    > but since I am not an expert in concurrent programming, and since it
    > appears to me that this is merely a conceptual-level design, I am sure
    > that you have already handled the objects of my fears, so I will not
    > mention them.
    >
    > --
    > Beware of bugs in the above code; I have only proved it correct, not
    > tried it. -- Donald E. Knuth


    I have found out the this is possible to a specific level using APT
    (Annotation Processing Tool). I just can't find any good resource on
    the internet. Just some here and there. I also have to find out not
    only invoking a method that has a desired annotation, but also to
    analyze what other method's it invokes. Anyway, I will be looking
    around to see and try to do this. I'll send feedbacks to this post if
    you are interested. There is already a proposal for a JSR-305 that
    uses annotations for concurrency issues (the team contains Brian Goetz
    - creator of FindBugs). I'll probably look this up to see what I can
    find out.
    pek, Oct 23, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ingo R. Homann
    Replies:
    0
    Views:
    543
    Ingo R. Homann
    Jul 26, 2005
  2. Casper
    Replies:
    1
    Views:
    332
    Nick Coghlan
    Nov 20, 2004
  3. Donnie Leen
    Replies:
    2
    Views:
    374
    Greg Ewing
    Nov 25, 2004
  4. Robin Haswell

    DB API and thread safety

    Robin Haswell, Jan 20, 2006, in forum: Python
    Replies:
    2
    Views:
    298
    Daniel Dittmar
    Jan 20, 2006
  5. grz01
    Replies:
    4
    Views:
    1,107
    Daniel Pitts
    Sep 28, 2009
Loading...

Share This Page