org.argouml.uml.reveng
Class ImportClassLoader

java.lang.Object
  extended byjava.lang.ClassLoader
      extended byjava.security.SecureClassLoader
          extended byjava.net.URLClassLoader
              extended byorg.argouml.uml.reveng.ImportClassLoader

public class ImportClassLoader
extends URLClassLoader

Class to help users reverse engineer class information from arbitrary .jar/.class file resources, like an import classpath.

can be used as follows:



 try{
 ImportClassLoader loader = ImportClassLoader.getInstance();
 // add paths...
 loader.addFile(new File("/opt/lib/myjar.jar"));

  Class clazz = loader.loadClass("org.xyz.MyException");
  Object ex = clazz.newInstance();
   cat.info("loaded class ok");
 }catch(Exception e){cat.warn("error loading class: "+e.toString());}


It supports adding and removing Files from the import classpath. And saving and loading the path to/from the users properties file.

It should be possible to make this the system class loader, but I haven't got this to work yet:


 final URLClassLoader urlClassLoader = new URLClassLoader(urls, cl);
 //create a new custom class with the default classloader as its parent
 try {
 EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
 eq.invokeAndWait(new Runnable() {
 public void run() {
 Thread.currentThread().setContextClassLoader(urlClassLoader);
 //this will replace the default system class loader with the new custom
 //classloader, so that the jvm will use the new custom classloader to
 // lookup a class
 }
 });
 //...


Nested Class Summary
 
Nested classes inherited from class java.lang.ClassLoader
 
Field Summary
private static ImportClassLoader instance
           
private static Logger LOG
          logger
 
Fields inherited from class java.net.URLClassLoader
 
Fields inherited from class java.security.SecureClassLoader
 
Fields inherited from class java.lang.ClassLoader
 
Constructor Summary
private ImportClassLoader(URL[] urls)
           
 
Method Summary
 void addFile(File f)
           
static ImportClassLoader getInstance()
          Try and return the existing instance if one exists.
static ImportClassLoader getInstance(URL[] urls)
          There is no default constructor for URLClassloader, so we should provide urls when creating the instance.
static URL[] getURLs(String path)
          Add the files for which the paths are given, and return in URL format.
 void loadUserPath()
          Get the user-configured path.
 void removeFile(File f)
          Remove the given file.
 void saveUserPath()
          Store the user-configured path.
 void setPath(Object[] paths)
           
 void setPath(String path)
          Add the file for which a path is given.
 String toString()
           
 
Methods inherited from class java.net.URLClassLoader
addURL, definePackage, findClass, findResource, findResources, getPermissions, getURLs, newInstance, newInstance
 
Methods inherited from class java.security.SecureClassLoader
defineClass
 
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findSystemClass, getPackage, getPackages, getParent, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, loadClass, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

LOG

private static final Logger LOG
logger


instance

private static ImportClassLoader instance
Constructor Detail

ImportClassLoader

private ImportClassLoader(URL[] urls)
Method Detail

getInstance

public static ImportClassLoader getInstance()
                                     throws MalformedURLException
Try and return the existing instance if one exists.

Returns:
the instance
Throws:
MalformedURLException - when the url is bad

getInstance

public static ImportClassLoader getInstance(URL[] urls)
                                     throws MalformedURLException
There is no default constructor for URLClassloader, so we should provide urls when creating the instance. We crate a new instance in this method.

Parameters:
urls - the URLs
Returns:
the instance of this class
Throws:
MalformedURLException - when the URL is bad

addFile

public void addFile(File f)
             throws MalformedURLException
Parameters:
f - the file to be added
Throws:
MalformedURLException - when the URL is bad

removeFile

public void removeFile(File f)
Remove the given file. But we can't remove the last file.

Parameters:
f - the file to be removed

setPath

public void setPath(String path)
Add the file for which a path is given.

Parameters:
path - the path in String format

getURLs

public static URL[] getURLs(String path)
Add the files for which the paths are given, and return in URL format.

Parameters:
path - the paths in String format
Returns:
the URLs

setPath

public void setPath(Object[] paths)
Parameters:
paths - the paths to the files to be added

loadUserPath

public void loadUserPath()
Get the user-configured path.


saveUserPath

public void saveUserPath()
Store the user-configured path.


toString

public String toString()
See Also:
Object.toString()


ArgoUML © 1996-2004 (20050222)ArgoUML HomepageArgoUML Developers' pageArgoUML Cookbook