I am currently hacking on a project where a base class is extended by a lot of beans.
There is a common method on this base class and basically the only change I need to do is to check if when a particular invocation fails the current instance invoking the method is an application scoped bean. If it is, I just have to log the error message and return a cached response; if it is not, I have to raise an exception.
Appart from using instanceof
on every application scoped bean extending this base class, is there any way to know if the object this
is an Application scoped bean?
You can check whether the class of some instance has particular annotations. For example, the call:
Annotation[] annotations = this.getClass().getAnnotations();
will give you all annotations of current instance. Or you may want to find out some certain annotation:
ApplicationScoped annotation = this.getClass().getAnnotation(ApplicationScoped.class);
which will return null
if class isn't annotated with @ApplicationScoped
In case of inheritance, the method getAnnotations()
returns annotations of a parent class too and annotated with @RequestScoped
class will return also an @ApplicationScoped
if its parent was declared so. On the other hand, JSF-managed beans will override the parent's scope.
Hope this helps you.