6 Replies Latest reply on Jul 26, 2013 2:06 AM by ansur

    JBoss extention doesn't find custom annotation

    ansur

      I'm building a JBoss extension which aims to detect a custom annotation being placed on an EJB implementation class. So the annotation is placed on the same level as @Singleton, @Stateless etc.

       

      My DeploymentUnitProcessor can detect the EJBs, but when I load the class like this:

       

      return moduleClassLoader.loadClass(viewDescription.getComponentDescription().getComponentClassName());
      

       

      ... I do get the Class object, but it only shows the EJB annotation (e.g. @Singleton) being present, not my custom one.

      Obviously the RetentionPolicy is Runtime.

       

      Is this due to the fact that whatever JBoss module is inspecting the class, doesn't have a notion of my custom Annotation?

      And if this is so, which module is this, and could I register a dependency to the module containing my annotation at runtime?

        • 1. Re: JBoss extention doesn't find custom annotation
          ctomc

          Hi,

           

          annotations are not pressent on classes you must process it as part of annotation processing phase.

          all annotations are part of jandex.

           

          take a look at https://github.com/wildfly/wildfly/blob/master/mail/src/main/java/org/jboss/as/mail/extension/MailSubsystemAdd.java

          and https://github.com/wildfly/wildfly/blob/master/mail/src/main/java/org/jboss/as/mail/extension/MailSessionDefinitionAnnotationParser.java

           

           

           

          --

          tomaz

          • 2. Re: JBoss extention doesn't find custom annotation
            jaikiran

            What does your code look like which checks for the annotation and what does the EJB code which has that annotation look like (including import statements)?

             

            By the way, which version of AS7 are you using to write this against?

            • 3. Re: JBoss extention doesn't find custom annotation
              ansur

              Ok so based on the Mail examples given by Tomaz, I've got something like this:

              - one DeploymentProcessor which registers information about my annotation and registers it within a EEModuleClassDescription:

               

               

                  /**
                   * {@inheritDoc}
                   */
                  @Override
                  public void deploy(final DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
                      final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
                      final EEModuleDescription eeModuleDescription = deploymentUnit.getAttachment(Attachments.EE_MODULE_DESCRIPTION);
                      final CompositeIndex compositeIndex = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.COMPOSITE_ANNOTATION_INDEX);
              
              
                      final List<AnnotationInstance> annotationInstances = compositeIndex.getAnnotations(SERVICEABILITY_DEFINITION);
                      for(final AnnotationInstance annotationInstance : annotationInstances) {
                          final AnnotationTarget annotationTarget = annotationInstance.target();
                          final ClassInfo classInfo = (ClassInfo) annotationTarget;
              
              
                          final String className = classInfo.name().toString();
                          final EEModuleClassDescription classDescription = eeModuleDescription.addOrGetLocalClassDescription(className);
                          final Map<String, ClassAnnotationInformation<Serviceable, ServiceabilityInformation>> annotationInformationMap = this.factory.createAnnotationInformation(compositeIndex);
              
              
                          if(annotationInformationMap.containsKey(className)) {
                              classDescription.addAnnotationInformation(annotationInformationMap.get(className));
                          }
                      }
                  }
              
              

               

              ... and later on I can retrieve that information the same way I register it here, using the class name of the implementation.

               

              This is a little bit different than what I saw in the mail example, so just to verify - is this the 'good' way of processing an annotation?

               

              FYI I'm using JBoss 7.2.0

              • 4. Re: JBoss extention doesn't find custom annotation
                ansur

                Maybe just an additional question - it's working like a charm now, but what I'm also wondering - what's the best way to (unit) test this without mocking - i.e. to get the proper behaviour derived from the JBoss modules as well?

                Or isn't there a way and is the preferred way integration tests?

                • 5. Re: JBoss extention doesn't find custom annotation
                  sfcoy

                  Use Arquillian for running in-container integration tests without mocking.

                   

                  A large portion of the AS7/WildFly codebase is tested using this tool.

                  • 6. Re: JBoss extention doesn't find custom annotation
                    ansur

                    I thought as much. Thanks!