Search code examples
javaannotationscucumbertestngjava-threads

Are annotations instance specific in java?


If I had two instances of a class instantiated in separate threads with the same annotation. And both threads were to modify the contents of the annotation for their class. Will the contents of the annotation be different for each instance or the same?

To set some context I am using testng and cucumber and would like to run the same test method twice with different tags. I was planning on editing the cucumber options at runtime via reflection and was sort of assuming that as they are in separate threads that would be fine (im pretty sure that was a dumb assumption)...

I'd seen the following example on how to modify the params of annotations at runtime


Solution

  • An instance of a class only creates new field primitives or references on the heap; it does not modify the class template. If you use reflection to modify the annotations, you are modifying the class template, even if the annotations are on fields.

    Threads should have no impact, apart from setting up race conditions if you do reflection in them.

    Apparently, you have written code to do this and are seeing some sort of results; are they consistent with that explanation?