PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/bsh/classpath/BshClassLoader.java

#
Java | 196 lines | 66 code | 28 blank | 102 comment | 15 complexity | 72fa2bf43f6cc089c0e82358998c2180 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*****************************************************************************
  2. * *
  3. * This file is part of the BeanShell Java Scripting distribution. *
  4. * Documentation and updates may be found at http://www.beanshell.org/ *
  5. * *
  6. * Sun Public License Notice: *
  7. * *
  8. * The contents of this file are subject to the Sun Public License Version *
  9. * 1.0 (the "License"); you may not use this file except in compliance with *
  10. * the License. A copy of the License is available at http://www.sun.com *
  11. * *
  12. * The Original Code is BeanShell. The Initial Developer of the Original *
  13. * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
  14. * (C) 2000. All Rights Reserved. *
  15. * *
  16. * GNU Public License Notice: *
  17. * *
  18. * Alternatively, the contents of this file may be used under the terms of *
  19. * the GNU Lesser General Public License (the "LGPL"), in which case the *
  20. * provisions of LGPL are applicable instead of those above. If you wish to *
  21. * allow use of your version of this file only under the terms of the LGPL *
  22. * and not to allow others to use your version of this file under the SPL, *
  23. * indicate your decision by deleting the provisions above and replace *
  24. * them with the notice and other provisions required by the LGPL. If you *
  25. * do not delete the provisions above, a recipient may use your version of *
  26. * this file under either the SPL or the LGPL. *
  27. * *
  28. * Patrick Niemeyer (pat@pat.net) *
  29. * Author of Learning Java, O'Reilly & Associates *
  30. * http://www.pat.net/~pat/ *
  31. * *
  32. *****************************************************************************/
  33. package org.gjt.sp.jedit.bsh.classpath;
  34. import java.net.*;
  35. import org.gjt.sp.jedit.bsh.BshClassManager;
  36. /**
  37. One of the things BshClassLoader does is to address a deficiency in
  38. URLClassLoader that prevents us from specifying individual classes
  39. via URLs.
  40. */
  41. public class BshClassLoader extends URLClassLoader
  42. {
  43. BshClassManager classManager;
  44. /**
  45. @param bases URLs JARClassLoader seems to require absolute paths
  46. */
  47. public BshClassLoader( BshClassManager classManager, URL [] bases ) {
  48. super( bases );
  49. this.classManager = classManager;
  50. }
  51. /**
  52. @param bcp URLs JARClassLoader seems to require absolute paths
  53. */
  54. public BshClassLoader( BshClassManager classManager, BshClassPath bcp ) {
  55. this( classManager, bcp.getPathComponents() );
  56. }
  57. /**
  58. For use by children
  59. @param classManager URLs JARClassLoader seems to require absolute paths
  60. */
  61. protected BshClassLoader( BshClassManager classManager ) {
  62. this( classManager, new URL [] { } );
  63. }
  64. // public version of addURL
  65. public void addURL( URL url ) {
  66. super.addURL( url );
  67. }
  68. /**
  69. This modification allows us to reload classes which are in the
  70. Java VM user classpath. We search first rather than delegate to
  71. the parent classloader (or bootstrap path) first.
  72. An exception is for BeanShell core classes which are always loaded from
  73. the same classloader as the interpreter.
  74. */
  75. public Class loadClass(String name, boolean resolve)
  76. throws ClassNotFoundException
  77. {
  78. Class c = null;
  79. /*
  80. Check first for classes loaded through this loader.
  81. The VM will not allow a class to be loaded twice.
  82. */
  83. c = findLoadedClass(name);
  84. if ( c != null )
  85. return c;
  86. // This is copied from ClassManagerImpl
  87. // We should refactor this somehow if it sticks around
  88. if ( name.startsWith( ClassManagerImpl.BSH_PACKAGE ) )
  89. try {
  90. return org.gjt.sp.jedit.bsh.Interpreter.class.getClassLoader().loadClass( name );
  91. } catch ( ClassNotFoundException e ) {}
  92. /*
  93. Try to find the class using our classloading mechanism.
  94. Note: I wish we didn't have to catch the exception here... slow
  95. */
  96. try {
  97. c = findClass( name );
  98. } catch ( ClassNotFoundException e ) { }
  99. if ( c == null )
  100. throw new ClassNotFoundException("here in loaClass");
  101. if ( resolve )
  102. resolveClass( c );
  103. return c;
  104. }
  105. /**
  106. Find the correct source for the class...
  107. Try designated loader if any
  108. Try our URLClassLoader paths if any
  109. Try base loader if any
  110. Try system ???
  111. */
  112. // add some caching for not found classes?
  113. protected Class findClass( String name )
  114. throws ClassNotFoundException
  115. {
  116. // Deal with this cast somehow... maybe have this class use
  117. // ClassManagerImpl type directly.
  118. // Don't add the method to BshClassManager... it's really an impl thing
  119. ClassManagerImpl bcm = (ClassManagerImpl)getClassManager();
  120. // Should we try to load the class ourselves or delegate?
  121. // look for overlay loader
  122. // Deal with this cast somehow... maybe have this class use
  123. // ClassManagerImpl type directly.
  124. // Don't add the method to BshClassManager... it's really an impl thing
  125. ClassLoader cl = bcm.getLoaderForClass( name );
  126. Class c;
  127. // If there is a designated loader and it's not us delegate to it
  128. if ( cl != null && cl != this )
  129. try {
  130. return cl.loadClass( name );
  131. } catch ( ClassNotFoundException e ) {
  132. throw new ClassNotFoundException(
  133. "Designated loader could not find class: "+e );
  134. }
  135. // Let URLClassLoader try any paths it may have
  136. if ( getURLs().length > 0 )
  137. try {
  138. return super.findClass(name);
  139. } catch ( ClassNotFoundException e ) {
  140. //System.out.println(
  141. // "base loader here caught class not found: "+name );
  142. }
  143. // If there is a baseLoader and it's not us delegate to it
  144. cl = bcm.getBaseLoader();
  145. if ( cl != null && cl != this )
  146. try {
  147. return cl.loadClass( name );
  148. } catch ( ClassNotFoundException e ) { }
  149. // Try system loader
  150. return bcm.plainClassForName( name );
  151. }
  152. /*
  153. The superclass does something like this
  154. c = findLoadedClass(name);
  155. if null
  156. try
  157. if parent not null
  158. c = parent.loadClass(name, false);
  159. else
  160. c = findBootstrapClass(name);
  161. catch ClassNotFoundException
  162. c = findClass(name);
  163. */
  164. BshClassManager getClassManager() { return classManager; }
  165. }