View Javadoc

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