Coverage Report - org.mule.util.scan.ImplementationClassScanner
 
Classes in this File Line Coverage Branch Coverage Complexity
ImplementationClassScanner
0%
0/28
0%
0/6
2.167
 
 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;
 8  
 
 9  
 import org.mule.util.scan.annotations.ClosableClassReader;
 10  
 
 11  
 import java.io.IOException;
 12  
 import java.io.InputStream;
 13  
 import java.net.URL;
 14  
 
 15  
 import org.objectweb.asm.ClassReader;
 16  
 import org.objectweb.asm.commons.EmptyVisitor;
 17  
 
 18  
 /**
 19  
  * Will determine if the class provide extends and thus is assignable from the implementatation class provied.
 20  
  */
 21  
 public class ImplementationClassScanner extends EmptyVisitor implements ClassScanner
 22  
 {
 23  
     private Class implementationClass;
 24  
 
 25  
     private boolean match;
 26  
 
 27  
     private String className;
 28  
 
 29  
     private ClassLoader classLoader;
 30  
 
 31  
     public ImplementationClassScanner(Class implementationClass)
 32  
     {
 33  0
         this(implementationClass, Thread.currentThread().getContextClassLoader());
 34  0
     }
 35  
 
 36  
     public ImplementationClassScanner(Class implementationClass, ClassLoader classLoader)
 37  0
     {
 38  0
         if(implementationClass.isInterface())
 39  
         {
 40  0
             throw new IllegalArgumentException("The class need to be an implementation not an interface");
 41  
         }
 42  0
         this.implementationClass = implementationClass;
 43  0
         this.classLoader = classLoader;
 44  0
     }
 45  
 
 46  
     public void visit(int i, int i1, String s, String s1, String superName, String[] interfaces)
 47  
     {
 48  
 
 49  0
         if(superName==null)
 50  
         {
 51  0
             return;
 52  
         }
 53  0
         else if(superName.replaceAll("/",".").equals(implementationClass.getName()))
 54  
         {
 55  0
             match = true;
 56  0
             className = s;
 57  
         }
 58  
         else
 59  
         {
 60  
             try
 61  
             {
 62  0
                 ImplementationClassScanner scanner = new ImplementationClassScanner(implementationClass);
 63  0
                 URL classURL = getClassURL(superName);
 64  0
                 InputStream classStream = classURL.openStream();
 65  0
                 ClassReader r = new ClosableClassReader(classStream);
 66  
                 
 67  0
                 r.accept(scanner, 0);
 68  0
                 match = scanner.isMatch();
 69  0
                 className = scanner.getClassName();
 70  
             }
 71  0
             catch (IOException e)
 72  
             {
 73  0
                 throw new RuntimeException(e);
 74  0
             }
 75  
 
 76  
         }
 77  0
     }
 78  
 
 79  
     public boolean isMatch()
 80  
     {
 81  0
         return match;
 82  
     }
 83  
 
 84  
     public String getClassName()
 85  
     {
 86  0
         return className;
 87  
     }
 88  
 
 89  
     public URL getClassURL(String className)
 90  
     {
 91  0
         String resource = className.replace(".", "/") + ".class";
 92  0
         return classLoader.getResource(resource);
 93  
     }
 94  
 }