Scrivio Interview library
DebuggingFoundations5 min read · updated 12 November 2026

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?

An annotation declared without @Retention gets the default retention, RetentionPolicy.CLASS: it is written into the class file but not exposed through reflection, so method.isAnnotationPresent(Audited.class) returns false every time. Adding @Retention(RetentionPolicy.RUNTIME) fixes it, and adding @Target(ElementType.METHOD) makes misuse on other elements a compile error. If the aspect looks at a proxy's or an interface's method rather than the implementation's, it may also need Spring's utilities to find the annotation on the target class's method.

Why this question is asked

This bug catches almost everyone who writes their first custom annotation. The annotation is clearly there in the source, the code compiles, and the aspect still insists it is not. Interviewers like it because the cause is a single missing line, and because the follow-up about proxies shows whether we understand how Spring AOP sees our methods. In this answer, we will reproduce the problem, see where the annotation actually goes, fix the declaration, and then deal with the second reason an aspect can miss an annotation: looking at the wrong Method object.

1Reproducing the Problem

Here is the annotation, a service that uses it, and the check the aspect makes.

Java
public @interface Audited { } // no meta-annotations at all
@Service
public class PaymentService {
@Audited
public void refund(long orderId) { ... }
}
Method m = PaymentService.class.getMethod("refund", long.class);
System.out.println(m.isAnnotationPresent(Audited.class)); // false
System.out.println(m.getAnnotations().length); // 0

Notice that getAnnotations() returns nothing at all for refund(). The method is not missing the annotation by accident; as far as reflection is concerned, it never had one.

2Why Reflection Cannot See It

Every annotation type has a retention policy that decides how long it survives. SOURCE annotations are dropped by the compiler. RUNTIME annotations are written to the class file in a section the JVM exposes through reflection. CLASS annotations are written to the class file too, but in a section marked invisible, which the JVM does not expose.

When @Retention is missing, Java uses CLASS. So the compiler faithfully recorded @Audited on refund() in PaymentService.class, under the attribute RuntimeInvisibleAnnotations, and the JVM, as designed, kept it away from getAnnotation and isAnnotationPresent. We can confirm it with javap.

Shell
$ javap -v -cp target/classes com.acme.PaymentService | grep -A2 refund
public void refund(long);
RuntimeInvisibleAnnotations:
0: #21() // com.acme.Audited
Step throughWhere @Audited ends up, with and without RUNTIME
  1. javac compiles @Audited on refund()
  2. @Retention(RUNTIME) declared?
    YesStored as RuntimeVisibleAnnotationsNoStored as RuntimeInvisibleAnnotations
  3. JVM loads PaymentService
  4. isAnnotationPresent(Audited.class)true only for the visible case

4 steps in order. Play them, or step through them one at a time.

So the annotation is physically in the class file. It is simply invisible at run time, which is exactly what CLASS retention means.

3The Fix: Declare RUNTIME Retention and a Target

We add @Retention(RetentionPolicy.RUNTIME) so the annotation is visible to reflection. While we are there, we add @Target(ElementType.METHOD), so that putting @Audited on a field or a class, where the aspect would never look, becomes a compile error instead of another silent no-op.

Java
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface Audited { }
Method m = PaymentService.class.getMethod("refund", long.class);
System.out.println(m.isAnnotationPresent(Audited.class)); // true
@Audited private int retries; // error: annotation interface not applicable to this kind of declaration

Remember to recompile everything that uses the annotation. Retention is applied when the annotated class is compiled, so a stale PaymentService.class from an incremental build can still carry the invisible form.

4The Second Trap: Looking at the Wrong Method

Retention was the main bug, but an aspect can miss a correctly declared annotation too. Spring AOP intercepts calls through a proxy. If the bean implements an interface and Spring uses a JDK dynamic proxy, the Method object the advice sees may be the interface's method, which has no @Audited, because we put the annotation on the implementation class. With a CGLIB proxy, code that inspects the proxy's class may look at the generated subclass rather than our class.

The safe approach is to resolve the method on the actual target class before looking for the annotation, and to use Spring's annotation utilities, which also search the hierarchy.

