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