Coverage Report - org.mule.util.scan.annotations.MetaAnnotationTypeFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
MetaAnnotationTypeFilter
0%
0/23
0%
0/4
0
MetaAnnotationTypeFilter$MetaAnnotationScanner
0%
0/10
0%
0/4
0
 
 1  
 /*
 2  
  * $Id: MetaAnnotationTypeFilter.java 20321 2010-11-24 15:21:24Z dfeist $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 package org.mule.util.scan.annotations;
 11  
 
 12  
 import java.io.IOException;
 13  
 import java.io.InputStream;
 14  
 import java.lang.annotation.Annotation;
 15  
 import java.net.URL;
 16  
 
 17  
 import org.apache.commons.logging.Log;
 18  
 import org.apache.commons.logging.LogFactory;
 19  
 import org.objectweb.asm.AnnotationVisitor;
 20  
 import org.objectweb.asm.ClassReader;
 21  
 
 22  
 /**
 23  
  * Will filter for a meta annotation type specified as the annotation class.  Meta annotations are annotations on other
 24  
  * annotations.  this filter allows discovery of annotations on a class that have the same meta annotation.
 25  
  */
 26  
 public class MetaAnnotationTypeFilter implements AnnotationFilter
 27  
 {
 28  
     /**
 29  
      * logger used by this class
 30  
      */
 31  0
     protected transient final Log logger = LogFactory.getLog(MetaAnnotationTypeFilter.class);
 32  
 
 33  
     private Class<? extends Annotation> annotation;
 34  
 
 35  
     private ClassLoader classLoader;
 36  
 
 37  
     /**
 38  
      * Creates an Meta Annotation Filter that look for Meta annotation on an annotation class
 39  
      * @param annotation the annotation class to read
 40  
      * @param basepath the base path of the location of the class. Specifying this will be more
 41  
      * reliable in applications that use Multiple classloaders since ASM uses the system classloader
 42  
      * to read classes and thus ignores child classloaders.  The basepath should also start with a scheme,
 43  
      * usually file: or jar:
 44  
      */
 45  
     public MetaAnnotationTypeFilter(Class<? extends Annotation> annotation, ClassLoader classLoader)
 46  0
     {
 47  0
         this.annotation = annotation;
 48  0
         this.classLoader = classLoader;
 49  0
     }
 50  
 
 51  
     /**
 52  
      * Creates an Meta Annotation Filter that look for Meta annotation on an annotation class. This constructor
 53  
      * will cause the class reading to read from the System clssloader
 54  
      * @param annotation the annotation class to read
 55  
      */
 56  
     public MetaAnnotationTypeFilter(Class<? extends Annotation> annotation)
 57  0
     {
 58  0
         this.annotation = annotation;
 59  0
         classLoader = Thread.currentThread().getContextClassLoader();
 60  0
     }
 61  
 
 62  
     public boolean accept(AnnotationInfo info)
 63  
     {
 64  
         try
 65  
         {
 66  0
             URL classUrl = getClassURL(info.getClassName());
 67  0
             if (classUrl == null)
 68  
             {
 69  0
                 logger.debug("Failed to load annotation class: " + info);
 70  0
                 return false;
 71  
             }
 72  
             
 73  0
             InputStream classStream = classUrl.openStream();
 74  0
             ClassReader r = new ClosableClassReader(classStream);
 75  
           
 76  0
             MetaAnnotationScanner scanner = new MetaAnnotationScanner(new AnnotationTypeFilter(annotation));
 77  0
             r.accept(scanner, 0);
 78  
 
 79  0
             return scanner.getClassAnnotations().size() == 1;
 80  
         }
 81  0
         catch (IOException e)
 82  
         {
 83  0
             logger.debug(e);
 84  0
             return false;
 85  
         }
 86  
     }
 87  
 
 88  
     private class MetaAnnotationScanner extends AnnotationsScanner
 89  
     {
 90  
         public MetaAnnotationScanner(AnnotationFilter filter)
 91  0
         {
 92  0
             super(filter);
 93  0
         }
 94  
 
 95  
         @Override
 96  
         public AnnotationVisitor visitAnnotation(final String desc, final boolean visible)
 97  
         {
 98  0
             currentAnnotation = new AnnotationInfo();
 99  0
             currentAnnotation.setClassName(getAnnotationClassName(desc));
 100  0
             if (log.isDebugEnabled())
 101  
             {
 102  0
                 log.debug("Annotation: " + getAnnotationClassName(desc));
 103  
             }
 104  
 
 105  
             // are we processing anything currently?
 106  0
             if (currentlyProcessing.nextSetBit(0) < 0)
 107  
             {
 108  
                 // no, this is a meta annotation
 109  0
                 currentlyProcessing.set(PROCESSING_CLASS);
 110  
             }
 111  
 
 112  0
             return this;
 113  
         }
 114  
 
 115  
     }
 116  
 
 117  
     public URL getClassURL(String className)
 118  
     {
 119  0
         String resource = className.replace(".", "/") + ".class";
 120  0
         return classLoader.getResource(resource);
 121  
     }
 122  
 }