Java
@Around("execution(* com.acme..*(..))")
public Object audit(ProceedingJoinPoint pjp) throws Throwable {
Method method = ((MethodSignature) pjp.getSignature()).getMethod(); // may be the interface method
Class<?> targetClass = AopUtils.getTargetClass(pjp.getTarget());
Method specific = AopUtils.getMostSpecificMethod(method, targetClass); // the implementation's method
Audited audited = AnnotationUtils.findAnnotation(specific, Audited.class); // also searches interfaces and superclasses
if (audited != null) auditLog.record(specific.getName(), pjp.getArgs());
return pjp.proceed();
}
Step throughHow the aspect finds the annotation through a proxy
CallerJDK proxyAudit aspectPaymentServiceImpl
refund(42) via Payments interface
advice gets Payments.refund, no @Audited
getMostSpecificMethod -> PaymentServiceImpl.refund
findAnnotation finds @Audited
proceed(): refund(42)
return

6 messages in order. Play them, or step through them one at a time.

Even simpler, let the pointcut do the matching: @Around("@annotation(audited)") with an Audited parameter binds the annotation directly, and Spring resolves it on the target method for us. That only works, of course, once the annotation has RUNTIME retention.

5Follow-up questions

Which Spring utility would you use to find the annotation on the implementation method?

AopUtils.getMostSpecificMethod(method, targetClass) turns an interface or proxy method into the method declared on the target class, and then AnnotationUtils.findAnnotation(method, Audited.class) or AnnotatedElementUtils.findMergedAnnotation looks for the annotation, searching superclasses and interfaces and following meta-annotations. Binding the annotation in the pointcut with @annotation(audited) does the same work automatically.

Would @Inherited help if the annotation were on an interface method?

No. @Inherited only applies to annotations on classes, through the superclass chain. Annotations on interface methods are never inherited by implementing methods through plain reflection, which is another reason to use Spring's findAnnotation, which does search interfaces.

Is it a problem if @Audited is placed on a private method?

Yes, in a different way. Spring AOP proxies cannot intercept private methods, or calls from inside the same class, so the advice never runs regardless of retention. The annotation should be on public methods called from other beans.

6Conclusion

In this answer, we saw that @Audited, declared without @Retention, got the default CLASS retention: the compiler wrote it into the class file as an invisible annotation, and reflection never reported it, so isAnnotationPresent always returned false. Declaring @Retention(RetentionPolicy.RUNTIME) fixed it, and @Target(ElementType.METHOD) turned misuse into a compile error. We also saw that an aspect working through a proxy may look at an interface method, so it should resolve the most specific method on the target class with AopUtils and look up the annotation with AnnotationUtils.findAnnotation or bind it in the pointcut.

What to say in the interviewAbout 70 seconds aloud

Open with

The annotation has no @Retention, and the default retention is CLASS. That means the compiler does write it into the class file, but as an invisible annotation, and the JVM does not expose it through reflection.

Then, if they want more

So isAnnotationPresent, getAnnotation and getAnnotations all behave as if it is not there; you can see it with javap under RuntimeInvisibleAnnotations. The fix is to add @Retention(RetentionPolicy.RUNTIME) to the annotation declaration.

I would also add @Target(ElementType.METHOD), so that if someone puts @Audited on a field or a class, where the aspect never looks, it is a compile error rather than another silent no-op, and I would make sure everything is recompiled. There is a second thing I would check in the aspect.

With Spring AOP, if the bean is behind a JDK dynamic proxy, the Method the advice sees can be the interface method, and the annotation is on the implementation. So I would resolve the method with AopUtils.getMostSpecificMethod against the target class and look it up with AnnotationUtils.findAnnotation, which also searches the hierarchy.

Or, simpler, write the pointcut as @annotation(audited) and let Spring bind the annotation directly.

About this answer

Last updated
12 November 2026
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 custom annotation is declared as @interface Audited {} and placed on service methods....": https://scrivio-interview-library.netlify.app/java/annotations/00fc51889a/