Getting Annotation for a Return Type (Java 1.5 Beta)

R

Raymond McCrae

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
 
D

Dale King

Raymond McCrae said:
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.
 
R

Raymond McCrae

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top