3 Replies Latest reply on Jun 1, 2012 6:45 AM by eric vantillard

    Processing custom annotations with a CDI extension and Javassist

    Oscar cs Newbie



      I am trying to modify the bytecode of some classes marked with a custom annotation when the application starts.


      If I am correct, the only way to do this is:

      1. Using a DeploymentUnitProcessor.

      2. Using a CDI extension.


      I think the second one is more portable and easy to implement so I have made a little prototype application (EAR) that includes one jar with the classes to process and another one with the CDI extension. It seems to scan and find the annotated classes successfully, but when I try to use de javassist ClassPool get method to retrieve the annotated class it throws a "javassit.NotFoundException".


      This is the problematic code snnipet:

      <T> void processAnnotatedType(@Observes ProcessAnnotatedType<T> pat, BeanManager beanManager) {
              try {
                   // If the annotation is present...
                  Class<? extends Annotation> clazz = (Class<? extends Annotation>)Class.forName("mypackage.MyAnnotation");
                  if(pat.getAnnotatedType().isAnnotationPresent(clazz)) {
                          // Get the annotated class name
                          String className = pat.getAnnotatedType().getJavaClass().getName();
                          // This is only a test to confirm that the class is located in the classpath
                          // And it works!!!
                          Class targetClass = Class.forName(className);
                          ClassPool cp = ClassPool.getDefault();
                          // But this throws a javassit.NotFoundException. Why???
                          CtClass cc = cp.get(className);
              catch(Exception e) {
                  throw new RuntimeException("Error processing annotation", e);


      Can somebody please tell me what I may be doing wrong?


      Thanks in advance.


      - Oscar