Scrivio Interview library
DebuggingIntermediate5 min read · updated 12 November 2026

A library reads annotations with getAnnotation(Tag.class). After a developer adds a second @Tag to a class, the library stops seeing any tag. Explain and fix.

@Tag is a repeatable annotation, so when a class carries two of them the compiler stores them inside the container annotation (say @Tags) instead of storing two @Tag annotations. getAnnotation(Tag.class) looks only for a @Tag placed directly on the class, finds none, and returns null. The fix is to read with getAnnotationsByType(Tag.class) or getDeclaredAnnotationsByType(Tag.class), which look through the container and work for one tag or many.

Why this question is asked

This bug is a good example of code that passes every test and then fails the first time someone uses a feature slightly differently. Nothing throws, nothing logs a warning; the library simply behaves as if the class had no tags at all. Interviewers like it because the explanation requires knowing what the compiler actually writes into the class file. In this answer, we will recap how repeatable annotations are stored, reproduce the failure, see why getAnnotation cannot find the tags, fix the library, and look at how to keep this from coming back.

1A Quick Recap of Repeatable Annotations

Since Java 8, an annotation type can be marked @Repeatable, which lets us use it more than once on the same element. The meta-annotation names a container type: another annotation whose value() method returns an array of the repeatable one. Our @Tag probably looks like the code below. If it were not repeatable, the second @Tag would not even compile, so the fact that the developer could add it tells us a container exists.

Java
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Repeatable(Tags.class)
public @interface Tag { String value(); }
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Tags { Tag[] value(); }

The repeating is a convenience of the source code only. In the class file, the compiler turns two or more @Tag annotations into one @Tags annotation that holds them all. A single @Tag stays as it is.

2Reproducing the Problem

Here is the library code and two classes, one tagged once and one tagged twice.

Java
@Tag("billing")
class InvoiceJob { }
@Tag("billing")
@Tag("nightly")
class ReconcileJob { }
// library code
static String tagOf(Class<?> type) {
Tag tag = type.getAnnotation(Tag.class);
return tag == null ? "untagged" : tag.value();
}
System.out.println(tagOf(InvoiceJob.class)); // billing
System.out.println(tagOf(ReconcileJob.class)); // untagged

Notice that the second class is not partly tagged; it loses both tags. Adding a tag removed the one that used to work. That is what makes this bug confusing to the developer who caused it.

3Why getAnnotation Finds Nothing

getAnnotation(Tag.class) asks one precise question: is there an annotation of type Tag present on this element? On InvoiceJob the answer is yes. On ReconcileJob the only annotation present is @Tags, and a Tags is not a Tag, so the answer is no and the method returns null. getAnnotation was designed before repeatable annotations existed and was deliberately left unchanged, so old code behaves the same on old annotations.

Java
System.out.println(ReconcileJob.class.getAnnotation(Tag.class)); // null
System.out.println(ReconcileJob.class.getAnnotation(Tags.class).value().length); // 2
System.out.println(InvoiceJob.class.getAnnotation(Tags.class)); // null
What each call returns
CallInvoiceJob (one tag)ReconcileJob (two tags)
getAnnotation(Tag.class)@Tag(billing)null
getAnnotation(Tags.class)null@Tags holding two tags
getAnnotationsByType(Tag.class)[billing][billing, nightly]
Only the ByType call gives the same kind of answer for both classes.

The last line shows that reading only the container is wrong too: with a single tag there is no container. A correct reader has to handle both shapes, which is exactly what the ByType methods do.

4The Fix: Read With the ByType Methods

getAnnotationsByType(Tag.class) returns every Tag that is present directly or inside its container, as an array. It returns an empty array, never null, when there are none. So the library becomes simpler, not more complicated.

Java
static List<String> tagsOf(Class<?> type) {
return Arrays.stream(type.getAnnotationsByType(Tag.class))
.map(Tag::value)
.toList();
}
System.out.println(tagsOf(InvoiceJob.class)); // [billing]
System.out.println(tagsOf(ReconcileJob.class)); // [billing, nightly]
System.out.println(tagsOf(Object.class)); // []

The return type had to change from one String to a List, and that is the real lesson: once an annotation is repeatable, the library's model must allow several values. If the library truly supports only one tag, it should make @Tag non-repeatable so that the second @Tag fails to compile, rather than silently ignoring both.

There is also getDeclaredAnnotationsByType. The difference only matters for classes and for annotations marked @Inherited: getAnnotationsByType also returns tags inherited from a superclass when the class has none of its own, while the Declared version looks only at the class itself. We pick whichever matches the library's intended rule.

5Keeping the Bug From Coming Back

The rule for library code is short: whenever an annotation is repeatable, or might become repeatable later, read it with getAnnotationsByType or getDeclaredAnnotationsByType, never with getAnnotation. We can search the codebase for getAnnotation calls on repeatable types during review, and a unit test with zero, one and two tags covers all three shapes the reader must handle.

Spring users have one more option. Spring's MergedAnnotations and AnnotatedElementUtils.getMergedRepeatableAnnotations look through containers and also through meta-annotations, annotations placed on other annotations, which plain reflection does not search. If the library already depends on Spring, those helpers give the most complete answer.

6Follow-up questions

Why did the Java designers not change getAnnotation to look inside containers?

Because that would change the behaviour of existing code. Before Java 8, people wrote containers by hand, and code that asked for the container type expected to get it. Leaving getAnnotation as it was and adding new ByType methods kept old programs working and gave new code a correct way to read repeated annotations.

Does the same problem happen with annotation processors?

Yes, in a similar form. Element.getAnnotation(Tag.class) in the javax.lang.model API also does not look into containers, while Element.getAnnotationsByType(Tag.class) does. Processors that walk getAnnotationMirrors() see the container mirror and must unpack it themselves.

What if the tag is placed as a meta-annotation on another annotation?

Plain reflection will not find it at all, because getAnnotationsByType only looks at annotations present on the element and their containers. To support meta-annotations, the library must walk the annotations of each annotation itself, or use a helper such as Spring's MergedAnnotations.

7Conclusion

In this answer, we saw that a second @Tag makes the compiler store both tags inside the @Tags container, so getAnnotation(Tag.class) finds no direct @Tag and returns null, and the class looks untagged. Reading with getAnnotationsByType or getDeclaredAnnotationsByType handles one tag and many tags in the same way, and library code that supports repeatable annotations should always use those methods and model several values.

What to say in the interviewAbout 80 seconds aloud

Open with

Tag is a repeatable annotation, and when a class has two of them the compiler doesn't store two Tag annotations. It stores a single container annotation, say Tags, whose value array holds both.

Then, if they want more

A single Tag is stored as itself. The library calls getAnnotation(Tag.class), which only looks for a Tag directly on the class. With one tag it finds it.

With two, the only thing directly present is the container, so getAnnotation returns null and the library thinks the class has no tags at all, which is why adding a tag made both disappear. The fix is to read with getAnnotationsByType(Tag.class), or getDeclaredAnnotationsByType if inherited tags should be ignored.

Those methods look through the container, return all tags in both cases, and return an empty array rather than null when there are none. Reading the container alone would be wrong too, because it does not exist when there is only one tag.

I would also change the library's model to a list of tags, add a test with zero, one and two tags, and in review treat getAnnotation on a repeatable type as a bug. If the library really supports only one tag, it should make Tag non-repeatable so the mistake fails at compile time.

About this answer

Last updated
12 November 2026
Versions it names
Java 8
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A library reads annotations with getAnnotation(Tag.class). After a developer adds a...": https://scrivio-interview-library.netlify.app/java/annotations/af9bbcfafc/