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