2

I want to know if it is possible to debug / see the flow of control what code gets executed for annotations. What I am trying to understand is like below:

(Not specific to Spring Framework annotation, can be any other annotation)

Consider the code snippet:

@Autowired(required=true)
private JPADBAccess jPADBAccess;

Now I want to understand what actions/code gets executed against the annotation? Is it possible to see.

I have difficulty in understand that how simply annotating does the work; there should be some code which would actually do some work; annotations are like just interface (@interface..), but where is the actual code which does the work?

CuriousMind
  • 8,301
  • 22
  • 65
  • 134
  • 2
    An annotation is just metadata. They do nothing on their own. Something needs to parse a `Class`, `Method`, `Field`, whatever, extract the annotation data and use it to achieve some goal. In Spring, `@Autowired` is handled by `AutowiredAnnotationBeanPostProcessor`. – Sotirios Delimanolis May 07 '16 at 16:19
  • 2
    The code that is annotated isn't running. It's some other unrelated code somewhere else that inspects your objects through reflection. – zapl May 07 '16 at 16:19
  • https://docs.oracle.com/javase/tutorial/java/annotations/ – Sotirios Delimanolis May 07 '16 at 16:21
  • Thanks a lot for your valuable inputs. So in general how do I know which code would get executed against an annotation? For example we have javax.annotation.Resource annotation? Agreed some other class/tool would execute some code, but how to identify what that code would be? – CuriousMind May 07 '16 at 16:34
  • There is no _in general_. The `@Autowired` annotation is completely (in terms of code) unrelated to `AutowiredAnnotationBeanPostProcessor`. Just because you've annotated a field, method, or constructor with `Autowired`, that doesn't mean that `AutowiredAnnotationBeanPostProcessor` will somehow be executed. You have to run it yourself (or through Spring). Similarly, I can write my own code that does something else entirely with the annotation. – Sotirios Delimanolis May 09 '16 at 00:14

2 Answers2

1

If you are dealing with open source libraries, you could take a look at the source code and search for where the annotation is used.

Debugging might also work if you want to see how the annotations are actually used.

For runtime annotations, you can try to debug the application and set breakpoints on Class methods like getAnnotation, getDeclaredAnnotations etc. If your debugger supports it (like the one in Eclipse does), then you can also use conditional breakpoints to e.g. only stop at the breakpoint if the parameter of getAnnotation has the right type.

This won't work if annotations are processed at compile time though. In that case you might try the same by debugging the annotation processor and set breakpoints at methods that load annotations.

kapex
  • 28,903
  • 6
  • 107
  • 121
0

The "scope" of an annotation is based on its location. For example, this annotation affects the entire class:

@SuppressWarnings("unused")
public class Foo{
    private Object anUnusedField;
    private Object anotherUnusedField;
    private void anUnusedMethod(Object unusedParameter){}
    /*
     * Here, the compiler will suppress all "the private field/method
     * Foo.[insert name here] is unused" warnings. It will warn about
     * the unusedParameter of anUnusedMethod
     */
}

This one affects a particular field:

public class Foo{
    private Object anUnusedField;
    @SuppressWarnings("unused") private Object anotherUnusedField;
    private void anUnusedMethod(Object unusedParameter){}
    /*
     * Here, the compiler won't tell that anotherUnusedField is not used.
     * But it'll tell that anUnusedField isn't used, anUnusedMethod isn't
     * called and unusedParameter isn't used in anUnusedMethod
     */
}

This one affects a particular method:

public class DifferentFoo{
    @SuppressWarnings("unused") private void anUnusedMethod(){}
    private void anotherUnusedMethod(){}
    /*
     * Here, the compiler will only tell that anotherUnusedMethod isn't
     * called
     */
}

Also note that you can't use all annotations on everything, e.g the following is invalid:

@Override protected Object someFieldFromTheSuperclass;
// Doesn't compile. Fields can't be overriden like methods
The SE I loved is dead
  • 1,517
  • 4
  • 23
  • 27