Getting Annotation for a Return Type (Java 1.5 Beta)

Discussion in 'Java' started by Raymond McCrae, Mar 2, 2004.

  1. Hi

    I can't find any way to get access to an annotation on the return type
    of a method. For example the code below complies however there is no
    method on the "Method" class (that I can see) that gives me access to
    the annoation.

    Could someone point me to something I may have misses.


    example:

    import java.lang.annotation.*;

    @Retention(value=RetentionPolicy.RUNTIME)
    @Target(value=ElementType.ANNOTATION_TYPE)
    public @interface MyAnnotation {
    String value();
    }


    public class TestClass {

    public static @MyAnnotation("hello")void myMethod() {
    }

    }


    Thank
    Raymond McCrae
    Raymond McCrae, Mar 2, 2004
    #1
    1. Advertising

  2. Raymond McCrae

    Dale King Guest

    "Raymond McCrae" <> wrote in message
    news:Vg61c.16426$Y%9.net...
    > Hi
    >
    > I can't find any way to get access to an annotation on the return type
    > of a method. For example the code below complies however there is no
    > method on the "Method" class (that I can see) that gives me access to
    > the annoation.
    >
    > Could someone point me to something I may have misses.


    > public static @MyAnnotation("hello")void myMethod() {


    The issue is that there is no way to annotate the return type of the method,
    thus there is no way to retrieve that annotation. Your example is annotating
    the method not the return type of the method. It really makes no sense to
    associate metadata with the return type instead of with the method. Each
    method has one and only one return type so the annotation goes on the method
    not the return type. It's just that the compiler is not too picky about
    where you put the methods annotations with respect to modifiers like public
    and static.

    --
    Dale King
    Dale King, Mar 3, 2004
    #2
    1. Advertising

  3. Thanks for clearing that up. Looking at it in retrospect you are right
    it doesn't make sense to have an annotation on a return type.

    Regards
    Raymond

    Dale King wrote:
    > "Raymond McCrae" <> wrote in message
    > news:Vg61c.16426$Y%9.net...
    >
    >>Hi
    >>
    >>I can't find any way to get access to an annotation on the return type
    >>of a method. For example the code below complies however there is no
    >>method on the "Method" class (that I can see) that gives me access to
    >>the annoation.
    >>
    >>Could someone point me to something I may have misses.

    >
    >
    >>public static @MyAnnotation("hello")void myMethod() {

    >
    >
    > The issue is that there is no way to annotate the return type of the method,
    > thus there is no way to retrieve that annotation. Your example is annotating
    > the method not the return type of the method. It really makes no sense to
    > associate metadata with the return type instead of with the method. Each
    > method has one and only one return type so the annotation goes on the method
    > not the return type. It's just that the compiler is not too picky about
    > where you put the methods annotations with respect to modifiers like public
    > and static.
    >
    > --
    > Dale King
    >
    >
    Raymond McCrae, Mar 3, 2004
    #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. srini

    java.text.Annotation

    srini, Apr 24, 2004, in forum: Java
    Replies:
    0
    Views:
    493
    srini
    Apr 24, 2004
  2. Ingo R. Homann
    Replies:
    0
    Views:
    540
    Ingo R. Homann
    Jul 26, 2005
  3. Christopher Benson-Manica

    Cyclic annotation element type

    Christopher Benson-Manica, Mar 15, 2007, in forum: Java
    Replies:
    1
    Views:
    414
    Mark Rafn
    Mar 16, 2007
  4. pek
    Replies:
    2
    Views:
    1,043
  5. Tom Anderson
    Replies:
    1
    Views:
    315
    Tom Anderson
    May 10, 2009
Loading...

Share This Page