Annotations and Retention

V

Vidar S. Ramdal

I've started playing around with annotations.

I'm trying to find out, runtime, if a given method has an annotation:

@java.lang.annotation.Target(value =
java.lang.annotation.ElementType.METHOD)
@java.lang.annotation.Retention(value =
java.lang.annotation.RetentionPolicy.RUNTIME)
public @interface RestrictedAccess {
...
}

And the method:

@RestrictedAccess public void methodName() {
...
}

Now I want to find the @RestrictedAccess annotation runtime. I have a
reference to the method from reflection:

RestrictedAccess restriction = method.getAnnotation(RestrictedAccess.class);

However, 'restriction' allways turns out as null. When I inspect the
'method' object, I can see three arrays (annotations,
parameterAnnotations, declaredAnnotations), which all are null.

I thought specifying RetentionPolicy.RUNTIME would assure that the
annotation would be available runtime, but no luck.

What's going on here?
 
R

Roland

I've started playing around with annotations.

I'm trying to find out, runtime, if a given method has an annotation:

@java.lang.annotation.Target(value =
java.lang.annotation.ElementType.METHOD)
@java.lang.annotation.Retention(value =
java.lang.annotation.RetentionPolicy.RUNTIME)
public @interface RestrictedAccess {
...
}

And the method:

@RestrictedAccess public void methodName() {
...
}

Now I want to find the @RestrictedAccess annotation runtime. I have a
reference to the method from reflection:

RestrictedAccess restriction =
method.getAnnotation(RestrictedAccess.class);

However, 'restriction' allways turns out as null. When I inspect the
'method' object, I can see three arrays (annotations,
parameterAnnotations, declaredAnnotations), which all are null.

I thought specifying RetentionPolicy.RUNTIME would assure that the
annotation would be available runtime, but no luck.

What's going on here?
Works for me:

import java.lang.reflect.Method;
public class TestRestrictedAccess {
@RestrictedAccess private void foo() {}
@RestrictedAccess public void bar() {}
public static void main(String[] args) throws Exception {
for (Method m : TestRestrictedAccess.class.getDeclaredMethods()) {
RestrictedAccess a = m.getAnnotation(RestrictedAccess.class);
System.out.print(m.getName());
System.out.print("\t- ");
System.out.print(a);
System.out.println();
}
}
}


@java.lang.annotation.Retention(value =
java.lang.annotation.RetentionPolicy.RUNTIME)
@java.lang.annotation.Target(value =
java.lang.annotation.ElementType.METHOD)
public @interface RestrictedAccess {
}



--
Regards,

Roland de Ruiter
` ___ ___
`/__/ w_/ /__/
/ \ /_/ / \
 
V

Vidar S. Ramdal

[...]
I'm trying to find out, runtime, if a given method has an annotation:

@java.lang.annotation.Target(value =
java.lang.annotation.ElementType.METHOD)
@java.lang.annotation.Retention(value =
java.lang.annotation.RetentionPolicy.RUNTIME)
public @interface RestrictedAccess {
...
}
[...]
Works for me:
[...]
@java.lang.annotation.Retention(value =
java.lang.annotation.RetentionPolicy.RUNTIME)
@java.lang.annotation.Target(value =
java.lang.annotation.ElementType.METHOD)
public @interface RestrictedAccess {
}

That's weird.

I just copied and pasted your code, and that works for me too.

I overlooked something perhaps important in my original code: I was
importing java.lang.annotation.*, and specifying only value =
RetentionPolicy.RUNTIME:

import java.lang.annotation.*;

@java.lang.annotation.Target(value = {ElementType.METHOD})
@java.lang.annotation.Retention(value = RetentionPolicy.RUNTIME)

This didn't work, but I can't see why.

Thanks for your help!
 

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,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top