View Javadoc
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          this(implementationClass, Thread.currentThread().getContextClassLoader());
34      }
35  
36      public ImplementationClassScanner(Class implementationClass, ClassLoader classLoader)
37      {
38          if(implementationClass.isInterface())
39          {
40              throw new IllegalArgumentException("The class need to be an implementation not an interface");
41          }
42          this.implementationClass = implementationClass;
43          this.classLoader = classLoader;
44      }
45  
46      public void visit(int i, int i1, String s, String s1, String superName, String[] interfaces)
47      {
48  
49          if(superName==null)
50          {
51              return;
52          }
53          else if(superName.replaceAll("/",".").equals(implementationClass.getName()))
54          {
55              match = true;
56              className = s;
57          }
58          else
59          {
60              try
61              {
62                  ImplementationClassScanner scanner = new ImplementationClassScanner(implementationClass);
63                  URL classURL = getClassURL(superName);
64                  InputStream classStream = classURL.openStream();
65                  ClassReader r = new ClosableClassReader(classStream);
66                  
67                  r.accept(scanner, 0);
68                  match = scanner.isMatch();
69                  className = scanner.getClassName();
70              }
71              catch (IOException e)
72              {
73                  throw new RuntimeException(e);
74              }
75  
76          }
77      }
78  
79      public boolean isMatch()
80      {
81          return match;
82      }
83  
84      public String getClassName()
85      {
86          return className;
87      }
88  
89      public URL getClassURL(String className)
90      {
91          String resource = className.replace(".", "/") + ".class";
92          return classLoader.getResource(resource);
93      }
94  }