13

Searchable.java

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Searchable { }

Obj.java

public class Obj {
    @Searchable
    String myField;
}

void main(String[] args)

Annotation[] annotations = Obj.class.getDeclaredField("myField").getAnnotations();

I would expect annotations to be containing my @Searchable. Though it is null. According to documentation, this method:

Returns all annotations present on this element. (Returns an array of length zero if this element has no annotations.) The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers.

Which is even more weird (to me), since it returns null instead of Annotation[0].

What am I doing wrong here and more important, how will I be able to get my Annotation?

Menno
  • 12,175
  • 14
  • 56
  • 88
  • 3
    It seems to work on my machine. What's the Java version you're using on runtime? Could you check the `@Retention(RetentionPolicy.RUNTIME)` line does exist on your custom annotation? How do you know it returns `null`? Could you provide the code snippet your using? – sp00m May 03 '13 at 14:01
  • 1
    Exactly. Works with JDK 1.7.0. – Andreas Fester May 03 '13 at 14:02
  • @sp00m I copied from my IDE, RUNTIME is available. I know it returns null since I debugged it. I'm using JDK 1.7.0. – Menno May 03 '13 at 14:16
  • @Aquillo Try a simple `System.out.println(annotations.length);` just after the last line you gave and check whether an NPE get thrown. – sp00m May 03 '13 at 14:19

4 Answers4

7

I just tested this for you, and it just works:

public class StackOverflowTest {

    @Test
    public void testName() throws Exception {

        Annotation[] annotations = Obj.class.getDeclaredField("myField").getAnnotations();

        System.out.println(annotations[0]);
    }
}

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Searchable {

}

class Obj {

    @Searchable
    String myField;
}

I ran it, and it produces the following output:

@nl.jworks.stackoverflow.Searchable()

Can you try running the above class in your IDE? I tried it with IntelliJ, openjdk-6.

Erik Pragt
  • 13,513
  • 11
  • 58
  • 64
  • This works, thanks. Will examine the problem further, will let you know. – Menno May 03 '13 at 14:18
  • 1
    I cannot seem to recreate my problem, but the issue is solved by copying your example and building it back to my case, thanks Erik! – Menno May 03 '13 at 14:29
2

Your code is correct. The problem is somewhere else. I just copied and run your code and it works.

It is possible that you are importing the wrong Obj class in your code you may want to check that first.

Adam Arold
  • 29,285
  • 22
  • 112
  • 207
1

In my case, i had forgotten to add

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)

to the method, so in the end it should look like:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}
Johannes Hinkov
  • 761
  • 8
  • 9
0

In my case, the error was in my own annotation. I fixed a couple of things, and it finally ended up like this:

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Target( { METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
public @interface MyAnnotation{
}

It works now

McCoy
  • 780
  • 1
  • 10
  • 21