2 Replies Latest reply on Feb 16, 2009 8:32 PM by demetrio812

    Scanning jar and classes for annotations

    demetrio812 Apprentice

      Hi,
      I've made some annotations I use for configuration, to find the annotation I used the project Scannotation, the only problem I had is to find the right path (since I cannot use the classpath) so at the end to find the paths I search the seam.properties file into the resources and get all the path with that file so I use that path to scan classes.


      Is it a good way to implement it? I should also use a personalized file but I don't want too introduce other requirement and the position of seam.properties is fine for me.


      This is the code I used:


      First I have to find the array with the URL where to search:


      import java.net.MalformedURLException;
      import java.net.URL;
      import java.util.ArrayList;
      import java.util.Enumeration;
      import java.io.IOException;
      
      public class SeamComponentsUrlFinder {
      
           public static URL[] findInSeamPath() {
                ArrayList<URL> list = new ArrayList<URL>();
                Enumeration<URL> resources;
      
                try {
                     resources = Thread.currentThread().getContextClassLoader()
                               .getResources("seam.properties");
                } catch (IOException ioe) {
                     throw new RuntimeException("error scanning seam.properties files",
                               ioe);
                }
      
                try {
                     while (resources.hasMoreElements()) {
                          URL url = resources.nextElement();
                          list.add(new URL(url.toExternalForm().replaceAll(
                                    "seam.properties", "")));
                     }
                } catch (MalformedURLException e) {
                     e.printStackTrace();
                }
                return list.toArray(new URL[list.size()]);
           }
      }
      



      Then I can search for classes with my own annotations, here an example using scannotation:



      URL urls[];
      // Get the URL where to scan
      urls = SeamComponentsUrlFinder.findInSeamPath();
      
      System.out.println("Cerco tutto...");
      
      AnnotationDB db = new AnnotationDB(); 
      try {
           db.scanArchives(urls);
      
           // Just scan Class Annotations
           db.setScanFieldAnnotations(false);
           db.setScanMethodAnnotations(false);
           db.setScanParameterAnnotations(false);
           
           Map<String, Set<String>> annotationIndex = db.getAnnotationIndex();
           
           // Classes with org.jboss.seam.annotations.Name annotation
           Set<String> entities = annotationIndex.get("org.jboss.seam.annotations.Name");
      
           // Classes with it.novaware.sn.core.annotations.SNetApp annotation
           Set<String> entities2 = annotationIndex.get("it.novaware.sn.core.annotations.SNetApp");
      } catch (IOException e) {
           e.printStackTrace();
      }
      



      Let me know what you think...


      Thanks!


      Demetrio