Annotations
22 questions in 7 subtopics.
Built-in annotations
What do@Override, @Deprecated, @SuppressWarnings, @FunctionalInterface and @SafeVarargs each do, and which of them change what the compiler does?A developer writes public boolean equals(Order other) in class Order and adds no annotation. HashSet<Order> still contains duplicates. What is wrong and how would @Override have helped?Your team owns a shared library and needs to remove a public method used by about 40 services. How do you use @Deprecated and related tooling to retire it safely?Meta-annotations
What are meta-annotations? Explain@Retention, @Target, @Documented, @Inherited and @Repeatable.Spring lets you write @RestController instead of @Controller plus @ResponseBody, and @GetMapping instead of @RequestMapping(method = GET). Java annotations cannot extend each other. How does this composition work?You are defining a set of composed annotations for your organisation's Spring starter, for example @InternalApi combining security, validation and transaction settings. What rules do you set so they stay understandable and safe?Retention policies and targets
ExplainRetentionPolicy.SOURCE, CLASS and RUNTIME with one real example of each. Which is the default when @Retention is missing?A custom annotation is declared as @interface Audited {} and placed on service methods. An aspect checks method.isAnnotationPresent(Audited.class) and it always returns false. Why?What do ElementType.TYPE_PARAMETER, TYPE_USE, MODULE and RECORD_COMPONENT targets allow, and what happens when an annotation on a record component has several targets such as FIELD and METHOD?Custom annotations
Write a custom annotation@Retry(times = 3) usable only on methods and readable at run time, and a small reflective helper that calls a method again on exception up to the configured count.A team proposes a custom annotation @Cacheable-like @Memoize that a library processes at run time with reflection on every call. Review this design: what are the costs and the alternatives?Teams in your organisation keep inventing custom annotations that trigger hidden behaviour at run time. Developers complain that code is hard to follow. What guidelines would you set?Over twenty years Java frameworks moved from XML configuration to annotations and now partly toward explicit Java configuration and build-time processing. What does that history teach about where configuration should live, and what would you standardise for the next decade?Annotation processing at compile time
How does a JSR 269 annotation processor work? Describe the rounds, what a processor can read, and what it can and cannot change.Outline an annotation processor that generates a Builder class for every class annotated@GenerateBuilder. Show the key methods you would override and how you would write the output.Your platform relies on Lombok across 300 services. A new JDK release breaks Lombok for a few weeks after release. How do you assess and manage the risk, and would you change course?Looking back, why did Java choose annotation processing that can only generate new files rather than macros that rewrite code? What does that mean for your long-term choice between compile-time code generation and run-time reflection frameworks?Repeatable annotations
Show how to declare a repeatable annotation@Schedule(cron = "...") so a method can carry it twice, and how to read all of them reflectively.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.Type annotations
What are type annotations introduced in Java 8? Give examples of where they can appear and how tools such as the Checker Framework use them.Your organisation wants to adopt nullness annotations across many repositories. Options include JSpecify, JSR 305, and framework-specific ones. How do you choose and roll this out?Java chose to add nullness and other properties as optional annotations checked by tools, rather than in the core type system as Kotlin did. What does that mean for long-term code quality strategy, and what would you standardise across the company?No question matches these choices.