2

I have done my research before asking but no luck.

I have a StartUp Singleton bean. In this bean I have an @Inject @Any Instance. I loop all the implementations and try to check if the class is annotated with a custom annotation. All the implementations(all the classes that I want to inspect) are Stateful or Stateless beans. Sometime the class I want is found and I can perform getClass().isAnnotationPresent(ClassNameAnnotation.class)

Most of the times I get a proxy object and in this case I cannot perform the above check. I cannot find a way to get the real object. I have tried to get the SuperClass but not luck. I will attach some of the code so you can have a better idea.

@Singleton
@Startup
public class CacheLoader {

    @Inject
    @Any
    private Instance<ClassNameA> aClasses;

   .......

    @Lock(LockType.READ)
    public void evaluate() {

        if (!aClasses.isUnsatisfied()) {
            for (ClassNameA className : aClasses) {
                if (className.getClass().isAnnotationPresent(ClassNameAnnotation.class)) {
                    ....
                }
            }
        }
    }

}

I tried to use the SuperClass of the proxy object but it does not return what I want. I tried also via Proxy.getInvocationHandler(). Even when I check the methods Proxy.isProxyClass(getClass()) or isSynthetic() does not return that the object is a proxy.

Thank you!

geminal
  • 23
  • 6
  • What type of annotation is it? If it's a qualifier you can probably use `aClasses.select(...)`. You can extend `AnnotationLiteral` to get a literal annotation value that you can use for in the select call. – Rob Spoor Apr 02 '22 at 15:40
  • No it is not a qualifier. I do not want to use a qualifier. I just want to "scan" all the classes with this annotation and take the information that is included. I cannot use it as a qualifier in my case.My annotation has these annotations @Target(value = ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) – geminal Apr 02 '22 at 15:58
  • Can you add `@Inherited` to your annotation? Then the proxy sub classes should inherit it. – Rob Spoor Apr 02 '22 at 16:07
  • Unfortunately it does not work. Something else that I do not know is why in different deployments I get sometime proxy for an object and some time no for the same one. – geminal Apr 02 '22 at 16:59
  • 1
    Like Rob Spoor writes, this is trivial to accomplish with a qualifier. On the other hand, I think it will most probably NOT work reliably without a qualifier. Can you explain (edit the question) what *exactly* are you trying to accomplish and why do you not want to use a qualifier? – Nikos Paraskevopoulos Apr 02 '22 at 21:14
  • The use of a qualifier is not so easy because I don't have any parameter to perform *implementations.select(....)* . I am not aware of how many implementations are there and if they are annotated. I am trying to generate a description from this annotations. Each annotation has information regarding each class. – geminal Apr 02 '22 at 23:05
  • Hello, so I see that I have to retry using it as a qualifier and I did. Again I have a proxy object of course but in this case I see the annotations(in debug mode). I tried to figure out how to take the annotations from proxy object using InvocationHandler. If you have any hint regarding this it would be helpful. – geminal Apr 08 '22 at 07:26

1 Answers1

1

I think you would be better served by using a CDI Portable Extension, rather than a Singleton EJB. A couple of reasons

  • In CDI, everything is a proxy. So like some of the commenters have said, using reflection would be very fragile as it's not part of the spec. You're dealing with classes that are defined at runtime. It may work if you tie yourself to implementation-specific details, but it could break between releases of your CDI container.

  • The CDI Container will do all of the annotation scanning for you :)

  • A portable extension runs on startup, before other stuff starts flying around your app

A google search gave me this guide, but there are lots of them: https://www.baeldung.com/cdi-portable-extension

I think you would want to hook in processAnnotatedType() if you're modifying the bean declarations, or afterBeanDiscovery() if you're just documenting them as you said.

We actually have a CDI Portable Extension we use internally that does some config magic for environments. One of the config params is an annotation that is not a qualifier annotation, which sounds like what you want... the CDI container can get you the type, from which you can inspect the annotations.

Finally, this is not directed related to your question but may be useful: If your annotations drive configuration through fields of the annotations, selecting them can be quite complicated because of how the Java type and inheritance system works with annotations. You may benefit by using AnnotationLitreal in those cases. Read up here on this useful utility class here: http://www.kurtsparber.de/?p=387

EDIT: Another side note... even thought I think you should switch to a Portable Extension, you shouldn't need @EJB's Singleton Startup anymore! you can do this with pure CDI: https://rmannibucau.wordpress.com/2015/03/10/cdi-and-startup/

Jonathan S. Fisher
  • 8,189
  • 6
  • 46
  • 84
  • Thank you for your answer! Maybe I will give it a try to see if CDI Portable Extension works for me. – geminal Apr 05 '22 at 20:59
  • Hello, I think this solution would help me, I tried it but there are other deployment issues due to compatibility. All the Singleton beans fail. – geminal Apr 08 '22 at 07:21
  • If you want to post those complete stack traces as another question with your code, I think people would be happy to help! – Jonathan S. Fisher Apr 08 '22 at 15:40
  • Sorry for my late response. So when I add in META-INF/services/javax.enterprise.inject.spi.Extension the package that contains my implementaion, deploy fails with below error. `Exception 0 : org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [X] with qualifiers [@Default] at injection point [[field] @Inject` I found a workaround and I use this dependency https://mvnrepository.com/artifact/org.reflections/reflections, I can scan all my classes and find the annotated ones, but I do not want this external dependency in my source code. – geminal May 02 '22 at 17:54
  • what is that exact contents of that file – Jonathan S. Fisher May 02 '22 at 18:34
  • 1
    actually you probably need create a new question on SO – Jonathan S. Fisher May 02 '22 at 18:34